51 single chip microcomputer to realize the water lamp

Publisher:玉树琼花Latest update time:2020-06-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 

Before implementing the running light, we need to mention that before writing the C51 code, you need to refer to the circuit diagram. Take the running light as an example. Let's look at Figure 2. The positive poles of the eight LEDs are connected to the +5V voltage through a bus. Looking at Figure 1 again, we can see that the negative poles of the eight LEDs are connected to the 8 I/O ports of LED and P2. According to the conduction condition of the diode, the LED can only be lit when the IO port of the microcontroller outputs a low level.


Paste the code below:

#include

 

#define uchar  unsigned char

#define uint   unsigned int 

 

sbit LED1 = P2^0;

sbit LED2 = P2^1;

sbit LED3 = P2^2;

sbit LED4 = P2^3;

sbit LED5 = P2^4;

sbit LED6 = P2^5;

sbit LED7 = P2^6;

sbit LED8 = P2^7;

 

/* ********************************************* */

// Function name: Delay()

// Function: millisecond delay

/* ********************************************* */

 

void delay(uint z)

{

uint x,y;

for(x = 0; x < z; x++)

for(y = 0; y < 113; y++);

}

// Function name: main()

// Function: Make an LED light flash

 

void main(void)

{

while(1)

{

LED1 = 0; //light on

delay(1000); //Continue on for 1s

LED1 = 1; //light off

delay(1000); //Continue to turn off for 1s

LED2 = 0;

delay(1000);

LED2 = 1;

delay(1000);

LED3 = 0;

delay(1000);

LED3 = 1;

delay(1000);

LED4 = 0; //light on

delay(1000); //Continue on for 1s

LED4 = 1; //light off

delay(1000); //Continue to turn off for 1s

LED5 = 0;

delay(1000);

LED5 = 1;

delay(1000);

LED6 = 0;

delay(1000);

LED6 = 1;

delay(1000);

LED7 = 0;

delay(1000);

LED7 = 1;

delay(1000);

}

 

}


After reading the first two circuit diagrams, you will probably understand what the code means. First, use the second method of using sbit: sbit bit variable name = SFR name ^ variable bit address value, for example: sbit LED1 = P2^0, which means that LED1 is defined as the first bit of P2 port for bit operation.


Then we wrote a millisecond delay function. Of course, the so-called delay here is an inaccurate delay. It is the "1 second" we tested after some debugging. If we want to truly and accurately achieve a one-second delay, we must also use a timer. Since the requirement here is not so accurate, we can use the millisecond delay function to achieve it.


Finally, our main function is the function function. As far as the MCU is concerned, we all know that the biggest function of the MCU is control, and the control of the MCU is also reflected in the output of high and low levels. The reason why the MCU can achieve so many functions is also due to the surrounding expansion modules. In C51, 1 is a high level and 0 is a low level, which means off and on for LED lights. In addition, we also call the delay function to realize the running light.

(Link: https://pan.baidu.com/s/1CtSpgtdKuNh5P3CmCqHuwA Extraction code: nb2i)


Finally, I would like to remind everyone that the C51 code should be analyzed based on the specific circuit diagram of the specific microcontroller model. Do not copy it !!!

Reference address:51 single chip microcomputer to realize the water lamp

Previous article:Two peripheral circuits of the microcontroller: reset circuit and clock circuit
Next article:51 MCU Getting Started Tutorial (3) - Digital Tube Display

Recommended ReadingLatest update time:2024-11-15 14:49

C51 Introduction 2, Timer and Interrupt Application
Experiment 1: Timer interrupt flashing light program: #define uchar unsigned char #define uint unsigned int #define ulong unsigned long #include reg52.h //Port setting sbit P10=P1^0; sbit K1=P3^2; char code dx516 _at_ 0x003b; //Main function void main(void) {       TMOD=0x01; //Set timer 0 to work in m
[Microcontroller]
C51 Definition of Special Function Registers in MCS51 Microcontroller
Before we start to talk about the definition of the special register (SPR) in the C51 microcontroller, let's first briefly introduce the two keywords "sbit" and "sfr" that we often see when developing 51 microcontrollers: SFR is used to assign a special function register of a microcontroller to a variable, so that the
[Microcontroller]
C51 Definition of Special Function Registers in MCS51 Microcontroller
C51 MCU Study Notes LED
Introduction LED is a light emitting diode, there is nothing much to say. The only point is that low level is effective! Schematic   Code section #include REGX52.H     void delay() { unsigned int i; for(i=0;i 30000;i++); } main() {   unsigned int i; unsigned char J;   while(1) { /*P0=0xFE; delay(
[Microcontroller]
C51 MCU Study Notes LED
C51 MCU Study Notes - IO Expansion (Serial to Parallel) Experiment - 74HC595
Purpose: To control the LED matrix to display in a row loop through the 74HC595 module. Compiler software: keil5 process: (1) First define the 74HC595 control pins and the dot array control port //Define 74HC595 control pins sbit SRCLK=P3^6; //Shift register clock input sbit RCLK=P3^5; //Storage register clock
[Microcontroller]
C51 MCU Study Notes - IO Expansion (Serial to Parallel) Experiment - 74HC595
C51 pulse width detection program
/**********************************************   315 wireless module PT2262 1.5M 270KHz can measure the pulse width   start time 5000us   high level time 490us low level   time 160us   HH=1   LL=0   LH=F ***************************************************/ #include typedef unsigned int uint; long plu; void
[Microcontroller]
"Beginner's C51 Self-study Notes" Serial Port
Parallel communication: Usually, each bit of a data byte is transmitted simultaneously using multiple data lines. The control is simple and the transmission speed is fast; due to the large number of transmission lines, the cost is high for long-distance transmission and it is difficult for all receivers to receive at
[Microcontroller]
Keil C51 keyboard decoding program
The keyboard decoding program is the most commonly used program in the development project. Its quality directly affects the entire program! Now I will introduce a very widely used Keil C51 keyboard decoding program. By continuously calling KeyBord() in the main program, you can scan the keyboard continuously! Cal
[Microcontroller]
intrinsics.h library function in c51
#ifndef __INTRINS_H__ #define __INTRINS_H__ extern void _nop_ (void); extern bit _testbit_ (bit); extern unsigned char _cror_ (unsigned char, unsigned char); extern unsigned int _iror_ (unsigned int, unsigned char); extern unsigned long _lror_ (unsigned long, unsigned char); extern unsigned char _crol_ (unsign
[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号