The microcontroller P1 switch controls the lower four-bit water lamp

Publisher:CelestialSoulLatest update time:2020-11-27 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The P1 switch controls the low four-bit running water light. The speed is a bit fast, so adjust it yourself. There are notes.

The microcontroller source program is as follows:

#include

#define led P1 //Can be changed to other ports

void delay(int x);

void left(int x);

void right(int x);

void flash1(int x);

void flash2(int x);

void delay(int x) //delay function 1ms (approximately)

{int i;

while(x)

{

  for(i=0;i<125;i++);

  x--;

}

}

void left(int x) //From P1^3 to P1^0 (I call it left shift)

{int i,j;

while(x)

  {for(i=0;i<3;i++) // loop 3 times

   {led=0xfe;                  

    delay(50); //Each light is on for 50ms

    for(j=0;j<3;j++) //4 digits in total

     {

      led=led<<1|0xf1;

      delay(50);

     }

   }

   x--;

}

}

void right(int x) //From the lowest bit to the highest bit (I call it right shift)

{int i,j;

while(x) //Others are the same as left shift

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

   { led=0xf7;

     delay(50);

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

     {

      led=led>>1|0xf8;

      delay(50);

     }

   }

  x--;

}

}

void flash1(int x) //Four-bit full flash (can be changed in many ways)

{int i;

while(x)

{led=0xf0;

  delay(100); //lights up for 100ms each time

  for(i=0;i<1;i++) //You can change the number of loops

   {

    led=~led|0xf0; //shield the upper four bits

    delay(100);

   }

   x--;

}

}

void flash2(int x) // flash every other bit (full flash change)

{int i;

while(x) //Basically the same as full flash

{led=0xf5;

  delay(100);

  for(i=0;i<1;i++)

  {

   led=~led|0xf0;

   delay(100);

  }

  x--;

}

}

main()

{while(1)

{led=0xff;

switch(led) //When all lights are off, multiple buttons will not work when pressed at the same time;

   {case 0xef :{left(2);}break; //Press P1^4 to shift left and change the number of loops

    case 0xdf :{right(2);}break; //Press P1^5 to move right

    case 0xbf :{flash1(2);}break;//Press P1^6 to execute full flash

    case 0x7f :{flash2(2);}break;//Press P1^7 to execute full flash change

        default:{led=0xff;}

   }

}

}

Copy code

#include

#define led P1 //Can be changed to other ports

void delay(int x);

void left(int x);

void right(int x);

void flash1(int x);

void flash2(int x);

void delay(int x) //delay function 1ms (approximately)

{int i;

while(x)

{

  for(i=0;i<125;i++);

  x--;

}

}

void left(int x) //From P1^3 to P1^0 (I call it left shift)

{int i,j;

while(x)

  {for(i=0;i<3;i++) // loop 3 times

   {led=0xfe;                  

    delay(50); //Each light is on for 50ms

    for(j=0;j<3;j++) //4 digits in total

     {

      led=led<<1|0xf1;

      delay(50);

     }

   }

   x--;

}

}

void right(int x) //From the lowest bit to the highest bit (I call it right shift)

{int i,j;

while(x) //Others are the same as left shift

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

   { led=0xf7;

     delay(50);

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

     {

      led=led>>1|0xf8;

      delay(50);

     }

   }

  x--;

}

}

void flash1(int x) //Four-bit full flash (can be changed in many ways)

{int i;

while(x)

{led=0xf0;

  delay(100); //lights up for 100ms each time

  for(i=0;i<1;i++) //You can change the number of loops

   {

    led=~led|0xf0; //shield the upper four bits

    delay(100);

   }

   x--;

}

}

void flash2(int x) // flash every other bit (full flash change)

{int i;

while(x) //Basically the same as full flash

{led=0xf5;

  delay(100);

  for(i=0;i<1;i++)

  {

   led=~led|0xf0;

   delay(100);

  }

  x--;

}

}

main()

