本帖最后由 zero949079783 于 2022-5-28 22:11 编辑
开发环境:VSCODE(gcc编译链)+STM32CubeMX(也可以使用HUAWEI-LiteOS-Studio) 。
代码:Scan_key 链接:https://pan.baidu.com/s/1uXfIR0GFQOBZPl1NfQP08w
提取码:6b0c
Scan_key.h- #ifndef __SCAN_KEY_H__
- #define __SCAN_KEY_H__
- #include "gpio.h"
- #include "tim.h"
- #define KEY_ON 0
- #define KEY_OFF 1
- #define delay_ms(x) HAL_Delay(x)
- //KEY控制的引脚
- #define KEY0_GPIO_Port GPIOE
- #define KEY0_Pin GPIO_PIN_4
- #define KEY0_IN() HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)
- uint8_t scan_key(void);
- #endif
Scan_key.c
main.c
- if(scan_key() == 1)
- printf("scan_key() = %d\r\n",scan_key());
- else if(scan_key() == 2)
- printf("scan_key() = %d\r\n",scan_key());
- else if(scan_key() == 3)
- printf("scan_key() = %d\r\n",scan_key());
BSP_USART.c
- #include "BSP_USART.h"
- void BSP_USART_Init(void)
- {
- }
- //重定向c库函数printf到串口,重定向后可使用printf函数(GCC)
- int _write(int fd, char *ptr, int len)
- {
- HAL_UART_Transmit(&huart1,(uint8_t *)ptr,len,10000);
- return (len);
- }
- ////重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数(GCC)
- int _read (int fd, char *ptr, int len)
- {
- *ptr=0X00;
- HAL_UART_Receive(&huart1,(uint8_t *)ptr,1,10000);
- return 1;
- }
- ///重定向c库函数printf到串口,重定向后可使用printf函数(KEIL)
- //重定义fputc函数
- int fputc(int ch, FILE *f)
- {
- HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,10000);
- return (ch);
- }
- ///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数(KEIL)
- int fgetc(FILE *f)
- {
- int ch;
- HAL_UART_Receive(&huart1,(uint8_t *)&ch,1,10000);
- return (ch);
- }
|