Timer and Counter. ECE473/573 Microprocessor System Design, Dr. Shiue

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

Flash Microcontroller. Memory Organization. Memory Organization

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

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

4 Character 5x7 LED Matrix Display

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

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

8051 hardware summary

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

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

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

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

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

DS1621 Digital Thermometer and Thermostat

Section 14. Compare/Capture/PWM (CCP)

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

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


Timer A (0 and 1) and PWM EE3376

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

8254 PROGRAMMABLE INTERVAL TIMER

Microtronics technologies Mobile:

Counters and Decoders

DS1621 Digital Thermometer and Thermostat

CHAPTER 11: Flip Flops

8051 MICROCONTROLLER COURSE

8085 MICROPROCESSOR PROGRAMS

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

MACHINE ARCHITECTURE & LANGUAGE

Hardware and Software Requirements

DP8570A DP8570A Timer Clock Peripheral (TCP)

Small Hardware Development and Prototyping Board for the SX28

HT46R14A Single Phase AC Induction Motor Frequency Converter Application

Serial Communications

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

TOSHIBA CMOS DIGITAL INTEGRATED CIRCUIT SILICON MONOLITHIC T6963C

Interfacing Analog to Digital Data Converters

Flexible Counter Series in DIN size 24 x 48 mm

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

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

TDA General description. 2. Features and benefits. Low power single card reader

ADS9850 Signal Generator Module

Digital Fundamentals

A High Resolution Performance Monitoring Software on the Pentium

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

Table 1 below is a complete list of MPTH commands with descriptions. Table 1 : MPTH Commands. Command Name Code Setting Value Description

Hello, and welcome to this presentation of the STM32L4 reset and clock controller.

Site Master S251B Antenna and Cable Analyzer

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

AN974 APPLICATION NOTE

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

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

Atmel Norway XMEGA Introduction

DS1678 Real-Time Event Recorder

DS Real Time Clock FEATURES PIN ASSIGNMENT PIN DESCRIPTION

Microprocessor & Assembly Language

DS1821 Programmable Digital Thermostat and Thermometer

Chapter 9 Latches, Flip-Flops, and Timers

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

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

Z80 Instruction Set. Z80 Assembly Language

6-BIT UNIVERSAL UP/DOWN COUNTER

8-bit Microcontroller with 2K/4K Bytes Flash AT89S2051 AT89S4051

Old Company Name in Catalogs and Other Documents

DS87C520/DS83C520 EPROM/ROM High-Speed Micro

8-bit Microcontroller. Application Note. AVR400: Low Cost A/D Converter

Instruction Set. Microcontroller Instruction Set. Instructions that Affect Flag Settings (1) The Instruction Set and Addressing Modes

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Flexible Active Shutter Control Interface using the MC1323x

Module 3: Floyd, Digital Fundamental

IMPLEMENTATION OF THE ZERO-ERROR ONE SECOND TIMING ALGORITHM FOR MICROCONTROLLER

Event counters in NOVA

APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.

Cascaded Counters. Page 1 BYU

DS87C530/DS83C530. EPROM/ROM Micro with Real Time Clock PRELIMINARY FEATURES PACKAGE OUTLINE DALLAS DS87C530 DS83C530 DALLAS DS87C530 DS83C530

W25Q80, W25Q16, W25Q32 8M-BIT, 16M-BIT AND 32M-BIT SERIAL FLASH MEMORY WITH DUAL AND QUAD SPI

PART B QUESTIONS AND ANSWERS UNIT I

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

Implementing a Real-Time Clock on the MSP430

Faculty of Engineering Student Number:

AN3252 Application note

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

NTE2053 Integrated Circuit 8 Bit MPU Compatible A/D Converter

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

Application Note [AN-029] Interfacing and set-up of Toshiba T6963C

DS1307ZN. 64 x 8 Serial Real-Time Clock

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

8-bit Microcontroller with 12K Bytes Flash and 2K Bytes EEPROM AT89S8253

HD44780U (LCD-II) A single HD44780U can display up to one 8-character line or two 8-character lines.

8-Bit Microcontroller with 8K Bytes Flash AT89S8252. Features. Description

8741A UNIVERSAL PERIPHERAL INTERFACE 8-BIT MICROCOMPUTER

css Custom Silicon Solutions, Inc.

Accurate Measurement of the Mains Electricity Frequency

LC7218, 7218M, 7218JM

HT1632C 32 8 &24 16 LED Driver

Hello, welcome to this presentation of the low power timer, or LPTMR, module for Kinetis MCUs. In this session you ll learn about the LPTMR, it s

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

AN3371 Application note

Transcription:

Timer and Counter 1

Timer and Counter Timer and Counter Measure the time/frequency of input signal. Generate outputs with variable frequency and pulse width. Can be controlled with software. Can be accessed thru interrupt and software. Structure of Timer/Counter (a series of divide-by-two flip flop:) 4 bit counter 2

