This program is a common colored light. It
controls four modes through the power switch, that is, you can change
a mode by turning off the power and then turning it on. Its functions mainly have four modes as follows: The first mode
: When the power is turned on, the four colors cycle once at a speed of 0.5 seconds, and then stop at the white color
. The second mode: After pressing the function key, it
cycles the four colors continuously at a speed of 1 second. The third mode: When the key is pressed, it will
remember the color of the second mode. For example, the second mode is now green.
When you press the key, it will flash first and then stop at the green color.
The fourth mode: When you press the switch again, it automatically returns to mode one, and in mode
three, when it is powered off, it can remember mode three. Some product features: Utilize
the power switch switching function, power-off data is saved, which reduces
the number of EEPROM erases. The program part is transferred from 51 single-chip microcomputer www.51hei.com and has been tested ok*/
/*Three, complete reference program: click to download */
#include
#include
#define uint unsigned int
#define uchar unsigned char
sfr DATA=0xE2; //Data register
sfr ADDRH=0xE3; //address register high eight bits
sfr ADDRL=0xE4; // address register lower eight bits
sfr CMD=0xE5; //Command mode register
sfr TRIG=0xE6; //Command trigger register
sfr CONTR=0xE7; //Control register
sbit RJ=P2^5; //Red light IO port
sbit GJ=P2^6; //Green light IO port
sbit BJ=P2^7; //Blue light IO port
uchar ding3,yin,x,m; // respectively timer variable, flag variable, and color flag variable
bit a=1,b,c; // respectively mode 1.2.3 flag bits
/************************************/
/***********Delay subroutine***********/
/************************************/
void delay(uint z)
{
uint x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
/************************************/
/*********Read EEPROM subroutine************/
/************************************/
uchar read(uint add)
{
CONTR=0x81; //Control register is turned on, 12MHZ crystal, so set the time to 01
CMD=0x01; //Set EEPROM to read mode (1)
ADDRH=(add>>=8); // give the high digit
ADDRL=add&(0x00ff); // give the lower digit
TRIG=0x46; //Activate
TRIG=0xb9; //Activate
_nop_();//delay
CONTR=0x00; //Protect
TRIG=0X00; //Protect
return DATA; //Return the read value
}
[page]
/************************************/
/*********Write EEPROM subroutine*********/
/************************************/
void program(uint add,uchar vale) //add is the address to write the number vale is the number to be stored
{
CONTR=0x81; //Control register is turned on, 12MHZ crystal, so set the time to 01
CMD=0X02; //Set EEPROM to write mode (2)
ADDRH=(add>>=8);
ADDRL = add&(0x00ff);
DATA=vale; //Put the number to be stored into the register
TRIG=0x46;
TRIG=0xb9;
_nop_();
CONTR=0x00;
TRIG=0X00;
}
/************************************/
/*****Erase EEPROM subroutine*******/
/************************************/
void sector(uint add)
{
CONTR=0x81; //Control register is turned on, 12MHZ crystal, so set the time to 01
CMD=0x03; //Set EEPROM to erase mode (3)
ADDRH=(add>>=8); // give the high digit
ADDRL=add&(0x00ff); // give the lower digit
TRIG=0x46;
TRIG=0xb9;
_nop_();
CONTR=0x00;
TRIG=0X00;
}
/************************************/
/*******Three modes display subroutine******/
/************************************/
void xian()
{
if(a!=0) //The first mode flashes once in a cycle at a speed of 0.5 seconds, and then stops at the white light
{
if(ding3>=10) //Has the timer reached 0.5 seconds?
{
ding3=0;
x++; //Add 1 to the flag to enter the next color
switch(x)
{
case 1:RJ=1;GJ=0;BJ=0;//red light on
break;
case 2:RJ=0;GJ=1;BJ=0;//green light
break;
case 3:RJ=0;GJ=0;BJ=1;//bright blue light
break;
case 4: RJ=1; GJ=1; BJ=1; a=0; // turn on white light, // clear flag a to 0 for another cycle
break;
}
}
}
if(b!=0) //The second mode flashes in a loop at a speed of 1 second.
{
if(ding3>=20)
{
ding3=0;
x++;
switch(x)
{
case 1: RJ=1; GJ=0; BJ=0; m=x; //Save the x variable to m so that it can accurately lock a state when entering mode 3
break;
case 2:RJ=0;GJ=1;BJ=0;m=x;
break;
case 3:RJ=0;GJ=0;BJ=1;m=x;
break;
case 4:RJ=1;GJ=1;BJ=1;m=x;x=0;
break;
}
}
}
if(c!=0) //The third mode remembers the color of the key pressed in mode 2 and then locks it.
{
RJ=0;GJ=0;BJ=0;
delay(550);
switch(m) //Judge the state of the last mode 3, and then determine which color to display
{
case 1:RJ=1;GJ=0;BJ=0;
break;
case 2: RJ=0; GJ=1; BJ=0;
break;
case 3:RJ=0;GJ=0;BJ=1;
break;
case 4:RJ=1;GJ=1;BJ=1;
break;
}
c=0; // Clear the flag to 0 so that it can be cycled again
}
}
/************************************/
/*************Main program************/
/************************************/
void main()
{
RJ=0; //Turn off the R LED
GJ=0; //Turn off the G LED
BJ=0; //Turn off B LED
TMOD=0X01; //Register working mode
EA=1; //Open the general interrupt
TR0=1; //Start the timer
ET0=1; //Open timer interrupt
IT0=1;
EX0=1;
TH0=(65536-50000)/256; //Assign value to high bit
TL0=(65536-50000)%256; //Assign value to low bit
if(read(0x2200)==0x03)//Check whether it is in the third mode when the power is turned on. If it is, read the data when the power is off (i.e. color)
{
m=read(0x2000); //Read the color when power is off
a=0; //turn off mode 1
b=0; //turn off mode 2
c=1; //Turn on mode 3
}
else
{
a=1; //Turn on mode 1
b=0; //turn off mode 2
c=0; //turn off mode 3
}
while(1)
xian(); //Call the display subroutine
}[page]
void zhong() interrupt 1 //T1 interrupt service routine
{
TH0=(65536-50000)/256; //Assign value to high bit
TL0=(65536-50000)%256; //Assign value to low bit
ding3++; //Increase the key counter by 1
}
void zhongk() interrupt 0
{
/************************************/
/****Key processing and data saving subroutine***/
/************************************/
EX0=0;
switch(yin) //Execute one-key multi-function program
{
case 0: //If 0 is equal to yin, then execute everything before break
delay(100);
sector(0x2200); //erase
program(0x2200,0x02); //Write the number 0x02 at address 0x2200
delay(100);
yin++; //Increase the variable by 1. This sentence is very important. It is used to execute the next switch statement when the next button is pressed.
a=0; //Change a to 0
c=0;
b=1; //Change b to 1, that is, open the second program in the key program
x = 0; // Clear the color count variable so that it starts the loop from red
break; // Exit switch statement
case 1:
delay(100);
sector(0x2200); //erase
program(0x2200,0x03); //Write the number 0x03 at address 0x2200
sector(0x2000); ////erase
program(0x2000,m); //Write variable m at address 0x2000 to form power-off protection in mode 3
delay(100);
yin++; //Increase the variable by 1. This sentence is very important. It is used to execute the next switch statement when the next button is pressed.
b=0;
c=1; //Open the third mode flag, i.e. locked state
a=0;
x = 0; // Clear the color count variable so that it starts the loop from red
break; // Exit switch statement
case 2:
delay(100);
sector(0x2200); //erase
program(0x2200,0x01); //Write the number 0x01 at address 0x2200
delay(100);
b=0; //Change b to 0, that is, close the last function
c=0; //Close the previous function
a=1; //Open the first function, i.e. 0.5 seconds of loop flashing
yin=0;
x = 0; // Clear the color count variable so that it starts the loop from red
break; // Exit switch statement
}
delay(255);
EX0=1;
}
Previous article:C51 program to decode wav music using microcontroller + DAC0832
Next article:51 MCU ADC0804 analog-to-digital conversion learning
- 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
- 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)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Fancy PCB Exhibition (2)
- [RVB2601 Creative Application Development] player_demo transplantation
- Mobil Comms(Beyond 2G) [ADI]--PPT Document
- MOS tube isolation drive circuit, if we drive high voltage MOS tube, we need to use transformer drive and integrated...
- Watch NXP LPC55S69 live broadcast and get the book "Embedded Security Processor Application and Practice" for free
- Smith chart impedance matching calculation software
- EEWORLD University ---- SimpleLink Academy: Develop your Bluetooth? Low Energy project
- dsPIC33EP32MC204PWM current limiting function problem
- Do you have any recommendations for low-power encryption products? I am using this on a smart device and have strict requirements on power consumption.
- Detailed explanation of diagrams: A quick guide for electronic engineers to read diagrams