[51 MCU Study Notes FIVE]----Independent Buttons

Publisher:素心轻语Latest update time:2017-01-05 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. 8 buttons control 8 LED lights


 1 /************************************************ *******************************

 2 Experiment name: 8 independent buttons control 8 LED lights (when a button is pressed, the corresponding LED lights up)

 3 Experimental time: 2015/1/20

 4 ************************************************* ******************************/

 5 #include

 6 

 7 #define LED_KEY P0 //LED light uses P0 port

 8 #define IO_KEY P1 //Independent key uses P1 port

 9 

10 void Delay(); //Delay function

11 unsigned char Key_Scan(); //Get key value

12 

13 void main()

14 {

15 unsigned char Key_Value, LED_Value;

16     

17 while(1)

18 {

19 Key_Value = Key_Scan();

20 switch(Key_Value)

twenty one {

22 case(0xFE): //K1 is pressed

23 LED_Value = 0x01;

24 break;

25 case(0xFD): //K2 is pressed

26 LED_Value = 0x02;

27 break;

28 case(0xFB): //K3 is pressed

29 LED_Value = 0x04;

30 break;

31 case(0xF7): //K4 is pressed

32 LED_Value = 0x08;

33 break;

34 case(0xEF): //K5 is pressed

35 LED_Value = 0x10;

36 break;

37 case(0xDF): //K6 is pressed

38 LED_Value = 0x20;

39 break;

40 case(0xBF): //K7 is pressed

41 LED_Value = 0x40;

42 break;

43 case(0x7F): //K8 is pressed

44 LED_Value = 0x80;

45 break;

46 default:

47 break;

48 }

49         

50 LED_KEY = LED_Value; //Light up the corresponding LED        

51 }

52 }

53 

54 unsigned char Key_Scan()

55 {

56 unsigned char Key_Value,i;

57 

58 if(IO_KEY != 0)

59 {

60 Delay(); // Eliminate jitter

61 

62 if(IO_KEY != 0xFF)

63 {

64 Key_Value = IO_KEY;

65 i = 0;

66 if((i<50)&&(IO_KEY != 0xFF)) //Judge whether the key is released

67 {

68 Delay();

69 i++;    

70 }

71 }

72 }

73 return Key_Value;

74 }

75 

76 void Delay() //Delay 10ms

77 {

78 unsigned char a,b,c;

79 

80 for(c=1;c>0;c--)

81 {

82 for(b=38;b>0;b--)

83 {

84 for(a=130;a>0;a--)

85 {};

86 }

87 }

88 }



The principle of independent buttons is this: when the button is not pressed, the corresponding port is in a high level state, and when the button is pressed, the corresponding port is in a low level state. Therefore, the corresponding function can be realized based on this phenomenon.


Another point that should be noted is that when the key is closed and opened, the contact will jitter.


           

In actual situations, what we need is the stable closed state. So we can use the delay method to solve this problem. The specific process is to first check if there is a key pressed. If a key is pressed, delay for a while and then check if there is a key pressed again.

If there is still a key pressed at this time, then it is really a key pressed.

The jitter time is about 10ms, so just use a sub-function with a delay of 10ms.

The 66th line of the code above that determines whether the key is released is also necessary. Although it makes little difference in this program, if some programs require that something happen only after the key is released, then this part becomes necessary.



 

2. LED display key value


 1 1 /*********************************************** ****

 2 2 Experiment name: LED displays key value (when a key is pressed, the corresponding LED will not light up)

 3 3 Experimental time: 2015/1/22

 4 4 ************************************************ ****/

 5 5 #include

 6 6 

 7 7 #define LED_KEY P0 //LED light uses P0 port

 8 8 #define IO_KEY P1 //Independent key uses P1 port

 9 9 

10 10 void Delay(); //Delay function

11 11 unsigned char Key_Scan(); //Get key value

12 12 

13 13 void main()

14 14 {

15 15 unsigned char Key_Value, LED_Value;

16 16     

17 17 while(1)

18 18 {

19 19 Key_Value = Key_Scan();

20 20 if (Key_Value != 0) //A key is pressed

21 21 {

22 22 LED_Value = Key_Value;

23 23 }

24 24         

25 25 LED_KEY = LED_Value; //Light up the corresponding LED        

26 26 }

27 27 }

28 28 

29 29 unsigned char Key_Scan()

