////////////////////////////////////////////////////////
In the 51 single-chip microcomputer, there is a special variable sbit (bit variable)
It is very easy to use it to do a variable inversion, nuber=!nuber does it.
Why do we need this feature?
Let's look at a program
Assume that LED is a light, LED=1 is on, LED=0 is off.
So I want it to flash for 200 milliseconds.
while(1)
{
LED=!LED;
delay_ms(200);
}
This is how the 51 single-chip microcomputer is written.
This is not advisable in C language, because we cannot map a certain bit. (In fact, I don't know how to map a certain bit of the register to operate)
So usually I use the functions LED_ON(), LED_OFF()
In this case, the procedure should be like this
while(1)
{
LED_ON()
delay_ms(100);
LED_OFF();
delay_ms(100);
}
Then I have a lot of other things in my while(1), and I have to run it every 10ms, so 51 should achieve 200ms flashing like this
while(1)
{
n++;
if(n==20){
n=0;
LED=!LED;
}
delay_ms(10);
}
It's also very simple, but what about the stm32 without bit variables? Shouldn't the program be rethought?
I'll provide a little method below
(1)
flag=1; //The flag variable should be
while(1)
{
n++;
if(n==20){
n=0;
if(flag==1)
LED_ON();
else LED_OFF();
flag*=-1 //Every time you multiply by -1, it actually changes from 1 to -1 and then to 1
}
delay_ms(10);
}
(2)
flag=1; //The flag variable should be
while(1)
{
n++;
if(n==20){
n=0;
if((flag&0x1)==1)
LED_ON();
else LED_OFF();
flag=!flag; //This is the same
}
delay_ms(10);
}
Of course, there are many other ways to implement it, which are not listed here.
Previous article:51 MCU Study Notes (Part 2)
Next article:51 MCU serial port baud rate calculation
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- RFID unattended intelligent weighing software system solution
- 【TI recommended course】#Live replay: TI's new generation of integrated PA Zigbee 3.0 and multi-protocol solutions#
- 200 Examples of Monolithic Switching Power Supply Design (2nd Edition)
- Physical Design Analysis of PCB Board in Switching Power Supply Design
- PCB Design
- Why can't I see the bluetooth function when running micropython on EPS32?
- Does anyone know how this kind of colored PCB is made? ?
- EEWORLD University Hall----Live Replay: Comprehensive explanation of TI MSP Academy tutorial
- TI CC2640R2F documentation for beginners
- New version of WEBENCH user guide