Top Banner
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
21

8051 Timers

Feb 01, 2016

Download

Documents

Cantale Dacian

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? - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 8051 Timers

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: 8051 Timers

Timers and Interrupts

8051 implementation

CSC321

Page 3: 8051 Timers

Using the timers

• Controlled through two special function registers (SFR)– TMOD – configure the timers– TCON – control the timers

CSC321

Page 4: 8051 Timers

TMOD

CSC321

Page 5: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

Set up by the TCON register

• The bits we didn’t talk about previously

CSC321

Page 13: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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: 8051 Timers

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