{while(1)

{led=0xff; //When all lights are off, multiple buttons will not work when pressed at the same time;

if(led==0xef) //Press P1^4 to shift left, you can modify the number of loops;

   {left(2);}

  else if(led==0xdf) //Press P1^5 to move right

   {right(2);}

  else if(led==0xbf) //Press P1^6 to execute full flash

   {flash1(2);}

  else if(led==0x7f) //Press P1^7 to execute full flash change

   {flash2(2);}

}

}



Keywords:MCU Reference address:The microcontroller P1 switch controls the lower four-bit water lamp

Previous article:Single chip temperature control infrared remote control fan source program servo control direction of DC motor
Next article:STC89C51 MCU and LCD12864 display sin function image

Recommended ReadingLatest update time:2024-11-15 15:04

Pingtouge announces open source MCU chip design platform
Everyone knows that Alibaba is making chips. Now, Alibaba's chip ambitions have been exposed. Just now, at the Internet Conference, Alibaba's Pingtouge announced the open source MCU chip design platform. This is another heavyweight product of Pingtouge after the launch of Hanguang 800 and Xuantie 910. Pingtouge has be
[Embedded]
Implementation and characteristics of single chip microcomputer serial port interrupt transmission
Suppose you want to send a group of data    Send , generally use the query send (loop send) method: unsigned char Send ;                                //Send amountunsigned char i;                                                          //Loop amountfor (i=0;i {             SBUF= Send ;                         
[Microcontroller]
51 single chip microcomputer learning record - digital tube dynamic display
I am currently learning about 51 digital tubes and interrupts, and plan to use a six-digit digital tube to implement a clock function. However, when using digital tube output, if no delay statement is added, the display will always be confusing, and sometimes the numbers cannot be distinguished at all. If there is a
[Microcontroller]
51 single chip microcomputer learning record - digital tube dynamic display
What are the application functions of MCS-51 microcontroller P0 port and P1 port?
What are the application functions of MCS-51 microcontroller P0 port? Answer: Port P0 is a three-state bidirectional port, commonly known as the data bus port, because only this port can be directly used for read/write operations on external memory. Port P0 can also be used to output the 8th-bit address of the ext
[Microcontroller]
ARM7 microcontroller (learning) - (KZ), PLL (phase-locked loop) - 01
After searching for a long time, I still couldn't find the frequency setting for Proteus simulation of LPC2106~~ Also, I still don't understand Startup.s~~ So I don't know how it is set up. But let's sort out the PLL first~~ PLL (Phase Locked Loop): Register Description: a. PLLCON register (PLLCON - 0X301FC080
[Microcontroller]
ARM7 microcontroller (learning) - (KZ), PLL (phase-locked loop) - 01
Vehicle anti-collision and automatic braking system based on single chip microcomputer
The purpose of this system is to design a vehicle anti-collision and automatic braking system based on a single-chip microcomputer. The system effectively solves the problem of rear-end collision caused by the driver's untimely reaction during driving, through real-time detection of the distance between the front and
[Microcontroller]
Introduction to the four working modes of 51 single-chip microcomputer timer/counter
1 Working mode 0  When the timer / counter T0 works in mode 0, only 13 bits of the 16-bit counter are used, namely the upper 8 bits of TH0 and the lower 5 bits of TL0, forming a 13-bit timer /counter.  1) Working in timing mode  2) Working in counting mode  2 Working mode 1  Timer T0 Working mode 1 is similar to wo
[Microcontroller]
MCS-51 microcontroller assembly language: What are the arithmetic operation instructions?
Addition instruction ADD   A,  Rn          ; A←(A)+(Rn) ADD   A,  @Ri         ;A←(A)+((Ri)) ADD   A,  direct        ;A←(A)+(direct) ADD   A,  #data       ;A←(A)+#data Add with carry instruction ADDC   A,  Rn        ; A←(A)+(Rn)+(Cy) ADDC A, @Ri ;A←(A)+((Ri )) +(Cy) ADDC   A, direct        ;A←(A)+(direc
[Microcontroller]
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号