The actual combat development adds a lock to the key of single chip microcomputer to prevent the detail of multiple trigger

  • 2020-06-12 10:08:58
  • OfStack

Recently, I have been working on the single chip microcomputer development of GPL32001 of sunyoung, and the main product is a piano.

On the piano, we can see with many key, keys, also has the function selection keys, in the face of so many buttons, just came out for a work friend must be pressure is bigger, the keys of the characteristics and common keys don't 1 sample, the keys of a button is composed of two buttons, one key store with different information, and key values.

So in the program I wrote the project requirements are like this, requires each key once can only trigger once, and when triggered to send out different key code, through the audio decoding box will read out the key code value, such as the first white key is key01-- > The corresponding key value is 0000 0001, which is 0x01, and the arrangement of the function keys is different from that of the keyboard. The arrangement of the function keys starts from the serial number key55, and the key value is also different from that of the keyboard. In view of such characteristics, the machine can be identified whether short circuit, open circuit and other hardware damage.

So, today I put forward a question is also common in the development of MCU, namely the key, the students have learned MCU have played the key, 1 is the beginning of the code:


if(key == 0)
bell = 0 ; 
else 
bell = 1 ; 

But in this case, assuming that it is in an infinite loop, if the button is detected as low as pressed, the button will be triggered by 1 and the branch of bell=0 will be executed continuously.

So I came up with a good idea, which is what I wrote in my project.

Define 1 static int lock; Then do the following, within an infinite loop of course:


// Get key status 
 data = *P_IOE_Data;
 if((data&0x0080))
 {
  IOE_lock = 0 ;
 }
 if((data&0x0080) == 0)
 {
   if(IOE_lock == 0)
  {
  play_sound_hightolow(0x33,Vol_value);
   }
  IOE_lock = 1 ;
 }

if((data & 0x0080) indicates that the key is not pressed, the key lock flag is 0, and the staic type will record the value of this flag variable when if((data) & When 0x0080) == 0), the key is pressed. I want to determine whether the key lock flag is 0. If it is 1, then the program will definitely not run play_sound_hightolow(). This function, so when the key is pressed, the lock initialization value is 0, the speaker emits the sound code, and the audio decoder reads the corresponding key value as 0x33. Immediately after reading, set the lock flag to 1. If 1 holds the button at this time, it is invalid because the lock flag is equal to 1, and the program does not enter the state of issuing code. When released, the state of the button changes from 1 to 0, then press the button again, it is effective, and then lock.

The advantage of this is that when the button is pressed, the issuing state is triggered only once, so that the 0x33 sound code is not connected to be issued only once. In the appropriate development of good use of the sign lock, can be very convenient and efficient to solve many problems.

conclusion


Related articles: