51 microcontroller: 220v household timer switch control system simulation (based on the inherent hardware of the development board)

This experiment is a small task of microcontroller development, because the system involves independent buttons (on/off keys, up, down, left and right keys, self-defined), LED digital tubes, buzzers, interrupts, timers and other related facilities. The use of 51 microcontroller is relatively comprehensive, so I will use it as an example to review.

The system uses a real-time clock, and can control the turn-on moment and length of time of the 220V power supply through settings.

Summary of the main features of 51 microcontroller

1. Microcontroller system framework

2. Microcontroller memory configuration characteristics

3 Microcontroller I/O port display

4 Introduction to interrupts

Things related to this little experiment

1. Resource definition:

Digital tube 1-8 function allocation:

Digital tube 1-2: minutes

Digital tube 3-4: seconds

Digital tube 5: idle, all black, mode display, 1: start time setting, 2: end time setting, 3: running

Digital tube 6-8: Display 220 or 000 button:

KEY1: Power on, set the key, click, power on, double-click to switch to setting mode, long press, the system shuts down;

KEY2: (click) left shift key, equivalent to the cursor, moves the current editing position, moves left

KEY3: (click) right shift key, equivalent to the cursor, moves the current editing position, moves right

KEY4: (Click) up, the value increases KEY5: (Click) down, the value decreases

2. Function implementation:

Turn on F1, click KEY1, with BEEP, digital tubes 1-4 display all 0s, digital tubes 5: black, digital tubes 6-8 display all 0s.

F2 to shut down, long press KEY1, with BEEP, shut down, the digital tube display is all black.

F3 sets the function switch. After powering on, double-click KEY1, the 5th digital tube lights up and displays 1-2-3 in a loop. . . F4 [10 minutes]. Enter the time setting state. After turning on the computer, double-click KEY1. The 5th digital tube displays 1 and 2. Enter the start time editing mode. By default, the 1-4 digital tubes display all 0s, and the last digit, which is the Four digits, flashing, 0.5 second period;

F5 flashes in a cycle. After entering the editing state, all 0s are displayed, and the last digit flashes. Click KEY2, and the flashing bit moves to the left, and it cycles; click KEY3, and the flashing bit moves to the right, and it cycles.

F6 value editing, after entering the editing state, all 0s are displayed, click KEY2, click KEY3, move and flash, click KEY4 or KEY5, the flashing bit will increase or decrease by 1, and cycle.

After setting F7, double-click KEY1, the 5th digital tube lights up and displays 3. The timing system starts to run and starts a timer with a time resolution of seconds (during simulation, use the “start time value” to update the current time value. This When the start time is up, start powering on), at this time, the 6-8 digits display 220, indicating that the 220 is powered on, and the beep starts at the same time, beeping once every 3 seconds, continuously, indicating the running status. When the end time value is reached, beep stops, and 220 displays bit 000, indicating power outage.

Note: The system’s default real-time clock starts from 0:00:00. The real-time clock runs after power on until shutdown. To simplify processing, the real-time clock does not need to be synchronized with Beijing time. For example: if the set start time is 12:00 and the end time is 12:01, then the real-time clock will start beeping when it reaches 12:00, and the 6-8 digits will display 220, indicating that the 220v power supply is turned on, 12: Beep is turned off at 01, indicating that the 220v power supply is disconnected, and 220 displays bit 000.

3. Code display:

3.1 Initial definition

#include "reg52.h"

typedef unsigned int u16; //Declare and define the data type
typedef unsigned char u8;

u8 code smgduan[18]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
//Display the value from 0~F

u16 flag=0,key_time,digit=0,digit2=0,state=0,duration=0,pause=0;
u16 a=0,b=0,c=0,d=0,a1=0,b1=0,c1=0,d1=0,a2=0,b2=0,c2=0,d2=0,e =0,f=0,h=0,i=0,j=0,k=0,l=200,m=1,n=0;
u16 key1=0,key1_state=0,only_k1_down=0,key1_is_s=0,key1_is_d=0,key1_is_l=0,key2=0,key2_state=0,key3=0,key3_state=0,key4=0,key4_state=0,key5 =0,key5_state=0,key2_is_s=0,key3_is_s=0,key4_is_s=0,key5_is_s=0;

