Realizing LED light flashing for 1 second based on C51 microcontroller

Publisher:Huixin8888Latest update time:2024-03-20 Source: elecfansKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The crystal oscillator frequency used by C51 is 11.0592MHz. An LED light is connected to P0.0 of C51. Now it is required to design a program to make the LED light flash at an interval of 1s.


We not only design the program, but also analyze this microcontroller project.


By the way, let’s take a look at the difficulty of this question~~~~~~~~~~~


By the way, this lesson will use the microcontroller timer》》See my previous article for a detailed introduction to the timer


Design a circuit diagram. The microcontroller uses 80C52RC, the crystal oscillator is 11.0592MHz, and an LED is connected to P0.0


535b78d97ebb595471abd37010f0fdcb_wKgaomVDDs-AX4Z-AAJ8DpvdvCI842.jpg

Let me explain here, the IO of C51 only supports weak pull-up, which means that the IO of the microcontroller can only provide a very weak current when it is pulled up. This current may not be enough to light an LED light (or the LED light). The brightness is very low), in order to let the LED light up normally, we use the pull-up method. Here is a brief introduction to how to use it.

194d5daa1762b0dfd0626c637243c64f_wKgZomVDDs-AZnFXAABHppr1zbQ096.jpg

When P0.0 is at low level, the LED light is on and lights up normally. When P0.0 is at high level, the LED is not on and goes out.


Considering the resources at hand, I used a ready-made development board to complete this program design. This development board uses a pull-up LED, and LED1 is connected to P1.0. The actual circuit diagram is modified as follows:

d1eba55d7fc788cf66b0af6a497893f1_wKgaomVBx7SABco-AADHqvUpCvE716.jpg

Now let’s analyze the program


Block diagram


111d48747cebc57998a18a912f7f525a_wKgZomVDDs-AODMjAAE32iA-mZ0238.jpg

First of all, we need to understand that the LED we use is in pull-up mode. When P1.0 = 1, the LED is turned off, and when P1.0 = 0, the LED is turned on.


Since we want to control LEDs, here’s how to do it


Three special instructions


sfr P0 = 0x80;

This instruction is stored in reg52.h, and the address of the IO group register has been defined. We can use keywords like P0, P1, and P2 directly. It is convenient for us to operate the IO registers. It is worth mentioning that the IO registers are bit addressable.


sbit LED = P1^0

The sbit instruction defines a name for a port on the IO. For example, define a name called LED for P1.0 (written as P1^0). Use the following method the next time you operate this IO.


sbit LED = P1^0;//This is the previous definition



LED= 1; //Write high level to P1.0IO

LED= 0; //Write low level to P1.0IO

In addition, you can also directly operate the entire IO group, the method


So, the next step is the procedure


【1】Software delay implementation


The so-called software delay is to achieve the delay effect by executing empty code. The delay achieved by this delay has a characteristic that the time is not accurate, because the time for each operation in the C language is not fixed in the microcontroller.


The procedure is as follows


#include



sbit LED = P1^0;



void delay(unsigned int xms) //The actual value of parameter xms needs to be provided when calling.

{

  unsigned int i,j; //Define unsigned integer variables i, j

  for(i=0; i< xms; i++) //Without ";" at the end, the next statement is the loop body. Parameter xms delay x milliseconds.

      for(j=0; j< 110; j++); //With ";" behind it, it means that the loop body of this for statement is an empty statement.

}//This is worth learning, the code here is delay code



void main()

{

  LED = 1; //Turn off the LED light first


  while(1)

  {

    LED = !LED; //LED status inversion

    /*

    Here is a description of the inversion

    Use "!" to negate it.

    Before inversion, LED = 1, after inversion, LED = 0

    */

    delay(1000);//Software delay 1000ms = 1s

  }

}


This is the effect, so what, it’s okay, but...


The question requires us to delay by 1s. The precise mention of 1s must mean that we are expected to use a timer to complete this project.


I think that if we use software delay, the teacher will give a very low score, and it will not reflect our ability.


Next we use a timer to complete this question


About the usage of timer


#include

void main()

{//Note that setting the initial value of the timer must be set in the main function



  TMOD = 0x01;


  /*Set the timer mode register TMOD to 0000 0001

  GATE = 0

  C/T = 0

  M1 =0

  M0=1

  Standard 16-bit timer

  */


  //Set the initial value of the timer first

  TL0 = 0x3A;//Set the low bit of the timer initial value

  TH0 = 0x8E;//Set the high bit of the timer initial value


  TR0 = 0; //Remember to set the timer to start


  while(1)

  {

    if (TF0 == 1)

      {//Here you can put down the code to be executed after your timer is full.


       //After overflow, you need to set the initial value for your timer again.

        TL0 = 0x3A;//Set the low bit of the timer initial value

        TH0 = 0x8E;//Set the high bit of the timer initial value


        TF0 = 0;//Set the initial value and then reset the timer

      }

  }

}

We need to set up a standard 16-bit timer. For the setting method and process, please refer to the article I wrote. The above is the setting process for the 16-bit timer.


The following is the program block diagram


0748eaa5ef138fd13182266f31113094_wKgaomVDDs-AOCBNAAFlVYYQ_qA721.jpg

Below is the code


#include



sbit LED = P1^0;

unsigned char counter = 0;



void main()

{//Note that setting the initial value of the timer must be set in the main function

  TMOD = 0x01;

  /*Set the timer mode register TMOD to 0000 0001

  GATE = 0

  C/T = 0

  M1 =0

  M0=1

  Standard 16-bit timer

  */


  //Set the initial value of the timer first

  TL0 = 0x4B; //Set the low bit of the timer initial value

  TH0 = 0xF5;//Set the high bit of the timer initial value

  TR0 = 1; //Remember to set the timer to start



  while(1)

  {

    if (TF0 == 1)

      {

        counter++;

          if(counter >200)

          {

            counter = 0;

            LED = !LED;

          }

        TL0 = 0x4B; //Set the low bit of the timer initial value

        TH0 = 0xF5;//Set the high bit of the timer initial value

        TF0 = 0;//Set the initial value and then reset the timer

      }

  }

}

picture

picture


Keywords:C51 Reference address:Realizing LED light flashing for 1 second based on C51 microcontroller

Previous article:51 microcontroller minimum system production steps
Next article:MCS-51 pin function description

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号