csc321 8051 timers since this is a microcontroller it mainly finds itself in embedded devices quite...
TRANSCRIPT
![Page 1: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/1.jpg)
CSC321
8051 Timers
• Since this is a microcontroller it mainly finds itself in embedded devices
• Quite often embedded devices need to synchronize events
• The way this is done is via time– We could do it by inserting a loop or a bunch of NOP commands
• Is this accurate?
– A better way to do it is to use timers• Basically an internal stop watch• The 8051 has two timers
![Page 2: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/2.jpg)
Timers and Interrupts
8051 implementation
CSC321
![Page 3: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/3.jpg)
Using the timers
• Controlled through two special function registers (SFR)– TMOD – configure the timers– TCON – control the timers
CSC321
![Page 4: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/4.jpg)
TMOD
CSC321
![Page 5: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/5.jpg)
Timer Modes
• Modes 1 and 2 are the most common– Mode 1: 16-bit timer, sets overflow when
complete
– Mode 2: 8-bit timer, reloads and sets overflow when complete
CSC321
![Page 6: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/6.jpg)
TCON
• Bits 5 and 7 are the overflow bits for each timer and are set by hardware
• Bits 4 and 6 turn the timers on/off
• More on bits 0-3 later
CSC321
![Page 7: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/7.jpg)
Timer control
• Setting bits TR0 and TR1 (setb TR0 and setb TR1 instructions) start the timers
• Clearing bits TR0 and TR1 (clr TR0 and clr TR1 instructions) stop the timers
CSC321
![Page 8: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/8.jpg)
Reading the timers
• If the timers are stopped, reading is trivial
mov R6, TL0
mov R7, TH0
• If the timers are running, it gets tricky
try: mov A, TH0
mov R6, TL0
cjne A, TH0, try
mov R7, ACSC321
We don’t want TH0 to roll over while we are reading TL0
![Page 9: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/9.jpg)
Programming the timer (part 1)
CSC321
; set timer 0 to 8-bit modemov TMOD, #0x02; start value mov TL0, #0x00; reset value when; the timer hits 0xFFmov TH0, #0x00; start timer 0setb TR0
; kill some timenopnopnopnopnop
![Page 10: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/10.jpg)
Programming the timer (part 2)
CSC321
; stop timer 0clr TR0
tryagain:; read the high byte of the timermov A, TH0; read the low byte of the timermov R6, TL0; read the high byte again to make sure it didn't change; while we were reading the low bytecjne A, TH0, tryagain
; keep the high byte if it didn't changemov R7, A; the full 16-bit timer is now in R7/R6
![Page 11: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/11.jpg)
8051 Interrupts
• A signal to let the CPU know that something out of the ordinary flow of instructions has occurred
• Various sources of interrupts in the 8051– Two external– Two internal (timers)– I/O (serial port)
CSC321
![Page 12: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/12.jpg)
Set up by the TCON register
• The bits we didn’t talk about previously
CSC321
![Page 13: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/13.jpg)
When an interrupt occurs
• Current instruction is allowed to complete
• Program counter (PC register) is saved on the stack (SP)
• Address of the interrupt service routine (ISR) is loaded to the program counter
CSC321
![Page 14: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/14.jpg)
Interrupt service routine
• Also known as an interrupt vector– The 8051 has six of them
– Usually the ISR is nothing more than a JMP to the actual subroutine (due to lack of space between interrupt vectors)
CSC321
![Page 15: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/15.jpg)
Enabling/Disabling interrupts
• To use an interrupt you must first enable it– They are disabled by default (power up)
– Enabling is a 2 step process• First enable all interrupts – setb EA
• Then enable individual interrupts – setb ET0
• See next slide for why 2 steps…
CSC321
![Page 16: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/16.jpg)
Why two steps?
• Because it makes turning them on and off very easy• Consider a critical section of code (more than 1
instruction)– You don’t want it to be interrupted
– but you don’t want to turn off a bunch of interrupts knowing you’ll have to turn them back on (that takes time)
– Using clr EA and setb EA does the trick in the shortest time possible
CSC321
![Page 17: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/17.jpg)
There is a little bit more…
• Since the interrupt vectors are in low address memory (see table) you can’t put your main program there– Address 0 is the default program memory address
• The 8051 assembler provides a solution
org address– Provides a way to force code to a particular address –
it’s not an instruction, but a directive
CSC321
![Page 18: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/18.jpg)
Programming interrupts (part 1)
• Setting up the ISR
CSC321
; org tells the assembler where to place the codeorg 0; start at the main program labeljmp main
; timer 0 ISR is at address 0x000Borg 0x000B; obviously not a good ISR but it makes the pointtimer0:jmp timer0reti
![Page 19: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/19.jpg)
Programming interrupts (part 1)
• The main program (almost the same as before)
CSC321
; start the main program at address 0x0030org 0x0030
main:; set timer 0 to 8-bit modemov TMOD, #0x02; start value mov TL0, #0xFC; reset value when; the timer hits 0xFFmov TH0, #0x00
; enable interruptssetb EAsetb ET0
![Page 20: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/20.jpg)
Programming interrupts (part 2)
• more main program (same as before)
CSC321
; start timer 0setb TR0
; kill some timenopnopnopnopnop
; stop timer 0;clr TR0
![Page 21: CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The](https://reader030.vdocuments.site/reader030/viewer/2022032703/56649d305503460f94a099eb/html5/thumbnails/21.jpg)
Programming interrupts (part 3)
• more main program (same as before)
CSC321
tryagain:; read the high byte of the timermov A, TH0; read the low byte of the timermov R6, TL0; read the high byte again to make sure it didn't change; while we were reading the low bytecjne A, TH0, tryagain
; keep the high byte if it didn't changemov R7, A; the full 16-bit timer is now in R7/R6