CoE3DJ4 Digital Systems Design. Chapter 4: Timer operation

Similar documents
CoE3DJ4 Digital Systems Design. Chapter 4: Timer operation

8051 Serial Port. Crystal TXD. I/O Device RXD. Embedded Systems Peripherals

Flash Microcontroller. Memory Organization. Memory Organization

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

Experiment # 9. Clock generator circuits & Counters. Eng. Waleed Y. Mousa

4 Character 5x7 LED Matrix Display

AVR Timer/Counter. Prof Prabhat Ranjan DA-IICT, Gandhinagar

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

ETEC 2301 Programmable Logic Devices. Chapter 10 Counters. Shawnee State University Department of Industrial and Engineering Technologies

Embedded. Engineer s. Development. Tool (EEDT 5.0)

Small Hardware Development and Prototyping Board for the SX28

Section 14. Compare/Capture/PWM (CCP)

8051 hardware summary

Counters and Decoders

Interfacing Analog to Digital Data Converters

Chapter 9 Latches, Flip-Flops, and Timers

ASYNCHRONOUS COUNTERS

8254 PROGRAMMABLE INTERVAL TIMER

Real-Time Clock. * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

CHAPTER 11: Flip Flops

Asynchronous counters, except for the first block, work independently from a system clock.

NTE2053 Integrated Circuit 8 Bit MPU Compatible A/D Converter

Module 3: Floyd, Digital Fundamental

ADS9850 Signal Generator Module

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

Cascaded Counters. Page 1 BYU

Timer A (0 and 1) and PWM EE3376

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

DIGITAL ELECTRONICS. Counters. By: Electrical Engineering Department

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

24-Bit Analog-to-Digital Converter (ADC) for Weigh Scales FEATURES S8550 VFB. Analog Supply Regulator. Input MUX. 24-bit Σ ADC. PGA Gain = 32, 64, 128

8-Bit Flash Microcontroller for Smart Cards. AT89SCXXXXA Summary. Features. Description. Complete datasheet available under NDA

Hardware and Software Requirements

University of Portsmouth Faculty of Technology Department of Electronic and Computer Engineering. Content

Serial Communications

Flash Microcontroller. Architectural Overview. Features. Block Diagram. Figure 1. Block Diagram of the AT89C core

To design digital counter circuits using JK-Flip-Flop. To implement counter using 74LS193 IC.

8085 MICROPROCESSOR PROGRAMS

Debouncing Switches. Mechanical switches are one of the most common interfaces to a uc.

6-BIT UNIVERSAL UP/DOWN COUNTER

DS1621 Digital Thermometer and Thermostat

Lesson-16: Real time clock DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK

DP8570A DP8570A Timer Clock Peripheral (TCP)

8051 MICROCONTROLLER COURSE

T.C. Yeditepe University. Department of Electrical-Electronics Engineering

Keil C51 Cross Compiler

Microprocessor & Assembly Language

PART B QUESTIONS AND ANSWERS UNIT I

Decimal Number (base 10) Binary Number (base 2)

AT89C Bit Microcontroller with 1 Kbyte Flash. Features. Description. Pin Configuration

BINARY CODED DECIMAL: B.C.D.

Below is a diagram explaining the data packet and the timing related to the mouse clock while receiving a byte from the PS-2 mouse:

Lecture-3 MEMORY: Development of Memory:

WEEK 8.1 Registers and Counters. ECE124 Digital Circuits and Systems Page 1

Contents COUNTER. Unit III- Counters

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

Old Company Name in Catalogs and Other Documents

DIGITAL COUNTERS. Q B Q A = 00 initially. Q B Q A = 01 after the first clock pulse.

Microtronics technologies Mobile:

DS1621 Digital Thermometer and Thermostat

Digital Logic Design Sequential circuits


DS1678 Real-Time Event Recorder

NJU6061. Full Color LED Controller Driver with PWM Control GENERAL DESCRIPTION PACKAGE OUTLINE FEATURES

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

