Independently build a new project and write and compile code to realize the change of the flashing state of the button control light and achieve the following task requirements:
[1] The program starts to run: the D4 light flashes, and the D3, D5, D6 lights go out.
[2] After pressing the SW1 button on the module to release, the D5 and D6 lights will blink alternately.
[3] Press the SW1 button again, D5, D6 lights off.
[4] Repeat the above two steps.
This question needs to define a flag for the state of the light. There are three states through the flag bit of the button.
State 1: D4 light flashes, D3, D5, D6 lights off.
State 2: D5 and D6 lights flash in turn.
State 3: D5 and D6 lights are off.
1 #include " ioCC2530.h " 2 3 #define D3 P1_0 4 #define D4 P1_1 5 #define D5 P1_3 6 #define D6 P1_4 7 #define SW1 P1_2 8 9 unsigned char stat = 0 ; // light status flag 10 11 / * ======================= Simple delay function ===================== === * / 12 void Delay (unsigned int t) 13 { 14 while (t--); 15 } 16 / * ======================= Port initialization function ================= ======= * / 17 void Init_Port () 18 { 19 P1SEL & = ~ 0x1b ; // Set P1_0, P1_1, P1_3, P1_4 to general I / O port 20 P1DIR | = 0x1b ; // Set P1_0 , P1_1, P1_3, P1_4 are set to the output direction 21 P1 & = ~ 0x1b ; // Turn off 4 LED lights 22 23 P1SEL & = ~ 0x04 ; // Set P1_2 to general I / O port 24 P1DIR & = ~ 0x04 ; // Set P1_2 to input direction 25 P1INP & = ~ 0x04 ; // Set P1_2 to pull up / down 26 P2INP & = ~ 0x40 ; // Set P1_2 to pull up 27 } 28 29 / * ==== ================= D4 light flashing function ====================== * / 30 void D4_Flicker () 31 { 32 D4 = 1 ; 33 Delay ( 60000 ); 34 D4 = 0 ; 35 Delay ( 60000 ); 36 } 37 38 / * ===================== D5D6 light flashing function ====================== * / 39 void D5D6_Flicker () 40 { 41 D3 = 0 ; 42 D4 = 0 ; 43 D5 = 1 ; 44 Delay ( 60000 ); 45 D5 = 0 ; 46 Delay ( 60000 ); 47 D6 = 1 ; 48 Delay ( 60000 ) ; 49 D6 = 0 ; 50 Delay ( 60000 ); 51 } 52 53 / * ======================= Key scan function ================== ======= * / 54 void Scan_Keys () 55 { 56 if (SW1 == 0 ) // SW1 button signal 57 found 57 { 58 Delay ( 100 ); // Delay for a moment, debounce 59 if (SW1 == 0 ) // Confirm as SW1 key signal 60 { 61 if (stat == 0 ) 62 { 63 stat =1 ; 64 } 65 else if (stat == 1 ) // Repeat 66 { 67 stat = 2 ; 68 } 69 else if (stat == 2 ) 70 { 71 stat = 1 ; 72 } 73 74 } 75 } 76 } 77 78 / * ========================== Main function ================== ========== * / 79 void main () 80 { 81 Init_Port (); // Port initialization 82 while ( 1 ) 83 { 84 Scan_Keys (); // Key scan 85 switch (stat) 86 { 87 case 0 : // Power on state, D4 flashes 88 D4_Flicker (); 89 break ; 90 case 1 : // Run status 1: D5 and D6 flash 91 D5D6_Flicker (); 92 break; 93 case 2 : // Run state 2: D5 and D6 are off 94 D5 = 0 ; 95 D6 = 0 ; 96 break ; 97 } 98 } 99 }