1. registers used in timer/counter th0, tl0, th1, tl1 tmod (timer mode register) tcon (timer...
TRANSCRIPT
11
Registers Used in Timer/CounterRegisters Used in Timer/Counter
TH0, TL0, TH1, TL1 TH0, TL0, TH1, TL1 TMOD (Timer mode register)TMOD (Timer mode register) TCON (Timer control register)TCON (Timer control register)
22
Timer registerTimer register
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
TH0 /TH1 TL0/TL1
33
TMOD RegisterTMOD Register::
Gate : When set, timer only runs while INT(0,1) is high.
C/T : Counter/Timer select bit.
M1 : Mode bit 1. M0 : Mode bit 0.
44
TCON Register:TCON Register:
TF1: Timer 1 overflow flag. TR1: Timer 1 run control bit. TF0: Timer 0 overflag. TR0: Timer 0 run control bit. IE1: External interrupt 1 edge flag. IT1: External interrupt 1 type flag. IE0: External interrupt 0 edge flag. IT0: External interrupt 0 type flag.
55
Timer modesTimer modes
Timer Mode 0 Timer Mode 0 :: Emulates 8048 counter/timer (13-bits).Emulates 8048 counter/timer (13-bits). 8-bit counter (TL0 or TL1).8-bit counter (TL0 or TL1). 5-bit prescaler (TH0 or TH1).5-bit prescaler (TH0 or TH1).
66
Timer Mode 1 Timer Mode 1 :: Simple 16-bit counter.Simple 16-bit counter. -Timer Mode 2 :-Timer Mode 2 : 8-bit auto-reload.8-bit auto-reload. Counter in TL0 or TL1.Counter in TL0 or TL1. Reload value in TH0 or TH1.Reload value in TH0 or TH1. Provides a periodic flag or interruptProvides a periodic flag or interrupt
77
Timer Mode 3 Timer Mode 3
Splits timer 0 into two 8-bit counter/timers.Splits timer 0 into two 8-bit counter/timers. First counter (TLO) acts like mode 0,First counter (TLO) acts like mode 0,
without prescaler.without prescaler. Second counter (TH0):Second counter (TH0): Counts CPU cycles.Counts CPU cycles. Uses TR1 (timer 1 run bit) as enable.Uses TR1 (timer 1 run bit) as enable. Uses TF1 (timer 1 overflow bit) as flag.Uses TF1 (timer 1 overflow bit) as flag. Uses Timer 1 interrupt.Uses Timer 1 interrupt.
88
TIMING MODE DIAGRAMTIMING MODE DIAGRAM
99
ANIMATED TIMER MODE:ANIMATED TIMER MODE:
TMOD TH1 TL1
FF
FB
TR1
FFFB FFFC FFFD FFFE FFFFFFFF+01
1 00 1
SFR
TF1
1010
ANIMATED TIMER MODE:ANIMATED TIMER MODE:
TMOD TH1 TL1
FF
FB
TR1
FFFB FFFC FFFD FFFE FFFFFFFF+01
1 00 1
SFR
TF1
1111
1212
SAMPLE PGM FOR TIMER:SAMPLE PGM FOR TIMER:
ORG 0HORG 0H START:SETB P1.0START:SETB P1.0 ACALL DELAYACALL DELAY CLR P1.0CLR P1.0 ACALL DELAYACALL DELAY SJMP STARTSJMP START
1313
SAMPLE PROGRAMSAMPLE PROGRAM
DELAY:MOV TMOD,#0001 0000B ;10HDELAY:MOV TMOD,#0001 0000B ;10H
MOV TL1,#00MOV TL1,#00
MOV TH1,#00MOV TH1,#00
SETB TR1SETB TR1
JNB TF1,LOOPJNB TF1,LOOP
CLR TR1CLR TR1
CLR TF1 CLR TF1
RETRET1414
1515
SCON REGISTERSCON REGISTER
1616
SM0,SM1SM0,SM1
- SM0, SM1 = Serial Mode:- SM0, SM1 = Serial Mode: 00 = Mode 0 : Shift register I/O expansion.00 = Mode 0 : Shift register I/O expansion. 01 = Mode 1 : 8-bit UART with variable baud 01 = Mode 1 : 8-bit UART with variable baud
rate.rate. 10 = Mode 2 : 9-bit UART with fixed baud 10 = Mode 2 : 9-bit UART with fixed baud
rate.rate. 11 = Mode 3 : 9-bit UART with variable baud 11 = Mode 3 : 9-bit UART with variable baud
rate.rate.
1717
SM2SM2
- SM2 :- SM2 : Mode 0 : Not used.Mode 0 : Not used. Mode 1 : 1 = Ignore bytes with no stop bit.Mode 1 : 1 = Ignore bytes with no stop bit. Mode 2,3 : 0 = Set receive interrupt (RI) on Mode 2,3 : 0 = Set receive interrupt (RI) on
all bytes.all bytes. : 1 = Set RI on bytes where bit 9 = 1.: 1 = Set RI on bytes where bit 9 = 1.
1818
REN = Enables receiver.REN = Enables receiver. - TB8 = Ninth bit transmitted (in modes 2 - TB8 = Ninth bit transmitted (in modes 2
and 3).and 3). - RB8 = Ninth bit received:- RB8 = Ninth bit received: Mode 0 : Not used.Mode 0 : Not used. Mode 1 : Stop bit.Mode 1 : Stop bit. Mode 2,3 : Ninth data bit.Mode 2,3 : Ninth data bit. - TI = Transmit interrupt flag.- TI = Transmit interrupt flag. - RI = Receive interrupt flag.- RI = Receive interrupt flag.
1919
2020
BAUD RATEBAUD RATE
TH1(DECIMAL) TH1(HEX) SMOD=0 SMOD=1
-3 FD 9,600 19,200
-6 FA 4,800 9,600
-12 F4 2,400 4,800
-24 F8 1,200 2,400
2121
2222
2323
Mode 1:TXMode 1:TX
2424
MODE 1:TXMODE 1:TX
8 BIT DATASBUF
DATA
8
BIT DATA
STOP BIT
START BIT
PARALLEL TO SERIAL TX
SERIAL DATA TRANSFER
STARTSTOP
TI = 0TI=1
2525
MODE 1:TXMODE 1:TX
8 BIT DATASBUF
DATA
8
BIT DATA
STOP BIT
START BIT
PARALLEL TO SERIAL TX
SERIAL DATA TRANSFER
STARTSTOP
TI = 0TI=1
2626
DATA TX SERIALLY:DATA TX SERIALLY:
Load TMOD RegisterLoad TMOD Register ;;timer1timer1 Load TH1 or TL1 values ; for Load TH1 or TL1 values ; for Baud rateBaud rate Load SCON register; Load SCON register; serial mode 0,1,2,3serial mode 0,1,2,3 SET TR1 ;SET TR1 ;To start the timerTo start the timer Character byte is written to SBUF;Character byte is written to SBUF; Check if TI=1;Check if TI=1; CLR TICLR TI Reload the value again.Reload the value again.
2727
SAMPLE PROGRAM:TXSAMPLE PROGRAM:TX MOV TMOD,#20HMOV TMOD,#20H MOV TH1,#-3MOV TH1,#-3 MOV SCON,#50HMOV SCON,#50H SETB TR1SETB TR1 AGAIN:MOV SBUF,#’Y’AGAIN:MOV SBUF,#’Y’ JNB TI ,HereJNB TI ,Here CLR TICLR TI SJMP AGAINSJMP AGAIN
2828
SERIAL DATA TRANSFERSERIAL DATA TRANSFER
DISPLAY
Y
N
8051
2929
SAMPLE PROGRAM:SAMPLE PROGRAM: MOV TMOD,#20HMOV TMOD,#20H MOV TH1,#-3MOV TH1,#-3 MOV SCON,#50HMOV SCON,#50H SETB TR1SETB TR1 AGAIN:MOV SBUF,#’Y’AGAIN:MOV SBUF,#’Y’ JNB TI,HereJNB TI,Here CLR TICLR TI MOV P1,#’N’MOV P1,#’N’ SJMP AGAINSJMP AGAIN
3030
Mode 1:RXMode 1:RX
3131
MODE 1:RXMODE 1:RX
SBUF
8
BIT DATA
STOP BIT
START BIT
PARALLEL TO SERIAL TX
SERIAL DATA RECEIVE
STOPSTART
8 BIT DATA
DATA RX
RI=0RI=1
3232
SAMPLE PROGRAM:RXSAMPLE PROGRAM:RX
Load Load TMODTMOD Value. Value. Load TH1 value ;baud rateLoad TH1 value ;baud rate Load SCON RegisterLoad SCON Register Set TR1 To start timer.Set TR1 To start timer. Clear RIClear RI Check the RI each time.Check the RI each time. When RI =1,Load the value from SBUF.When RI =1,Load the value from SBUF.
3333
SAMPLE PROGRAM:RXSAMPLE PROGRAM:RX
MOV TMOD,#20HMOV TMOD,#20H MOV TH1,#-3MOV TH1,#-3 MOV SCON,#50HMOV SCON,#50H SETB TR1SETB TR1 CLR RICLR RI LOOP:JNB RI,LOOPLOOP:JNB RI,LOOP MOV A,SBUFMOV A,SBUF ENDEND
3434
SERIAL PORT TRANSMITTERSERIAL PORT TRANSMITTER
3535
SERIAL MODE 2SERIAL MODE 2
8 BIT DATASBUF
DATA
8
BIT DATA
STOP BIT
START BIT
PARALLEL TO SERIAL TX
SERIAL DATA TRANSFER
STARTSTOP
TI = 0TI=1
TB8=0 TB8=1
3636
RECEIVE DATARECEIVE DATA
3737
SERIAL MODE 2 :RXSERIAL MODE 2 :RX
SBUF
8
BIT DATA
STOP BIT
START BIT
PARALLEL TO SERIAL TX
SERIAL DATA RECEIVE
STOPSTART
8 BIT DATA
DATA RX
RI=0RI=1
RB8=0
3838
Interrupt :Interrupt :
3939
Interrupts ProgrammingInterrupts Programming
An An interruptinterrupt is an external or internal event that is an external or internal event that interrupts the microcontroller to inform it that a device interrupts the microcontroller to inform it that a device needs its service.needs its service.
Interrupts vs. PollingInterrupts vs. Polling A single microcontroller can serve A single microcontroller can serve severalseveral devices. devices. ThThereere are are twotwo ways to do that: ways to do that:
– interrupts interrupts – polling.polling.
The program which is associated with the interrupt is The program which is associated with the interrupt is called the called the interrupt service routineinterrupt service routine (ISR) or (ISR) or interrupt interrupt handlerhandler..
4040
Steps in executing an interruptSteps in executing an interrupt FFinishinish current current instruction and s instruction and saaves the PC on stack.ves the PC on stack.
JJumps to a fixed location in memory umps to a fixed location in memory depend on type of depend on type of interruptinterrupt
SStarts to execute the interrupt service routine until RETI tarts to execute the interrupt service routine until RETI (return from interrupt)(return from interrupt)
Upon executing the RETI the microcontroller returns to Upon executing the RETI the microcontroller returns to the place where it was interrupted. Getthe place where it was interrupted. Get poppop PC from PC from stack stack
4141
PROGRAM COUNTERPROGRAM COUNTER
It saves the address next insturction going It saves the address next insturction going to be executed.to be executed.
4242
MAIN PGM
ISRINTERRUPTPROGRAM
END
4343
ExampleExample A 10khz square wave with 50% duty cycleA 10khz square wave with 50% duty cycle
ORG ORG 00 ;Reset entry poit;Reset entry poit
LJMPLJMP MAINMAIN ;Jump above interrupt;Jump above interrupt
ORGORG 000BH000BH ;Timer 0 interrupt vector;Timer 0 interrupt vector
T0ISR:CPLT0ISR:CPL P1.0P1.0 ;Toggle port bit;Toggle port bit
RETIRETI ;Return from ISR to Main program;Return from ISR to Main program
ORG 0030HORG 0030H ;Main Program entry point;Main Program entry point
0030:MAIN:0030:MAIN: MOVMOV TMOD,#02HTMOD,#02H ;Timer 0, mode 2;Timer 0, mode 2
0032:0032: MOVMOV TH0,#-50TH0,#-50 ;50 us delay;50 us delay
0034:0034: SETBSETB TR0TR0 ;Start timer;Start timer
0036:0036: MOVMOV IE,#82HIE,#82H ;Enable timer 0 interrupt;Enable timer 0 interrupt
0038:0038: SJMPSJMP $$ ;Do nothing just wait;Do nothing just wait
ENDEND
A 10khz square wave with 50% duty cycleA 10khz square wave with 50% duty cycle
ORG ORG 00 ;Reset entry poit;Reset entry poit
LJMPLJMP MAINMAIN ;Jump above interrupt;Jump above interrupt
ORGORG 000BH000BH ;Timer 0 interrupt vector;Timer 0 interrupt vector
T0ISR:CPLT0ISR:CPL P1.0P1.0 ;Toggle port bit;Toggle port bit
RETIRETI ;Return from ISR to Main program;Return from ISR to Main program
ORG 0030HORG 0030H ;Main Program entry point;Main Program entry point
0030:MAIN:0030:MAIN: MOVMOV TMOD,#02HTMOD,#02H ;Timer 0, mode 2;Timer 0, mode 2
0032:0032: MOVMOV TH0,#-50TH0,#-50 ;50 us delay;50 us delay
0034:0034: SETBSETB TR0TR0 ;Start timer;Start timer
0036:0036: MOVMOV IE,#82HIE,#82H ;Enable timer 0 interrupt;Enable timer 0 interrupt
0038:0038: SJMPSJMP $$ ;Do nothing just wait;Do nothing just wait
ENDEND4444
INTERRUPT PGMINTERRUPT PGM
MAIN PGM
ISR
STACK POINTER
0B
0A
09
08 PCL
PCH
INTERRUPT
38
00
4545
Interrupt SourcesInterrupt Sources
Original 8051 has 6 sources of interruptsOriginal 8051 has 6 sources of interrupts– Reset Reset – Timer 0 overflowTimer 0 overflow– Timer 1 overflowTimer 1 overflow– External Interrupt 0External Interrupt 0– External Interrupt 1External Interrupt 1– Serial Port events (buffer full, buffer empty, etc)Serial Port events (buffer full, buffer empty, etc)
Enhanced version has 22 sourcesEnhanced version has 22 sources– More timers, programmable counter array, ADC, more external More timers, programmable counter array, ADC, more external
interrupts, another serial port (UART)interrupts, another serial port (UART)
4646
Interrupt VectorsInterrupt Vectors
Each interrupt has a Each interrupt has a specificspecific place in place in codecode memory where memory where program execution (interrupt service routine) begins.program execution (interrupt service routine) begins.
External Interrupt 0: External Interrupt 0: 0003h0003h
Timer 0 overflow: Timer 0 overflow: 000Bh000Bh
External Interrupt 1:External Interrupt 1: 0013h0013h
Timer 1 overflow: Timer 1 overflow: 001Bh001Bh
Serial : 0023hSerial : 0023h
Timer 2 overflowTimer 2 overflow(8052+)(8052+) 002bh 002bh
Note: that there are only 8 memory locations between vectors.
4747
SJMPSJMP main main ORGORG 03H03H
ljmpljmp int0srint0srORGORG 0BH0BHljmpljmp t0srt0srORGORG 13H13Hljmpljmp int1srint1srORGORG 1BH1BHljmpljmp t1srt1srORGORG 23H23Hljmpljmp serialsrserialsrORGORG 30H30H
main:main: ……
ENDEND
ISRs and Main Program in 8051
4848
Interrupt Enable Register :Interrupt Enable Register :
EA : Global enable/disable. --- : Undefined. ET2 :Enable Timer 2 interrupt. ES :Enable Serial port interrupt. ET1 :Enable Timer 1 interrupt. EX1 :Enable External 1 interrupt. ET0 : Enable Timer 0 interrupt. EX0 : Enable External 0 interrupt..
4949
Enabling and disabling an interrupt
by bit operationRecommended in the middle of program
SETB EA ;Enable All SETB ET0 ;Enable Timer0 ovrf SETB ET1 ;Enable Timer1 ovrfSETB EX0 ;Enable INT0SETB EX1 ;Enable INT1SETB ES ;Enable Serial port
by mov instructionRecommended in the first of program
MOV IE, #10010110B
SETB IE.7SETB IE.1SETB IE.3SETB IE.0SETB IE.2 SETB IE.4
5050
Timer ISRTimer ISR
Notice thatNotice that– There is no need for a “CLR TFx” instruction in There is no need for a “CLR TFx” instruction in
timer ISR timer ISR – 8051 clears the TF internally upon jumping to 8051 clears the TF internally upon jumping to
ISRISR
Notice that Notice that – We must reload timer in mode 1We must reload timer in mode 1– There is no need on mode 2 (timer auto reload)There is no need on mode 2 (timer auto reload)
5151
External interrupt type controlExternal interrupt type control By low nibble of Timer control register By low nibble of Timer control register TCONTCON
IE0IE0 ( (IE1IE1): External interrupt 0(1) edge flag. ): External interrupt 0(1) edge flag.
– setset by CPU when external interrupt edge ( by CPU when external interrupt edge (H-to-LH-to-L) is detected. ) is detected.
– Does not affected by H-to-L while ISR is executed(no int on int)Does not affected by H-to-L while ISR is executed(no int on int)
– ClearCleared by CPU when ed by CPU when RETIRETI executed. executed.
– does does not not latch low-levellatch low-level triggered interrupt triggered interrupt
IT0IT0 ((IT1IT1): interrupt 0 (1) type control bit. ): interrupt 0 (1) type control bit.
– Set/cleared by softwareSet/cleared by software
– IT=1 edge trigger IT=1 edge trigger
– IT=0 low-level triggerIT=0 low-level trigger
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0Timer 1 Timer0 for Interrupt
(MSB) (LSB)
5252
External Interrupts
IE0 (TCON.3)
0003
INT0(Pin 3.2) 0
12
IT0
Edge-triggered
Level-triggered (default)
IE1 (TCON.3)
INT0(Pin 3.3) 0
12
IT1
Edge-triggered
Level-triggered (default)
0013
5353
Interrupt PrioritiesInterrupt Priorities What if What if twotwo interrupt sources interrupt at the interrupt sources interrupt at the same same
timetime??
The interrupt with the The interrupt with the highesthighest PRIORITY gets serviced PRIORITY gets serviced firstfirst..
All interrupts have a power on All interrupts have a power on defaultdefault priority order. priority order.
1.1. External interrupt 0 (INT0)External interrupt 0 (INT0)
2.2. Timer interrupt0 (TF0)Timer interrupt0 (TF0)
3.3. External interrupt 1 (INT1)External interrupt 1 (INT1)
4.4. Timer interrupt1 (TF1)Timer interrupt1 (TF1)
5.5. Serial communication (RI+TI)Serial communication (RI+TI)
Priority can also be set to “high” or “low” by Priority can also be set to “high” or “low” by IPIP reg. reg.
5454
Interrupt Priorities (IP) RegisterInterrupt Priorities (IP) Register
IP.7IP.7: reserved: reserved
IP.6IP.6: reserved: reserved
IP.5IP.5: timer 2 interrupt priority bit(8052 only): timer 2 interrupt priority bit(8052 only)
IP.4IP.4: serial port interrupt priority bit: serial port interrupt priority bit
IP.3IP.3: timer 1 interrupt priority bit: timer 1 interrupt priority bit
IP.2IP.2: external interrupt 1 priority bit: external interrupt 1 priority bit
IP.1IP.1: timer 0 interrupt priority bit: timer 0 interrupt priority bit
IP.0IP.0: external interrupt 0 priority bit: external interrupt 0 priority bit
--- PX0PT0PX1PT1PSPT2---
5555
Interrupt Priorities ExampleInterrupt Priorities Example
MOV IP , #00000100BMOV IP , #00000100B or or SETB IP.2SETB IP.2 gives priority order gives priority order1.1. Int1Int1
2.2. Int0Int0
3.3. Timer0Timer0
4.4. Timer1Timer1
5.5. SerialSerial
MOV IP , #00001100BMOV IP , #00001100B gives priority order gives priority order1.1. Int1Int1
2.2. Timer1Timer1
3.3. Int0Int0
4.4. Timer0Timer0
5.5. SerialSerial
--- PX0PT0PX1PT1PSPT2---
5656
Interrupt inside an interruptInterrupt inside an interrupt
--- PX0PT0PX1PT1PSPT2---
A high-priority interrupt can interrupt a low-priority interrupy
All interrupt are latched internally
Low-priority interrupt wait until 8051 has finished servicing the high-priority interrupt
5757
Example of external interuuptExample of external interuuptORG 0000HORG 0000HLJMP MAINLJMP MAIN
;;
;interrupt service routine (ISR) ;interrupt service routine (ISR) ;for hardware external interrupt INT1;for hardware external interrupt INT1;;
ORG 0013HORG 0013HSETB P1.1SETB P1.1MOV R0,200MOV R0,200
WAIT:WAIT: DJNZ R0,WAITDJNZ R0,WAITCLR P1.1CLR P1.1RETIRETI
;;
;main program for initialization;main program for initialization;;
ORG 30HORG 30HMAIN:MAIN: SETB IT1SETB IT1 ;on negative edge of INT1;on negative edge of INT1
MOV IE,#10000100BMOV IE,#10000100BWAIT2:WAIT2: SJMP WAIT2SJMP WAIT2
ENDEND
5858