Timers and interrupts

Similar documents
Lecture 9. Timer Operations and Programming

The 8051 microcontroller has two 16-bit timers/counters called T0 and T1.

CHAPTER TIMER PROGRAMMING


Interrupts. EE4380 Fall 2001 Class 9. Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

Chapter 09. Programming in Assembly

8051 Timers. Class 7 EE4380 Fall Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

FACULTY OF ENGINEERING LAB SHEET

8051 Peripherals. On-Chip Memory Timers Serial Port Interrupts. Computer Engineering Timers

CPEG300 Embedded System Design. Lecture 8 Timer

ELEG3923 Microprocessor Ch.9 Timer Programming

Chapter 09. Programming in Assembly

Department of EIE / Pondicherry Engineering College. Timer/Counters. Department of EIE / Pondicherry Engineering College 1

MODEL ANSWER SUBJECT- MICROCONTROLLER(12187) CLASS-EJ5E CLASS TEST-02 Q1.)Attempt any THREE of the following.


MCS -51 Programmer s Guide and Instruction Set

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

CoE3DJ4 Digital Systems Design. Chapter 6: Interrupts

8-bit Microcontroller with 8K Bytes In-System Programmable Flash AT89S52

C51 Family. C51 Family Programmer s Guide and Instruction Set. Summary

Introduction To MCS-51

Mod-3: Interrupts,Timer operation,serial communication 1

AN108 IMPLEMENTING A REALTIME CLOCK. Relevant Devices. Introduction. Key Points. Overview

CPEG300 Embedded System Design. Lecture 6 Interrupt System

VRS540-4kB Flash, 128B RAM, 25~40MHz, 8-Bit MCU

8051 Interrupt Organization

INTERRUPTS PROGRAMMING

8XC151SA and 8XC151SB Hardware Description

VRS550-8kB Flash, 256B RAM, 25~40MHz, 8-Bit MCU VRS560-16kB Flash, 256B RAM, 40MHz, 8-Bit MCU

80C51 Block Diagram. CSE Overview 1

The Final Word on 8051 Microcontroller

Chapter 6 Interrupts. (I. Scott Mackenzie) By: Masud-ul-Hasan

Interrupt Programming: Interrupts vs. Polling Method:

APPENDIX A. References

8XC51RA RB RC Hardware Description

2. Write an 8051 program to generate a square wave of 25 khz at pin P2.3 using XTAL = 12 MHz. Solution:

VRS570 32K Flash, 1kB RAM, 25~40MHz, 8-Bit MCU VRS580 64K Flash, 1kB RAM, 25~40MHz, 8-Bit MCU

By the end of Class. Outline. Homework 5. C8051F020 Block Diagram (pg 18) Pseudo-code for Lab 1-2 due as part of prelab

8051 microcontrollers

Embedded Controller Programming

CoE3DJ4 Digital Systems Design. Chapter 5: Serial Port Operation

8-BIT MICROCONTROLLER

Rev. No. History Issue Date Remark

Timer Counter and Interrupt. Equation (16 bits counter, Mode 1, 16MHz):

MCS-51 Serial Port A T 8 9 C 5 2 1

EE6502- MICROPROCESSOR AND MICROCONTROLLER

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

Principle and Interface Techniques of Microcontroller

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

8051 Core Specification

8051 Timers and Serial Port

Serial I-O for Dinesh K. Sharma Electrical Engineering Department I.I.T. Bombay Mumbai (version 14/10/07)

CHAPTER 11 INTERRUPTS PROGRAMMING

Microcontroller and Embedded Systems:

Chapter 3. Bit Addressable Area. By DeccanRobots

AT Bit Spread- Spectrum Microcontroller. Preliminary. Features. Description. Pin Configuration

INTEGRATED CIRCUITS DATA SHEET. P89C738; P89C739 8-bit microcontrollers Dec 15. Product specification File under Integrated Circuits, IC20

UNIT THE 8051 INSTRUCTION SET AND PROGRAMMING

Timer programming

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text

