Cheat Sheet PDF
Cheat Sheet PDF
Based on the book: AVR microcontroller and embedded systems Using Assembly and C by Muhamad Ali, Sarmad
Naimi, Sepehr Naimi
by christina_
25 Αυγούστου 2018
Περιεχόμενα
1 TIMERS 2
1.1 STEP TO PROGRAM TIMER0 IN NORMAL MODE . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 FINDING VALUES TO LOAD INTO THE TIMER . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 CLEAR TIMER0 ON COMPARE MATCH (CTC) MODE . . . . . . . . . . . . . . . . . . . . . 4
1.4 Timer1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Timer1 modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Normal Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.7 CTC Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 ADC 17
3.1 Programming ADC using polling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Programming ADC using interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 DAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Examples 38
5.1 Flashing Led example ( credits to : Maylo) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6 Σημειώσεις 40
TeX Gyre Schola
1
MIKPO 2 -
1 TIMERS
• TCNTn(timer/counter): counts up w/every puls, reset->zero
• TOVn(Timer Overflow) flag: when timer overflows, it is set to 1, must be cleared later(clear by setting
1)
• OCRn(Output Compare Register): The content of the OCR is compared to the content of TCNT and
when they are equal the OCFn(Output Compare Flag) will be set
• TIFR(Timer/Counter Interrupt Flag Register): Contains the flag of different timers Timers are in
the I/O registers so we use IN and OUT instructions.
(
Σχήμα 1: TCCR0
2
MIKPO 2 -
2. Load the value to TCCR0 indicating which mode is to be used and the prescaler option.
3. Monitor the TOV0 and get out of the loop when it is set
4. Stop the timer by disconnecting the clock source with [LDI r20, 0x00, out TCCR0, r20]
5. Clear the TOV0 flag for the next round
6. Start again by loading the register TCNT0
1
2 .INCLUDE "M32DEF. INC" cycles
3 .MACRO INITSTACK ; SET STACK
4 LDI R20 , HIGH(RAMEND)
5 OUT SPH, R20
6 LDI R20 , LOW(RAMEND)
7 OUT SPL, R20
8 .ENDMACRO
9 INITSTACK
10 LDI R16 , 1<<5 ; R16=00010 0000 FOR PB5
11 SBI DDRB, 5 ; PB5 AS AN OUTPUT
12 LDI R17 , 0
13 BEGIN:
14 OUT PORTB, R17 ;CLEAR PORTB
15 RCALL DELAY ;CALL TIMER DELAY 3
16 EOR R17 , R16 ;TOGGLE D5 OF R17 1
17 OUT PORTB, R17 ;TOGGLE PB5 1
18 RJMP BEGIN 2
19 −−−−−−−−−−−−−
20 DELAY:
21 LDI R20 , 0XF2 ;LOAD TIMER 1
22 OUT TCNT0, R20 1
23 LDI R20 , 0X01 1
24 OUT TCCR0, R20 ;TIMER 0 , NORMAL MODE, NO PRESCALER 1
25
26 AGAIN:
27 IN R20 , TIFR ;READ TIFR 1
28 SBRS R20 , TOV0 ; IF TOV0 IS SET SKIP NEXT INSTRUCTION 1/2
29 RJMP AGAIN 2
30 LDI R20 , 0X00 1
31 OUT TCCR0, R20 ;STOP TIMER 1
32 ;CLEAR TOV0 FLAG
33 LDI R20 , (1<<TOV0) 1
34 OUT TIFR , R20 1
35 RET 4
36 = 24
Each clock has period 1/XTAL. It also needs an extra clock when it rolls from FF to 00 and sets the TOV
flag. Concluding: Delay = (FF-XX+1) · 1/F where FF is 0b111111111 and XX is the TCNT initial value. or
(256 -NNN) · 1/F where NNN is the value of TCNT0 reg in decimal
3
MIKPO 2 -
2. Divide the desired time delay by Tclock . This says how many clocks we need (n).
3. Perform x = 256-n
4. Convert x to hex,say xx. This is the initial value to be loaded into the timer’s register
5. Set TCNT0 = xx
4
MIKPO 2 -
If TCCNT0 is bigger than OCR0 then it counts up until FF , sets the TOV0 flag and then it counts up
until OCR0. The TMSK register containts the interrupt enable bits for the timers. We must set HIGH the
bits of the interrupts we want to enable.
*OCR0 = number of pulses we want -1.
2. Divide the wanted delay with Tclock . This gives us the number n of pulses needed.
3. Perform 256 - n.
PRESCALING
XT AL XT AL
• Find the prescaler: desired delay = N ·21 6
=⇒ desireddelay = N ·(OCR+1) =⇒ OCR = ..
prescaler
• Max Delay = 256 · XT AL
XT AL
• new frequency = prescaler
1.4 Timer1
Since Timer1 is a 16bit timer, its 16bit register is split in two bytes:TCNT1L(Low Byte) and TCNT1H(High
Byte). It also has 2 control registers: TCCR1A and TCCR1B.Of course there is the TOV01 flag. Timer1 has
also prescaler options:1:1, 1:8, 1:64, 1:256 and 1:1024. There are two OCR registers in timer1 : OCR1A and
OCR1B, and each one has a seperate flag which acts independently. Whenever TCNT1 equals OCR1A, the
OCF1A flag will be set on the next timer clock. When TCNT1 equals OCR1B, the OCF1B flag will be set
on the next timer clock. OCR1A and OCR1B are both 16bit registers. For example, OCR1A is made from
2 8bit registers OCR1AH and OCR1AL.
There is also a register named ICR1 which is used for operations like capturing
5
MIKPO 2 -
6
MIKPO 2 -
The WGM13, WGM12, WGM11 and WGM10 bits define the mode of Timer1.
7
MIKPO 2 -
8
MIKPO 2 -
9
MIKPO 2 -
10
MIKPO 2 -
11
MIKPO 2 -
Polling: In polling the microcontroller always monitors the status of a given device. When status is met,
it performs the service. After that it moves on to monitor the next device until each on is served. It is not
12
MIKPO 2 -
1. It finishes the instruction it is currently executing and saves the baddress of the next instruction(program
counter) on the stack.
2. It jumps to a fixed location in memory called the interrupt vector table. The interrupt vector table
directs the microcontroller to the address of the interrupt service routine (ISR).
3. The microcontroller starts to execute the interrupt service subroutine until it reaches the last instruction
of the subroutine, which is RETI ( return from interrupt)
4. Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted.
First, it gets the program counter (PC) address from the stack by popping the top bytes of the stack
into the PC. Then it starts to execute from that address.
*Notice: In the ISR the number of POPs and PUSHes must be equal otherwise it might have a problem
in step 4. Because the service routine for an interrupt is too long to fit into the memory space allocated, a
JMP instruction is placed in the vector table to point at the address of the ISR. It redirects the processor
away from the interrupt vector table.
Upon reset, interrupts are disabled. D7 of SREG is responsible for enabling and disabling interrupts
globally. We set it HIGH with SEI and LOW with CLI. If I=1 , each interrupt is enabled by setting to
HIGH the interrupt enable (IE) flag bit for that interrupt. If I=0, no interrupt will be responded to, even
if the IE bit is high. When it is serving an interrupt , it writes I=0 so it cannot serve any more interrupts
until this one is over. If we want it to, we should write SEI in the ISR. With the RETI instruction, it
automatically writes I=1. In the ISR for TIMER0 , there is no need to clear TOV0 flag since the AVR clears
it internally upon jumping to the interrupt handling table.
We cannot change the reset location 0x00 and the timers’ overflow addresses. We just use them to redirect
in location with more space (arbitrary address) where we write the ISR.
13
MIKPO 2 -
14
MIKPO 2 -
15
MIKPO 2 -
The ISC2 bit of MCUCSR register defines whether INT2 activates in the falling edge or in the rising
edge(0 is falling edge triggered).
16
MIKPO 2 -
If two interrupts are activated at the same time, the interrupt with the higher priority is served first.
The lower the address in the interrupt vector, the higher the priority of the interrupt is.
3 ADC
For an n-bit ADC the step size is
Vref
2n
We also have n outputs
D0 − Dn−1
with
Vin
Dout =
step size
17
MIKPO 2 -
When SC (Start of Conversion) is activated the ADC starts converting the Vin to an n-bit digital number.
When the conversion is completed the EOC (End of Conversion) signal notifies the CPU that the converted
data is ready.
There are 5 major registers: ADCH(High Data), ADCL(Low Data), ADCSRA(ADC Control and Status
Register), ADMUX(ADC multiplexer selection register) and SPIOR(Special Function I/O register).
18
MIKPO 2 -
In AVR , the first conversion takes 25 ADC clocks cycles and every other 13 ADC cycles.
2. Turn on the ADC module in the AVR because it is disabled upon the power on reset.
4. Select voltage reference and ADC input channels. Use REFS0 and REFS1 bits in the ADMUX registers
to select voltage reference and the MUX4:0 bits in the ADMUX to select the ADC input channel.
6. Wait for the conversion to finish by polling the ADIF bit in ADCSRA.
7. After the ADIF bit has gone HIGH, read the ADCL and ADCH registers to get the digital data
output.Notice: You HAVE to read ADCL BEFORE ADCH
9. If you want to select another Vref source or input channel go back to step 4
1
2 .INCLUDE "M32DEF. INC"
3 LDI R16 , 0xFF
4 OUT DDRB, R16 ;MAKE PORTB AN OUTPUT
5 OUT DDRD, R16 ;MAKE PORTD AN OUTPUT
6 LDI R16 , 0
7 OUT DDRA, R16 ;MAKE PORTA AN INPUT FOR ADC
8 LDI R16 , 0x87
9 OUT ADCSRA, R16 ;ENABLE ADC AND SELECT CK/128
10 LDI R16 , 0xC0 ; 2.56 Vref , ADC0 SINGLE ENDED
11 OUT ADMUX, R16 ; INPUT, RIGHT−JUSTIFIED DATA
12 READ_ADC:
13 SBI ADCSRA, ADSC ;START CONVERSION
14 KEEP_POLLING: ;WAIT FOR CONVERSION TO END
15 SBIS ADCSRA, ADIF ; IS THE CONVERSION OVER YET?
16 RJMP KEEP_POLLING ;KEEP POLLING END OF CONVERSION
17 SBI ADCSRA, ADIF ;WRITE 1 TO CLEAR ADIF FLAG
18 IN R16 , ADCL ;YOU HAVE TO READ THE ADCL FIRST
19 OUT PORTD, R16 ; GIVE THE LOW BYTE TO PORTD
20 IN R16 , ADCH ;READ ADCH AFTER ADCL
19
MIKPO 2 -
3.3 DAC
Create a stair ramp using DAC
1
2 LDI R16 , 0xFF
3 OUT DDRB, R16 ;MAKE PORTB AN OUTPUT
4 AGAIN:
5 INC R16 ;INCREMENT R16
6 OUT PORTB, R16 ;SENT R16 TO PORTB
7 NOP ;LET DAC TO RECOVER
20
MIKPO 2 -
8 NOP
9 RJMP AGAIN
OCR0 + 1
Duty Cycle = · 100 for non-inverted mode
256
and
255 − OCR0
Duty Cycle = · 100 for inverted mode
256
Example: To generate a wave with duty cycle of 75% in non inverted mode, calculate the OCR0. Solution:
75 = (OCR0) · 100/256->OCR0=191
21
MIKPO 2 -
The wave generator is in non inverted Fast PWM mode, which means that on compare match the OC0 pin
will be set high. The OCR0 register is loaded with 00; so compare match occures when TCNT0 reaches
99. When the timer reaches the top value and overflows, the interrupt request occures, and the OCRo
22
MIKPO 2 -
bugger is loaded with 157(the 2’s complement of 99). The next time that the timer reaches the top value,
the contnetns of the OCR0 buffer(157) will be loaded into the OCR0 register. Then the second interrupt
occurs and the OCR0 buffer will be loaded with 99(2s complement of 157)
23
MIKPO 2 -
4.4 Difference betweeen Phase Correct PWM mode and Fast PWM mode
24
MIKPO 2 -
25
MIKPO 2 -
26
MIKPO 2 -
In non inverted PWM, the duty cycle of the generated wave increases when the value if OCR1A increases,
In inverted PWM, the duty cycle of generated wave decreases when the value of OCR1A increases. The
frequency of the generated wave is:
Foscillator
Fgenerated wave =
(Top + 1) · N
OCR1x + 1
Duty Cycle = · 100 for non-inverted mode
Top + 1
and
Top − OCR1x
Duty Cycle = · 100 for inverted mode
Top + 1
27
MIKPO 2 -
28
MIKPO 2 -
29
MIKPO 2 -
30
MIKPO 2 -
31
MIKPO 2 -
32
MIKPO 2 -
33
MIKPO 2 -
The Duty Cycle of the generated wave in Phase Correct PWM is:
OCR1A
Duty Cycle = · 100 for non-inverted mode
Top
and
Top − OCR1A
Duty Cycle = · 100 for inverted mode
Top
34
MIKPO 2 -
35
MIKPO 2 -
In modes 10 abd 11, the Top value can be specified by ICR1 and the OCR1A registers.
36
MIKPO 2 -
37
MIKPO 2 -
If we use mode 11 instead of mode 10, OCR1A is bufferd, and the contents of the buffer will be loaded
into OCR1A, when the timer reaches its top value. In mode 11 we can only use the OC1B wave generator
and we cannot use the OC1A wave generator since the OCR1A register is used for defining the Top value.
5 Examples
5.1 Flashing Led example ( credits to : Maylo)
• Led in PortB.3 +Vcc (PortPin = 0 -> Led On, Negative Logic)
• AVR 4MHz
For 0.5sec in 4Mhz we need ( f = N/T, f=1/T): 4000000 = N/0.5 -> N=4000000/2 = 26 cycles. (max for
8bits 28 − 1)
Timer counts clock cycles so we set prescaler to 1024. 2000000/1024=1953,25≃ 1953. This still doesn’s
fit in 8bits(255), but in 16bits(2 registers) If we chose 64 prescaler(from TCCRnB): 2000000/64=31250
(accuracy). 0x0000-31250 = -31250 in TCNT1. 31250 = 0111101000010010 so -31250 will be its complement.
If we add 1 to it then we have: 1000010111101110 ISR will reload timer1 every time it is called(CTC bit
in TCCRnB). To eneable timer we must:
38
MIKPO 2 -
39
MIKPO 2 -
17 CBI PORTB, 3
18 LOOP:
19 RJMP LOOP
6 Σημειώσεις
• LDI R20,1«5 means: set the 5th bit of R20 to 1.
• memory allocation:
.dseg
.org 0x0060− > f or Ram
array_f lag : .Byte n(bytes − 8 bit f or every cell)
.cseg
• store in RAM:
> initialize
ldi zh, high(array_f lag)
ldi zl, low(array_f lag)
> store
st z+, R16
40