STC89C52 microcontroller timer

Publisher:as233632621Latest update time:2022-10-19 Source: csdnKeywords:STC89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

How to set the timer?

For example:

Timing 50 microseconds


First of all, 50000 ÷ 1.085 = 46,082.94930875576. Starting from 0 and adding 19,453 times will overflow.     This calculation will have errors, but the impact is relatively small and can be ignored.

TH0 = (65535 - 46082) / 256; // Place the quotient in the upper eight bits

TL0 = (65535 - 46082) % 256; // Put the remainder in the lower eight bits

Or 65535 - 46082 = 19453


In hexadecimal we get: 4BFD

Binary gets: 0100 1011 1111 1101 

Put the obtained hexadecimal numbers into TH0 and TL0 respectively;

TH0 = 0x4b;

TL0 = 0xfd; //Timing 50ms


timer

Timer 0 working mode 1 16-bit timing mode, digital tube dynamically displays 0-10 stopwatch.


#include

#include


#define uint unsigned int

#define uchar unsigned char


sbit DU = P2^6;//digital tube segment selection

sbit WE = P2^7;//Nigital tube segment selection


//Common cathode digital tube segment selection table 0-9

uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};


/*====================================

Function : delay(uint z)

Parameter : z delay millisecond setting, value range 0-65535

Return value : None

Description : 12T/Fosc11.0592M millisecond delay

====================================*/

void delay(uint z)

{

uint x,y;

for(x = z; x > 0; x--)

for(y = 114; y > 0; y--);


/*====================================

Function : display(uchar i)

Parameter : i displays the numerical value, the value range is 0-255

Return value : None

Description : Three-digit common cathode digital tube dynamic display

====================================*/

void display(uchar i)

{

uchar bai, shi, ge;

bai = i / 100; //236 / 100 = 2

shi = i % 100 / 10; //236 % 100 / 10 = 3

ge = i % 10;//236 % 10 =6

//The first digital tube 

P0 = 0XFF;//Clear broken code

WE = 1;//Open bit selection latch

P0 = 0XFE; //1111 1110

WE = 0; //Latch bit selection data

DU = 1; //Open segment selection latch

P0 = tabel[bai];//

DU = 0; //Latch segment selection data

delay(5);


//The second digital tube

P0 = 0XFF;//Clear broken code

WE = 1;//Open bit selection latch

P0 = 0XFD; //1111 1101

WE = 0; //Latch bit selection data

DU = 1; //Open segment selection latch

P0 = tabel[shi];//

DU = 0; //Latch segment selection data

delay(5);


//The third digital tube

P0 = 0XFF;//Clear broken code

WE = 1;//Open bit selection latch

P0 = 0XFB; //1111 1011

WE = 0; //Latch bit selection data

DU = 1; //Open segment selection latch

P0 = tabel[ge];//

DU = 0; //Latch segment selection data

delay(5);

}

//Timer 0 initialization

void timer0Init()

{

TR0 = 1; //Start timer 0

TMOD = 0X01; //Timer operating mode 1, 16-bit timer counting mode

TH0 = 0x4b;

TL0 = 0xfd; //Timing 50ms


/*

   How to set the time? For example, the timer is 50 microseconds

First of all, 50000 ÷ 1.085 = 46,082.94930875576. Starting from 0 and adding 19,453 times will overflow.     This calculation will have errors, but the impact is relatively small and can be ignored.

TH0 = (65535 - 46082) / 256; // Place the quotient in the upper eight bits

TL0 = (65535 - 46082) % 256; // Put the remainder in the lower eight bits

Or 65535 - 46082 = 19453

In hexadecimal we get: 4BFD

Binary gets: 0100 1011 1111 1101 

Put the obtained hexadecimal numbers into TH0 and TL0 respectively;

TH0 = 0x4b;

TL0 = 0xfd; //Timing 50ms

*/

}


void main(){//The main function itself will loop

uchar mSec, Sec; //Milliseconds and seconds storage variables

timer0Init();//Timer 0 initialization

while(1)

{

if(TF0 == 1)//Determine whether it overflows

{

TF0 = 0;//Software clears the overflow flag bit

TH0 = 0x4b;

TL0 = 0xfd; //Timing 50ms

mSec++;//50ms to

if(mSec == 20)

{

mSec = 0;

Sec++; //1 second is up

}

}

display(Sec); //Nigital tube display function

if(Sec > 59)

Sec = 0; // Clear seconds 

}

}  

Keywords:STC89C52 Reference address:STC89C52 microcontroller timer

Previous article:STC89C52 microcontroller serial communication
Next article:STC89C52 microcontroller matrix keyboard

Recommended ReadingLatest update time:2024-11-16 10:34

Simulation and Program of Stepper Motor Controlled by STC89C52RC MCU
This single chip microcomputer is STC89C52RC, this program is applied to step motor--28BYJ, a small stepper motor, with a power supply voltage of DC5V. It can be directly connected to the single chip microcomputer and driven by the UL2003 driver chip. The digital tube is a common cathode interface, which controls th
[Microcontroller]
Simulation and Program of Stepper Motor Controlled by STC89C52RC MCU
Realizing 8*8 LED heartbeat effect based on STC89c52 microcontroller
#include "reg51.h" //This file defines some special function registers of the microcontroller #include "intrins.h" typedef unsigned int u16; //declare and define the data type typedef unsigned char u8; sbit SRCLK=P3^6; //Use hc595 chip sbit RCLK=P3^5; //Use hc595 chip sbit SER=P3^4; //Use hc595 chip u8 leddua
[Microcontroller]
STC89C52 microcontroller uses timer to flash LED light
#include reg52.h #include intrins.h #define uint unsigned int #define uchar unsigned char sbit LED1 = P1^0;//LED1 //Timer 1 initialization void timer1Init() { TR1 = 1; //Start timer 1 TMOD |= 0X10; //Timer 1 working mode 1, 16-bit timing mode TH1 = 0x4b; TL1 = 0xfd; //Timing 50ms } void main()//mai
[Microcontroller]
STM32 one timer outputs 4 PWMs with different frequencies and adjustable duty cycles
main.c /*********************************************      Title: Exercise on operating USART      Software platform: MDK-ARM Standard Version4.70      Hardware platform: stm32f4-discovery        Main frequency: 168M      Periph_Driver_version: V1.0.0            Description: Use one timer (TIM3) to implement four PW
[Microcontroller]
Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 06 - Dynamic Digital Tube Driver
1 Dynamic Scan So what is dynamic scanning? For example: if there are two digital tubes and we want to display the number "12", we can first turn on the high-order bit select transistor, then control the segment select to display "1", and then turn on the low-order bit select transistor after a certain delay, and th
[Microcontroller]
Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 06 - Dynamic Digital Tube Driver
Schematic diagram of liquid level remote monitor based on STC89C52 microcontroller
This design consists of a master machine and a slave machine. The host is composed of STC89C52 microcontroller circuit + Bluetooth host circuit + ultrasonic ranging module circuit + power supply circuit. The slave machine is composed of STC89C52 microcontroller circuit + LCD1602 liquid crystal display circuit + Blue
[Microcontroller]
Schematic diagram of liquid level remote monitor based on STC89C52 microcontroller
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号