//I want to make a 15-channel color light, use P0.0~P0.7 and P2.0~P2.6 ports for output; the preset number is 15 1s (decimal 32767).
//Set two buttons +1 (P3.3) and -1 (P3.4).
//You can subtract to decimal 32047. If you continue to press the -1 button after reaching 32047, you can return to 32767 and subtract again.
//The same is true for addition. After adding to 32767, press the +1 button to return to 32047 and add 1 again.
//Set another button (P2.7). When P2.7 is grounded, subtract the decimal number 1712 from the currently displayed number to display it.
//Looking for a single-chip color light program.
Best answer:
//===========================
#include
sbit K_1 = P3^3; //+
sbit K_2 = P3^4; //-
sbit D_1 = P2^7; //Display mode selection
//----------------------------
void delay_ms(unsigned int n)
{
unsigned char j;
while(n--) for(j = 0; j < 230; j++); //230 is obtained from experimental test!
}
void main()
{
unsigned int m;
m = 32767;
while(1) {
if (D_1 == 1) {
P2 = ~(m / 256);
P0 = ~(m % 256);
}
else {
P2 = ~ ((m - 1712) / 256);
P0 = ~((m - 1712) % 256);
}
if (K_1 == 0) {
delay_ms(10);
if (K_1 == 0) {
m++;
if (m > 32767) m = 32047;
while(K_1 == 0);
} }
if (K_2 == 0) {
delay_ms(10);
if (K_2 == 0) {
m--;
if (m < 32047) m = 32767;
while(K_2 == 0);
} } }
}
//=============================
The PROTEUS simulation screen of this program is as follows:
Previous article:On the Accuracy of Single Chip Microcomputer Measuring Signal Frequency
Next article:Determine the order in which the microcontroller keys are pressed
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
- A look inside the U.S. Army's counter-artillery radar system
- Qorvo Broadband Access CATV and FTTH Product Selection Guide
- Use MOS tube or triode?
- Award-winning live broadcast | Power supply, motor, electronic control, new energy vehicle power design technology is coming
- Why is this TVS designed like this?
- I called Knowles today and it was hilarious.
- Is there any sensor that can detect whether there is someone in front through the tinted glass?
- Should the filter capacitor and bleeder resistor in the circuit be placed before or after the anti-reverse polarity diode?
- Which teacher can explain this circuit?
- [2022 Digi-Key Innovation Design Competition] 1. STM32F7508-DK Unboxing