8051 Interrupts & Timers

advertisement
8051 Interrupts & Timers
Abdul Qadir Javaid
E204
abdulqadir_17@hotmail.com
abdul.q.javaid@theiet.org
Diagram taken from
‘The 8051 Microcontroller
Architecture,
Programming &
Applications’ by
Kenneth J. Ayala
•
Interrupts
• Two ways to make the computer program
make aware of the internal and external
conditions of the circuit
• Software instructions and the hardware
signals
• 5 interrupts in 8051
• 3 internal interrupts: timer flag 0, timer flag1
& Serial port interrupt
• 2 external at pins INT0 & INT1
• Timer flag interrupt: The timer/counter
overflow flag TF0 & TF1 is set to 1.
• Serial Port interrupt
• External interrupt: INT0 & INT1.
• 2 ways to set the interrupt flags IE0 & IE1 in
TCON to 1.
Interrupt Registers
Bit Symbol
7
EA
6
5
4
-ET2
ES
3
ET1
2
EX1
1
0
ET0
EX0
Function
Enable Interrupt bit. If logic 0 clears the
interrupts. Set to 1 enables.
Not implemented.
Reserved for future use.
Enable Serial port. If 1 then enables. If 0 then
disables.
Enable timer 1 overflow. If 1 then enables. If 0
then disables.
Enable external interrupt 1 if set to 1 to
enable INT1
Enable timer 0 overflow if set to 1.
Enable external interrupt 0 if set to 1 to
enable INT0
Bit
7
6
5
4
Symbol
--PT2
PS
3
2
1
0
PT1
PX1
PT0
PX0
Function
Not implemented
Not implemented
Set for future use
Priority for Serial port interrupt.
Set or cleared by programmer.
Priority of timer 1 overflow interrupt
Priority of external interrupt
Priority of timer 0 overflow interrupt
Priority of external interrupt 0
oInterrupt causes a hardware call to one of the memory locations
oProgrammer should place a service routine at that address
oInterrupt saves the PC of the program on the stack
Oscillator
‘f’ is smallest
interval of
time
1 instruction
takes 1 machine
cycle
12MHz crystal
1msec for one
instruction
TCON Register
Bit 7/5(TF1/TF0): Timer1/0 overflow flag. Set when timer rolls from all
ones to zeros. Cleared when processor executes the interrupt routine.
Bit 6/4(TR1/TR0): Timer1/0 run control bit. Set to one to enable timer
to count. Cleared to zero to stop. Does not reset.
Bit 3/1(IE1/IE0): External Interrupt 1/0 Edge flag. Set to 1 when highlow signal at port 3.3(INT1) & port 3.2(INT0). Cleared by interrupt
service routine.
Bit 2/0(IT1/IT0): External Interrupt 1/0 type control bit.
THX
THL
Not Bit Addressable
Bit 7/3 (Gate) : OR gate enable bit. Controls RUN/STOP of timer 1/0. If
TRx = 1, Gate = 1, timer runs when INT is high.If Gate = 0, timer runs
when TRx = 1.
Bit 6/2 (C/T) : Set to 1 for timer 1/0 to act as Counters for external
input pulses at pins 3.5(T1) or 3.4(T0). Set to 0 for timer.
Bit 5/1 (M1) : Timer/Counter Operating mode select bit 1.
Bit 4/0 (M0) : Timer/Counter Operating mode select bit 0.
M1
M0
Mode
0
0
0
0
1
1
1
0
2
1
1
3
TIMER MODE 0
• THX as 8-bit counter
• TLX as 5-bit counter
TIMER MODE 1
• Similar to Mode 0
• Behaves as a 16 bit timer
• E-g If its required for timer to overflow after
1000 counts then load FCh in THX and 18h in
TLX.
• 16 bit counter max value is (65535)
• If 6MHz crystal used then timer flag sets in
0.1311 sec
TIMER MODE 2
•
•
•
•
•
Only TLX used as counter
THX to hold a value
Auto-reload mode
TLX counts up from number in THX
Overflows & Initialised to the number in THX
TIMER MODE 3
• So far Timer 0 and Timer 1 has been shown to
be behaving independently
• Not true for Mode 3
• If mode 3 chosen for timer 0, then placing
timer1 in mode 3 causes it to stop counting
• TR1 & TF1 used by timer 0
• Timer 0 becomes 2 separate 8-bit counters
Contd....
• TL0 overflows(00h-FFh) & sets timer flag TF0
• TH0 behaves under the control of TR1 & sets
TF1 when overflows
• If timer 0 in mode 3: Timer 1 can be used in
mode 0, 1 & 2 with 1 exception: No interrupts
will be generated
• Timer1 can be used for baud rate generation
of serial port
Difference between Timer & Counter
• Source of clock pulses
• Timer : Clock pulses from oscillator
• Counter: Pin T0(P3.4) for counter 0
Pin T1(P3.5) for counter 1
C/T bit must be set for counter
• Maximum input frequency is
Oscillator frequency/24
Example
START
Turn off the LED
IF push button is On THEN
Initialize timer 1 to genrate interrupts
Wait for timer interrupt
ENDIF
END
Timer 1 initialization
START
Enable timer 1 interrupts
Set timer 1 to 8-bit auto reload mode
Load timer value
Enable microcontroller interrupts
Turn on timer 1
END
Timer 1 Interrupt service routine
START
IF the required time has elapsed THEN
Turn LED on or off
ENDIF
END
CODE
#include<reg51.h>
sbit LED = P1^7;
sbit push_button = P3^2;
int count;
void init_timer();
/*Main Program*/
main()
{
count = 0;
LED = 1;
while(push_button == 1)
{
}
init_timer();
for(;;)
{
}
}
/*Timer 1 Initialization routine*/
void init_timer()
{
ET1=1 ;
TMOD = 0x20;
TH1 = 0x6;
EA = 1;
TR1 = 1;
}
//Initialize count
//LED is OFF
//Scan for the push button
//Initialize timer1
//Endless loop
//End of main program
//Enable timer 1
//Timer 1 in mode 2
//For 250msec pulse
//Enable interrupts
//Turn on timer 1
Contd...
/*Timer 1 interrupt service routine*/
timer1() interrupt 3
{
count++;
//increment count
if(count ==100)
{
count = 0; //Reset count
LED = ~LED;
}
}
Download