51 MCU Introduction LED Flashing Program

Publisher:脑洞飞翔Latest update time:2021-02-01 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: This section briefly introduces the use of proteus simulation software. The key lies in the codes of the components needed, such as resistors are resistors, capacitors are capacitors, etc. However, you don't need to remember them deliberately. You will remember them naturally after using them many times. When you encounter new components, you can search them online. The example in the above picture is the minimum system of the microcontroller and the LED light,......


This section briefly talks about the use of proteus simulation software. The key lies in the codes of the required components, such as resistor is resistor, capacitor is capacitor, etc., but you don’t need to remember them deliberately. You will remember them naturally after using them many times. If you encounter new components, just search them online.

The example in the figure above is the minimum system of a single-chip microcomputer and an LED light. The list of components in this schematic is shown in the left column. If you want to add a component, click "P" and a dialog box for selecting components will pop up, which includes all the components that come with Proteus. At this time, you need to enter the component code to search, or if you know the category of the component, you can search directly one level at a time.

In this section, we need to make the LED flash. As you can imagine, the flashing of the LED requires the level of P0.0 to change at a certain frequency. The faster the frequency, the faster the LED flashes. Knowing this, the corresponding C code is very easy.


#include


sbit led = P0^0;


void delayxms(unsigned int xms);


void main()


while(1)


led = 0; //LED lights up


delayxms(1000); //delay 1s


led =1; //LED turns off


delayxms(1000); //delay 1s


void delayxms(unsigned int xms) //delay function


unsigned int i,j;


for(i=xms;i>0;i--) //Through the nested for loop, realize the xms delay


for(j=110;j>0;j--);


Compared with the first section, this section of C code has an additional parameter function delayxms. Each time the value of xms is different, the delay time implemented by the function is also different. For example, in this example, to implement a 1s delay, xms is assigned a value of 1000. When writing a microcontroller program, you must develop good programming habits. In actual projects, there will be many functions, and it is common to have multiple header files and source files separated. Therefore, when writing a program, we can understand it this way, that is, the program has two parts, one is the declaration area of ​​variables and functions, which tells the microcontroller what variables and functions I will use in this program; the second part is the function implementation, that is, the specific implementation code of the declared function. In this example, the C code, after including the header file, declares the led and delayxms functions, telling the microcontroller that the operation of the led in the following code is actually the operation of P0.0. The parameter type of the delayxms function is an unsigned integer and has no return value. Why can the delayxms function implement the delay of xms? Here we need to explain the clock cycle of the microcontroller.


Clock cycle


The clock cycle is the smallest unit in the MCU cycle, which is defined as the reciprocal of the clock frequency. For example, in this example, the clock frequency is 11.0592MHz, so the clock cycle is its reciprocal. One machine cycle is equal to 12 clock cycles, which is approximately equal to 1.09us. In one clock cycle, the MCU completes the most basic action, which is equivalent to the beating of a human heart. Obviously, the higher the clock frequency of the MCU, the faster the MCU works. Of course, this is limited to the hardware design of the MCU. Advanced MCUs such as Freescale and STM32 have a frequency multiplication function. The C code in the MCU takes time to execute each time. Through the breakpoint function in the KEIL software simulation, we can calculate the time required for code execution, which is why the delayxms function is written like this. Of course, it can be done more accurately, that is, using the built-in module timer of the MCU, which we will talk about in the third lesson.


In addition, the while loop and for loop in this C code are essentially the same. Both loops continue when certain conditions are met, and exit the loop when the conditions are not met. First, let's talk about the while loop in the main function. The while loop is very intuitive to understand. When the condition is true (non-zero), the loop will be executed. Because the code in the microcontroller is executed sequentially, if there is no while (1) dead loop, the effect achieved is that the LED lights up and turns off once, and it cannot light up a second time because the program has been executed. For the for loop in the delay function, the first part of the for loop is the initial value of the control variable, the second part is the condition that the control variable must meet, and the third part is the operation of the control variable after executing a loop. For example, in this example, first i=xms satisfies the condition i>0, then enter the for loop, and then reduce the i variable by 1. If the condition i>0 is still met, then the loop continues. It's that simple.


Reference address:51 MCU Introduction LED Flashing Program

Previous article:Design of automobile anti-theft system based on time trigger mode
Next article:About the microcontroller pin read and write operations

Recommended ReadingLatest update time:2024-11-16 16:01

Analysis of the innovative 360-degree LED filament bulb
 Tungsten filament incandescent lamps are about to be eliminated. Many people are nostalgic for the incandescent lamps that have been used for more than 100 years. LED lighting will eventually replace tungsten filament incandescent lamps. This is an inevitable trend of modern scientific and technological developme
[Power Management]
Analysis of the innovative 360-degree LED filament bulb
tiny6410 linux miscellaneous device led driver
#include linux/miscdevice.h #include linux/kernel.h #include linux/module.h #include linux/init.h #include linux/fs.h #include linux/types.h #include linux/errno.h #include linux/ioctl.h #include linux/cdev.h #include linux/device.h #include linux/ioport.h #include asm/io.h #include asm/uaccess.h   #defin
[Microcontroller]
LED "optical film" indoor lighting is popular in the market
The current dilemma of LED indoor lighting products: In recent years, with the continuous development of my country's lighting technology, especially the continuous expansion of the application of the latest generation of LED light sources both indoors and outdoors, coupled with the trend of energy con
[Power Management]
LED lighting has become the core competition in the lighting market and has a promising development trend
Faced with huge market opportunities and such fierce competition in the lighting market, the world's major LED companies are increasing their R&D efforts, new technologies are constantly making breakthroughs, and the pace of innovation in LED lighting technology is accelerating. Find and consolidate your position in
[Power Management]
What are the shortcomings of LED lighting technology?
单个LED功率低。为了获得大功率,需要多个并联使用,例如汽车尾灯,并且单个大功率LED价格很贵。LED另外一个致命缺点,显色指数低!在LED照射下显示的顏色没有白炽灯真实,这要从光谱分佈上来分析,属于技术问题。 LED lighting Click here to view all news photos Another disadvantage is the "light spot". Due to the defects in the manufacturing process of the white light LED itself and the matching error
[Power Management]
What are the shortcomings of LED lighting technology?
Teach you the selection and installation skills of LED light strips
   When decorating, you will usually see LED light strips in the decoration list, but most owners don’t know what they are. This is actually normal. First, they are not used much, and second, the light strips are hidden. We can only see their light but not their shape. In fact, this is the highest realm of using light
[Power Management]
Teach you the selection and installation skills of LED light strips
Development and application of LED street lights
        1. Introduction   For a long time, traditional light sources such as high-pressure sodium lamps and some metal halide lamps have played a leading role in road lighting. In recent years, the application of LED lighting in China has developed rapidly. Many manufacturers have launched high-power LED street lamp
[Power Management]
Development and application of LED street lights
How to choose constant current module when designing LED street lights?
We know that all LEDs must be powered by a constant current source, but many street lamp manufacturers currently look for a suitable constant current module after the LED module has been designed . However, they do not know that this design method will encounter problems, at least making this design not optima
[Industrial Control]
How to choose constant current module when designing LED street lights?
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号