sbit beep=P2^0;

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;

sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit k4=P1^3;
sbit k5=P1^4;

3.2 Digital tube part

void Display(u16 a,u16 b,u16 c,u16 d,u16 e,u16 f,u16 g,u16 h)
{ P0=0;
\t\t  
switch(i) //Bit selection, select the lit digital tube,
{
case(0):
LSA=0;LSB=0;LSC=0;P0=smgduan[a]; break;//Display the 1st bit
case(1):
LSA=1;LSB=0;LSC=0;P0=smgduan[b]; break;//Display the 2nd digit
case(2):
LSA=0;LSB=1;LSC=0;P0=smgduan[c]; break;//Display the 3rd digit
case(3):
LSA=1;LSB=1;LSC=0;P0=smgduan[d]; break;//Display the 4th digit
case(4):
LSA=0;LSB=0;LSC=1;P0=smgduan[e]; break;//Display the 5th digit
case(5):
LSA=1;LSB=0;LSC=1;P0=smgduan[f]; break;//Display the 6th bit
case(6):
LSA=0;LSB=1;LSC=1;P0=smgduan[g]; break;//Display the 7th bit
case(7):
LSA=1;LSB=1;LSC=1;P0=smgduan[h]; break;//Display the 8th bit
}
i + + ;
if(i>=8)
i=0;
\t
}

3.3 Function to control flickering

(17 is a number that does not exist and represents destruction)

void flash1()
{
if(digitP0<=250)
Display(a,b,c,d,e,0,0,0);
else
Display(17,b,c,d,e,0,0,0);
}
void flash2()
{
if(digitP0<=250)
Display(a,b,c,d,e,0,0,0);
else
Display(a,17,c,d,e,0,0,0);
}
void flash3()
{
if(digitP0<=250)
Display(a,b,c,d,e,0,0,0);
else
Display(a,b,17,d,e,0,0,0);
}
void flash4()
{
if(digitP0<=250)
Display(a,b,c,d,e,0,0,0);
else
Display(a,b,c,17,e,0,0,0);
} 

3.4 Key functions, including click, double-click, and long press

void key1_down()
{
         if(k1==0 & amp; & amp;only_k1_down==0)
                                     {only_k1_down=1;flag=0;}
         if(flag>=3 & amp; & amp;k1==0)
                                     {only_k1_down=0;key1=0;}
}
\t\t
void key1_up()
{ if(k1==1)
                    key1=1;
}

void key1judge()
{
      key1_down();key1_up();

switch(key1_state)
{
    case 0:
                         if(key1==1)
                                           {k=1;}
                         if(key1==0 & amp; & amp;k==1)
                                          {key1_state=1;key_time=0;k=0;}
                         break;

    case 1:

                         if(key_time<300 & amp; & amp;key1==1)

                                         {key1_state=2;duration=2*key_time;pause=0;}
                        if(key_time>=300 & amp; & amp;key1==0) //Long press k1
                                          {key1_state=0;key1_is_l=1;}
                        break;

    case 2:

                         if(pause<duration)
                                         {if(key1==0)
                                                    {key1_state=3;key_time=0;}
                                          } //Click k1
                         else
                                         {key1_state=0;key1_is_s=1;}
                        break;

    case 3:

                        if(key_time<300 & amp; & amp;key1==1)
                                        {key1_state=0;key1_is_d=1;}
                        if(key_time>300 & amp; & amp;key1==0) //Double-click k1

                                        {key1_state=0;key1_is_l=1;}
                                              break;
}
}


void key2judge()
{
          switch(key2_state)
                             {case 0:
                                      if(k2==0)
                                               {key2_state=1;key_time=0;}
                                       break;

                             case 1:
                                      if(key_time<300 & amp; & amp;k2==1) //k2 click
                                                            {key2_is_s=1;key2_state=0;}
                                      if(key_time>=300)
                                                            {key2_state=0;}
                                      break;
                             }
}