30 30 {

31 31 unsigned char Key_Value,i;

32 32 

33 33 if(IO_KEY != 0)

34 34 {

35 35 Delay(); //eliminate jitter

36 36 

37 37 if(IO_KEY != 0xFF)

38 38 {

39 39 Key_Value = IO_KEY;

40 40 i = 0;

41 41 if((i<50)&&(IO_KEY != 0xFF)) //Judge whether the key is released

42 42 {

43 43 Delay();

44 44 i++;    

45 45 }

46 46 }

47 47 }

48 48 return Key_Value;

49 49 }

50 50 

51 51 void Delay() //Delay 10ms

52 52 {

53 53 unsigned char a,b,c;

54 54 

55 55 for(c=1;c>0;c--)

56 56 {

57 57 for(b=38;b>0;b--)

58 58 {

59 59 for(a=130;a>0;a--)

60 60 {};

61 61 }

62 62 }

63 63 }

 


3. Press buttons to select pattern display


  1 /***********************************************

  2 Experiment name: Press key to select pattern display

  3 Experimental time: 2015/1/22

  4************************************************/

  5 #include

  6 #include

  7 

  8 #define IO_KEY P1

  9 #define LED_KEY P0

 10 

 11 void Delay(unsigned char c); //delay function

 12 unsigned char Key_Scan(); //Key scan function

 13 void Key1(); //Press key 1

 14 void Key2(); //Press key 2

 15 void Key3(); //Press key 3

 16 void Key4(); //Press key 4

 17 void Key5(); //Press key 5

 18 void Key6(); //Press key 6

 19 void Key7(); //Press key 7

 20 void Key8(); //Press key 8

 twenty one 

 22 void main()

 twenty three {

 24 unsigned char Key_Value;

 25 

 26 while(1)

 27 {

 28 Key_Value = Key_Scan();

 29 switch(Key_Value)

 30 {

 31 case (0xFE):

 32 Key1();

 33 break;

 34 case (0xFD):

 35 Key2();

 36 break;

 37 case(0xFB):                                                                  

 38 Key3();

 39 break;

 40 case(0xF7):

 41 Key4();

 42 break;

 43 case(0xEF):

 44 Key5();

 45 break;

 46 case (0xDF):

 47 Key6();

 48 break;

 49 case (0xBF):

 50 Key7();

 51 break;

 52 case(0x7F):

 53 Key8();

 54 break; 

 55 default:

 56 break; 

 57 }

 58 }

 59 }

 60 

 61                                 

 62 void Delay(unsigned char c) //When c=1, delay 10ms

 63 {

 64 unsigned char a,b;

 65 

 66 for(;c>0;c--)

 67 {

 68 for(b=38;b>0;b--)

 69 {

 70 for(a=130;a>0;a--)

 71 {};

 72 }

 73 }

 74 }

 75 

 76 unsigned char Key_Scan()

 77 {

 78 unsigned char Key_Value,i;

 79 if(IO_KEY != 0)

 80 {

 81 Delay(1); //eliminate jitter

 82 

 83 if(IO_KEY != 0)

 84 {

 85 Key_Value = IO_KEY;

 86 i = 0;

 87 if((i<50)&&(IO_KEY != 0xFF)) //Judge whether the key is released

 88 {

 89 Delay(1);

 90 i++;

 91 }                

 92 }

 93 }

 94 return Key_Value;    

 95 }

 96 

 97 //LED moves left and flashes twice

 98 void Key1()

 99 {

100 unsigned char i;

101 LED_KEY = 0x01;

102 for(i=0;i<16;i++) //Loop twice

103 {

104 LED_KEY = _crol_(LED_KEY,1); //Circularly shift left one position

105 Delay(15); //Delay                    

106 }            

107 }

108 //LED moves right and flashes twice

109 void Key2()

110 {

111 unsigned char i;

112 LED_KEY = 0x80;

113 for(i=0;i<16;i++)

114 {

115 LED_KEY = _cror_(LED_KEY,1); //Circularly shift right one position

116 Delay(15); //Delay        

117 }

118 }

119 //LED flashes 16 times alternately, with intervals

120 void Key3()

121 {

122 unsigned char i;

123 for(i=0;i<16;i++)

124 {

125 LED_KEY = 0xAA;

126 Delay(15);

127 LED_KEY = 0x55;

128 Delay(15);

129 }

130 }

131 //LED flashes 16 times in reverse and alternately, with intervals of light on

132 void Key4()

133 {

134 unsigned char i;

135 for(i=0;i<16;i++)

136 {

137 LED_KEY = 0x55;

138 Delay(15);

139 LED_KEY = 0xAA;

140 Delay(15);

141 }

142 }

