【C51 Self-study Notes】Timer

Publisher:火星叔叔Latest update time:2022-03-09 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

CPU Timings:

Oscillation period: The period of the oscillation source that provides the timing signal for the microcontroller (crystal oscillator period or external oscillation period)


State cycle: 2 oscillation cycles are 1 state cycle, represented by S. The oscillation cycle is also called the S cycle or clock cycle.


Machine cycle: 1 machine cycle contains 6 state cycles and 12 oscillation cycles.


Instruction cycle: The total time taken to complete one instruction, measured in machine cycles.


For example: When the external crystal oscillator is 12MHz, the specific values ​​of the 4 time periods of the 89S52 microcontroller are:


Oscillation period = 1/12us;


Status cycle = 1/6us;


Machine cycle = 1us;


Instruction cycle = 1~4us;


Notice:


The timer/counter and the microcontroller's CPU are independent of each other. The timer/counter works automatically without the CPU's involvement.


The 51 single-chip microcomputer has two sets of timer/counters, which are called timer/counters because they can both time and count.


structure:

It consists of two registers, THx and TLx, with high 8 bits and low 8 bits.


TMOD is the working mode register of the timer/counter, which determines the working mode and function


TCON is a control register that controls the start and stop of T0 and T1 and sets the overflow flag.

control:

TMOD is used to set its working mode


TCON is used to control its startup and interrupt application.

GATE is the gate bit: it is used to control whether the start of the timer is affected by the external interrupt source signal.


When GATE=0: not affected by external factors; (Start: TR0 or TR1 in TCON is 1)

When GATA=1: affected by external factors; (start: TR0 or TR1 is 1, and the external interrupt pin INT0/1 is also high)

C/T: Timing/Counting mode selection bit. C/T = 0 is timing mode; C/T = 1 is counting mode.


M1M0: Working mode setting bit. The timer/counter has four working modes. (mainly 01)

TF1 (TCON.7): T1 overflow interrupt request flag. When T1 counts overflow, TF1 is automatically set to 1 by hardware. After the CPU responds to the interrupt, TF1 is automatically cleared to 0 by hardware. When T1 is working, the CPU can query the status of TF1 at any time. Therefore, TF1 can be used as a flag for query testing. TF1 can also be set to 1 or cleared to 0 by software, which has the same effect as setting 1 or clearing 0 by hardware.


TR1 (TCON.6): T1 operation control bit. When TR1 is set to 1, T1 starts to work; when TR1 is set to 0, T1 stops working. TR1 is set to 1 or cleared to 0 by software. Therefore, the start and stop of the timer/counter can be controlled by software. //Manual main operation TR


TF0 (TCON.5): T0 overflow interrupt request flag, its function is similar to TF1.


TR0 (TCON.4): T0 operation control bit, its function is similar to TR1.


 


Timer initial value calculation formula:


In timer mode: N = t/ Tcy


The formula for calculating the initial count value is: X=2^(number of timer bits)-N.


The initial value of the timer can also be obtained by directly complementing the number of counts.


In counting mode, the counting pulse is an external pulse on the T0 pin.


 


How to use the timer:

Assign values ​​to TMOD to determine the working mode of T0 and T1.


Calculate the initial value and write it into TH0, TL0 or TH1, TL1.


Assign values ​​to EA and enable timer interrupt.


Set TR0 or TR1 to start the timer/counter timing or counting.


#include

#define uchar unsigned char 

#define uint unsigned int 

 

unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};

 

uchar num,kk;

 

 

void main()

{

TMOD=0x01; //Set timer 0 to working mode 1

TH0=(65536-50000)/256; //Set the initial value

TL0=(65536-50000)%256;

EA=1;      //Turn on the general interrupt switch

ET0=1;      //Enable timer interrupt

TR0=1;      //Start the timer

while(1)

{

if(kk==20) //The digital tube displays after the timer runs 20 times

{

kk=0;

P1=smg_du[num];

num++;

if(num==10) //loop digital tube

num=0;

}

}

 

}

 

void timer0 () interrupt 1

{

//After entering the function, only method 2 can automatically load

//Manually load the initial value

TH0=(65536-50000)/256; 

TL0=(65536-50000)%256;

kk++; // kk adds one each time it comes in

 

}

Keywords:C51 Reference address:【C51 Self-study Notes】Timer

Previous article:[C51 Getting Started Notes] IIC bus + E2PROM chip (24C02)
Next article:【C51 Self-study Notes】Interrupt System

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号