Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
互斥锁mutex互斥量
请问为什么用互斥量的现象是按一下按键,他还是会有抖动,灯还是会闪两次呢?
发布于 2022-10-01 10:59:52 浏览:684
订阅该版
我的需求是,按一下按键灯只闪烁一次,解决轮询读取按键中抖动的问题,理论上线程2获得互斥量期间线程1不是不会识别的吗?为什么还会出现抖动识别问题,按一下灯还是会闪2-3次呢? 如下是我代码: ```c /* USER CODE BEGIN 0 */ static void thread1_entry(void *parameter) { while(1) { rt_err_t result; result = rt_mutex_take(mutex, RT_WAITING_FOREVER); if (result == RT_EOK) { while(1) { if(!HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4)) { rt_mutex_release(mutex); rt_thread_mdelay(200); break; } // rt_thread_mdelay(100); } } } static void thread2_entry(void *parameter) { while(1) { rt_err_t result; result = rt_mutex_take(mutex, RT_WAITING_FOREVER); if (result == RT_EOK) { HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET); rt_thread_mdelay(500); HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET); rt_thread_mdelay(500); // HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_5); rt_mutex_release(mutex); // rt_thread_mdelay(400); } } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* 创建互斥锁 */ rt_err_t result; mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO); if (mutex == RT_NULL) { rt_kprintf("create dynamic mutex failed.\n"); return -1; } result = rt_event_init(&event, "event", RT_IPC_FLAG_PRIO); if (result != RT_EOK) { rt_kprintf("init event failed.\n"); return -1; } /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* 创建线程 1,名称是 thread1,入口是 thread1_entry*/ Key_scanf = rt_thread_create("thread1", thread1_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); /* 如果获得线程控制块,启动这个线程 */ if (Key_scanf != RT_NULL) rt_thread_startup(Key_scanf); Key_handler = rt_thread_create("thread2", thread2_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); /* 如果获得线程控制块,启动这个线程 */ if (Key_handler != RT_NULL) rt_thread_startup(Key_handler); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ /* USER CODE END 3 */ } ``` ```c /* USER CODE BEGIN 0 */ static void thread1_entry(void *parameter) { while(1) { rt_err_t result; result = rt_mutex_take(mutex, RT_WAITING_FOREVER); if (result == RT_EOK) { while(1) { if(!HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4)) { rt_mutex_release(mutex); rt_thread_mdelay(200); break; } // rt_thread_mdelay(100); } } } static void thread2_entry(void *parameter) { while(1) { rt_err_t result; result = rt_mutex_take(mutex, RT_WAITING_FOREVER); if (result == RT_EOK) { HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET); rt_thread_mdelay(500); HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET); rt_thread_mdelay(500); // HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_5); rt_mutex_release(mutex); // rt_thread_mdelay(400); } } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* 创建互斥锁 */ rt_err_t result; mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO); if (mutex == RT_NULL) { rt_kprintf("create dynamic mutex failed.\n"); return -1; } result = rt_event_init(&event, "event", RT_IPC_FLAG_PRIO); if (result != RT_EOK) { rt_kprintf("init event failed.\n"); return -1; } /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* 创建线程 1,名称是 thread1,入口是 thread1_entry*/ Key_scanf = rt_thread_create("thread1", thread1_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); /* 如果获得线程控制块,启动这个线程 */ if (Key_scanf != RT_NULL) rt_thread_startup(Key_scanf); Key_handler = rt_thread_create("thread2", thread2_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); /* 如果获得线程控制块,启动这个线程 */ if (Key_handler != RT_NULL) rt_thread_startup(Key_handler); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ /* USER CODE END 3 */ } ```
查看更多
1
个回答
默认排序
按发布时间排序
lchnu
2022-10-01
Witness, Understand, Skill
仅限于你现有框架的探讨,在thread1_entry中,一般是这么去抖的: ```c while(1) { if(!HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4)) // low level { rt_thread_mdelay(20); if(!HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4)) // low level { /* Do something. Here we release mutex*/ rt_mutex_release(mutex); while(!HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4)) // waiting untill key is released { rt_thread_mdelay(10); } } } } ```
撰写答案
登录
注册新账号
关注者
0
被浏览
684
关于作者
Nealnealgo
这家伙很懒,什么也没写!
提问
4
回答
0
被采纳
0
关注TA
发私信
相关问题
1
互斥量操作,关中断时间长
2
互斥量不能在中断例程中使用的原因
3
互斥量例程中线程优先级的疑问
4
互斥量释放失败是什么原因?
5
线程和中断如何互斥?
6
关于互斥量优先级的问题
7
线程和中断如何互斥,第二篇
8
rtt stable2.0.x mutex
9
有什么避免中断包含mutex的技巧呢
10
挂载在操作系统上的文件系统操作,需要用互斥锁防止多线程操作问题吗?
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
RT-Thread动态模块
2
RT-Thread项目助手v0.3 | Ubuntu与MacOS平台的RT-Thread Env
3
【FRA156测评DM-MCX】- 环境配置篇
4
【基于HPM6750+RW007的网页服务器】---SD卡文件系统
5
有关RT_thread studio使用WDT的一点经验
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
rt-smart
RTC
FAL
I2C_IIC
ESP8266
UART
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
GD32
定时器
ADC
flashDB
编译报错
socket
中断
rt_mq_消息队列_msg_queue
Debug
keil_MDK
SFUD
msh
ulog
C++_cpp
MicroPython
本月问答贡献
lchnu
3
个答案
2
次被采纳
张世争
1
个答案
2
次被采纳
a1012112796
9
个答案
1
次被采纳
三世执戟
8
个答案
1
次被采纳
聚散无由
5
个答案
1
次被采纳
本月文章贡献
jinchanchan
9
篇文章
13
次点赞
ssdd45555
3
篇文章
2
次点赞
聚散无由
1
篇文章
4
次点赞
rvcore
1
篇文章
1
次点赞
lvdongchina
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部