Blue Bridge Cup embedded - key control LED

I Introduction to key functions

The M4 development board contains five keys, one of which is the Reset key and the other four are general keys.

 

 

It can be seen from the schematic diagram that the keys B0-B4 are externally connected with the pull-up resistance to VDD, and GND is connected when pressed, so the key will read 1 when it pops up and 0 when pressed;

When in use, you only need to initialize the GPIO related to the key, and then read the level for operation.

II Use cubeMx for key initialization

(directly initialize to input mode, No pull)

void Key_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PB0 PB1 PB2 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

Then you need to use the function to read the key signal. Generally speaking, you need to do key anti chattering, but the test found that there is no problem without anti chattering. Here we still add anti chattering.

unsigned char Key_Scan(void)
{
    if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
    {
        //Key_Delay(10);
        Key_Val = 8;
    }
    else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == GPIO_PIN_RESET)
    {
        //Key_Delay(10);
        Key_Val = 1;
    }
    else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET)
    {
        //Key_Delay(10);
        Key_Val = 2;
    }
    else if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET)
    {
        //Key_Delay(10);
        Key_Val = 4;
    }
    else
        Key_Val = 0;
    return Key_Val;
}

//Key_Delay Function applies to 80 MHz,Realize one cycle delay 1 ms;
void Key_Delay(unsigned int ms)
{
    unsigned int i,j;
    for(i=0;i<ms;i++)
        for(j=0;j<8000;j++);
}

Finally, call key in the main function_ Scan function is enough. Here, we need to pay attention to the different functions of long press and short press. Therefore, we need to make some judgments logically.

After the function entry comes in, first read a current key value, and then read a new key 'value after a period of time, so as to judge whether to press long or short;

On this basis, par is used to judge and perform the functions of long press and short press respectively.

 

However, if you think about it directly, you will find that the LED can realize the long press function when it is pressed (always pressed), but it will jump to realize the short press function as soon as it pops up; So you need to use a latch idea

After the long press signal is determined and obtained, it is not allowed to change to the short press signal.

void Key_Get(void)
{
    unsigned i;
    Key_Currentval = Key_Scan();
    if(Key_Currentval == 0 || (Key_Currentval == ucLed>>4))
        goto loop;
    Key_Delay(1000);
    Key_Pastval = Key_Scan();
    if((Key_Pastval == Key_Currentval)&&(Key_Pastval!=0))
    {
        par = 1;
    }
    else if(Key_Pastval != Key_Currentval)
        par = 0;
    //Press the key briefly
    if(par==0)
    {
        if(Key_Currentval==1)
            ucLed = 0x01;
        else if(Key_Currentval==2)
            ucLed = 0x02;
        else if(Key_Currentval==4)
            ucLed = 0x04;
        else if(Key_Currentval==8)
            ucLed = 0x08;
    }
    else if(par==1)
    {        
        if(Key_Pastval==1)
            ucLed = 0x10;
        else if(Key_Pastval==2)
            ucLed = 0x20;
        else if(Key_Pastval==4)
            ucLed = 0x40;
        else if(Key_Pastval==8)
            ucLed = 0x80;
    }
    loop:
    {
        i = i;
    }
}

//Ps: as a novice, I added some unnecessary judgment sentences. I think this can occupy less cpu resources in my spare time. I don't know whether this is reasonable. Welcome to discuss.

END

Added by andylai on Wed, 26 Jan 2022 07:11:40 +0200