80C451 operation of port 6

Preliminary W77C32 8 BIT MICROCONTROLLER GENERAL DESCRIPTION FEATURES

User-defined Download Application Note

8051 Microcontroller. Ali Ziya Alkar 1

7.2.1 Timer 2 Capture LSB... 24

P89V52X2. 1. General description. 2. Features. 8-bit 80C51 low power 8 kb flash microcontroller with 256 B RAM, 192 B data EEPROM

DS89C420 Ultra High-Speed Microcontroller User s Guide

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

SM5964B 8-Bit Micro-controller 64KB with ISP Flash & 1KB RAM embedded

8051 Microcontroller memory Organization and its Applications

8051 Microcontrollers

WINTER 14 EXAMINATION

8051 Microcontroller

High-Speed Microcontroller User s Guide

8051 Microcontroller

MicroConverter Technical Note uc001 MicroConverter I 2 C Compatible Interface

Q 1 a) Attempt any THREE of the following: 12 TMOD.7 TMOD.6 TMOD.5 TMOD.4 TMOD.3 TMOD.2 TMOD.1 TMOD.0 GATE C/T M1 M0 GATE C/T M1 M0

The Microcontroller. Lecture Set 3. Major Microcontroller Families. Example Microcontroller Families Cont. Example Microcontroller Families

8051 I/O and 8051 Interrupts

APPLICATION NOTE 600 Ultra High Speed Microcontroller Hardware Enhances Serial Port Capability

Interrupts, timers and counters

The Timers/Counters The Serial Interface The Interrupt System Reset P0.0-P0.7 P2.0-P2.7. Port 2 Drivers. Port 2 Latch

8-bit Microcontroller with 2/4-Kbyte Flash AT89LP2052 AT89LP4052

Microcontroller and Applications

Programming the 8051/8052

W77IE58 8-BIT MICROCONTROLLER. Table of Contents-- Publication Release Date: December Revision A2

Memory organization Programming model - Program status word - register banks - Addressing modes - instruction set Programming examples.

INTEGRATED CIRCUITS. AN408 80C451 operation of port 6

Department of Electronics and Instrumentation Engineering Question Bank

SECTION 1: INTRODUCTION 5 SECTION 2: ORDERING INFORMATION 5 SECTION 3: ARCHITECTURE 5

ULTRA-HIGH-SPEED FLASH MICROCONTROLLER USER S GUIDE

8051 Microcontroller

Serial communication

MEGAWIN MPC89L516X2. 8-bit micro-controller. Features

Timer-1 can be run using the internal clock, fosc/12 (timer mode) or from any external source via pin T1 (P3.5) (Counter mode).

UNIT MICROCONTROLLER AND ITS PROGRAMMING

Lecture 10. Serial Communication

Microcontroller Intel [Instruction Set]

Preliminary W77E58 8 BIT MICROCONTROLLER. Table of Contents-- Publication Release Date: March Revision A1

Transcription:

Timers and interrupts CSCI 255: Introduction to Embedded Systems Keith Vertanen Copyright 2011

Timers Overview Creating fixed pauses Calculate length of events Counts events Generate baud rate for serial communication Interrupts Allow routine to interrupt normal execution Can be triggered by timers 2

Timer basics Increment every instruction cycle 11,059,200 ticks second / 12 ticks per cycle 921,600 increments per second Independent of normal program 8052 has three timers Timer 0, 1, 2 Using various SFRs Timer0, Timer1 Timer2 3

Timer0, Timer1 Timer 0/1 Provide exactly same functionality Allows timing or counting two different things Four different modes: Mode 0 = 13 bit timer (legacy) Mode 1 = 16-bit timer Mode 2 = 8-bit auto-reload Mode 3 = Split timer 4

Mode 1, 16-bit timer Timer mode 1 Counts from 0 up to 65535, rolls around to 0 High byte in TH0/TH1, low byte TL0/TL1 e.g. TH0 = 3, TL0 = 232, value = 256 * 3 + 232 = 1000 Set mode 1 using two bits in TMOD TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 0 1 = mode 1 for timer 1 0 1 = mode 1 for timer 0 5

