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 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:
Read I/O port function in STM32 firmware library:
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:
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.
Previous article:MCU C language program and data storage
Next article:STM8L151C8 study notes 5: low power consumption
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Two power modules with the same voltage connected in parallel?
- How to choose the inductor of BOOST boost power supply?
- "Invite you to disassemble" Episode 1 --- Xiaomi 45W charging head disassembly
- The experience of debugging L-band RF power amplifier is in "2019.1.1" for your reference
- 2021 ON Semiconductor Avnet RSL10 Bluetooth SoC Development and Design Competition 4th Post (Bluetooth Current)
- MSP430FR5969 Remote Upgrade
- Circuit help, how do you analyze this circuit?
- EEWORLD University Hall----Live Replay: Microchip Security Series 15 Simplifies Security Application Design with dsPIC33/PIC24 and ATECC608 Devices
- EEWORLD University - Designing Wide Input DC/DC Converters for Thermostat Applications
- Win a JD.com card! Check in at Infineon's new SiC MOSFET pop-up store