Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
互斥锁mutex互斥量
请问为什么用互斥量的现象是按一下按键,他还是会有抖动,灯还是会闪两次呢?
发布于 2022-10-01 10:59:52 浏览:722
订阅该版
我的需求是,按一下按键灯只闪烁一次,解决轮询读取按键中抖动的问题,理论上线程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
被浏览
722
关于作者
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
在LPC1114上移植 RT-Thread Nano 3.1.5
2
求助can通讯下怎末运用485
3
STM32F407ZGT适配串口V2驱动
4
【CFP】2025 RT-Thread全球技术大会演讲征集开始啦!
5
在 RT-Thread Studio 环境中为 ART-Pi 2 移植 CherryUSB(以 usb_device 为例)
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
cubemx
ESP8266
WIZnet_W5500
UART
ota在线升级
BSP
PWM
flash
packages_软件包
freemodbus
潘多拉开发板_Pandora
ADC
GD32
定时器
flashDB
编译报错
keil_MDK
socket
中断
rt_mq_消息队列_msg_queue
Debug
ulog
SFUD
msh
C++_cpp
at_device
本月问答贡献
聚散无由
9
个答案
6
次被采纳
RTT_逍遥
8
个答案
2
次被采纳
a1012112796
5
个答案
2
次被采纳
三世执戟
4
个答案
2
次被采纳
加缪
2
个答案
2
次被采纳
本月文章贡献
wake_mirco
2
篇文章
7
次点赞
mushroom
1
篇文章
9
次点赞
张世争
1
篇文章
7
次点赞
RTT_逍遥
1
篇文章
6
次点赞
Jack_____
1
篇文章
5
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部