void key3judge()
{
             switch(key3_state)
                              {case 0:
                                      if(k3==0)
                                                {key3_state=1;key_time=0;}
                                        break;

                               case 1:
                                       if(key_time<300 & amp; & amp;k3==1) //k3 click
                                                               {key3_is_s=1;key3_state=0;}
                                        if(key_time>=300)
                                                                  { key3_state=0;}
                                           break;
                             }
}

void key4judge()
{
              switch(key4_state)
                               { case 0:
                                         if(k4==0)
                                                    {key4_state=1;key_time=0;}
                                          break;

                                 case 1: //k4 click
                                         if(key_time<300 & amp; & amp;k4==1)
                                                    {key4_is_s=1;key4_state=0;}
                                           if(key_time>=300)
                                                        {key4_state=0;}
                                            break;
                                 }
}

void key5judge()
{ switch(key5_state)
                                {case 0:
                                         if(k5==0)
                                                              {key5_state=1;key_time=0;}
                                            break;

                                 case 1:
                                         if(key_time<300 & amp; & amp;k5==1)
                                                             {key5_is_s=1;key5_state=0;}
                                          if(key_time>=300)
                                                             {key5_state=0;}
                                             break;
                               }
} 

3.5 Timer interrupt

(You can view it in conjunction with the timer interrupt block diagram above)

/********************************************** *********************/
void Timer0Init()
{
TMOD|=0X01;//Select timer 0 mode, working mode 1, only use TR0 to open and start.
TH0=0XFC; //Assign an initial value to the timer, timing 1ms
TL0=0X18;
ET0=1; //Enable timer 0 interrupt
EA=1; //Turn on total interrupt
TR0=1;//Open timer
}

void Timer1Init()
{
TMOD|=0X10;//Select timer 1 mode, working mode 1, only use TR1 to open and start.
TH1=0XFC; //Assign an initial value to the timer, timing 1ms
TL1=0X18;
ET1=1; //Enable timer 1 interrupt
EA=1; //Turn on total interrupt
TR1=1;//Open timer
}


void Timer0() interrupt 1
{
 TH0 = 0xfc;
  TL0 = 0x18;
 digit + + ; //1ms
 if(digit==0)
{key_time + + ;pause + + ;flag + + ;l + + ;}
    
}

void Timer1() interrupt 3
{
 TH1 = 0xfc;
 TL1 = 0x18;
digit2 + + ; //1ms
if(digit2 00==0)
{d + + ;n + + ;}
  
}

The main function uses the state machine method to realize the combination of various functions.

(The ability to write state machine code is very important)

