初级会员
- 积分
- 50
- 金钱
- 50
- 注册时间
- 2021-1-22
- 在线时间
- 10 小时
|
楼主 |
发表于 2022-11-16 16:19:59
|
显示全部楼层
代码中带了空格好像就漏掉了后面的,重新贴上
c文件
- #include "matrixkeypad.h"
- volatile uint8_t bit_flag = 0x00;
- uint8_t BoardScan(void) {
- uint8_t high_bit = 0xf0, low_bit = 0x0f;
- // High_Bit_Scan_Init();
- LOW_PORT_OUT();
- HIGH_PORT_IN();
- HIGH_PORT_PULLUP();
- HAL_GPIO_WritePin(LOW_PORT_C, LOW_PORT_PIN, GPIO_PIN_RESET);
- delay_times();
- high_bit = ((uint8_t) (HIGH_PORT_R->IDR & HIGH_PORT_PIN) << 2) & 0xf0;
- // high_bit = (uint8_t) ((HAL_GPIO_ReadPin(HIGH_PORT_R, H4_Pin) << 7) |
- // (HAL_GPIO_ReadPin(HIGH_PORT_R, H3_Pin) << 6) |
- // (HAL_GPIO_ReadPin(HIGH_PORT_R, H2_Pin) << 5) |
- // (HAL_GPIO_ReadPin(HIGH_PORT_R, H1_Pin) << 4)) & 0xf0;
- // printf("high_bit is ok! high_bit is %#04x. \r\n", high_bit);
- if (high_bit != 0xf0) {
- delay_times();
- high_bit = ((uint8_t) (HIGH_PORT_R->IDR & HIGH_PORT_PIN) << 2) & 0xf0;
- // high_bit = (uint8_t) ((HAL_GPIO_ReadPin(HIGH_PORT_R, H4_Pin) << 7) |
- // (HAL_GPIO_ReadPin(HIGH_PORT_R, H3_Pin) << 6) |
- // (HAL_GPIO_ReadPin(HIGH_PORT_R, H2_Pin) << 5) |
- // (HAL_GPIO_ReadPin(HIGH_PORT_R, H1_Pin) << 4)) & 0xf0;
- // printf("high_bit is ok! high_bit is %#04x. \r\n", high_bit);
- if (high_bit != 0xf0) {
- // Low_Bit_Scan_Init();
- HIGH_PORT_OUT();
- LOW_PORT_IN();
- LOW_PORT_PULLUP();
- HAL_GPIO_WritePin(HIGH_PORT_R, HIGH_PORT_PIN, GPIO_PIN_RESET);
- delay_times();
- low_bit = (uint8_t) ((LOW_PORT_C->IDR & LOW_PORT_PIN) >> 12) & 0x0f;
- // low_bit = (uint8_t) ((HAL_GPIO_ReadPin(LOW_PORT_C, L4_Pin) << 3) |
- // (HAL_GPIO_ReadPin(LOW_PORT_C, L3_Pin) << 2) |
- // (HAL_GPIO_ReadPin(LOW_PORT_C, L2_Pin) << 1) |
- // (HAL_GPIO_ReadPin(LOW_PORT_C, L1_Pin) << 0)) & 0x0f;
- // printf("low_bit is ok! low_bit is %#04x. \r\n", low_bit);
- bit_flag = high_bit | low_bit;
- }
- }
- delay_times();
- // __ASM volatile ("NOP");
- // printf("bit_flag is ok! bit_flag is %#04x. \r\n", (uint8_t) ~bit_flag);
- return (uint8_t) ~bit_flag;
- }
- void delay_times(void) {
- osDelay(1);
- // HAL_Delay(1);
- }
复制代码 .h文件
- #ifndef __MATRIXKEYPAD_H__
- #define __MATRIXKEYPAD_H__
- #include "main.h"
- #include "gpio.h"
- #include "retarget.h"
- #include "cmsis_os2.h"
- #define HIGH_PORT_R GPIOC
- #define LOW_PORT_C GPIOD
复制代码
task代码
- void StartKeyScanTask(void *argument) {
- /* USER CODE BEGIN StartKeyScanTask */
- uint8_t last_key_flag = 0x00;
- /* Infinite loop */
- for (;;) {
- uint8_t temp_key_flag = BoardScan();
- #if (TSAK_NOTIFICATION == 1)
- // 任务通知版本,可以使用 任务通知功能 直接进行任务间传输,不需要全局变量。
- if (temp_key_flag != last_key_flag) {
- printf("key_flag is %#04x. \r\n", temp_key_flag);
- xTaskGenericNotify(LedTaskHandle, temp_key_flag, eSetValueWithOverwrite, NULL);
- last_key_flag = temp_key_flag;
- }
- #else
- // 使用全局变量key_flag版本,非任务通知
- if (temp_key_flag != key_flag) {
- printf("key_flag is %#04x. \r\n", temp_key_flag);
- key_flag = temp_key_flag;
- }
- #endif
- osDelay(100);
- }
- /* USER CODE END StartKeyScanTask */
- }
复制代码
重贴代码与之前代码相同,不过是新建了一个工程后测试的,hal_delay()函数和osdelay()函数做延时运行均变得正常了,hal_delay()的定时器由TIM1换成了TIM10,其他配置均相同。
放这里分享用吧, 具体原因还是不明,非常奇怪。
|
|