Timer control Starting and overflow Must set bit to start running Timer sets a bit whenever it overflows You must clear overflow bit to watch for next overflow TCON Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Set to 1 when timer 1 overflows Set to 1 when timer 0 overflows 1 = Timer 1 running 0 = Timer 1 stopped 1 = Timer 0 running 0 = Timer 0 stopped 6

Mode 1 example MOV TMOD, #00000001b ; Set timer0 mode to 01 = mode 1 SETB TR0 ; Start timer0 running Loop: ; Toggle all the LEDs every time P2.0 is pressed JB P2.0, $ JNB P2.0, $ CPL A MOV P0, A JMP Loop Shorthand for code address of this line TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 TCON Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 7

Mode 1 overflow example MOV TMOD, #00000001b ; Set timer0 mode to 01 = mode 1 SETB TR0 ; Start timer0 running Loop: JNB TF0, $ INC P0 CLR TF0 JMP Loop ; Wait for overflow bit to be set ; Increment P0 so LEDs change ; Clear the overflow bit TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 TCON Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 8

Precise timing Goal: create 1 second pause using timer Each timer increment = 1 cycle 0.05 sec * 921,600 cycles/sec = 46080 cycles Set timer to overflow this often Start timer off part way through its 16-bit cycle 65536-46080 = 19456 Repeat 20 times 9

Pause 1 second Start: MOV TMOD, #00000001b ; Put timer 0 in 16-bit mode MOV R0, #20 ; Loop, 0.05 * 20 = 1.0 sec Loop: CLR TF0 ; Clear overflow bit MOV TH0, #76 ; High byte 19,456 = 76 * 256 MOV TL0, #00 ; Low byte of 0 SETB TR0 JNB TF0, $ CLR TR0 ; Start timer0 running ; Wait for overflow ; Stop timer0 running DJNZ R0, Loop TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 TCON Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 10

Reading 16-bit timer Problem: timer continues to increment while we read high and low byte Timer value 3839 = 14 * 256 + 255 High byte 14, low byte 255 If we read high byte then low byte MOV R0, TL0 (1 cycle) MOV R1, TH0 (1 cycle) Actual result: High byte 15, low byte 255 11

Reading 16-bit timer Solution 1: stop the timer Will lose a small amount of time CLR TR0 MOV A, TH0 MOV R0, TL0 SETB TR0 ; Stop timer0 ; Copy high byte of timer ; Copy low byte of timer ; Restart timer0 Solution 2: read until no change in high byte Repeat: MOV A, TH0 ; Copy high byte of timer MOV R0, TL0 ; Copy low byte of timer CJNE A, TH0, Repeat ; Repeat until no change in high 12

Timing event length Increment only in presence of external signal Hook something up to P3.2 Timer only increments if pin high Microcontroller handles the check of P3.2 Set GATE0/1 bit in TMOD TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 if GATE1 = 1, P3.2 must be high for timer1 to increment if GATE0=1, P3.2 must be high for timer0 to increment 13

Counting events Use timer to count discrete events Hook something up to P3.4 Timer increments if it detects "event" Event = one-to-zero transition 1/24 * 11.0592 Mhz = 460,800 events/second or less Set C/Tx bit in TMOD TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 if 1, counts in timer1 events occurring on P3.4 if 1, counts in timer0 events occurring on P3.4 14

Timer mode 2 Mode 1, 8-bit auto-reload Counts from 0 up to 255, in low byte: TLx Resets to fixed value in high byte: THx Useful for establishing baud rate in serial comm Set mode 2 using two bits in TMOD TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 1 0 = mode 1 for timer 1 1 0 = mode 1 for timer 0 15

Auto-reload example Start: MOV TMOD,#00100000b MOV TH1, #253 SETB TR1 Loop: MOV P0, TL1 JMP Loop ; Timer 1, 8-bit auto reload ; Set reload value 253 (9,600 baud) ; Start timer1 running ; Copy timer value to P0 TMOD Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 TCON Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 16

