Use Timer 0 to generate multiple time intervals

Publisher:创新驿站Latest update time:2012-12-01 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Use the microcontroller timer 0 to generate multiple time intervals. In the program, use timer 0 to interrupt once every 50ms, and then use global variables tt, t1, t2, t3, t4, t5 to count the number of interrupts to obtain 100ms, 1s, 2s, 4s, 8s, 16s intervals. In each timing interval, turn the LED light on and off once. The source program is as follows:
#include < AT89X51.H>
unsigned char tt,t1,t2,t3,t4,t5; //Global time interval variable
unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,
0x82,0xF8,0x80,0x90}; //Seven-segment digital tube decoding table
void main(void) //Main function
{
TMOD=0x01; //Timer 0 runs in working mode 1, GATE=0, C/T=0
TH0=(65536-15536)/256; //Set the initial value of the high 8 bits of timer 0
TL0=(65536-15536)%256; //Set the initial value of the low 8 bits of timer 0
//If the machine cycle is 1μs, the count is 50000, which is equivalent to 50ms.
TR0=1; //Start timer
ET0=1; //Enable timer 0 interrupt
EA=1; //Enable total interrupt
while(1) //Infinite loop
{
if(t1>5) P2_0=0; else P2_0=1; //Blink the LED according to the time interval variable value
if(t2>10) P2_1=0; else P2_1=1;
if(t3>20) P2_2=0; else P2_2=1;
if(t4>40) P2_3=0; else P2_3=1;
if(t5>80) P2_4=0; else P2_4=1;
P1=dispcode[t4/10]; //Display the tens digit of interval variable t4
P0=dispcode[t4%10]; //Display the ones digit of interval variable t4
}}
void t0(void) interrupt 1 //Timer 0 interrupt service routine
{
TH0=(65536-15536)/256; //Set the initial value of the high 8 bits of timer 0 (3CH)
TL0=(65536-15536)%256; //Set the initial value of the low 8 bits of timer 0 (B0H)
tt++; //Every time an interrupt occurs, tt increases by 1
if(tt==2) //If tt is equal to 2, which is equivalent to 100ms, execute the following statement
{
tt=0; //Clear tt, so the count value of tt is 0---1---2 (0), each interval is 50ms
t1++;
if(t1==10){t1=0;} t2
++; if(t2==20){t2=0;} t3++; if(t3==40){t3=0;} t4++; if(t4==80){t4=0;} t5++; if(t5==160){t5=0;} P2_6=~P2_6; //Every 100ms, the LED connected to P2_6 flashes once } }








Reference address:Use Timer 0 to generate multiple time intervals

Previous article:51 MCU timer 0 interrupt service routine
Next article:51 MCU timer 0 works in mode 2

Recommended ReadingLatest update time:2024-11-16 14:58

PIC Timer (TIMER0)
    For example, if the internal instruction cycle clock is set as the clock source and the frequency division is 1:4 for a 4M crystal oscillator, the timer increment is 1us (but the PIC18F microcontroller is Three-stage pipeline, one instruction cycle = 1/4 clock/crystal cycle. For example: 8M crystal
[Microcontroller]
PIC Timer (TIMER0)
ARM learning timer Timer0 experiment
/* Project: Timer0 experiment   Writer: SHOW         Time: 2011/10/16    Hareware: Hardware platform: mini2440, J-link.   Function: Use timer 0 to make LED1 flash at 1s intervals.   Direction: The main knowledge of this experiment is the setting of timer and timer clock source. The whole experiment idea is as fol
[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号