Timer and Counter Not a power of 2 multiple of the clock freq. Q1: Count-by-10 counter Timer: when the incoming clock frequency is known. Get clock from Oscillator frequency (1/12), then by setting the preload value. We can generate a fixed period of time known to the designers. Counter: when the incoming clock is irregular Get clock from external pin, and we are only interested in the number of occurrence of this pulse. This is called counter (counting events) 3

Timer 0 and Timer 1 Two 16-bit timers: Timer 0 and Timer 1 Timer 0 (16-bit) TH0 TL0 Timer 1 (16-bit) TH1 TL1 4

Setup the Timer and Counter 2 registers: TMOD and TCON TMOD: Timer Mode Register Set the various timer operation mode. TMOD is an 8-bit register where the lower 4 bits are set aside for timer 0 and the upper 4 bits are set aside for timer 1. MSB LSB GATE C/!T M1 M0 GATE C/!T M1 M0 Timer 1 Timer 0 5

TMOD Register GATE C/!T M1 M0 GATE: To start and stop the timer GATE=1 HW control : is enabled only while INTx pin is 1 and TRx control pin (in TCON) is set.!int1!int0 P3.3 P3.2 GATE=0 SW control (used frequently) 6

TMOD Register C/!T: Timer or counter selection C/!T = 0 Timer (input from internal system clock) the Osc. Crystal (1/12) is used to trigger the timer. C/!T = 1 Counter (input from Tx input pin) M1 and M0: Mode selection for timer and counter T1 P3.5 T0 P3.4 Mode M1 M0 0 0 0 13-bit timer/counter mode 1 0 1 16-bit timer/counter mode 2 1 0 8-bit auto reload timer/counter mode 3 1 1 split timer/counter mode 7

TCON Register MSB LSB TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Timer 1 Timer 0 Timer 1 Timer 0 TF1: Timer 1 overflow flag TF1=1: Timer/counter 1 overflows. TF1=0: processor vectors to the interrupt services. TR1: Timer 1 run control bit TR1=1: turn Timer 1 ON TR1=0: turn Timer 1 OFF 8

TCON Register MSB LSB TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Timer 1 Timer 0 Timer 1 Timer 0 IE1: External interrupt 1 edge flag IE1=1: external interrupt is detected. IE1=0: when interrupt is processed. IT1: Interrupt 1 type control bit IT1=1: falling edge. IT1=0: low level triggered external interrupt. 9

Run Timer (SW Control) MSB TMOD LSB GATE=0 GATE=0 MSB TR1 Timer 1 Timer 0 TR0 TCON Timer 1 Timer 0 Timer 1 Timer 0 Gate=0, SETB TR1 Run Timer 1 SETB TR0 Run Timer 0 Gate=0, CLR TR1 OFF Timer 1 CLR TR0 OFF Timer 0 LSB 10

Timer and Counter Q2: Indicate which timer and which mode are selected for each of the following (a) MOV TMOD, #01H, (b) MOV TMOD, #20H, and (c) MOV TMOD, #12H. Q3: Find the timers clock frequency and its period for various 8051-based systems with the following crystal frequencies. (a) 12MHz, (b) 16MHz, and (c) 11.0592MHz. Q4: Find the value for TMOD if we want to program Timer 0 in mode 2, use 8051 XTAL for the clock source, and use instructions to start and stop the timer. 11

Timer Mode 1 Program Mode 1: 16-bit timer operation Steps 0000 ~ FFFFH to be loaded into the timers registers TH and TL. Start timer: SETB TR0 (i.e. Run Timer 0) Timer overflow flag (TF1 or TF0) should be monitored 0000 0001 : FFFF 0000 OV Stop timer: CLR TR0 (OFF Timer 0) Repeat: TH and TL are loaded again TF reset to 0 12

