Beruflich Dokumente
Kultur Dokumente
Contents
1 Interrupt Vectors and their Descriptions for ATmega128 2
2 External Interrupts 2
3 Ports 3
4 Sleep Mode 4
5 Timers 5
5.1 Intro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2 Timer 1,3 Operation Control . . . . . . . . . . . . . . . . . . . . 5
5.3 Timer 0 Operation Control . . . . . . . . . . . . . . . . . . . . . 6
5.4 Timer 2 Operation Control . . . . . . . . . . . . . . . . . . . . . 8
5.5 Timer Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.6 Interrupts and Flags . . . . . . . . . . . . . . . . . . . . . . . . . 9
7 Analog Comparator 11
8 Code Snippets 12
8.1 Initialization Routines . . . . . . . . . . . . . . . . . . . . . . . . 12
8.2 Timer 0: CTC, Pin toggle, ISR . . . . . . . . . . . . . . . . . . . 13
8.3 Polling for an A/D conversion . . . . . . . . . . . . . . . . . . . . 14
1
1 Interrupt Vectors and their Descriptions for
ATmega128
2 External Interrupts
Registers: EIMSK, EIFR, EICRA, EICRB, MCUCR
2
Enabling Interrupts To enable an external interrupt INTn, n=(0-7), enable
(logical 1) bit n in register EIMSK:
EIMSK BIT 7 6 5 4 3 2 1 0
INTn 7 6 5 4 3 2 1 0
EIFR BIT 7 6 5 4 3 2 1 0
INTFn 7 6 5 4 3 2 1 0
Interrupt Settings Finally, to set the trigger of interrupt INTn, choose one
of these functions:
bit ISCn1 bit ISCn0 what triggers an interrupt
0 0 Low level of input causes interrupt
0 1 Reserved (n=0-3), Any change(n=4-7)
1 0 Falling edge causes interrupt
1 1 Rising edge causes interrupt
Interrupt Sense Control Bits 0 and 1 for each of the 8 external interrupts
are arranged into EICRA and EICRB.
EICRA BIT 7 6 5 4 3 2 1 0
ISC ISC31 ISC30 ISC21 ISC20 ISC11 ISC10 ISC01 ISC00
EICRB BIT 7 6 5 4 3 2 1 0
ISC ISC71 ISC70 ISC61 ISC60 ISC51 ISC50 ISC41 ISC40
3 Ports
ATmega128 has 4 external ports, A, B, C, D. Each port has 3 assigned registers.
For these tables, x={A, B, C, D}
PORTx is used for writing data to port x by using out PORTx, Temp.
DDRx sets which bits of port x will be used for input and which for output:
ldi Temp, 0b11110000
out DDRB, Temp
lets us use PORTB7 - PORTB4 for output and PINB3 - PINB0 for input. The
full function of ports, as well as pull-up resistors, is summarized in Table 3. The
alternative function of PORTB pins is in Table 3
3
Table 2: Port Function
DDxn PORTxn PUD (SFIOR Bit) I/O PUR Comment
0 0 X Input No Tri-state (High impendance)
0 1 0 Input Yes Pin will source current if exit is low
0 1 1 Input No Tri-state (High impendance)
1 0 X Output No Output low (Sink)
1 1 X Output No Output high (Source)
4 Sleep Mode
AVR can enter a sleep mode to reduce energy consumption and noise (during
A/D Conversions). The sleep functionality is defined by bits 5 - 2 of MCUCR,
and sleep mode starts with the sleep command.
BIT 5 4 3 2
Name SE SM1 SM0 SM2
4
• Idle: SPI, USART, AnComp, A/DC, TWSI, Timers, Watchdog, Inter-
rupts are on (only CPU clock off)
• ADCNR: ADC, External Interrupts, TWSI, Timer0, Watchdog are on. A
conversion starts automatically (if A/D is turned on)
• Power-Down: External Interrupts, TWSI, Watchdog are on
• Power-Save: Same as Power-Down. If AS0 in ASSR is 1, TCCNT0 will
run and cause a wake-up on overflow or Output Compare
• Standby: If an external crystal is selected, this makes MCU go into
standby for 6 cycles
• Ext. Standby: Same as Standby, but TCCNT0 keeps running
5 Timers
5.1 Intro
ATmega128 has four timers:
1. Timer/Counter 0: 8-bit counter
2. Timer/Counter 1: 16-bit counter
3. Timer/Counter 2: 8-bit counter
4. Timer/Counter 3: 16-bit counter
Each timer comes with its own Timer/Counter Control Register (TCCRn).
In addition, 16-bit timers (n=1,3) also provide an Output Compare Register
(TCCRn) and an Input Capture Register (ICRn).
Timer interrupts are masked by Timer Interrupt Mask Register (TIMSK)
and the corresponding flags are stored in Timer Interrupt Flag Register (TIFR).
TCNTn Register 8-bit timers store their content in TCNT0 and TCNT2,
respectively. 16-bit timers use double-buffered registers TCNTnL and TCNTnH
to store their content. Specific care must be taken to load or store values in these
registers: When TCNTnL is read, hardware copies the content of TCNTnH to
a temporary register. After that, the correct value of TCNTnH can be read:
in r16, TCNT1L ; When this is done, Temp <- TCNT1H
in r17, TCNT1H ; Not the actual content of TCNT1H
This is done so a change in the timer value won’t mess up the values read:
If TCNT1H is 00000000 and TCNT1L is 11111111, reading TCNT1L without
using Temp would result in probably reading TCNT1H value as 00000001.
5
• Bits 7-2 set the Compare Output Mode for channels A, B, and C
• Bits 1 and 0 control waveform generation. (see Table 6)
6
Table 6: Waveform Generation Mode
Mode WGMn3:0 Mode TOP Update of OCRnx TOV Flag Set
0 0000 Normal 0xFFFF Immediate MAX
1 0001 PWM, Phase Correct, 8-bit 0x00FF TOP BOTTOM
2 0010 PWM, Phase Correct, 9-bit 0x01FF TOP BOTTOM
3 0011 PWM, Phase Correct,10-bit 0x03FF TOP BOTTOM
4 0100 CTC OCRnA Immediate MAX
5 0101 Fast PWM, 8-bit 0x00FF BOTTOM TOP
6 0110 Fast PWM, 9-bit 0x01FF BOTTOM TOP
7 0111 Fast PWM,10-bit 0x03FF BOTTOM TOP
8 1000 PWM Ph&Freq Correct ICRn BOTTOM TOP
9 1001 PWM Ph&Freq Correct OCRnA BOTTOM BOTTOM
10 1010 PWM, Phase Correct ICRn TOP BOTTOM
11 1011 PWM, Phase Correct OCRnA TOP BOTTOM
12 1100 CTC ICRn IMMEDIATE MAX
13 1101 reserved - - -
14 1110 FAST PWM ICRn BOTTOM TOP
15 1111 FAST PWM OCRnA BOTTOM TOP
7
5.4 Timer 2 Operation Control
TCCR2 Register The register is described below
BIT 7 6 5 4 3 2 1 0
Function FOC2 WGM20 COM21 COM20 WGM21 CS22 CS21 CS20
Table 10: Timer0, Timer2 - Compare Output for Phase Correct PWM mode
COMn1 COMn0 Description
0 0 Normal Port Operation
0 1 Reserved
1 0 Clear OCn on compare match when up counting set OCn in inverse
1 1 Set OCn on compare match when up counting, clear OCn in inverse
8
5.5 Timer Output
By setting the proper mode in the COM bits, the timer can automatically (i.e.,
without software intervention) change the logic level of some port bits. Table 3
shows the output pins for each timer. In order for these to be used as output,
a logical 1 must be written in the corresponding DDRB pin.
9
6 AVR A/D Converter
ATmega128 has an embedded A/D Converter. It converts analog signals present
in some of the PORTF pins to digital values, stored in ADCL and ADCH
V
registers. The input range [0 Vref ] is split in 2n intervals of width Q = 2ref
n .
For Vref =2.56V and n = 10, Q = 2.5mV. The digital output is calculated:
Y = round( VQin ) and is presented in ADCL and ADCH. If a differential input is
used, the output is in two’s complement.
The ADC is controlled by registers ADMUX and ADCSR.
10
Table 13: Analog Signal Options
MUX4:0 Input + Input - Gain MUX4:0 Input + Input - Gain
00000 ADC0 1 10000 ADC0 ADC1 1
00001 ADC1 1 10001 ADC1 ADC1 1
00010 ADC2 1 10010 ADC2 ADC1 1
00011 ADC3 1 10011 ADC3 ADC1 1
00100 ADC4 1 10100 ADC4 ADC1 1
00101 ADC5 1 10101 ADC5 ADC1 1
00110 ADC6 1 10110 ADC6 ADC1 1
00111 ADC7 1 10111 ADC7 ADC1 1
01000 ADC0 ADC0 10 11000 ADC0 ADC2 1
01001 ADC1 ADC0 10 11001 ADC1 ADC2 1
01010 ADC0 ADC0 200 11010 ADC2 ADC2 1
01011 ADC1 ADC0 200 11011 ADC3 ADC2 1
01100 ADC2 ADC2 10 11100 ADC4 ADC2 1
01101 ADC3 ADC2 10 11101 ADC5 ADC2 1
01110 ADC2 ADC2 200 11110 1.22 V
01111 ADC3 ADC2 200 11111 0V
7 Analog Comparator
The analog comparator of AVR compares two inputs and sets its output to 1
if Ain0 > Ain1 . It can be set to trigger Timer1’s Input Capture. It also has a
dedicated interrupt. AnComp’s inputs are PORTE2 and PORTE3 (Ain0 , Ain1 )
11
• ACIC - Analog Comparator Input Capture Enable: Input Capture of
Timer1 will be triggered by AnComp.
• ACIS1:0 - Interrupt Select (See Table 7)
8 Code Snippets
8.1 Initialization Routines
. i n c l u d e ” m128def . i n c ” ; alias file
. d e f temp = r 1 6 ; definition
. equ p i = 3 . 1 4 1 5 9 ; constant
. cseg
. o r g $0
12
jmp r e s e t ; reset interrupt vector
. o r g $xx
jmp ISR ; some ISR v e c t o r
reset :
main :
...
8.2 Timer 0: CTC, Pin toggle, ISR
. o r g $0
jmp r e s e t
. o r g $20
jmp T0 ISR
reset :
...
; timer 0 setup
l d i temp , 120
out OCR0, temp ; compare t i m e r with v a l u e 120
main :
rjmp main
...
T0 ISR :
push temp
i n temp , SREG
13
push temp
pop temp
out SREG, temp
pop temp
reti
8.3 Polling for an A/D conversion
reset :
; s e t up ADC c h a n n e l 3 , no a l i g n m e n t avcc v o l t a g e
l d i temp , 0 b010000111
out ADMUX, temp
; now ADCSRA
l d i temp , 0 b10100010
out ADCSRA, temp
s b i ADCSRA, ADSC ; s t a r t c o n v e r s i o n , we have f r e e r u n n i n g mode s o we do i t once
main :
s b i s ADCSRA, ADIF ; s k i p i f end o f c o n v e r s i o n
rjmp main
s b i ADCSRA, ADIF ; c l e a r i t
; do work h e r e
rjmp main
14