Study Notes: Learning MCU from Scratch (3) Timer

Publisher:DelightfulSmileLatest update time:2021-07-30 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Timers are very important in microcontroller applications. Microcontrollers can count external pulses and accurately time them. 51 has two timers, T0 and T1, with four working modes. They use TH0, TL0 and TH1 respectively, and TL1 as special function registers. TMOD is used to set the working mode, and TCON controls the start and stop of the counter. They are all 0 when the microcontroller is reset.

Timer Principle

TCON


TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0


TF1 and TF0 - overflow flag, = set to 1 when overflow


TR1 and TR0-counting operation control, =1 starts


TMOD


GATE C/T M1 M0 GATE C/T M1 M0


High 4 bits for timer 1, low 4 bits for timer 0


GATE gate control bit


When GATE=0, TR0/TR1 starts the counter; when GATE=1, TR0/TR1 and INT0/INT1 start the timer together.


C/T counting and timing mode selection


C/T=0 timer; C/T=1 counts external pin (T0 P3.4 or T1 P3.5) pulse (negative transition) counts


M1 M0 Working mode selection (0-4)


Mode 0 (M1=0, M0=0)


13-bit counter (THx low 5 bits and TLx 8 bits), 0~8191


After overflow, the software needs to set the initial count value


Method 1 (M1=0, M0=1)


16-bit counter (THx8 bits and TLx8 bits), 0~65535


After overflow, the software needs to set the initial count value


Method 2 (M1=1, M0=0)


8-bit counter (THx8 bits and TLx8 bits), 0~255


Automatically restore the initial value of the count after overflow (THx stores the initial value)


Method 3 (M1=1, M0=1)


Only applicable to T0, treat T0 as two 8-bit counters


TLx8 bits, similar to mode 0


THx8 bits, occupying TR1 and TF0 of T1


Machine cycle T = 12/fosc


Timer/Counter Initialization

Since the function of the timer/counter is determined by software programming, it is usually necessary to initialize the timer/counter before using it so that it works according to the set function. The steps for initialization are generally as follows:


1. Determine the working mode (i.e. assign value to TMOD);


2. Preset the initial value of timing or counting (the initial value can be directly written into TH0, TL0 or TH1, TL1);


3. Open the interrupt of the timer/counter as needed (assign a value to the IE bit directly);


4. Start the timer/counter (if it is specified to be started by software, TR0 or TR1 can be set to "1"; if it is specified to be started by the external interrupt pin level, the start level needs to be added to the external pin. When the start requirements are met, the timer starts counting or timing according to the specified working mode and initial value).


Example: Timer C program example


#include


unsigned int t0=0;

unsigned int t1=0;


sbit l0=P1^0; //LED 0

sbit l1=P1^1; //LED 1


void main()

{

   TMOD = 0x02; //t0 working mode 2; T1 working mode 1


   TH0=0x00;

   TL0=0x00;

   TH1=0x00;

   TL1=0x00;


   EA=1; //Open interrupt

   ET0=1; //T0 interrupt is enabled

   ET1=1; //T1 interrupt enabled

   TR0=1; //Start T0

   TR1=1; //Start T1


   l0=0;

   l1=0;


   while(1)

   {


      //idling, waiting for interruption

   }

}


void timer0() interrupt 1 using 2 //Timer 0 interrupt, using register bank 2

{


   //Method 2: Dispose in TH, no need to assign initial value


   t0++;


   //Too fast, need delay

   if(t0>=100)

   {

      t0=0;

      l0=~l0;

   }

}


void timer1() interrupt 3 using 3 //Timer 1 interrupt, using register bank 2

{

   TH1=0;

   TL1=0;

   t1++;

   if(t1>=100)

   {

      t1=0;

      l1=~l1;

   }

}

Reference address:Study Notes: Learning MCU from Scratch (3) Timer

Previous article:Study Notes: Learning MCU from Scratch (2) Full eight-segment digital tube display
Next article:Study Notes: Learning MCU from Scratch (5) A/D Conversion

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号