51 single chip microcomputer LED water lamp

Publisher:码字狂人Latest update time:2020-04-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

type of data

Before writing the running light program, let's understand the data types.

image.png

When using corresponding data, you should pay attention to overflow problems. If there is overflow, the compiler may compile it through, but the execution result will be wrong.


Flowing lights

Before operating the LED, you must first understand what level the LED is lit at and what pins the LED is connected to. Usually you need to look up the schematic diagram. Here my 8 LEDs are connected to port P2 and light up at a low level (because the ability to sink current is stronger than the ability to source current).


1. Functional cycle water light:

#include "reg52.h"   

#include //Function header file

  

#define led P2 //Define the P2 port of the entire LED


/* Delay function */

void delay(unsigned int del) //10ms * del

{

unsigned int i,j;

for(i=0; i for(j=0; j<1827; j++);

}


/* while(i--);//delay 10 * i us; because a single instruction is 1us; */


void main()

{

unsigned char i; //Use char to save RAM

led=0xfe; //Initial state turns on LED0, 1111 1110

while(1)

{

for(i=0;i<7;i++) //Shift the led one position to the left in a loop

{

led=_crol_(led,1);

delay(10); // Delay about 100ms, LED display is faster

}

// At this time, the LED is 0111 1111

for(i=0;i<7;i++) //Move the LED right one bit in a loop

{

led=_cror_(led,1);

delay(10); 

}

}

}


Logical shift (fill 0) running light

#include "reg52.h"   

#include //This is the function header file

  

#define led P2 //Operate the entire P2 port


/* Delay function */

void delay(unsigned int del) //10ms * del

{

unsigned int i,j;

for(i=0; i for(j=0; j<1827; j++)      

    ;

}


/* while(i--);//Delay of about 10us; because a single instruction takes 1us; */


void main()

{

unsigned  char i;

led=0xfe; //Initial state turns on LED0

while(1)

{

for(i=0;i<8;i++)// 1111 1110

{

P2=~(0x01< delay(10); // Delay about 100ms, quite fast

}

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

{

P2=~(0x80>>i); //Shift 1 right by i bits, then invert the result and assign it to port P2

delay(10); 

}

}

}


The two methods have the same effect. So far, the LED operation is complete.

Reference address:51 single chip microcomputer LED water lamp

Previous article:"Beginner's C51 Self-study Notes" - Flowing Light Implementation (Bit Operation)
Next article:Beginner's guide to 51 MCU - simple water lamp program

Recommended ReadingLatest update time:2024-11-16 07:40

"Hit the point" LED lightning protection design strategy
LED lighting has gradually replaced traditional lighting to become the dominant lighting market. Due to the second revolution in the lighting field, LED lighting technologies such as improvements in lighting efficiency (higher lumens per watt), secondary optical components (better lenses/reflectors), and stronger he
[Embedded]
5.1. Press button SW1 to control the on and off of LED1
As can be seen from the figure, the P1_2 pin can sense the state of SW1. When SW1 is not pressed, P1_2=1 high level, and when SW1 is pressed, P1_2=0 grounded. Note: P1_2 pin only senses SW1 and does not affect SW1. #include "ioCC2530.h" //reference CC2530 header file /*********
[Microcontroller]
5.1. Press button SW1 to control the on and off of LED1
Linux driver - LED blinking
Connect the development board and PC, then turn on the power switch of the S3C development board. After successful startup, execute the build.sh script file to compile and install the LED driver. The build script file will automatically upload the driver's .ko file to the S3C development board and install it. T
[Microcontroller]
【tiny6410】LED bare board program
start.s file The startup code is very simple, mainly doing some hardware-related settings: telling the CPU the base address of the peripherals, turning off the watchdog, and setting up the stack (to prepare for calling the C language) .globl _start _start: /* Hardware related settings*/
[Microcontroller]
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号