完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
嗨,伙计们,我正试图找出最佳的做法来评估按键和呼叫功能的基础上。每个键都应该调用一个函数,这个函数应该执行直到不同的键被按下。为了实现这一点,我现在有一个称为Key的全局变量char,它设置在低优先级ISR的改变部分上的中断。正如你可以想象的,这个变量对已经被按下的键进行了腐蚀,并且可以是4个可能的值之一。实际上,我觉得钥匙的功能就像是各种各样的标志。属性:PIC18F26K40,MPLABX V4.01,XC8 V1.44。
以上来自于百度翻译 以下为原文 Hi guys, I am trying to figure out the best practice to evaluate key presses and call functions based on them. Each key should call one function, and that function should execute until a different key is pressed. To accomplish this at the moment, I have a global volatile char called Keythat is set the Interrupt on Change portion of the low priority ISR. As you can imagine this variable corrosponds to the key that has been pressed, and can be one of 4 possible values. In effect I feel like Key is functioning like a flag of sorts. Properties: PIC18F26K40, MPLABX v4.01, XC8 v1.44 |
|
相关推荐
12个回答
|
|
也许这个中断方法不是最好的做法。MIN()函数只是一段时间(1)在所有模块被初始化和稳定之后,也许主()应该不断轮询按钮。按钮去抖动不是一个问题,因为我已经建立了通过电容器的硬件去抖动。任何想法都非常感谢。/ ***************************************************/IF(PIR0BITS.IOCIF=1){if(key)!I/OBBF2=1){//S1中断标志ASM(“重置”);IOCBFBIT.IOCBF2=0;{I/BOBF3==1){//S2中断标志密钥=1;功能1(密钥);IOCBFBITS IOCBF3=0;}(IOCBFBIT.IOCBF4=1){//S3中断标志密钥=2;功能2(密钥);IOCBFBIT.IOCBF4=0;};= 0){Key=0;} IF(如果(IOCBFBITS IOCBF5=1){//S4中断标志密钥=3;函数3(key);IOCBFBITS IOCBF5=0;} PIR0BITS.IOCIF=0;/ /清除全局标志上的更改标志}
以上来自于百度翻译 以下为原文 Perhaps this interrupt method is not in best practice. The main() function is just a while(1) after all modules are initialized and stable, perhaps the main() should be constantly polling the buttons? Button debouncing is not an issue, as I have established hardware debouncing via capacitors. Any ideas are greatly appreciated. /************************************* ****** INTERRUPT ON CHANGE ISR ****** *************************************/ if (PIR0bits.IOCIF == 1) { if (Key != 0) { Key = 0; } if (IOCBFbits.IOCBF2 == 1){ //S1 Interrupt Flag asm("RESET"); IOCBFbits.IOCBF2 = 0; } if (IOCBFbits.IOCBF3 == 1){ //S2 Interrupt Flag Key = 1; Function_1(Key); IOCBFbits.IOCBF3 = 0; } if (IOCBFbits.IOCBF4 == 1){ //S3 Interrupt Flag Key = 2; Function_2(Key); IOCBFbits.IOCBF4 = 0; } if (IOCBFbits.IOCBF5 == 1){ //S4 Interrupt Flag Key = 3; Function_3(Key); IOCBFbits.IOCBF5 = 0; } PIR0bits.IOCIF = 0; //Clear the Global Interrupt on Change Flag } |
|
|
|
你的按钮检测效果如何?如果需要任何类型的“去抖动”,那么在常规的定时器中断,轮询按钮,而不是IOC时要容易得多。一旦你有了击键,正如你所说的,你会把它当作一个标志。你很可能在你的主()函数中有一个“超级循环”。在密钥值上使用开关()语句,并调用四个函数中的一个。
以上来自于百度翻译 以下为原文 How well is your button detecting working? If there's any sort of "debouncing" required, it is MUCH easier to do it in a regular timer interrupt, polling the buttons, rather than IOC. Once you have your keystroke, as you say, you'd treat it as a flag. Most likely you would have a "super loop" in your main() function. Each time around the loop, you would use a switch() statement on the key value, and call one of four functions. |
|
|
|
按钮检测效果很好。它进入每一个函数都很好,但是它会在while循环中挂起。
以上来自于百度翻译 以下为原文 Button detection works really well. It enters every function just fine, but it gets hung up in the while loop. |
|
|
|
这意味着什么?我不关注你的问题。编辑:我没有看到你的第二个帖子,我回答。不要调用ISR内部的函数!只需在ISR内部设置函数,并从非中断代码中调用函数即可。
以上来自于百度翻译 以下为原文 Edit: I hadn't seen your second post when I replied. Do NOT call the functions from inside the ISR! Just set the function inside the ISR, and do the function call from your non interrupt code. |
|
|
|
更新了。你的函数是怎么写的?他们做了一个手术然后退出,还是自己停留在一个无止境的循环中?当你按下一个新的按钮时,你需要多长时间中止一个函数然后移动到下一个按钮?我们只能猜测当你不显示代码时你的要求是什么。
以上来自于百度翻译 以下为原文 Updated. How are your functions written? Do they do one operation then exit, or stay in an endless loop themselves? When you press a new button, how quickly do you need to abort one function and move to the next? We can only guess what your requirements are when you don't show the code. |
|
|
|
可以让“key”成为一个易失的全局整数,然后在主循环中使用一个开关(key)语句。你也可以只做一个函数(key)来执行相应的任务。还可以有一个“NeWKEY”变量,只在按下一个不同的键时才起作用。
以上来自于百度翻译 以下为原文 You can make "key" a volatile global integer, and then in the main loop use a switch(key) statement. You could also make just one function(key) that performs tasks accordingly. It may also be good to also have a "newkey" variable to act only when a different key is pressed. volatile int key; static int newkey = 0; while(1) { if (key != newkey) { switch(key) { case 0: funct0; break; case 1: funct1; break; } newkey = key; } } |
|
|
|
显示更多代码,这样我们就可以看到,当你修复坏ISR时,“坚持”。你说(1),所以它永远不会退出没有断言。CN将检测按钮向下/向上。计时器可以用来赋予按钮更多的功能。
以上来自于百度翻译 以下为原文 Reveal more code so that we can see the, "while" that is sticking once you've fixed the bad isr. You did say while(1) so it will never exit without a break statement. CN will detect button down/up. A timer can be used to give buttons more functions. |
|
|
|
空格函数2(挥发性char key){AdcLIX初始化();ANSELABIIT.ANSELA4=1;而(key=2){LCDA CULL();LCDA SETA游标(2,1);UINT16YT A=ADCJY单(0x04);UDIVIFT B=UDIV(A,62);(未签名int C=B”;C!= 0;C--){}
以上来自于百度翻译 以下为原文 void Function_2 (volatile char Key) { ADC_Initialize(); ANSELAbits.ANSELA4 = 1; while (Key == 2) { Lcd_Clear(); Lcd_Set_Cursor(2,1); uint16_t a = ADC_Single(0x04); udiv_t b = udiv(a, 62); for (unsigned int c = b.quot; c != 0; c--) { } |
|
|
|
这是行不通的,因为在调用函数时,您传递的是关键变量的值,因此它不会看到该变量的任何更改。如果您希望函数看到它的变化,则必须传递一个指针,或者,因为它是全局变量,所以不要作为PARAMET传递它。呃,直接访问变量。不管怎样,如果我这样做,我会让函数运行一次并退出,让你的main函数决定接下来调用哪个函数。
以上来自于百度翻译 以下为原文 That won't work, because you are passing the value of the Key variable when you call the function, so it will NOT see any changes to that variable. You would have to pass a pointer to the variable if you wanted the function to see it change, or, as it is a global variable, do not pass it as a parameter, just access the variable directly. Anyway, if I was doing it, I would make the function run once and exit, and have your main function determine which function to call next. |
|
|
|
好吧,我会尝试访问它而不把它作为一个参数,看看它是否有效。我宁愿与最佳实践相一致,那么你会用PStechPaul建议的方法来做吗?或者有一些其他的程序来调用适当的函数?
以上来自于百度翻译 以下为原文 Okay, I'll try just accessing it without passing it as an argument, to see if that works. I would rather be in accordiance with best practices, so would you do it in the method that PStechPaul has suggested or have some other routine for the main() to call the appropriate function? |
|
|
|
保罗描述的方法与我刚才提到的基本相同,即拥有主()函数来进行密钥变量测试,并调用适当的例程。他在每一个新的按键上都添加了一个改进,只调用一次例程,但我认为这不是你想要的。
以上来自于百度翻译 以下为原文 Paul is describing essentially the same method as I just mentioned. i.e. having the main() function doing the key variable testing, and calling the appropriate routine. He added a refinement to only call the routine once on each new keypress, but I don't think that's what you want. |
|
|
|
是的,我想那是什么(关键)!你是正确的,我希望它不断循环通过该函数,直到一个不同的键被按下。
以上来自于百度翻译 以下为原文 Yea, I thought thats what that if(key != newkey) line meant. You are correct that I want it to constantly cycle through the function until a different key is pressed. |
|
|
|
只有小组成员才能发言,加入小组>>
5183 浏览 9 评论
2005 浏览 8 评论
1932 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3178 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2229 浏览 5 评论
739浏览 1评论
626浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
510浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
637浏览 0评论
535浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-27 23:23 , Processed in 1.605442 second(s), Total 99, Slave 82 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号