51 MCU colored light control (including function description, principle and C program)

Publisher:创新思绪Latest update time:2012-08-02 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*1. Functional description:
This program is an ordinary colored light. It controls four modes through the power switch, that is, turning off the power and then turning on the power can change a mode. Its functions mainly have four modes as follows:

The first mode: When the power is on, the four colors cycle once at a speed of 0.5 seconds, and then stop cycling at the white color, that is, white light is displayed

The second mode: After pressing the function key, it will cycle through the four colors at a speed of 1 second.

The third mode: When the button is pressed, it will remember the color of the second mode. For example, the second mode is now green. When you press the button, it will flash first and then stop at green.

The fourth mode: When you press the switch again, it automatically returns to mode 1, and in mode
3, when it is powered off, it can remember mode 3. Some product features: Utilize
the power switch switching function, power-off data preservation, and reduce
the number of EEPROM erases. The program part is transferred from 51 single-chip microcomputer www.51hei.com and has been tested ok*/

/*2. Circuit schematic diagram:

Click to browse the next page

/*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

}

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

/*********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

}

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;

}

Reference address:51 MCU colored light control (including function description, principle and C program)

Previous article:Design of GPS information voice broadcast system based on memory microcontroller
Next article:51 MCU ADC0804 analog-to-digital conversion learning

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号