Escolar Documentos
Profissional Documentos
Cultura Documentos
Week
1
Lecture (2h)
11
Lab 8
Tutorial 9
Lab 9
Tutorial 10
Lab 10
Tutorial 11
Lab 11
10
Tutorial 8
8
9
Lab 7
L9: Timers
Tutorial 7
L8: Serial communications
4
5
2
3
Tutorial (1h)
12
13
Lab 12
L13: Revision lecture
Lecture 9 references
Lecture 9 references
M. Mazidi, J. Mazidi, R. McKinlay, The 8051 microcontroller and
embedded systems using assembly and C, 2nd ed., Pearson
Prentice Hall, 2006, [Chapters 9].
Lam Phung
Lam Phung
Lecture 9s sequence
9.1
9.2
Timers in ATmega16
9.3
Timer applications
Lam Phung
get_device_status;
if (service_required){
}
normal_execution;
}
normal_execution
Interrupt
while (1){
Polling
service_routine;
Lam Phung
interrupt
signal
runs
interrupt_
service_
routine
instruction k+1
Using polling, the CPU must continually check the devices status.
6. CPU executes the ISR
Using interrupt:
A device will send an interrupt signal when needed.
Lam Phung
Lam Phung
Vector No.
Program Address
$000
Description
Reset
$002
INT0_vect
$004
INT1_vect
$006
TIMER2_COMP_vect
$008
TIMER2_OVF_vect
Timer/Counter2 Overflow
$00A
TIMER1_CAPT_vect
$00C
TIMER1_COMPA_vect
$00E
TIMER1_COMPB_vect
$010
TIMER1_OVF_vect
Timer/Counter1 Overflow
10
$012
TIMER0_OVF_vect
Timer/Counter0 Overflow
11
$014
SPI_STC_vect
12
$016
USART_RXC_vect
USART, Rx Complete
13
$018
USART_UDRE_vect
14
$01A
USART_TXC_vect
USART, Tx Complete
15
$01C
ADC_vect
16
$01E
EE_RDY_vect
EEPROM Ready
17
$020
ANA_COMP_vect
Analog Comparator
18
$022
TWI_vect
19
$024
INT2_vect
20
$026
TIMER0_COMP_vect
21
$028
SPM_RDY_vect
Lam Phung
10
Vector No
An interrupt with a lower Vector No will have a higher priority.
2. Use C macro ISR() to declare the interrupt handler and update IVT.
Program Address
11
Lam Phung
12
The C macro ISR() is used to declare the handler for a given interrupt.
It basic syntax is given as
ISR(interrupt_vector_name){
// code for interrupt handler here
}
where interrupt_vector_name is given in Table 9.1.
Example: To process interrupt 'RXD Complete and put the received
character in Port B, we write
ISR(USART_RXC_vect){
PORTB = UDR;
}
Lam Phung
13
Description
RESET_vect
Reset
INT0_vect
INT1_vect
TIMER2_COMP_vect
TIMER2_OVF_vect
Timer/Counter2 Overflow
TIMER1_CAPT_vect
TIMER1_COMPA_vect
TIMER1_COMPB_vect
TIMER1_OVF_vect
Timer/Counter1 Overflow
10
TIMER0_OVF_vect
Timer/Counter0 Overflow
11
SPI_STC_vect
12
USART_RXC_vect
USART, Rx Complete
13
USART_UDRE_vect
14
USART_TXC_vect
USART, Tx Complete
15
ADC_vect
16
EE_RDY_vect
EEPROM Ready
17
ANA_COMP_vect
Analog Comparator
18
TWI_vect
19
INT2_vect
20
TIMER0_COMP_vect
21
SPM_RDY_vect
14
Lam Phung
TXCIE
UDRIE
RXEN
TXEN
UCSZ2
RXB8
TXB8
at baud rate 1200, no parity, 1 stop bit, 8 data bits, clock speed 1MHz.
1 to enable RX Complete Interrupt, valid only if Global Interrupt Flag = 1 and RXC = 1
For any interrupt, the ATmega16 manual can be searched to learn how
interrupt is triggered.
Lam Phung
15
Lam Phung
16
#include <avr/io.h>
#include <avr/interrupt.h>
Write
a C the
interrupt-driven
program
to toggle port B whenever a switch
To test
serial RXD interrupt
example:
on the STK500 board is pressed. The program should use an external
Connect RXD pin (pin D.0) to RXD pin of RS232 Spare.
interrupt.
Connect TXD pin (pin D.1) to TXD pin of RS232 Spare.
void USART_init(void){
// Normal speed, disable multi-proc
UCSRA = 0b00000000;
// Enable Tx and Rx pins, enable RX interrupt
UCSRB = 0b10011000;
//
//
//
//
Lam Phung
initialise USART
enable interrupt subsystem globally
set port B for output
infinite loop
17
18
External
interrupts on ATmega16
and
ATmega8515
are similar.
Write
a C interrupt-driven
program to
toggle
port B whenever
a switch
#include <avr/io.h>
void USART_init(void){
// Normal speed, disable multi-proc
UCSRA = 0b00000000;
// Enable Tx and Rx, disable interrupts
UCSRB = 0b00011000;
int main(void) {
USART_init(); // initialise USART
DDRB = 0xFF; // set port B for output
while (1) {
// infinite loop
// Poll until RXC flag = 1
while ((UCSRA & (1<<RXC)) == 0x00){;}
PORTB = UDR; // received character is displayed on port B
}
return 0;
}
University of Wollongong, 2010.
For comparison, the program below uses polling for the same effect.
Lam Phung
Lam Phung
Lam Phung
20
Read/Write
Initial value
INT1
INT0
R/W
0
R/W
0
INT2
R/W
0
IVSEL
IVSEL
R
0
R
0
R
0
R/W
0
R/W
0
21
Lam Phung
R/W
0
R/W
0
SM2
SE
R/W
0
SM1
R/ W
0
SM0
22
Lam Phung
R/W
0
R/W
0
R/W
0
R/W
0
ISC11
ISC10
ISC01
ISC00
MCUCR
R/W
0
JTD
R/W
0
ISC2
R/W
0
-
R/ W
0
JTRF
R/W
0
R/W
0
R/W
0
R/W
0
WDRF
BORF
EXTRF
PORF
MCUCR = (1<<ISC10);
MCUCSR
23
Lam Phung
24
#include <avr/io.h>
Write
a C the
interrupt-driven
program
to toggle port B whenever a switch
To test
external interrupt
example:
on the STK500 board is pressed. The program should use an external
Connect INT1 pin (pin D.3) to switch SW7 on STK500 board.
interrupt.
Connect GRD pin of Port D to GRD pin of SW connector.
#include <avr/interrupt.h>
ISR(INT1_vect){
PORTB = (~PORTB);
int main(void) {
DDRB = 0xFF;
// infinite loop
return 0;
}
Lam Phung
25
Lam Phung
26
Timer terminology
Input Capture:
An input signal is connected to a pin, designated as input capture
pin, of the timer.
Examples include
Output Compare:
Lam Phung
27
Lam Phung
28
29
Lam Phung
Lam Phung
30
Study plan
Timer 1
Timer 2
In Lecture 9, we focus on
- 8-bit counter
- 16-bit counter
- 8-bit counter
- 10-bit prescaler
- 10-bit prescaler
- 10-bit prescaler
- PWM
- PWM
- PWM
- Frequency generation
- Frequency generation
- Frequency generation
- Event counter
- Event counter
- Event counter
- Output compare
- Output compare: 2
- Output compare
operations of Timer 1,
using Timer 1 overflow interrupt,
Functions
channels
- Input capture
Operation
modes
- Normal mode
- Normal mode
- Normal mode
- Clear timer on
- Clear timer on
- Clear timer on
compare match
compare match
compare match
- Fast PWM
- Fast PWM
- Fast PWM
31
Lam Phung
32
Timer 1: An overview
16-bit counter.
External clock
pin
Timer/Counter
Control Registers
33
Lam Phung
34
used in this
lecture
Lam Phung
35
Lam Phung
36
Lam Phung
37
ICNC1
ICES1
WGM13 WGM12
CS12
CS11
CS10
Lam Phung
38
Clock select
input capture edge select: 1 will select rising edge, 0 will select falling edge
If using the internal clock, we can set Timer 1 to run at a speed that is
8, 64, 256 or 1024 times slower than the internal clock.
Lam Phung
39
Lam Phung
40
OCIE2
TOIE2
OCIE0
TOIE0
OCF2
TOV2
ICF1
OCF1A OCF1B
TOV1
OCF0
TOV0
For Timer 0
For Timer 0
This register has flags that indicate when a timer interrupt occurs.
University of Wollongong, 2010.
Lam Phung
41
Lam Phung
42
Analysis
Internal system clock: 1MHz.
With no prescaler, Timer 1 will increment every 1 s.
Coding
Write code to enable & intercept Timer 1 overflow interrupt.
Use interrupt handler to count the number of overflows.
When number of overflows = 31, toggle port B.
Lam Phung
43
Lam Phung
44
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(TIMER1_OVF_vect){
// handler for Timer1 overflow interrupt
overflow_count++;
// increment overflow count
if (overflow_count >= 31){ // when 2s has passed
overflow_count = 0;
// start new count
PORTB = ~PORTB;
// toggle port B
}
}
int main(void) {
DDRB = 0xFF;
// set port B for output
PORTB = 0x00;
// initial value of PORTB
overflow_count = 0; // initialise overflow count
TCCR1A = 0b00000000; // normal mode
TCCR1B = 0b00000001; // no prescaler, internal clock
Timer Overflows
TCNT1
n=0
TCNT1 = 0
TCNT1
n
time interval t
// infinite loop
start at time t1
stop at time t2
}
Lam Phung
45
Lam Phung
46
volatile uint32_t n;
ISR(TIMER1_OVF_vect){
// handler for Timer1 overflow interrupt
n++;
// increment overflow count
}
int main(void) {
int i, j;
uint32_t elapse_time;
Approach:
Clear Timer 1 when the code starts.
Record Timer 1 when the code finishes.
n = 0;
// reset n
TCNT1 = 0;
// reset Timer 1
sei();
// enable interrupt subsystem globally
// ----- start code -------------for (i = 0; i < 100; i++)
for (j = 0; j < 1000; j++){;}
// ----- end code ----------------elapse_time = n * 65536 + (uint32_t) TCNT1;
cli();
// disable interrupt subsystem globally
return 0;
}
Lam Phung
47
Lam Phung
48
Implementation:
TCCR1A = 0b00000000;
TCCR1B = 0b00000001;
Input capture interrupt is triggered.
Read ICR1 register.
period
Lam Phung
square waveform
49
measure_period.c
50
Testing measure_period.c
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
Write
a C the
interrupt-driven
program
to toggle port B whenever a switch
To test
code for measuring
period:
on the STK500 board is pressed. The program should use an external
Connect Input Capture pin (D.6) to square wave generator on
interrupt.
WishMaker.
uint16_t period;
ISR(TIMER1_CAPT_vect){
period = ICR1;
TCNT1 = 0;
PORTB = ~(period >>
}
Lam Phung
int main(void) {
DDRB = 0xFF;
TCCR1A = 0b00000000;
TCCR1B = 0b11000001;
TIMSK = 0b00100000;
sei();
while (1){;}
return 0;
//
//
//
//
//
normal mode
no prescaler, rising edge, noise canceller
enable Timer 1 input capture interrupt
enable interrupt subsystem globally
infinite loop
}
Lam Phung
51
Lam Phung
52
Extending measure_period.c
Summary
What we learnt in this lecture:
The code given here assumes that there is no timer overflow between
two rising edges of the square signal.
In Lab 9, you are required to extend the code so that a correct period is
measured for low-frequency signals.
Lab 9: Timers
Complete the online Pre-lab Quiz for Lab 9.
The measure period should also be sent through the serial port to PC
for testing.
Lam Phung
53
54