Steps to Program for Timer 0, Mode 1 Example Steps: 1. Load the TMOD value (eg. MOV TMOD, #21H) 2. Load TH0 and TL0 (eg. MOV TL0, #9CH & MOV TH0, 0FFH) 3. Start the timer (SETB TR0) 4. Keep monitor the timer flag (TF0) target: JNB TF0, target Jump out only if TF0=1 5. Stop the timer 0 (CLR TR0) 6. CLR TF0 7. Repeat Step 2. 13

Timer Delay Calculation For XTAL=11.0592MHz Hexdecimal (FFFF-YYXX+1) *1.085µs TH TL OR (65535-NNNNN) *1.085µs NNNNN is a decimal value derived from YYXXH 14

Examples Q5: In the following, we are creating a square wave of 50% duty cycle (with equal portions high and low) on the P1.5 bit. Timer 0 is used to generate the time delay. MOV TMOD, #01 Here: MOV TL0, #0F2H MOV TH0, 0FFH CPL P1.5 ACALL Delay SJMP Here Delay: SETB TR0 Again: JNB TF0, Again CLR TR0 CLR TF0 RET 15

Examples Q6: See the example in Q5, calculate the amount of time delay in the DELAY subroutine generated by the timer. Assume XTAL =11.0592MHz. Q7: Find the delay generated by timer 0 in the following code (Do not include the overhead due to instructions). CLR P2.3 MOV TMOD, #01 Here: MOV TL0, #3EH MOV TH0, #0B8H SETB P2.3 SETB TR0 Again: JNB TF0, Again CLR TR0 CLR TF0 CLR P2.3 16

Examples Q8: Modify TL and TH in Q7 to get the largest time delay possible. Find the delay in ms. (Do not include the overhead due to instructions) Q9: examples in Q7 and Q8 did not reload TH and TL, since it was a single pulse. The following program generates a square wave on P1.5 continuously using Timer 1 for a time delay. Find the frequency of the square wave if XTAL=11.0592MHz. (Not include overhead due to instructions) 17

Examples Assume we know the amount of timer delay, then how to find the values for TL and TH Q10: Assume the XTAL =11.0592MHz, what value do we need to load into the timers registers if we want to have a time delay of 5ms? Show the program for timer 0 to create a pulse width of 5ms on P2.3. Q11: Assume that XTAL =11.0592MHz, write a program to generate a square wave of 2 KHz frequency on P1.5. Q12: Assume that XTAL =11.0592MHz, write a program to generate a square wave of 50Hz frequency on P2.3. 18

Timer Mode 0 Mode 0: 13-bit Timer/counter mode 0000 ~ 1FFFH 19

Timer Mode 2 Mode 2: 8-bit auto reload Timer/counter mode (00 ~ FFH) In auto reload, TH is loaded with the initial count and a copy of it is given to TL. This reloading leaves TH unchanged still holding a copy of original values. This mode has many applications, including setting the baud rate in serial communication. 20

Mode 2 Programming Timer Mode 2 8 bit 00 ~FFH TH copy to TL Start SETB TR0, or TR1 TL increased FFH (OV monitoring) TH reload to TL TL is reloaded automatically with the original value kept by the TH register. To repeat the process, we must simply clear TF and let it go without any need by the programmer to reload the original value. 21

Steps to Programming for Timer Mode 2 1. Load TMOD (example for Timer 1) 2. Load TH1 3. Start timer: (SETB TR1) 4. Monitoring the OV: (loop: JNB TF1, loop) 5. CLR TF1 6. Go to step 4. (Note that: NOT Step 2) 22

Mode 2 Examples Q14: Assume that XTAL=11.0592MHz, find (a) the frequency of square wave generated on P1.0 in the following program, and (b) the smallest frequency achievable in this program, and the TH value to do that. MOV TMOD, #20H MOV TH1, #5 SETB TR1 Back: JNB TF1, Back CPL P1.0 CLR TF1 SJMP Back 23

Mode 2 Examples Q15: Assume that XTAL=11.0592MHz, find the frequency of a square wave generated on P1.0 MOV TMOD, #02H MOV TH0, #0 Again: MOV R5, #250 ACALL Delay CPL P1.0 SJMP Again Delay: SETB TR0 Back: JNB TF0, Back CLR TR0 CLR TF0 DJNZ R5, Delay RET 24

Mode 2 Examples Q16: Assume that we are programming the timer for mode 2, find the value (in hex) loaded into TH for each of the following cases. MOV TH1, #-200 MOV TH1, #-3 MOV TH0, #-48 MOV TH0, #-60 MOV TH1, #-12 25

Mode 2 Examples Q17: Find (a) the freq of the square wave generated in the following code and (b) the duty cycle of this wave. MOV TMOD, #02H MOV TH0, #-150 Again: SETB P1.3 ACALL Delay ACALL Delay CLR P1.3 ACALL Delay SJMP Again Delay: SETB TR0 Back: JNB TF0, Back CLR TR0 CLR TF0 RET 26

Mode 2 Examples Q18: Find (a) the freq of the square wave generated in the following code and (b) the duty cycle of this wave. MOV TMOD, #02H MOV TH0, #-150 Again: SETB P1.3 ACALL Delay ACALL Delay CLR P1.3 ACALL Delay SJMP Again Delay: SETB TR0 Back: JNB TF0, Back CLR TR0 CLR TF0 RET 27

Counter C/!T=0: As Time, using 8051 s crystal as the source of the frequency. C/!T=1: As counter, a pulse outside of the 8051 that increments the TH and TL registers When the C/!T=1, the counter counts up as pulses are fed from Pins P3.4 (for counter 0) or P3.5 (for counter 1) T1 T0 14 15 P3.5 P3.4 28

Counter Examples Q19: Assume that clock pulses are fed into pin T1, write a program for counter 1 in mode 2 to count the pulses and display the state of TL1 count on P2. P2 8 LEDs T1 P3.5 29

Counter Examples Q20: Assume that 1Hz freq pulse is connected to pin P3.4, write a program to display counter 0 on an LCD. Set the initial value of Th0 to 60. 1Hz P1 8 LCD T0 P3.4 30