/********************************************** *******/
void main()
{
      Timer0Init();
Timer1Init();
      TR1=0;
while(1)
{
      if(state==1 & amp; & amp;l<25)
                         {if(digit%4==0)
                                           { beep=~beep;}
}
      if(state==0 & amp; & amp;l<25)
                        {if(digit%4==0)
                                            {beep=~beep;}
}




       if(state==8)

\t   
                       { if(n%3==0 & amp; & amp;n!=0)
                                                {l=0;}
                          if(digit%4==0 & amp; & amp;l<25)
                                               {beep=~beep;}
                        }



  
       if(key_time>=4000)
                             { key_time=0;}
       if(pause>=4000)
                            {pause=0;}
       if(flag>=4000)
                            {flag=0;}
        if(l>=400)
                            {l=200;}
       if(digit >= 8000)
                           {digit = 0;}

       if(d==10)
                        {c + + ;d=0;}
       if(c==6)
                        {b + + ;c=0;}
       if(b==10)
                         {a + + ;b=0;}
       if(a==6)
                         {a=0;b=0;c=0;d=0;}
       if(digit2>=4000)
                         {digit2=0;
                                    if(n>999)
                                             {n=0;}
                         }


       if(digit@==0)
                          {key1judge();key2judge();key3judge();key4judge();key5judge();}
        if(key1_is_l==1)
                           {state=0;key1_is_l=0;}

switch(state)
{
case 0:
           P0=0;
           TR1=0;
           if(m==0)
          { m=1;l=0;}
          if(key1_is_s==1)
          {state=1;key1_is_s=0;}
          a=0;b=0;c=0;d=0;
           break;

case 1:

          Display(0,0,0,0,17,0,0,0);
          if(m==1)
                             {l=0;m=0;}
          if(key1_is_d==1)
                            {state=2;e=1;j=0;key1_is_d=0;}
         break;

case 2:
         flash4();

        if(key2_is_s==1)
                        {state=3;key2_is_s=0;}
        if(key3_is_s==1)
                        {state=5;key3_is_s=0;}
       if(key4_is_s==1)
                        {d + + ;key4_is_s=0;}
       if(key5_is_s==1)
                       { key5_is_s=0;
                          if(d>0)
                          {d--;}
                       }
       if(key1_is_d==1 & amp; & amp;j==0)
                       {state=6;key1_is_d=0;}
      if(key1_is_d==1 & amp; & amp;j==1)
                       {state=7;key1_is_d=0;}
      break;

case 3:
           flash3();

       if(key2_is_s==1)
                       {state=4;key2_is_s=0;}
       if(key3_is_s==1)
                        {state=2;key3_is_s=0;}
       if(key4_is_s==1)
                        {c + + ;key4_is_s=0;}
       if(key5_is_s==1)
                       {key5_is_s=0;
                         if(c>0)
                          {c--;}
                       }
      if(key1_is_d==1 & amp; & amp;j==0)
                        {state=6;key1_is_d=0;}
      if(key1_is_d==1 & amp; & amp;j==1)
                        {state=7;key1_is_d=0;}
       break;

case 4:
        flash2();
     
if(key2_is_s==1)
                     {state=5;key2_is_s=0;}
     if(key3_is_s==1)
                     {state=3;key3_is_s=0;}
     if(key4_is_s==1)
                     {b + + ;key4_is_s=0;}
      if(key5_is_s==1)
                     {key5_is_s=0;
                       if(b>0)
                       {b--;}
                     }
     if(key1_is_d==1 & amp; & amp;j==0)
                    {state=6;key1_is_d=0;}
     if(key1_is_d==1 & amp; & amp;j==1)
                    {state=7;key1_is_d=0;}
      break;

case 5:
        flash1();
       if(key3_is_s==1)
                      {state=4;key3_is_s=0;}
       if(key2_is_s==1)
                      {state=2;key2_is_s=0;}
       if(key4_is_s==1)
                       {a + + ;key4_is_s=0;}
       if(key5_is_s==1)
                      {key5_is_s=0;
                        if(a>0)
                        {a--;}
                       }
       if(key1_is_d==1 & amp; & amp;j==0)
                      {state=6;key1_is_d=0;}
       if(key1_is_d==1 & amp; & amp;j==1)
                      {state=7;key1_is_d=0;}
       break;

case 6:
     
a1=a;b1=b;c1=c;d1=d;a=0;b=0;c=0;d=0;e=2;
 
       j=1;state=2;
       break;

case 7:
       a2=a;b2=b;c2=c;d2=d;
       a=0;b=0;c=0;d=0;e=3;
       state=8;
       break;

case 8:
       TR1=1;
                    Display(a,b,c,d,3,f,f,0);
                    
if(a==a1 & amp; & amp;b==b1 & amp; & amp;c==c1 & amp; & amp;d==d1)
                                                   {f=2;}
                    if(a==a2 & amp; & amp;b==b2 & amp; & amp;c==c2 & amp; & amp;d==d2)
                                                    {f=0;}
       break;
}
}
}

The summary is that 51 is still very playable. Secondly, it is necessary to exercise the ability of large projects. During the experiment, it was found that it would take a long time to debug projects of this level. This is because the C language programming ability is still weak, but I deepened my understanding of the hardware and fundamentally understood the function of the code.