Timer 2 Timer 2 8052 adds a third timer 16-bit auto-reload mode Capture mode Controlled by T2CON SFR Two extra SFRs for reload/capture values Low byte RCAP2L, high byte RCAP2H T2CON Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2 17

Precise timing redux Goal: create 1 second pause using timer 2 Timer2 can automatically reset 16-bit start value Note: Keil AT89S8253, missing timer2 SFRs: ; Declare all the timer2 related SFRs T2CON EQU 0C8h RCAP2L EQU 0CAh RCAP2H EQU 0CBh TL2 EQU 0CCh TH2 EQU 0CDh TF2 EQU T2CON.7 EXF2 EQU T2CON.6 RCLK EQU T2CON.5 TCLK EQU T2CON.4 EXEN2 EQU T2CON.3 TR2 EQU T2CON.2 C_T2 EQU T2CON.1 CP_RL2 EQU T2CON.0 ET2 EQU IE.5 18

Timer 2, 1 second pause Start: MOV T2CON, #00000000b ; Put timer 2 in 16-bit reload MOV RCAP2L, #0 ; Reload low byte 0 MOV RCAP2H, #76 ; Reload high byte 7 MOV TL2, #0 ; Start timer at low byte 0 MOV TH2, #76 ; Start timer at high byte 7 SETB TR2 ; Start timer2 Pause1Sec: MOV R0, #20 Loop: JNB TF2, $ CLR TF2 DJNZ R0, Loop JMP Pause1Sec ; Loop, 0.05 * 20 = 1.0 sec ; Wait for overflow ; Clear overflow flag ; Loop forever 19

Capture mode Timer 2, capture mode On external event, copy timer value to RCAP2H/L Allows precise recording of event time Triggered on 1-0 transition on P1.1 Timer continues to run Set CP/RL2 = 1, EXEN2 = 1 T2CON Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2 20

Interrupts Intro to interrupts Microcontroller interrupts your normal program Interrupt on: Timer0/1/2 overflow/reload Reception/transmission serial data External event (P3.3, P3.2) Go to fixed code address Run some code and then return 21

Goal: Interrupt example Monitor button, if pressed buzz Also increment binary counter every second Build main program to handling buzzing Build Interrupt Service Routine (ISR) for counter Use timer2 auto-reload Interrupt every 0.05 seconds Every 20 interrupts: Increment counter variable Update the LEDs 22

Buzzing main program BUZZER EQU P3.0 BUTTON EQU P2.0 Main: SETB BUZZER ; Turn buzzer off JB BUTTON, $ ; Loop if button not pressed CLR BUZZER ; Turn buzzer on JNB BUTTON, $ ; Wait for button release JMP Main 23

Setting up timer & interrupt Start: MOV T2CON, #00000000b ; Put timer 2 in 16-bit reload MOV RCAP2L, #0 ; Reload low byte 0 MOV RCAP2H, #76 ; Reload high byte 7 MOV TL2, #0 ; Start timer at low byte 0 MOV TH2, #76 ; Start timer at high byte 7 SETB EA SETB ET2 SETB TR2 ; Enable global interrupts ; Enable timer2 overflow interrupt ; Start timer2 24

Interrupt service routine ; Timer 2 overflow ISR ORG 002Bh PUSH ACC PUSH PSW ; Address of ISR ; Save off accumulator ; Save off PSW INC R0 ; Another 0.05 has passed CLR TF2 ; Clear overflow flag CJNE R0, #20, Done ; Leave if not to 20 yet MOV R0, #0 ; Reset count to 0 INC R1 MOV A, R1 CPL A MOV P0, A Done: POP PSW POP ACC RETI ; Update the P0 LED counter ; Restore PSW ; Restore accumulator ; Return from ISR 25

Timers Summary Provide precise time delays Time events Count events Interrupts Allow concurrent processing Based on interrupt signal From timer From receiving/transmitting data From external signal 26