Commit 32d40949 authored by Petter Breedveld's avatar Petter Breedveld

Latest LF version, lacking some comments

parent 642b8eed
......@@ -28,7 +28,7 @@
* Check the datasheet to find out which pins on which ports the different LEDs and buttons are connected to.
*/
#define LED0 4 // LED0 is connected to pin 4 on port B
#define LED0 4 // LED0 is connected to pin 4 on port B (PIN4_bp)
int main(void){
/**
......
......@@ -15,7 +15,7 @@
#include <util/delay.h>
/*
* Checkout the ATtiny817 datasheet to find the correct ports and pins
* Check out the ATtiny817 datasheet to find the correct ports and pins
*/
// LED
......
/*
* PWM.c
* PWM-LF.c
*
* Created: 29.01.2017 23:43:39
* Author : Petter
......@@ -26,35 +26,35 @@ bool SW1_pressed,SW2_pressed; //Keeping track of button status
int main(void)
{
//Set LED pins as output
PORTB_DIR |= (1 << LED1) | (1 << LED2) | (1 << LED3);
PORTB.DIRSET = (1 << LED1) | (1 << LED2) | (1 << LED3);
//Because the LEDs are active low, we invert the output
PORTB_PIN0CTRL |= (1 << PORT_INVEN_bp);
PORTB_PIN1CTRL |= (1 << PORT_INVEN_bp);
PORTB_PIN4CTRL |= (1 << PORT_INVEN_bp);
PORTB.PIN0CTRL |= (1 << PORT_INVEN_bp);
PORTB.PIN1CTRL |= (1 << PORT_INVEN_bp);
PORTB.PIN4CTRL |= (1 << PORT_INVEN_bp);
//Pullups
PORTA_PIN5CTRL |= (1 << PORT_PULLUPEN_bp);
PORTA_PIN6CTRL |= (1 << PORT_PULLUPEN_bp);
PORTA_PIN7CTRL |= (1 << PORT_PULLUPEN_bp);
PORTA.PIN5CTRL |= (1 << PORT_PULLUPEN_bp);
PORTA.PIN6CTRL |= (1 << PORT_PULLUPEN_bp);
PORTA.PIN7CTRL |= (1 << PORT_PULLUPEN_bp);
//Set clock prescaler div256
TCA0_SINGLE_CTRLA |= (TCA_SINGLE_ENABLE_bm); //Enable timer
TCA0_SINGLE_CTRLB |= (0x05 << TCA_SINGLE_WGMODE0_bp); //Set mode to single slope
TCA0.SINGLE.CTRLA |= (TCA_SINGLE_ENABLE_bm); //Enable timer
TCA0.SINGLE.CTRLB |= (0x05 << TCA_SINGLE_WGMODE0_bp); //Set mode to single slope
TCA0_SINGLE_CTRLB |= (TCA_SINGLE_CMP0EN_bm) | (TCA_SINGLE_CMP1EN_bm);
TCA0.SINGLE.CTRLB |= (TCA_SINGLE_CMP0EN_bm) | (TCA_SINGLE_CMP1EN_bm);
TCA0_SINGLE_PER = 0x0fff; //We set our top to have a sufficiently high frequency (Top at 16 bit (0xffff) ~25Hz, 12 bit (0x0fff) ~400Hz)
TCA0_SINGLE_CMP0 = 0x0000;
TCA0_SINGLE_CMP1 = 0x0fff;
TCA0.SINGLE.PER = 0x0fff; //We set our top to have a sufficiently high frequency (Top at 16 bit (0xffff) ~25Hz, 12 bit (0x0fff) ~400Hz)
TCA0.SINGLE.CMP0 = 0x0000;
TCA0.SINGLE.CMP1 = 0x0fff;
while(1){
if (!(PORTA_IN & (1 << SW1))){
if (!(PORTA.IN & (1 << SW1))){
if(!SW1_pressed){
TCA0_SINGLE_CMP0 = (((TCA0_SINGLE_CMP0 << 1) + 1) & (0x0fff)); //Shift in a 1, and cut off excess to 12 bit
TCA0_SINGLE_CMP1 >>= 1;
TCA0.SINGLE.CMP0 = (((TCA0.SINGLE.CMP0 << 1) + 1) & (0x0fff)); //Shift in a 1, and cut off excess to 12 bit
TCA0.SINGLE.CMP1 >>= 1;
SW1_pressed = true;
}
}
......@@ -65,8 +65,8 @@ int main(void)
if (!(PORTA_IN & (1 << SW2))){
if(!SW2_pressed){
TCA0_SINGLE_CMP1 = (((TCA0_SINGLE_CMP1 << 1) + 1) & (0x0fff)); //Shift in a 1, and cut off excess to 12 bit
TCA0_SINGLE_CMP0 >>= 1;
TCA0.SINGLE.CMP1 = (((TCA0.SINGLE.CMP1 << 1) + 1) & (0x0fff)); //Shift in a 1, and cut off excess to 12 bit
TCA0.SINGLE.CMP0 >>= 1;
SW2_pressed = true;
}
}
......
/*
* TIMER.c
* TIMER-LF.c
*
* Created: 29.01.2017 23:43:39
* Author : Petter
......@@ -23,34 +23,34 @@ int main(void)
First set up the LEDs like in the previous exercise. You can either copy paste from the previous exercise or start fresh.
*/
PORTB_DIR |= (1 << LED1) | (1 << LED2);
PORTB_OUTTGL |= (1 << LED1); //Only to start with LEDs off
PORTB.DIR = (1 << LED1) | (1 << LED2);
PORTB.OUTTGL = (1 << LED1); //Only to start with LEDs off
//We will be using a timer overflow interupt with timer A
//We set the prescaler to clk=clk/256
TCA0_SINGLE_CTRLA |= (TCA_SINGLE_CLKSEL_DIV256_gc) | (TCA_SINGLE_ENABLE_bm);
TCA0.SINGLE.CTRLA |= (TCA_SINGLE_CLKSEL_DIV256_gc) | (TCA_SINGLE_ENABLE_bm);
//Next we Enable timer interupts on overflow
TCA0_SINGLE_INTCTRL |= ( TCA_SINGLE_OVF_bm);
TCA0.SINGLE.INTCTRL |= (TCA_SINGLE_OVF_bm);
uint16_t counterTop = 0x4000;
TCA0_SINGLE_PER = counterTop;
TCA0.SINGLE.PER = counterTop;
//Enable global interrupts
sei();
while(1){
//Do other things?
}
}
ISR(TCA0_OVF_vect){
PORTB_OUTTGL |= (1 << LED1) | (1 << LED2);
PORTB.OUTTGL = (1 << LED1) | (1 << LED2);
TCA0_SINGLE_INTFLAGS |= ( TCA_SINGLE_OVF_bm); //Clear the interupt flag
TCA0.SINGLE.INTFLAGS |= ( TCA_SINGLE_OVF_bm); //Clear the interupt flag
  • Interrupt-flagg slettes vanligvis (og også i dette tilfellet) ved å skrive en 1 til flagget. Siden dette er et helt register fult av intflag vil i så fall INTFLAGS |= whatever Altid slette alle flaggene i dette registeret. Erstatt med TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm og kommenter akkurat denne problemstillingen?

Please register or sign in to reply
}
\ No newline at end of file
......@@ -6,46 +6,48 @@
*/
#define F_CPU 3333333UL
#define BAUD_9600 ((4UL*F_CPU)/9600)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdbool.h>
void uart_init(int baud){
int baudRate = ((4UL*F_CPU)/baud); //CLK_PER = CLK_CPU, see 10.2.1
void uart_init(unsigned long baud){
//From chapter 24.3 in datasheet
PORTB_OUT |= (1 << 2);
PORTB_DIR |= (1 << 2); //Setting up TX pin as output
USART0_BAUDH = (baudRate >> 8); //Shift register right by 8 bits
USART0_BAUDL = baudRate; //Set baud rate
//USART_CTRLC CMODE bits default async, 1 stop bit, 8 bit character size
USART0_CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX
USART0_CTRLA |= (1 << USART_RXCIE_bp); //Enable interrupts on incomming data
PORTB.OUTSET = (1 << PIN2_bp);
PORTB.DIRSET = (1 << PIN2_bp); //Setting up TX pin as output
USART0.BAUDH = (baud >> 8); //Shift register right by 8 bits
USART0.BAUDL = baud; //Set baud rate
//USART.CTRLC CMODE bits default to async, 1 stop bit, 8 bit character size
USART0.CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX
USART0.CTRLA |= (1 << USART_RXCIE_bp); //Enable interrupts on incomming data
}
// function to transmit data
void uart_transmit(unsigned char data){
while (!(USART0_STATUS & (1 << USART_DREIF_bp)));
USART0_TXDATAH = 0x00; //Is needed!
USART0_TXDATAL = data;
while (!(USART0.STATUS & (1 << USART_DREIF_bp))){
//wait for previous transmit to finish
};
USART0.TXDATAH = 0x00; //Is needed!
USART0.TXDATAL = data;
}
int main(void)
{
uart_init(9600); //start uart with baudrate of 9600
uart_init(BAUD_9600); //start uart with baudrate of 9600
sei(); //Important for anything here to work
while (1)
{
//Do nothing, interrupts handle everything
}
}
......@@ -53,7 +55,8 @@ int main(void)
//Interrupt service routine for the receiver.
ISR(USART0_RXC_vect){
uart_transmit(USART0_RXDATAL);
USART0_STATUS |= (1 << USART_RXCIF_bp);
uint8_t data = USART0_RXDATAL;
//Do things with data:
uart_transmit(data + 1); //Example: Shift all characters by one step A -> B, B -> C etc.
}
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Atmel Studio Solution File, Format Version 11.00
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{54F91283-7BC4-4236-8FF9-10F437C3AD48}") = "Task1_LF", "Task1_LF\Task1_LF.cproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{571AD486-B75D-41DA-B29C-67D05CD878FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|AVR = Debug|AVR
Release|AVR = Release|AVR
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
......@@ -37,9 +37,9 @@ SUBDIRS :=
# Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../display.c \
../adc.c \
../main.c \
../spi.c
../uart.c
PREPROCESSING_SRCS +=
......@@ -49,28 +49,28 @@ ASM_SRCS +=
OBJS += \
display.o \
adc.o \
main.o \
spi.o
uart.o
OBJS_AS_ARGS += \
display.o \
adc.o \
main.o \
spi.o
uart.o
C_DEPS += \
display.d \
adc.d \
main.d \
spi.d
uart.d
C_DEPS_AS_ARGS += \
display.d \
adc.d \
main.d \
spi.d
uart.d
OUTPUT_FILE_PATH +=Task1.elf
OUTPUT_FILE_PATH +=Task1_LF.elf
OUTPUT_FILE_PATH_AS_ARGS +=Task1.elf
OUTPUT_FILE_PATH_AS_ARGS +=Task1_LF.elf
ADDITIONAL_DEPENDENCIES:=
......@@ -121,13 +121,13 @@ all: $(OUTPUT_FILE_PATH) $(ADDITIONAL_DEPENDENCIES)
$(OUTPUT_FILE_PATH): $(OBJS) $(USER_OBJS) $(OUTPUT_FILE_DEP) $(LIB_DEP) $(LINKER_SCRIPT_DEP)
@echo Building target: $@
@echo Invoking: AVR/GNU Linker : 4.9.2
$(QUOTE)D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe$(QUOTE) -o$(OUTPUT_FILE_PATH_AS_ARGS) $(OBJS_AS_ARGS) $(USER_OBJS) $(LIBS) -Wl,-Map="Task1.map" -Wl,--start-group -Wl,-lm -Wl,--end-group -Wl,--gc-sections -mmcu=attiny817 -B "D:\Atmel\7.0\Packs\atmel\ATtiny_DFP\1.1.102\gcc\dev\attiny817"
$(QUOTE)D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe$(QUOTE) -o$(OUTPUT_FILE_PATH_AS_ARGS) $(OBJS_AS_ARGS) $(USER_OBJS) $(LIBS) -Wl,-Map="Task1_LF.map" -Wl,--start-group -Wl,-lm -Wl,--end-group -Wl,--gc-sections -mmcu=attiny817 -B "D:\Atmel\7.0\Packs\atmel\ATtiny_DFP\1.1.102\gcc\dev\attiny817"
@echo Finished building target: $@
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Task1.elf" "Task1.hex"
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "Task1.elf" "Task1.eep" || exit 0
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objdump.exe" -h -S "Task1.elf" > "Task1.lss"
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Task1.elf" "Task1.srec"
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-size.exe" "Task1.elf"
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Task1_LF.elf" "Task1_LF.hex"
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "Task1_LF.elf" "Task1_LF.eep" || exit 0
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objdump.exe" -h -S "Task1_LF.elf" > "Task1_LF.lss"
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "Task1_LF.elf" "Task1_LF.srec"
"D:\Atmel\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-size.exe" "Task1_LF.elf"
......@@ -139,5 +139,5 @@ $(OUTPUT_FILE_PATH): $(OBJS) $(USER_OBJS) $(OUTPUT_FILE_DEP) $(LIB_DEP) $(LINKER
clean:
-$(RM) $(OBJS_AS_ARGS) $(EXECUTABLES)
-$(RM) $(C_DEPS_AS_ARGS)
rm -rf "Task1.elf" "Task1.a" "Task1.hex" "Task1.lss" "Task1.eep" "Task1.map" "Task1.srec" "Task1.usersignatures"
rm -rf "Task1_LF.elf" "Task1_LF.a" "Task1_LF.hex" "Task1_LF.lss" "Task1_LF.eep" "Task1_LF.map" "Task1_LF.srec" "Task1_LF.usersignatures"
\ No newline at end of file
:1000000019C02AC029C028C027C026C025C024C0C6
:1000100023C022C021C020C01FC01EC01DC01CC0E4
:100020001BC01AC019C018C017C016C0A2C014C087
:1000300013C012C011241FBECFEFCDBFDFE3DEBF60
:100040002EE3A0E0BEE301C01D92A430B207E1F7A9
:100050000E944E000C94E0000C940000E0E0F6E0FA
:1000600080818460808381818260818382818061DC
:100070008283828182838081816080830895E0E031
:10008000F6E08683838581608387808581608087B1
:10009000838580FFFDCF8091100608956CE675E0A2
:1000A00080E090E00E9497000E942E007894CDECB2
:1000B00086E00E943F0080958C9F912D112496959B
:1000C00096959695392F330F232F220F220F230F4A
:1000D000821B8093013E9C9F812D112486958695DD
:1000E0008695382F330F232F220F220F230F921BB9
:1000F0009093023E8093033E8093003E805D0E9479
:10010000AF008091023E805D0E94AF008091013E71
:10011000805D0E94AF0080E20E94AF002AE28CE284
:100120009AE0215080409040E1F70000C1CF0F934A
:100130001F93E0E2F4E024E025832183072F182FAA
:10014000292F3327E0E0F8E0018760878681806C03
:1001500086838581806885831F910F910895E0E0F3
:10016000F8E0948195FFFDCFE0E0F8E01382828310
:1001700008951F920F920FB60F9211242F933F9361
:100180004F935F936F937F938F939F93AF93BF939F
:10019000EF93FF93809100088F5F0E94AF00FF9163
:1001A000EF91BF91AF919F918F917F916F915F91EF
:1001B0004F913F912F910F900FBE0F901F90189568
:0401C000F894FFCFE1
:00000001FF
Task1_LF.elf: file format elf32-avr
Sections:
Idx Name Size VMA LMA File off Algn
0 .data 00000000 00803e00 00803e00 00000238 2**0
CONTENTS, ALLOC, LOAD, DATA
1 .text 000001c4 00000000 00000000 00000074 2**1
CONTENTS, ALLOC, LOAD, READONLY, CODE
2 .bss 00000004 00803e00 00803e00 00000238 2**0
ALLOC
3 .comment 00000030 00000000 00000000 00000238 2**0
CONTENTS, READONLY
4 .note.gnu.avr.deviceinfo 0000003c 00000000 00000000 00000268 2**2
CONTENTS, READONLY
5 .debug_aranges 00000078 00000000 00000000 000002a4 2**0
CONTENTS, READONLY, DEBUGGING
6 .debug_info 00002043 00000000 00000000 0000031c 2**0
CONTENTS, READONLY, DEBUGGING
7 .debug_abbrev 000018bf 00000000 00000000 0000235f 2**0
CONTENTS, READONLY, DEBUGGING
8 .debug_line 000004f3 00000000 00000000 00003c1e 2**0
CONTENTS, READONLY, DEBUGGING
9 .debug_frame 000000f0 00000000 00000000 00004114 2**2
CONTENTS, READONLY, DEBUGGING
10 .debug_str 00000fc2 00000000 00000000 00004204 2**0
CONTENTS, READONLY, DEBUGGING
11 .debug_loc 00000147 00000000 00000000 000051c6 2**0
CONTENTS, READONLY, DEBUGGING
12 .debug_ranges 00000048 00000000 00000000 0000530d 2**0
CONTENTS, READONLY, DEBUGGING
Disassembly of section .text:
00000000 <__vectors>:
0: 19 c0 rjmp .+50 ; 0x34 <__ctors_end>
2: 2a c0 rjmp .+84 ; 0x58 <__bad_interrupt>
4: 29 c0 rjmp .+82 ; 0x58 <__bad_interrupt>
6: 28 c0 rjmp .+80 ; 0x58 <__bad_interrupt>
8: 27 c0 rjmp .+78 ; 0x58 <__bad_interrupt>
a: 26 c0 rjmp .+76 ; 0x58 <__bad_interrupt>
c: 25 c0 rjmp .+74 ; 0x58 <__bad_interrupt>
e: 24 c0 rjmp .+72 ; 0x58 <__bad_interrupt>
10: 23 c0 rjmp .+70 ; 0x58 <__bad_interrupt>
12: 22 c0 rjmp .+68 ; 0x58 <__bad_interrupt>
14: 21 c0 rjmp .+66 ; 0x58 <__bad_interrupt>
16: 20 c0 rjmp .+64 ; 0x58 <__bad_interrupt>
18: 1f c0 rjmp .+62 ; 0x58 <__bad_interrupt>
1a: 1e c0 rjmp .+60 ; 0x58 <__bad_interrupt>
1c: 1d c0 rjmp .+58 ; 0x58 <__bad_interrupt>
1e: 1c c0 rjmp .+56 ; 0x58 <__bad_interrupt>
20: 1b c0 rjmp .+54 ; 0x58 <__bad_interrupt>
22: 1a c0 rjmp .+52 ; 0x58 <__bad_interrupt>
24: 19 c0 rjmp .+50 ; 0x58 <__bad_interrupt>
26: 18 c0 rjmp .+48 ; 0x58 <__bad_interrupt>
28: 17 c0 rjmp .+46 ; 0x58 <__bad_interrupt>
2a: 16 c0 rjmp .+44 ; 0x58 <__bad_interrupt>
2c: a2 c0 rjmp .+324 ; 0x172 <__vector_22>
2e: 14 c0 rjmp .+40 ; 0x58 <__bad_interrupt>
30: 13 c0 rjmp .+38 ; 0x58 <__bad_interrupt>
32: 12 c0 rjmp .+36 ; 0x58 <__bad_interrupt>
00000034 <__ctors_end>:
34: 11 24 eor r1, r1
36: 1f be out 0x3f, r1 ; 63
38: cf ef ldi r28, 0xFF ; 255
3a: cd bf out 0x3d, r28 ; 61
3c: df e3 ldi r29, 0x3F ; 63
3e: de bf out 0x3e, r29 ; 62
00000040 <__do_clear_bss>:
40: 2e e3 ldi r18, 0x3E ; 62
42: a0 e0 ldi r26, 0x00 ; 0
44: be e3 ldi r27, 0x3E ; 62
46: 01 c0 rjmp .+2 ; 0x4a <.do_clear_bss_start>
00000048 <.do_clear_bss_loop>:
48: 1d 92 st X+, r1
0000004a <.do_clear_bss_start>:
4a: a4 30 cpi r26, 0x04 ; 4
4c: b2 07 cpc r27, r18
4e: e1 f7 brne .-8 ; 0x48 <.do_clear_bss_loop>
50: 0e 94 4e 00 call 0x9c ; 0x9c <main>
54: 0c 94 e0 00 jmp 0x1c0 ; 0x1c0 <_exit>
00000058 <__bad_interrupt>:
58: 0c 94 00 00 jmp 0 ; 0x0 <__vectors>
0000005c <adc_init>:
*/
#include <avr/io.h>
void adc_init(){
ADC0.CTRLA |= (ADC_RESSEL_8BIT_gc); //Set resolution, we choose 8 bits
5c: e0 e0 ldi r30, 0x00 ; 0
5e: f6 e0 ldi r31, 0x06 ; 6
60: 80 81 ld r24, Z
62: 84 60 ori r24, 0x04 ; 4
64: 80 83 st Z, r24
ADC0.CTRLB |= (ADC_SAMPLNUM_ACC4_gc); //OPTIONAL: We can use multiple samples if we like, example here with 4
66: 81 81 ldd r24, Z+1 ; 0x01
68: 82 60 ori r24, 0x02 ; 2
6a: 81 83 std Z+1, r24 ; 0x01
ADC0.CTRLC |= (ADC_REFSEL_VDDREF_gc); //We select to use the supply voltage (VDD) as voltage reference
6c: 82 81 ldd r24, Z+2 ; 0x02
6e: 80 61 ori r24, 0x10 ; 16
70: 82 83 std Z+2, r24 ; 0x02
ADC0.CTRLC |= (ADC_PRESC_DIV2_gc); //ADC clock prescaler, best accuracy when run below 2MHz. (Here div2 ~1.46 MHz)
72: 82 81 ldd r24, Z+2 ; 0x02
74: 82 83 std Z+2, r24 ; 0x02
ADC0.CTRLA |= (ADC_ENABLE_bm); //Enable the ADC
76: 80 81 ld r24, Z
78: 81 60 ori r24, 0x01 ; 1
7a: 80 83 st Z, r24
7c: 08 95 ret
0000007e <adc_read>:
}
uint8_t adc_read(uint8_t channel){
ADC0.MUXPOS = channel; //Select input on the ADC mux
7e: e0 e0 ldi r30, 0x00 ; 0
80: f6 e0 ldi r31, 0x06 ; 6
82: 86 83 std Z+6, r24 ; 0x06
ADC0.INTFLAGS |= (ADC_RESRDY_bm); //Clear the results ready flag
84: 83 85 ldd r24, Z+11 ; 0x0b
86: 81 60 ori r24, 0x01 ; 1
88: 83 87 std Z+11, r24 ; 0x0b
ADC0.COMMAND |= (ADC_STCONV_bm); //Start a conversion
8a: 80 85 ldd r24, Z+8 ; 0x08
8c: 81 60 ori r24, 0x01 ; 1
8e: 80 87 std Z+8, r24 ; 0x08
while(!(ADC0.INTFLAGS & ADC_RESRDY_bm)); //Wait for the results ready flag to be set
90: 83 85 ldd r24, Z+11 ; 0x0b
92: 80 ff sbrs r24, 0
94: fd cf rjmp .-6 ; 0x90 <adc_read+0x12>
return ADC0.RESL; //Return 8 bit result
96: 80 91 10 06 lds r24, 0x0610 ; 0x800610 <__TEXT_REGION_LENGTH__+0x700610>
}
9a: 08 95 ret
0000009c <main>:
uint8_t data, ones, tens, hundreds;
int main(void)
{
uart_init(BAUD_9600);
9c: 6c e6 ldi r22, 0x6C ; 108
9e: 75 e0 ldi r23, 0x05 ; 5
a0: 80 e0 ldi r24, 0x00 ; 0
a2: 90 e0 ldi r25, 0x00 ; 0
a4: 0e 94 97 00 call 0x12e ; 0x12e <uart_init>
adc_init();
a8: 0e 94 2e 00 call 0x5c ; 0x5c <adc_init>
sei();
ac: 78 94 sei
while (1)
{
data = adc_read(6);
data = 0xff - data;
ones = (data%10);
ae: cd ec ldi r28, 0xCD ; 205
sei();
while (1)
{
data = adc_read(6);
b0: 86 e0 ldi r24, 0x06 ; 6
b2: 0e 94 3f 00 call 0x7e ; 0x7e <adc_read>
data = 0xff - data;
b6: 80 95 com r24
ones = (data%10);
b8: 8c 9f mul r24, r28
ba: 91 2d mov r25, r1
bc: 11 24 eor r1, r1
be: 96 95 lsr r25
c0: 96 95 lsr r25
c2: 96 95 lsr r25
c4: 39 2f mov r19, r25
c6: 33 0f add r19, r19
c8: 23 2f mov r18, r19
ca: 22 0f add r18, r18
cc: 22 0f add r18, r18
ce: 23 0f add r18, r19
d0: 82 1b sub r24, r18
d2: 80 93 01 3e sts 0x3E01, r24 ; 0x803e01 <ones>
data /= 10;
tens = (data%10);
d6: 9c 9f mul r25, r28
d8: 81 2d mov r24, r1
da: 11 24 eor r1, r1
dc: 86 95 lsr r24
de: 86 95 lsr r24
e0: 86 95 lsr r24
e2: 38 2f mov r19, r24
e4: 33 0f add r19, r19
e6: 23 2f mov r18, r19
e8: 22 0f add r18, r18
ea: 22 0f add r18, r18
ec: 23 0f add r18, r19
ee: 92 1b sub r25, r18
f0: 90 93 02 3e sts 0x3E02, r25 ; 0x803e02 <tens>
data /= 10;
f4: 80 93 03 3e sts 0x3E03, r24 ; 0x803e03 <data>
hundreds = (data%10);
f8: 80 93 00 3e sts 0x3E00, r24 ; 0x803e00 <_edata>
uart_transmit(hundreds + 48);
fc: 80 5d subi r24, 0xD0 ; 208
fe: 0e 94 af 00 call 0x15e ; 0x15e <uart_transmit>
uart_transmit(tens + 48);
102: 80 91 02 3e lds r24, 0x3E02 ; 0x803e02 <tens>
106: 80 5d subi r24, 0xD0 ; 208
108: 0e 94 af 00 call 0x15e ; 0x15e <uart_transmit>
uart_transmit(ones + 48);
10c: 80 91 01 3e lds r24, 0x3E01 ; 0x803e01 <ones>
110: 80 5d subi r24, 0xD0 ; 208
112: 0e 94 af 00 call 0x15e ; 0x15e <uart_transmit>
uart_transmit(0x20); //Space
116: 80 e2 ldi r24, 0x20 ; 32
118: 0e 94 af 00 call 0x15e ; 0x15e <uart_transmit>
#else
//round up by default
__ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
#endif
__builtin_avr_delay_cycles(__ticks_dc);
11c: 2a e2 ldi r18, 0x2A ; 42
11e: 8c e2 ldi r24, 0x2C ; 44
120: 9a e0 ldi r25, 0x0A ; 10
122: 21 50 subi r18, 0x01 ; 1
124: 80 40 sbci r24, 0x00 ; 0
126: 90 40 sbci r25, 0x00 ; 0
128: e1 f7 brne .-8 ; 0x122 <main+0x86>
12a: 00 00 nop
12c: c1 cf rjmp .-126 ; 0xb0 <main+0x14>
0000012e <uart_init>:
#include <avr/io.h>
#include <avr/interrupt.h>
void uart_init(unsigned long baud){
12e: 0f 93 push r16
130: 1f 93 push r17
//From chapter 24.3 in datasheet
PORTB.OUTSET = (1 << PIN2_bp);
132: e0 e2 ldi r30, 0x20 ; 32
134: f4 e0 ldi r31, 0x04 ; 4
136: 24 e0 ldi r18, 0x04 ; 4
138: 25 83 std Z+5, r18 ; 0x05
PORTB.DIRSET = (1 << PIN2_bp); //Setting up TX pin as output
13a: 21 83 std Z+1, r18 ; 0x01
USART0.BAUDH = (baud >> 8); //Shift register right by 8 bits
13c: 07 2f mov r16, r23
13e: 18 2f mov r17, r24
140: 29 2f mov r18, r25
142: 33 27 eor r19, r19
144: e0 e0 ldi r30, 0x00 ; 0
146: f8 e0 ldi r31, 0x08 ; 8
148: 01 87 std Z+9, r16 ; 0x09
USART0.BAUDL = baud; //Set baud rate
14a: 60 87 std Z+8, r22 ; 0x08
//USART.CTRLC CMODE bits default to: async, 1 stop bit, 8 bit character size
USART0.CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX
14c: 86 81 ldd r24, Z+6 ; 0x06
14e: 80 6c ori r24, 0xC0 ; 192
150: 86 83 std Z+6, r24 ; 0x06
USART0.CTRLA |= (1 << USART_RXCIE_bp); //Enable interupts on RX
152: 85 81 ldd r24, Z+5 ; 0x05
154: 80 68 ori r24, 0x80 ; 128
156: 85 83 std Z+5, r24 ; 0x05
}
158: 1f 91 pop r17
15a: 0f 91 pop r16
15c: 08 95 ret
0000015e <uart_transmit>:
// function to transmit data
void uart_transmit(unsigned char data){