C51 MCU applet (I)

Publisher:sclibinLatest update time:2020-06-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I haven't touched a microcontroller for a long time, so I'm a little rusty. I still vaguely remember sitting in front of a computer screen and learning the microcontroller by myself. I went through the basics of microcontrollers and now it's quite interesting. Attached is a small microcontroller program that I learned when I was a beginner. Please give me your advice!


Use P1 port as output port, connect 8-bit LEDs used for logic level display. Design the program to make the LEDs light up first with odd numbers and then with even numbers at intervals of 300ms, and repeat 3 times; one LED lights up and down 3 times; two LEDs flow from both sides to the middle 3 times; then flow from the middle to both sides 3 times; all 8 LEDs flash 3 times; turn off all LEDs and stop.


Comprehensive experimental procedures:


#Library definition

#include

#include


sbit led=P1^0; #bit definition

int i,j,k,num,e,p,m;            

void delay(int k) #define delay function

{

 while(k--);

}

void timer0() # Initialization of timer 0

{

  TMOD=0X01; #Working mode

    TR0=1;          

    EA=1; # Enable total interrupt

    ET0=1;

    TH0=0XDc; #The timing time needs to be converted

    TL0=0X00;

}

void Pled()     

{

          EA=0;

                        //P1=0xfe;

                //delay(50000);

for(p=0;p<3;p++){               

    P1=0xfe;

    for(num=0;num<8;num++)

    {

   delay(50000);

     P1=_crol_(P1,1); #_crol_ (left shift function)

        if(num==6)

        {

            for(e=0;e<6;e++){

                delay(50000);

    P1=_cror_(P1,1); #_cror_ (left shift function)


 }

 }


            }

}

}

void flowmiddle()

{

for(m=0;m<3;m++)

{   

  P1=0x7e;

    delay(50000);

    P1=0XBD;

    delay(50000);

    P1=0XDB;

    delay(50000);

    P1=0XE7;

    delay(50000);

}

for(m=0;m<3;m++)

{   

        P1=0XE7;

    delay(50000);

    P1=0XDB;

    delay(50000);

    P1=0XBD;

    delay(50000);

    P1=0x7e;

    delay(50000);

}

  for(m=0;m<3;m++){

            P1=0xff;

            delay(50000);

            P1=0;

            delay(50000);

  }

}

void main()

{

    timer0();

     while(1){

       if(j==7)

         { j=0;

     Pled();

     flowmiddle();

} if(j==0)

{

P1=0xff;

}

     }

}

void time0() interrupt 1 #Timer 0 interrupt response

{

    TH0=0Xdc;

    TL0=0X00; //10ms

    i++;

    if(i==30)

    { i=0; 

        j++;

        if(j%2==0){

        P1=0x55;}

        if(j%2!=0)

        {

     P1=0xaa;

}  

  }

}


Instructions for the experimental implementation: Use timer 0 to set a timer for 300ms. By judging whether the variable j is an odd or even number after the timer expires, light up the corresponding small lights. When the variable reaches 7, clear the variable value and execute left and right shift operations, as well as operations flowing to the middle.


Reference address:C51 MCU applet (I)

Previous article:51 MCU learning knowledge points
Next article:51 MCU Learning Summary Series (0) MCU Minimum System

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号