8051 MCU (STC89C52) two countdown timers count synchronously

Publisher:等风来88888Latest update time:2020-08-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Here, timer 0 is required to provide 5ms precise timing, so each timing cycle requires (5*10^-3)/(1*10^-6)=5000 times of adding 1 count, so the initial value of timer 0 is set to 65536 - 5000 = 60536 = EA84H.


Here we first implement two countdown timers working simultaneously, and the next step is to consider how to implement two countdown timers working asynchronously.

#include <STC89C5xRC.H>

 

void delay() //Use timer 0 to achieve 5ms precise timing

{

TMOD = 0x01;

TH0 = 0xEA;

TL0 = 0x84; //65536 - 5000 = 60036

 

TF0 = 0; //Set the overflow flag to 0

TR0 = 1; //Start timer 0

while(TF0 == 0);

TR0 = 0; // Pause timer 0

}

 

void disp_digit(int d, int r)

{

unsigned char code DIG_CODE[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};

//First countdown

//Display the single digit

P2 = 0; // P2 = 0 -> (P24, P23, P22) = (0, 0, 0) -> the first number from the right lights up

P0 = DIG_CODE[d % 10];

delay(); //5ms precise timing

//Display the tens digit

P2 = 1 << 2; //P2 = 0000 0100 -> (P24, P23, P22) = (0, 0, 1) -> the second digit from the right lights up

P0 = DIG_CODE[d / 10];

delay(); //5ms precise timing

//Second countdown

//Display the single digit

P2 = 6 << 2; //P2 = 0001 1000 -> (P24, P23, P22) = (1, 1, 0) -> The seventh number from the right lights up

P0 = DIG_CODE[r % 10];

delay(); //5ms precise timing

//Display the tens digit

P2 = 7 << 2; //P2 = 0001 1100 -> (P24, P23, P22) = (1, 1, 1) -> The eighth number from the right lights up

P0 = DIG_CODE[r / 10];

delay(); //5ms precise timing

}

 

int main()

{

int i;

int sec1, sec2;

while(1)

{

sec1 = 15;

sec2 = 15;

while(sec1 >= 0)

{

for(i = 0; i < 50; i++)

{

disp_digit(sec1, sec2); // takes about 20ms

}//20ms*50=1000ms=1s

sec1 --;

sec2 --;

}

}

return 0;

}


Reference address:8051 MCU (STC89C52) two countdown timers count synchronously

Previous article:MCU Learning Journey (IV) Buzzer Singing
Next article:8051 MCU (STC89C52) timer achieves 10ms accurate timing

Recommended ReadingLatest update time:2024-11-15 16:18

8051/2 microcontroller commonly used local communication methods UART, RS485, I2C, SPI UART serial communication 1
1. Description 1. Serial port UART, baud rate: 9600 When connecting a device, generally only GND RX TX is connected, and Vcc is not connected to avoid power supply conflicts with the target device. 1.1 RS485 standard (+2V ~ +6V: 1 / -6V ~ -2V: 0) 1.2 RS232 standard (-15V ~ -3V: 1 / +3V ~ +15V: 0), MAX232 needs
[Microcontroller]
8051/2 microcontroller commonly used local communication methods UART, RS485, I2C, SPI UART serial communication 1
STC89C52 controls 74HC595 and 74HC138 dual-color 16x16 dot matrix screen to display Chinese characters in a loop
Introduction In addition to using MAX7219, some common LED dot matrices also use 74HC595. The former can be refreshed actively, while the latter requires the host computer to actively scan and refresh. The one in my hand is a 16x16LED dot matrix module from Defeilai. The model printed on the board is LY-LED16x16B V2
[Microcontroller]
STC89C52 controls 74HC595 and 74HC138 dual-color 16x16 dot matrix screen to display Chinese characters in a loop
A brief discussion on the learning method of 8051
It has been more than two years since I started learning 51 single-chip microcomputers in my sophomore year. During this period, I played with AVR, Freescale, and later bought an ARM9 experimental board. I first used it as a single-chip microcomputer to run some simple programs on the bare metal, and later switched to
[Microcontroller]
8051 Interrupt Sources
I wrote a C program on Keil to figure out the implementation of interrupts. I took notes:     1. Five interrupt sources of 8051    serial number  Interrupt Sources  Entry address   0  /INT0  0x0003   1  TIMER0  0x000b   2  /INT1  0x0013   3  TIMER1  0x001b  
[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]
Flash security options for C8051F
The set of security lock bytes stored at addresses 0xFDFE and 0xFDFF protects the FLASH memory so that its contents cannot be read or modified through the JTAG interface. Each bit in the security lock byte protects an 8k-byte memory block. Clearing a bit in the read lock byte to 0 prevents reading the corresponding FLA
[Microcontroller]
Implement 8051 serial communication using polling and interrupt processing
#include STC89C5xRC.H //Previously, the serial port communication was realized by polling. Here, the front-end and back-end system structure is used to realize the function. int s; void UART_INT() interrupt 4 //UART interrupt handler {     if(RI == 1) //If data is received     {         RI = 0;         s = SBUF;
[Microcontroller]
Implement 8051 serial communication using polling and interrupt processing
Design and application research of memory oscilloscope based on enhanced 8051 microcontroller
1 Introduction The oscilloscope is a basic instrument for electronic measurements. Due to its real-time, intuitive and vivid graphics display characteristics, it is also one of the commonly used instruments in general physics laboratories. As we all know, oscilloscopes are made based on the principles of input voltage
[Microcontroller]
Design and application research of memory oscilloscope based on enhanced 8051 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号