Digital Systems Based on Principles and Applications of Electrical Engineering/Rizzoni (McGraw Hill

Counters & Shift Registers Chapter 8 of R.P Jain

A High Resolution Performance Monitoring Software on the Pentium

AP TriCore. Sinusodial 3-Phase Output Generation Using The TriCore General Purpose Timer Array. Microcontrollers

Flip-Flops, Registers, Counters, and a Simple Processor

Design and Implementation of Home Monitoring System Using RF Technology

Digital Fundamentals

Design Example: Counters. Design Example: Counters. 3-Bit Binary Counter. 3-Bit Binary Counter. Other useful counters:

Atmel Norway XMEGA Introduction

Counters. Present State Next State A B A B

MACHINE ARCHITECTURE & LANGUAGE

Memory Elements. Combinational logic cannot remember

AVR305: Half Duplex Compact Software UART. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Master/Slave Flip Flops

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

Objectives. Basics of Serial Communication. Simplex vs Duplex. CMPE328 Microprocessors (Spring ) Serial Interfacing. By Dr.

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

LC7218, 7218M, 7218JM

(Refer Slide Time: 00:01:16 min)

WTV020-SD MODULE WTV020-SD-20S and WTV020-SD-16P

css Custom Silicon Solutions, Inc.

Introduction the Serial Communications Huang Sections 9.2, 10.2 SCI Block User Guide SPI Block User Guide

M25P05-A. 512-Kbit, serial flash memory, 50 MHz SPI bus interface. Features

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

AN974 APPLICATION NOTE

HD44780U (LCD-II) (Dot Matrix Liquid Crystal Display Controller/Driver)

Systems I: Computer Organization and Architecture

DS2155 T1/E1/J1 Single-Chip Transceiver

Asynchronous Counters. Asynchronous Counters

COMBINATIONAL and SEQUENTIAL LOGIC CIRCUITS Hardware implementation and software design

Lesson 12 Sequential Circuits: Flip-Flops

The components. E3: Digital electronics. Goals:

Quick Start Tutorial. Using the TASKING* Software Development Tools with the Intel 8x930 Family Evaluation Board

Embedded Based Automated Student Attendance Governing System

Transcription:

CoE3DJ4 Digital Systems Design Chapter 4: Timer operation

Timer There are two 16-bit timers each with four modes of operation Timers are used for (a) interval timing, (b) event counting or (c) baud rate generation for built in serial port Each 16-bit timer is in fact an asynchronous counter therefore the 16 th or last flip-flop divides the input clock frequency by 2 16 =65536 The output of last stage clocks an overflow flip-flop In interval timing applications, a timer is programmed to overflow at a regular interval and set the timer overflow flag Flag is used to synchronize the program to perform an action (e.g., turning on a light) Interval timing can be used to measure the elapsed time

TCON and TMOD Event counting is used to determine the number of occurrences of an event An event is an external stimulus that provides a 1 to 0 transition 8051 timers are accessed using six of SFRs (i.e., TCON, TMOD, TL0, TL1, TH0,TH1)

Timer Mode Register The TMOD register (89H) contains two groups of 4 bits that set the operating mode for Timer 0 and Timer 1. TMOD is not bit addressable and is loaded once at the beginning of a program to initialize the timer MSB LSB GATE C/T M1 M0 GATE C/T M1 M0 Timer 1 Timer 0 Bit 7 6 5 4 3 2 1 0 Name GATE C/T M1 M0 GATE C/T M1 M0 Gate bit. If set, timer 1 will only increment while INT1 is high. Counter/timer select bit 1 = event counter external timing signal 0 = interval timer internal timing signal Mode bit 1 Mode bit 0 Timer 0 Gate bit Timer 0 counter/timer select bit Timer 0 M1 bit Timer 0 M0 bit Description

Timer Mode Definitions Using the M1 and M0 bits of the TMOD register, we can set the timers to operate in one of 4 possible modes: M1 M0 Mode Description 0 0 0 13-bit timer mode 0 1 1 16-bit timer mode 1 0 2 8-bit auto reload mode 1 1 3 Split timer mode: Timer 0: TL0 is an 8-bit timer controlled by timer 0 mode bits; TH0 the same except controlled by timer 1 mode bits. Timer 1: Stopped.

Timer Control Register The TCON register (88H) contains status and control bits for Timer0 and Timer1. Bit addressable. MSB LSB TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Bit Name Description TCON.7 TCON.6 TCON.5 TCON.4 TCON.3 TCON.2 TCON.1 TCON.0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Timer 1 overflow flag Timer 1 run-control bit. Used to turn the timer on/off Timer 0 overflow flag Timer 0 run-control bit. External Interrupt 1 edge flag External Interrupt 1 type flag External Interrupt 0 edge flag External Interrupt 0 type flag

TCON and TMOD Upper four bits in TCON (TCON.4 to TCON.7) are used to turn timers on and off (TR0,TR1) or signal a timer overflow (TF0,TF1) The lower four bits in TCON are used to detect and initiate external interrupts THx is used to represent TH0 or TH1

Timer Modes Mode 0 is a 13-bit timer mode and is not generally used in new designs Timer high-byte (THx) is cascaded with five least-significant bits of the timer low-byte (TLx) to form a 13-bit timer Upper three bits of TLx are not sued Mode 1 is a 16-bit timer mode Clock is applied to combined high and low timer registers (TLx/THx) Timer counts up and an overflow occurs on FFFF to 0000 transition and sets the timer overflow flag. The timer continues to count Overflow flag is the TFx bit in TCON

Timer Modes Mode 2 is 8-bit auto reload mode Timer s low-byte (TLx) operates as an 8-bit timer while timer high-byte (THx) holds a reload value When the count overflows from FF not only the flag is set, but the value in THx is loaded into TLx. Counting continues from this value up to next FF Mode 3 is split timer mode and is different for each timer Timer 0 in mode 3 is split into two 8-bit timers. TL0 and TH0 act as separate timers with overflow setting the TF0 and TF1 bits respectively Timer 1 can be switched into one of the other modes. The only limitation is that the usual timer 1 overflow flag TF1, in not affected by Timer 1 since it is connected to TH0.

Clocking sources How the timers are clocked? There are two sources for clock which can be selected by writing to the counter/timer (C/T) bit in TMOD when the timer is initialized. One source is for interval timing and the other one for event counting Interval timing: If C/T=0, timer is clocked from on-chip oscillator A divide by 12 stage is added to reduce the clocking frequency (for a 12 MHz oscillator the timer clock will be 1 MHz.

Clocking sources Event counting: If C/T=1, timer is clocked from an external source In most applications, external source supplies timer with a pulse upon the occurrence of an event, and the timer is an event counter External clock comes through Port 3 pins: P3.4 is the external clocking for Timer 0 and P3.5 is the clocking input for Timer 1. In counter applications, timer registers are incremented in response to a 1 to 0 transition

Starting, stopping and controlling timers Simplest method for starting and stopping timers is with runcontrol bit (TRx) in TCON TRx is clear after a system reset, therefore, timers are disabled by default TRx is set by software to start timers Example: to start Timer 0 SETB TR0 To stop Timer 0: CLR TR0 Another method for controlling timers is with GATE bit in TMOD and external input INTx (INT0 and INT1 are on Port 3, pins 2 and 3) Setting GATE=1 allows timer to be controlled by INTx

Starting, stopping and controlling timers Assume INT1 is low but pulses high for a period of time to be measured. Initialize Timer 1 for mode 2, 16 bit timer mode withtl1/th1=0000h, GATE=1 and TR1=1 When INT1 goes high, timer is gated on and is clocked at a rate of 1 MHz. When INT1 goes low, timer is gated off and duration of pulse in microseconds is the count in TL1/TH1

Initializing and accessing timer registers Timers are usually initialized once at the beginning of a program to set the correct operating mode Within the body of a program, timers are started, stopped, flag bits tested, cleared, timer registers read or updated and so on. Example: MOV TMOD, #00010000 B This instruction sets M1=0, M0=1 (for mode 1), leave C/T=0 and GATE=0, for internal clocking. Timer will not begin working until TR1 is set If an initial count is necessary timer registers (e.g., TL1/TH1) must also be initialized. Timer counts up and sets the overflow flag on an FFFFH to 0000H transition

Initializing and accessing timer registers Example: a 100 us interval could be timed by initializing TL1/TH1 to 100 counts less than 0000H which is FF9CH. MOV TL1,#9CH MOV TH1,#FFH SETB TR1 Overflow flag is automatically set 100 us later. A loop can check to see when the overflow flag is set WAIT: JNB TF1, WAIT When timer overflows, it is necessary to stop timer and clear the overflow flag: CLR TR1 CLR TF1

Initializing and accessing timer registers In some applications it is necessary to read the value in timer registers on the fly Possible problem: if we read the low byte first and the high byte second and between these two reads the low byte overflows into high byte we have a wrong read value Solution: Read high-byte first, then low-byte and read highbyte again. If high-byte has changed repeat read operation AGAIN: MOV A, TH1 MOV R6,TL1 CJNE A, TH1, AGAIN MOV R7,A

Short intervals and long intervals Interval in microseconds 10 256 65536 No limit Technique Software tuning 8-bit timer with auto reload 16 bit timer 16 bit timer plus software loops Write a program that creates a periodic waveform on P1.0 with as high a frequency as possible. What are frequency and duty cycle of waveform? LOOP: SETB P1.0 CLR P1.0 SJUMP LOOP Creates a pulse waveform on P1.0 with a period of 4 us. Signal is high for 1 us (duty cycle of 25%) Period of waveform can be lengthened by inserting NOP instructions into loop

Short intervals and long intervals Example: Write a program using Timer 0 to create a 10 khz square wave on P1.0 MOV TMOD,#02H MOV TH0,#-50 SETB TR0 LOOP: JNB TF0, LOOP CLR TF0 CPL P1.0 SJMP LOOP This program creates a square wave on P1.0 with a high-time of 50 us and low time of 50 us. CPL is a complement bit instruction

Short intervals and long intervals Example: write a program using timer 0 to create a 1 khz square wave on P1.0 MOV TMOD,#01H LOOP: MOV TH0,#FEH MOV TL0,#0CH SETB TR0 WAIT: JNB TF0,WAIT CLR TR0 CLR TF0 CPL P1.0 SJMP LOOP A 1kHz square wave requires a high time of 500 us and a low time of 500 us. Sine the interval is longer than 256 us, mode 2 cannot be used. Mode 1 (16 bit) is required.

Short intervals and long intervals Example: A buzzer is connected to P1.7, and a debounced switch is connected to P1.6. Write a program that reads logic level provided by the switch and sounds the buzzer for 1 second for each 1 to 0 transition detected.

HUNDRED EQU 100 COUNT EQU -10000 ORG 8100H MOV TMOD, #01H LOOP: JNB P1.6, LOOP WAIT: JB P1.6, WAIT SETB P1.7 CALL DELAY CLR P1.7 SJMP LOOP DELAY: MOV R7,#HUNDRED AGAIN: MOV TH0,#HIGH CONT MOV TL0,#LOW COUNT SETB TR0 WAIT2: JNB TF0,WAIT2 CLR TF0 CLR TR0 DJNZ R7,AGAIN RET END