CSC321 8051 Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The.

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

Autumn 2012C.-S. Shieh, EC, KUAS, Taiwan1 The 8051 Family Microcontroller Chin-Shiuh Shieh Department of Electronic Engineering.
A look at interrupts What are interrupts and why are they needed.
8051 Core Specification.
Introduction of Holtek HT-46 series MCU
68HC11 Polling and Interrupts
Interrupts What is an interrupt? What does an interrupt do to the “flow of control” Interrupts used to overlap computation & I/O – Examples would be console.
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
A look at interrupts What are interrupts and why are they needed in an embedded system? Equally as important – how are these ideas handled on the Blackfin.
A look at interrupts What are interrupts and why are they needed.
Chapter 6 Interrupts (I. Scott Mackenzie).
Architecture of the 8051 INTERNAL DATA BUS Oscillator & Timing Programmable I/O (32 Pins) 80C51 CPU 64K byte Expansion Control Serial I/O 4K Program Memory.
Embedded Systems UNIT 3. Pin Details of 8051 Pins 1-8: Port 1 Each of these pins can be configured as an input or an output. Pin 9: The positive voltage.
1.  8051 Timers “count up,” incrementing the Timer’s respective “count register” each time there is a triggering clock pulse. 2  When the “count register”
The 8051 Microcontroller and Embedded Systems
INTERRUPTS PROGRAMMING
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
ECE 265 – LECTURE 12 The Hardware Interface 8/22/ ECE265.
CHAPTER TIMER PROGRAMMING Timers The 8051 has two timers/counters, they can be used as ◦ Timers to generate a time delay ◦ Event counters.
1 Chapter 4 Timer Operation (I. Scott MacKenzie).
A Simple Tour of the MSP430. Light LEDs in C LEDs can be connected in two standard ways. Active high circuit, the LED illuminates if the pin is driven.
CIT 673 Created by Suriyong1 MCS51 ASSEMBLY Language Resources
Chapter 4 TIMER OPERATION
CoE3DJ4 Digital Systems Design Chapter 4: Timer operation.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
1. Registers Used in Timer/Counter  TH0, TL0, TH1, TL1  TMOD (Timer mode register)  TCON (Timer control register) 2.
Microprocessors 1 MCS-51 Interrupts.
8051 Micro controller. Architecture of 8051 Features of 8051.
Intel 8051 Another family of microcontroller is the Intel 8051 family. The basic 8051 microcontroller has four parallel input/output ports, port 0, 1,
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Jump, Loop, and Call Instructions
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
Lecture 3 CSE 341 – Microprocessors Lecture 3 Md. Omar Faruqe UB 1228
kashanu.ac.ir Microprocessors Interrupts Lec note 8.
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
The 8051 Microcontroller Chapter 6 INTERRUPTS. 2/29 Interrupt is the occurrence of a condition an event that causes a temporary suspension of a program.
Timer Programming in Assembly and C Prepared By:
80C51 Block Diagram 1. 80C51 Memory Memory The data width is 8 bits Registers are 8 bits Addresses are 8 bits – i.e. addresses for only 256.
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application.
Serial I/O Port.
80C51 Block Diagram ECE Overview.
Assembly Language * * * * * * * 8051 Timer
68HC11 Interrupts & Resets.
8051 Timers Timers are the peripherals of the 8051 Microcontroller.
Timer Source: under
BVM Engineering College Electrical Engineering Department : Microprocessor and Microcontroller Interfacing Interrupts of 8051 Prepared by:
Interrupt Source: under
Introduction to Micro Controllers & Embedded System Design Interrupt
UNIT 5 TIMRERS/COUNTERS
* * * * * * * 8051 Interrupts Programming.
EMT 348: Microcontroller Timer/counter
Interrupt.
Introduction to Micro Controllers & Embedded System Design Timer Operation Department of Electrical & Computer Engineering Missouri University of Science.
Timer.
Source: Motor Source:
ECE 3430 – Intro to Microcomputer Systems
8051 Timers / Counters It has two timers Timer 0 and Timer 1.
Interrupt Programming
Timer Source: under
Interrupt Source: under
Interrupt.
Interrupt Source: under
Timer Source: under
Interrupts of 8051 Introduction 8051 Interrupt organization
COMP3221: Microprocessors and Embedded Systems
8051 Development System Details
Presentation transcript:

CSC 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

Timers and Interrupts 8051 implementation CSC321

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

TMOD CSC321

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

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

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

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, A CSC321 We don’t want TH0 to roll over while we are reading TL0

Programming the timer (part 1) CSC321 ; set timer 0 to 8-bit mode mov TMOD, #0x02 ; start value mov TL0, #0x00 ; reset value when ; the timer hits 0xFF mov TH0, #0x00 ; start timer 0 setb TR0 ; kill some time nop

Programming the timer (part 2) CSC321 ; stop timer 0 clr TR0 tryagain: ; read the high byte of the timer mov A, TH0 ; read the low byte of the timer mov R6, TL0 ; read the high byte again to make sure it didn't change ; while we were reading the low byte cjne A, TH0, tryagain ; keep the high byte if it didn't change mov R7, A ; the full 16-bit timer is now in R7/R6

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

Set up by the TCON register The bits we didn’t talk about previously CSC321

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

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

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

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

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

Programming interrupts (part 1) Setting up the ISR CSC321 ; org tells the assembler where to place the code org 0 ; start at the main program label jmp main ; timer 0 ISR is at address 0x000B org 0x000B ; obviously not a good ISR but it makes the point timer0: jmp timer0 reti

Programming interrupts (part 1) The main program (almost the same as before) CSC321 ; start the main program at address 0x0030 org 0x0030 main: ; set timer 0 to 8-bit mode mov TMOD, #0x02 ; start value mov TL0, #0xFC ; reset value when ; the timer hits 0xFF mov TH0, #0x00 ; enable interrupts setb EA setb ET0

Programming interrupts (part 2) more main program (same as before) CSC321 ; start timer 0 setb TR0 ; kill some time nop ; stop timer 0 ;clr TR0

Programming interrupts (part 3) more main program (same as before) CSC321 tryagain: ; read the high byte of the timer mov A, TH0 ; read the low byte of the timer mov R6, TL0 ; read the high byte again to make sure it didn't change ; while we were reading the low byte cjne A, TH0, tryagain ; keep the high byte if it didn't change mov R7, A ; the full 16-bit timer is now in R7/R6