STM8L151C8 study notes 2: KEY driver

Publisher:数字驿站Latest update time:2022-01-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Function: Use the button to turn the LED light on and off.

The high and low levels of the button are determined by an external circuit. If the button is not pressed, the default is a high level; if the button is pressed, it is a low level.


Key detection mainly generates key signals based on the level state of the I/O port corresponding to the key.


Hardware Circuit

KEY:

KEY

Key Programming

1. Read I/O port function

From the GPIO_ReadInputDataBit() function in stm8l15x_gpio.c, the function returns the value by reading the I/O port status through the GPIO IDR register, then & the corresponding GPIO_Pin_x, and finally forcing the result to BitStatus.


The return value of STM8 may be greater than 1. The reason is that after the original type of number is forced to be converted into BitStatus, the returned result is still a value other than 0 or 1 (possibly 0x02, 0x04, 0x08, 0x20, 0x40, 0x80). The forced conversion is invalid because these values ​​cannot be found in the element values ​​when the enumeration is defined, and the result exceeds the enumeration range.

Read I/O port function in STM8 firmware library:

insert image description here

Read I/O port function in STM32 firmware library:

insert image description here

So the GPIO_ReadInputDataBit() function in STM8 can be changed as follows:


BitStatus GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin)

{

  if((GPIOx->ODR & (uint8_t)GPIO_Pin) != RESET)

  {

      return SET;

  }

  else

  {

      return RESET;

  }

}


This way the return value will no longer be greater than 1.


2.KEY initialization function

Generally, there are two working modes of KEY GPIO:


1. Floating input mode (GPIO_Mode_In_FL_No_IT, GPIO_Mode_In_FL_IT)


2. Pull-up input mode (GPIO_Mode_In_PU_No_IT, GPIO_Mode_In_PU_IT)


Since the hardware circuit does not have a pull-up resistor and I do not need the external interrupt function, I set the button I/0 port to GPIO_Mode_In_PU_No_IT.


/* Includes ------------------------------------------------------------------*/

#include "key.h"

#include "delay.h"


/* Parameter Definition ------------------------------------------------------------------*/

/*KEY GPIO*/

#define KEY1_PORT       GPIOE

#define KEY2_PORT       GPIOD


/*KEY PIN*/

#define KEY1_PIN        GPIO_Pin_5

#define KEY2_PIN        GPIO_Pin_0

#define KEY3_PIN        GPIO_Pin_1

#define KEY4_PIN        GPIO_Pin_2


/*KEY Read*/

#define KEY1 GPIO_ReadInputDataBit(KEY1_PORT, KEY1_PIN)

#define KEY2 GPIO_ReadInputDataBit(KEY2_PORT, KEY2_PIN)

#define KEY3 GPIO_ReadInputDataBit(KEY2_PORT, KEY3_PIN)

#define KEY4 GPIO_ReadInputDataBit(KEY2_PORT, KEY4_PIN)


/*KEY Press Value*/

#define KEY1_PRESS      1

#define KEY2_PRESS      2

#define KEY3_PRESS      3

#define KEY4_PRESS      4


/* Source Functions ------------------------------------------------------------------*/

void Key_Init(void)

{

  /*KEY1 Init*/

  GPIO_Init(KEY1_PORT,KEY1_PIN,GPIO_Mode_In_PU_No_IT);

  /*KEY2 Heat*/

  GPIO_Init(KEY2_PORT,KEY2_PIN|KEY3_PIN|KEY4_PIN,GPIO_Mode_In_PU_No_IT);

}


3.KEY Scan Function

The key scan is based on the key scan written by Atom Brother, as follows:


uint8_t Key_Scanf(void)

{

  static uint8_t key_up=1;//button press and release flag   

  if(key_up&&(KEY1==0||KEY2==0||KEY3==0|KEY4==0))

  {

    Delay_ms(10); //de-jitter

    key_up=0;

    if(KEY1==0)                 return KEY1_PRESS;

    else if(KEY2==0)            return KEY2_PRESS;

    else if(KEY3==0)            return KEY3_PRESS;

    else if(KEY4==0)            return KEY4_PRESS;

  }else if(KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1)key_up=1;      

  return 0; // No button pressed

}


Functional implementation part

The main functions are as follows:

image.png

main.c is as follows:


/* Includes ------------------------------------------------------------------*/

#include "stm8l15x.h"

#include "led.h"

#include "key.h"

#include "stdio.h"

/* Parameter Definition ------------------------------------------------------------------*/

uint8_t LED1_State = 1,LED2_State = 0;

/* Functions Declaration ------------------------------------------------------------------*/

void Key_Function(void);

void LED_Flash(void);


/* Source Functions ------------------------------------------------------------------*/

/**

  * @brief  CLK Config.

  * @param  None

  * @retval None

  */

void Clk_Config(void)

{

   CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); //System 1 frequency division, 16M

}


/**

  * @brief  Main program.

  * @param  None

  * @retval None

  */

int main(void)

{

  /*System Init*/

  Clk_Config();

  Key_Init();

  Led_Heat();

  /* Infinite loop */

  while (1)

  {

//    LED_Flash();

    Key_Function();

  }

}

/**

  * @brief  LED Flash.

  * @param  None

  * @retval None

  */

void LED_Flash(void)

{

  Led_Set(LED2, LED2_State);

  Led_Set(LED1, LED1_State);

  LED1_State = !LED1_State;

  LED2_State = !LED2_State;

  Delay_ms(500); 

}

/**

  * @brief  Key function.

  * @param  None

  * @retval None

  */

void Key_Function(void)

{

   uint8_t Buff[40];

   uint8_t Key;

   

   Key = Key_Scanf();

   /*KEY1 Function*/

   if(Key == KEY1_PRESS)

   {

     printf("KEY1 PRESS!rn");

     LED1_State = !LED1_State;

   }

   /*KEY2 Function*/

   if(Key == KEY2_PRESS)

   {

     LED2_State = !LED2_State;

   }

   /*KEY3 Function*/

   if(Key == KEY3_PRESS)

   {

     LED1_State = 1;

   }

   /*KEY4 Function*/

   if(Key == KEY4_PRESS)

   {

     LED2_State = 1;

   }

   /*LED Function*/

   Led_Set(LED1, LED1_State);

   Led_Set(LED2, LED2_State);

}


Summarize

Due to my previous habitual thinking in programming (I didn’t often look at the functions provided in the firmware library), I suffered a great loss in key judgment. I guess I learned a lesson.

Reference address:STM8L151C8 study notes 2: KEY driver

Previous article:MCU C language program and data storage
Next article:STM8L151C8 study notes 5: low power consumption

Latest Microcontroller Articles
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号