type of data
Before writing the running light program, let's understand the data types.
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 #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 } /* 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 #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 ; } /* 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. for(j=0; j<1827; j++); for(j=0; j<1827; j++)
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Question about the value of pull-up resistor
- I would like to ask what specific things I need to learn to write drivers for hardware in FPGA
- Analog Discovery 2 Review (3) Frequency Response Test Tool
- Initialization and configuration of TCP/IP stack Development of network applications
- Transformer secondary side
- Help: Canaan K510 kit text display abnormal guess and solution progress
- EEWORLD University Hall----Hard Rock Technology STM32 Motor Control
- TI CC1352P dual-band multi-protocol wireless solution
- 【Free Trial】DFRobot micro:bit Programming Car-McQueen
- It’s strange, why are the phones sold in Huawei Experience Stores cheaper than those sold online?