C language uses the timer method to control the LED light to flash at a speed of 1S

Publisher:Serene123Latest update time:2015-06-29 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Recently, because I can't catch up with the progress of the advanced class, the tutorials given by the teacher are always intermittent, so I plan to study with the netizens who passed the exam on November 1st. Haha, this is also a lesson for me. In 3D learning, you must persist in taking one step at a time. Persistence will surely bring miracles, but you must keep up with everyone. No matter how busy you are in the future, you must work hard to realize your plan. You can't be like this time, because of the so-called exam, you missed several classes, but it doesn't matter. It's always good to learn some lessons. I can give myself some time to learn more basic knowledge.

Today I started to learn the counter of the microcontroller. Haha, it may be because I always couldn't set the timer accurately when programming in C language. When I learned that I could make the microcontroller C language timer accurate after learning the timer/counter, I felt really motivated to learn it. Although it took me a long time to write a simple program, it felt so refreshing. I think the key to learning the timer/counter is to learn how to program and control TCOM, TMOD, TH and TL. The main thing is the accurate calculation of TH and TL. And how to use it with the loop statement after the completion of the time.

The program I wrote today uses the microcontroller timer to control the LED light to flash at a speed of 1S. My crystal oscillator is 12MHZ. First, let the timer overflow once in 0.02S to make TF1=1. Then use I++ to loop 50 times, which adds up to 1S. The program is as follows:

#include
sbit LED = P1^2;
void main()
{ unsigned char i;
 TMOD |= 0x10;
 TMOD &= 0xdf;
 TH1 = 0xb1;
 TL1 = 0xdf;
 TCON |= 0x40;
 while(1)
 {
  if(TF1==1)
  {
   TF1 = 0;
   TH1 = 0xb1;
   TL1 = 0xdf;
   i++;
    if(i==50)
    {
     i=0;
     LED=~LED;
    }
   }
 }
}

Reference address:C language uses the timer method to control the LED light to flash at a speed of 1S

Previous article:Digital tube dynamic display
Next article:Single chip traffic light experiment

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号