ATmega103 Timer0 and Interrupts 23/02/20 Tim Sumner, Imperial
ATmega103 Timer0 and Interrupts 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 1 The Need for Processor Interrupts Up to now if you wanted to do something in a program as soon as a bit was set (key pressed, bit set in a register, voltage exceeded a given threshold,) you had to keep reading the bit until it changed ! This is clearly not an efficient way of doing things .. This is why interrupts were introduced as a means of getting the processors attention on demand Tim Sumner, Imperial College, 23/02/20 Rm: 1009, x47552 2 Processor Interrupts Interrupts are subroutine calls initiated not by an rcall command but by hardware signals. These hardware signals cause the processor to jump to interrupt service routines. At the end they return control to your program just where it was just before the
interrupt occurred. 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 3 The ATmega103 Timers and Interrupts A device that counts time (Timer) could be one of the reasons for interrupt (internal) 24 different reasons to interrupt the CPU and execute an interrupt service routine. Eight external interrupt inputs 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 4 The ATmega103 Memory Map The first 24 2-byte
addresses in flash program memory are reserved for interrupts: your program jumps to one of these addresses if an interrupt occurs. 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 5 Interrupts mapping ;Address;Labels;Code; Comments 23/02/20 $0000 jmp RESET ; Reset Handler $0002 jmp EXT_INT0 ; IRQ0 Handler - PortD $0004 jmp EXT_INT1 ; IRQ1 Handler - PortD $0006 jmp EXT_INT2 ; IRQ2 Handler - PortD
$0028 jmp UART_TXC ; UART TX Complete Handler $002A jmp ADC ; ADC Conversion Complete Handler $002C jmp EE_RDY ; EEPROM Ready Handler $002E jmp ANA_COMP ; Analog Comparator Handler Tim Sumner, Imperial College, Rm: 1009, x47552 7 Using interrupts Global enable via the status register Masks to work at bit level within devices Control registers to select type of signal 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552
8 Global Level: Status Register D7 D6 D5 D4 D3 D2 D1 D0 I T H S V N Z C By now you should know what
The V,N,Z,C bits are. In order to use any interrupts on ATmega103 you must set the I bit using the command sei 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 9 At device level: EIMSK use to mask which external interrupts are used EICR used to control how interrupts are recognised EIFR flags to show which have been triggered 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 10 Example: The Timer/Counter Mask Register TIMSK 23/02/20 D7 D6
1 PCK0/1024 Tim Sumner, Imperial College, Rm: 1009, x47552 14 Timer/Counter0 TCNT0 D7 D6 D5 D4 D3 D2 D1 D0 This is the counter preset .These 8-bit registers contain the value of the Timer/Counters. Both Timer/Counters are realized as up or up/down (in PWM mode) counters with read and write access. If the Timer/Counter is written to and a clock source is selected, it continues counting in the timer clock cycle after it is preset with the written value. 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 15 Example Program In the example program the 4 Mhz
clock of the ATmega103 is pre-scaled by 256 and the timer zero is loaded with $B2. So the counter overflows ($00) every 250 x 256 x ($FF-$B2 + 1) nsec (approx every 5 msec) and causes an interrupt. Every 200 interrupts a counter is incre- mented and the results is displayed on the PORTB LEDs. 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 16 Interrupt Section jmp Init jmp xxx ; 2 word instruction to set correct vector ; next interrupt nop ; use this two liner if no routine available reti . . . jmp TIM0_OVF ; Timer 0 Overflow interrupt Vector nop ; Vector Addresses are 2 words apart reti .
. . ;******************************************************* At Program address 0 your program jumps to the initialization Here it comes under hardware control when Timer0 overflows 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 17 Interrupt initialization ; ; **** Timer0 Setup Code **** ; ldi r16,$06 ; Timer 0 Setup out TCCR0, r16 ; Timer - PRESCALE TCK0 BY 256 ; (devide the 4 Mhz clock by 256) ldi r16,$b2 ; load timer with n=178 out TCNT0,r16
; The counter will go off ; in (256 - n)*256*250 nsec ; ; **** Interrupts Setup Code **** ; ldi r16, $01 ; Timer Interrupt Enables out TIMSK, r16 ; T0: Overflow ; 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 18 Interrupt Service Routine ;******************************************** TIM0_OVF: in R4,SREG ;save SREG inc r17 ; increment cycle nop cpi r17,$C8 ;compare cycle with 200 brne again ;if <> jump to again out PORTB, r18 ; send stuff to PORTB inc r18 ; Increment the portB number clr r17 ;clear cycle and start counting
; again 200 interrupts again: ldi r16,$B2 ;reload timer value out TCNT0,r16 out SREG,r4 ;restore sreg reti ;*********************** 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 When an interrupt occurs : D7 of SREG is set to 0 and the program jumps to the interrupt service routine It is a good idea to save the information stored on the status register and restore it at the end of the program reti sets the interrupt bit, in the SREG, back to 1 so the next interrupt can be serviced
19 Main Program Main: ; ; waisting time nop nop nop nop ; rjmp main But the timer is counting in the background and when it overflows it causes an interrupt forcing the program to execute the interrupt service routine. After that it comes back in the loop where it was when the interrupt occurredsee STUDIO demo. 23/02/20 Tim Sumner, Imperial College, Rm: 1009, x47552 20
Elderly Frequency of cancer increases with age Most cancer deaths occur between 55-75 Children 10% of all childhood deaths Leukemia/lymphoma, CNS tumors, sarcoma Neoplasia Outline Tumor nomenclature Tumor characteristics Epidemiology Cancer incidence Environmental variables Age Heredity Three Types of Hereditary...
33 Days to Morning Glory Retreat. The . Consoling the Heart of Jesus . retreat consists of 10 DVD talksthat are 35 minutes in length and a . Retreat Companion. for each participant. Depending on how . our 33 Days...
U predvečerje, iznenada, Ni od kog iz dubine gledan, Pojavio se ponad grada Oblak jedan. Vjetar visine ga je njiho, I on je stao da se žari, Al oči sviju ljudi bjehu Uprte u zemne stvari. I svak je išo...
Angular and linear adjustments are made simultaneously. Hand methods are long and complex not often done. Computer adjustment through existing software make it feasible, which is why it is often used today Area, Inverse, Intersection Once rectangular coordinates are established...
Critique le mercantilisme (où seules les exportations comptent) Critique le pop internationalism = il existe une contrainte extérieure qui nécessite des sacrifices Critique le protectionnisme La participation aux échanges est source de richesses Alors comment expliquer que certains pays ne...