完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
本帖最后由 dddd12344444 于 2015-4-16 20:18 编辑
我用定时器写的扫描,每个10ms扫描一次按键,我在以下里面计数,发现计数自动增加,不知该怎么处理?求高手教以下如何实现按键计数, void tiM2_IRQHandler(void) { u8 temp; if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET ) { if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == Bit_RESET){ temp = 0; } else{ temp = 1; } KeyPrev = KeyCurrent; KeyCurrent = temp; if((KeyPrev == 0) && (KeyCurrent == 0)){ KeyStatus = 1; KeyCount++; } else{ KeyStatus = 0; } TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update); } } 在main文件这样写 while(1) { if ( KeyStatus == 0 ) { switch(KeyCount) { case 0:function1(); break; case 1:function2(); break; default: function3(); break; } } |
|
相关推荐
10 个讨论
|
|
|
本帖最后由 dddd12344444 于 2015-4-18 20:56 编辑
谢谢,F103ze,帮忙看看我该怎么写按键弹起,进而进行判断?给个思路,新手求指导!! |
|
|
|
|
|
|
|
|
本帖最后由 武力戡乱 于 2015-4-18 21:56 编辑
呵呵,我也是新手。你这个程序,细看好像很多地方有点没看懂。我试试吧。 Bit_RESET这里按字面理解,是没有按键发生。就是说。temp = 1为按键发生。 void TIM2_IRQHandler(void) { u8 temp; if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET ) { if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == Bit_RESET){ temp = 0; } else{ temp = 1; } KeyPrev = KeyCurrent; KeyCurrent = temp; if((KeyPrev == 1) && (KeyCurrent == 1)){ if(KeyStatus == 0) { KeyStatus = 1; KeyCount++; 按键计数 } } else{ KeyStatus = 0; } TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update); } } 在main文件这样写 while(1) { if ( KeyStatus == 1) { switch(KeyCount) { case 0:function1(); break; case 1:function2(); break; default: function3(); break; }while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == Bit_RESET); KeyStatus = 0; } } |
|
|
|
|
|
|
|
|
本帖最后由 dddd12344444 于 2015-4-19 21:53 编辑
哦,谢谢, 我编进程序试一下 [size=14.2857151031494px] if((KeyPrev == 1) && (KeyCurrent == 1)){ if(KeyStatus == 0) [size=14.2857151031494px] [size=14.2857151031494px] 这个地方怎么写[size=14.2857151031494px] if(KeyStatus == 0),在这之前[size=14.2857151031494px]KeyStatus状态没读? |
|
|
|
|
|
|
|
你正在撰写讨论
如果你是对讨论或其他讨论精选点评或询问,请使用“评论”功能。
STM32F405驱动DS1302时钟模块,输出时间错乱该怎么排查?
2810 浏览 2 评论
stm32f405rgt6驱动DS1302ZN出现时间错乱问题
2420 浏览 1 评论
stm32用fsmc读取ad7606采集数据,数据不变,只有开发版复位才更新数据
2288 浏览 0 评论
2404 浏览 1 评论
1643 浏览 1 评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 19:34 , Processed in 0.977280 second(s), Total 55, Slave 48 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
1949