Use the timer to control the LED light, turn on for 1 second and turn off for 1 second

Publisher:advancement3Latest update time:2015-07-22 Source: 51heiKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//*f=6MHZ, timer 0 generates 100us timing in working mode 2 (auto-reload 8-bit timer mode) and query mode,
//And output a continuous square wave r with a period of 2s at P0.0 to control the LED light, that is, on for 1s and off for 1s*//
 
#include
sbit d1=P0^0;
void timer();
unsigned int num,tt;
void main()
{
    TMOD=0X02; //Adjust working mode//
    TH0= 0Xce; //Set initial value
    TL0=0Xce;
    IE=0X00; //Disable interrupt
    TR0=1; //Timer start     
    
   while(1) //Large loop
  {
  
    if(num==10000) //This big loop must be added, otherwise the program starts from the beginning and all variables are reset.
                      //The value of d1 remains unchanged, because num is always 0 here //
     d1=!d1; // You can also put these three sentences after the timing program, so that after the timing program, the value of num is 10000,
		//When the required timing is reached, (it is not reset because the program is not completed)//
  num=0; // After that, output d1 will change as required//
   
   while(num<10000) //100us timing program
     {
       if(TF0) //Check whether the 100us timing has arrived? //
    timer();
    
      }
    }
 
}
 void timer()
  {
      TF0=0; //The query method must be cleared by software, otherwise TF will always overflow to 1, and the timing will only be accurate for the first time//
   num++;
  }
Keywords:Timer Reference address:Use the timer to control the LED light, turn on for 1 second and turn off for 1 second

Previous article:LED pattern lamp test-C51 program
Next article:Inversion matrix keyboard

Recommended ReadingLatest update time:2024-11-16 13:25

PIC16F15323 microcontroller (interrupt and timer Timer0)
The development environment selected is MPLAB X IDE v5.50 and xc8-v2.32-full-install-windows-x64-installer. 1 Basic principles 2 Implementation Code The code is mainly written based on FIGURE 25-1 and the interrupt logic block diagram, so that the code is readable and easy to understand. However, some registe
[Microcontroller]
PIC16F15323 microcontroller (interrupt and timer Timer0)
C51 MCU's precise delay program (Timer)
Using timer/counter to achieve accurate delay Single-chip microcomputer systems generally use 11.059 2 MHz, 12 MHz or 6 MHz crystal oscillators. The first one is easier to generate various standard baud rates, and the machine cycles of the latter two are 1 μ s and 2 μ s respectively, which is convenient for accurate d
[Microcontroller]
Cortex M3S811 Study Notes-TIMER32
Today, we have clarified the timing functions under 32-bit. First of all, there are only three functions under 32-bit, 32-bit single-cycle timing, 32-bit multi-cycle timing, and an RTC that requires an external clock. We will not use RTC for the time being, so ignore it. Grasp the overall situation, it is a timer us
[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号