51 MCU Learning Part 6 - 1.3 How to Play with Flowing Lights

Publisher:温柔的爱情Latest update time:2016-12-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Last time we lit up a running light LED1. Actually, lighting up just one running light is a bit monotonous.

Let's try something different this time.

1. Lighting up multiple running lights

   a Open keil and close the original c file

Delete the one on the left, because I don't want to recreate the project.

Create a c file according to yesterday's method and add it to the project (you will do the same in the future, so please have your own naming rules, otherwise you will not be able to tell them apart. I named it LED1.1 here)

 

 

b Write the code. (If you want to light up the 1357 running lights)

#include
sbit LED1 = P1^0;
sbit LED3 = P1^2;
sbit LED5 = P1^4;
sbit LED7 = P1^6;
void main()
{
LED1 = 0;
LED3 = 0;
LED5 = 0;
LED7 = 0;
}

The above is the code,

 

 

c. As last time, connect USB and burn the program (remember to cold boot)

Here is an explanation. Since it is in the same project, the name of the hex file remains unchanged.

Now you can see the 1 3 5 7 running lights lit up.

The pixel of the mobile phone is not very good, but you should be able to see it clearly on the board.

2 Turning the running lights on and off

  It's a bit boring to keep the running lights on all the time, let's make them move

  We already know that LED1 = 0; set it to low level to light up,

         LED1 = 1; set to high level to turn off

  So how do you make a running lamp light up for a period of time and then turn it off for a period of time?

  This can be done with a delay function. In the delay function, do nothing.

  Create a new c file, pay attention to your naming, it is best not to use Chinese characters, and form your own naming rules

  a Write the code

Explanation:

  #define uint unsingned int is a macro definition, which we have learned in C language.

  // is a single line comment 

  /* is a multi-line comment */

  for(i=1000; i>0; i--) ;

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

  1 Why use two for loops?

    Because the delay time of a for loop is not enough for people to notice, two for loops are nested. This way, it is obvious to the human eye.

  2 Why is there a semicolon directly after the second for loop?

    A direct semicolon means that no code will be executed and it is simply used for delay.

 

  b Burning program

  c Observe the running lights

     Observe the running lights carefully. The first running light will light up for a certain period of time, then turn off for a certain period of time, and then light up again in this cycle.

     Because there is a while(1) loop, where is 1, the statements in the while loop will be executed continuously.

     This is also a very important point. Many MCU programs have a while (1) loop.

     This is determined by each microcontroller. If there is no loop in the program of some microcontrollers, it will execute from the beginning to the end, and then return to the beginning to continue executing. Repeat. Some microcontrollers will stop after executing it once more. Some may enter a disordered running state after execution, and you just don’t know where the program has run! Therefore, when writing a microcontroller program, it is best to add a while loop. Even if your program only runs once, you should add while(1); at the end to make it always point to this statement without any misoperation.

     Also, this is an imprecise delay, because for some reasons it is not possible to achieve a very precise delay here.

 

3 The running lights light up back and forth

   We have lit up the flowing lights, but how do we light up these flowing lights one by one?

  a Some students may think of this method

   In fact, it is also possible to write it like this, and you will see the running lights light up from 1 to 4 in sequence

   Here P1 = 0xff is written. This means that when all 4 running lights are lit, all 8 bits of P1 port are set to 1.

   That is, all of them are off. In this way, you can see the running lights light up one by one, all go out, and then light up one by one.

   In fact, there is another way.

 

  b Function usage of header file intrinsics

      intrins is a header file that contains many functions. Here are two of them.

      _crol_ Characters rotate left

      _cror_ Characters rotate right

      Take _cror_ as an example

      We all know that the P1 port controls the water lamp. P1 has eight

      By default, P1 = 0xff // 1111 1111 

      If you write the following code

      First, to use the _cror_ function, you first need the header file intrinsics

      The statement is #include

      Here a temp=0x7f // 0111 1111 is defined

      P1 = temp, so the eighth running light is on at first.

      

      The for loop means repeating this operation 8 times

      temp = _cror_(temp,1);

      The above statement means that port P1 is circularly shifted right by one position.

      

      So the seventh running light is on, and the others are off.

      There is another point here. A delay function is defined. Delay is a function with no return value.

      There are two definition methods, which are consistent with those in C language.

      Then you will see that the eighth running lamp lights up first, and then lights up one by one, while the others are indeed extinguished.

      Well, the usage of _crol_ is the same. In addition to lighting up one by one, you can also light up one at a time.

      temp = _cror_(temp,2); just change 1 to 2

      


Reference address:51 MCU Learning Part 6 - 1.3 How to Play with Flowing Lights

Previous article:51 MCU Learning Part 6 - 1.4 Display of Digital Tube
Next article:51 MCU Learning Part 6 - 1.2 The first C51 program (lighting up the running light)

Latest Microcontroller Articles
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号