143 //LED flashes 16 times alternately, adjacent lights

144 void Key5()

145 {

146 unsigned char i;

147 for(i=0;i<16;i++)

148 {

149 LED_KEY = 0xCC;

150 Delay(15);

151 LED_KEY = 0x33;

152 Delay(15);

153 }

154 }

155 //LED flashes 16 times in reverse and alternately, and the adjacent

156 void Key6()

157 {

158 unsigned char i;

159 for(i=0;i<16;i++)

160 {

161 LED_KEY = 0x33;

162 Delay(15);

163 LED_KEY = 0xCC;

164 Delay(15);

165 }

166 }

167 //Key1()--Key6(),Key8() all come again

168 void Key7()

169 {

170 Key1();

171 Key2();

172 Key3();

173 Key4();

174 Key5();

175 Key6();

176 Key8();

177 }

178 // Total destruction

179 void Key8()

180 {

181 LED_KEY = 0x00;

182 }


 


4. Press the button to move the LED light left and right


  1 /************************************************ *******************************

  2 * Experiment name: Press the button to control the LED to move left and right

  3 * Experimental time ; 2015/1/22

  4 ************************************************* ******************************/

  5 #include

  6 #include

  7 

  8 //--Define the IO port to be used--//

  9 sbit K1 = P1^0; //Corresponding to button K1

 10 sbit K2 = P1^1; //Corresponding to button K2

 11 #define LED_KEY P0 //LED uses P0 port

 12 

 13 //--Define global functions--//

 14 void Delay10ms(unsigned int c); //Delay 10ms

 15 unsigned char Key_Scan();

 16 

 17 /************************************************ *******************************

 18 * Function name: main

 19 * Function: Main function

 20****************************************************** ******************************/

 21 void main(void)

 twenty two {

 23 unsigned char ledValue, keyNum;

 twenty four 

 25 ledValue = 0x01;

 26 

 27 while (1)

 28 {    

 29 keyNum = Key_Scan(); //Scan keyboard

 30 

 31 if (keyNum == 1) //If the key value returns 1

 32 {

 33 ledValue = _crol_(ledValue, 1); //Left loop

 34 }

 35 else if (keyNum == 2)

 36 {

 37 ledValue = _cror_(ledValue, 1); //Right loop

 38 }

 39 

 40 LED_KEY = ledValue; //Turn on the LED

 41 }                

 42 }

 43 

 44 /************************************************ *******************************

 45 * Function name: Key_Scan()

 46 * Function: Scan keyboard

 47 ************************************************* ******************************/

 48 

 49 unsigned char Key_Scan()

 50 {

 51 unsigned char keyValue = 0 , i; //Save key value

 52 

 53 //--Detect button 1--//

 54 if (K1==0) //Check whether button K1 is pressed

 55 {

 56 Delay10ms(1); //eliminate jitter

 57 

 58 if (K1==0) //Check again whether the button is pressed

 59 {

 60 keyValue = 1;

 61 i = 0;

 62 while ((i<50) && (K1==0)) //Detect whether the key is released

 63 {

 64 Delay10ms(1);

 65 i++;

 66 }

 67 }

 68 }

 69 

 70 //--Detect button 2--//

 71 if (K2==0) //Check whether button K1 is pressed

 72 {

 73 Delay10ms(1); //eliminate jitter

 74 

 75 if (K2==0) //Check again whether the button is pressed

 76 {

 77 keyValue = 2;

 78 i = 0;

 79 while ((i<50) && (K2==0)) //Detect whether the key is released

 80 {

 81 Delay10ms(1);

 82 i++;

 83 }

 84 }

 85 }

 86 

 87 return keyValue; //Return the value of the key value read

 88 }

 89 

 90 /************************************************ *******************************

 91 * Function name: Delay10ms

 92 * Function: Delay function, delay 10ms

 93 ************************************************* ******************************/

 94 

 95 void Delay10ms(unsigned int c) //Error 0us

 96 {

 97 unsigned char a, b;

 98 

 99 //--c has been assigned a value when it is passed, so there is no need to assign a value in the first sentence of the for statement--//

100 for (;c>0;c--)

101 {

102 for (b=38;b>0;b--)

103 {

104 for (a=130;a>0;a--);

105 }

106            

107 }       

108 }


Reference address:[51 MCU Study Notes FIVE]----Independent Buttons

Previous article:51 MCU register group settings
Next article:51 single chip perpetual calendar

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号