完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,这里有菜鸟。几周前刚开始用微控制器,所以我希望你能为我澄清一些事情。我使用的是PIC16F1619的好奇板和一些外部部件,比如继电器、LED和开关,看看能做什么。我很感兴趣的是,你可以使用一个开关来选择多个功能,所以我设置了一个锁存继电器,在第一个按钮推送一个方法,然后把另一个放在同一个按钮的下一个按钮上。我自己无法理解这一点,所以我修改了我发现的一段代码。WEB,可能是完全错误的方式,但我注意到,只有当我松开按钮时,继电器才会切换。我不相信这是纯粹的阿德反弹问题,因为它是如此的一致和可重复的。你能告诉我下面的代码,为什么会发生这样的事情?因为我不明白为什么当按钮被按下时,继电器就不会扔。我对此有一些逻辑上的反驳吗?顺便说一下,我现在正在通过GooCum和其他教程工作。谢谢。
以上来自于百度翻译 以下为原文 Hi there Newbie here. Just started with microcontrollers a few weeks ago so I hope you can clear a few things up for me. I'm using the Curiosity board with the PIC16F1619 and a few external components such as a relay, LED's and a switch just to see what can be done. I'm very interested in how you can use just one switch to select multiple functions so I set up a latching relay to throw one way on the first button push then throw the other on the next button push of the same button. I could not figure this out on my own so I modified a piece of code I found the web, probably it's the incorrect way to do it altogether BUT I've noticed that the relay seems to switch only when I let off of the button. I don't believe it is purely a de-bounce issue as it is so consistent and repeatable. So can you tell me from the code I have below, WHY this is happening? Because I don't see why the relay doesn't just throw when the button is pushed. Do I have some of my logic backwards on this? BTW, I am working through Gooligum and other tutorials at the moment. Thank you. #include "mcc_generated_files/mcc.h" #define _XTAL_FREQ 500000 /* Main application */ void main(void) { // initialize the device SYSTEM_Initialize(); TRISC1 = 1; //Switch Input TRISB4 = 0; //Relay Output A TRISA4 = 0; //Relay Output B LATB4 = 0; //Output A Start LOW LATA4 = 0; //Output B Start LOW ANSA4 = 0; ANSB4 = 0; ANSC1 = 0; while (1) { while(IO_RC1_PORT == 0); IO_RB4_LAT = 1; __delay_ms(20); IO_RB4_SetLow(); while(IO_RC1_PORT == 1); __delay_ms(20); while(IO_RC1_PORT == 0); IO_RA4_LAT = 1; __delay_ms(20); IO_RA4_SetLow(); while(IO_RC1_PORT == 1); __delay_ms(20); } } |
|
相关推荐
4个回答
|
|
“IOIRB4GSELWOW()”的定义是什么?我假设它与“IOIRB4LILA= 0”相同。因此,假设IOYRC4xPORT=1,按钮被释放,程序将在第二个循环中等待。当按钮被按下时,在20毫秒延迟之后,控制将传递到下一个循环。当按钮被释放时,它将退出该循环,设置RA4高,等待20毫秒,并设置IOIRA4低。然后它将等待另一个按钮按压,在这个按钮上,它将执行最后20毫秒延迟并跳转到循环的顶部,在那里它将等待按钮被释放。然后,它将设置RB4高,等待20毫秒,设置RB4低,然后再等待它在哪里开始。因此,作为编码,中继将只得到一个脉冲时,按钮被释放。也许您的意思是在按下按钮时运行的继电器中包含继电器的操作。这样地:
以上来自于百度翻译 以下为原文 What is the definition of "IO_RB4_SetLow()"? I assume it is the same as "IO_RB4_LAT = 0". So, assuming IO_RC4_PORT == 1 with the button released, the program will wait in the second loop. When the button is pressed, after a 20 mSec delay, control will pass to the next loop. When the button is released it will exit that loop, set RA4 high, wait 20 mSec, and set IO_RA4 low. Then it will wait for another button press, on which it will execute the last 20 mSec delay and jump to the top of the loop, where it will wait for the button to be released. Then it will set RB4 high, wait 20 mSec, set RB4 low, and then wait again where it started. So, as coded, the relay will only get a pulse when the button is released. Perhaps you meant to include the operation of the relay within the loops that run when the button is pressed. Like this: while(IO_RC1_PORT == 1) __delay_ms(20); //loop with delay IO_RA4_LAT = 1; //set high __delay_ms(20); //wait 20 mSec IO_RA4_SetLow(); //set low while(IO_RC1_PORT == 0); //loop until button released |
|
|
|
你好,谢谢你的帮助。是的,“IOIRB4GETWORW())是“IOYRB4LLA= 0”,是MCC生成的宏。我想我知道这个程序会在第二个循环中等待,但是我不清楚为什么当引脚看到0个时,继电器就不会发生脉冲。也许我还不明白这些循环是如何执行的。最终,当按下按钮时,我试图对继电器进行脉冲,然后在那个状态下等待,直到再次按下按钮。我已经通过简单地将“IOYRC1A端口”变量转换成它们在我的第一篇文章中的内容来显示这种行为。我猜我不理解何时循环正在执行。我认为它会执行代码,因为如果它在“IOORC1LPORT==0”中看到“0”,它将紧接着执行代码,猜不到。
以上来自于百度翻译 以下为原文 Hello, thank you for the help. Yes "IO_RB4_SetLow()" is "IO_RB4_LAT = 0." It's a macro that the MCC generated. I guess I understand that the program would wait in the second loop but I'm not clear on why the relay would not pulse as soon as the pin sees the 0? Maybe I don't understand yet how the loops execute. Ultimately I was trying to pulse the relay when the button is pressed, then wait in that state until the button is pressed again. I have gotten it to display that behavior by simply switching the "IO_RC1_PORT" variables to the opposite of what they were in my first post. I guess I'm not understanding when the loops are executing. I thought it would execute the code in order as in if it saw a "0" as in "IO_RC1_PORT == 0" it would then execute the code immediately following that, guess not. while(IO_RC1_PORT == 1); IO_RB4_LAT = 1; __delay_ms(20); IO_RB4_SetLow(); while(IO_RC1_PORT == 0); __delay_ms(20); while(IO_RC1_PORT == 1); IO_RA4_LAT = 1; __delay_ms(20); IO_RA4_SetLow(); while(IO_RC1_PORT == 0); __delay_ms(20); |
|
|
|
问题是,在“WITH”语句之后立即放置了分号,因此它创建了一个循环,除了对Butt状态的检查之外没有其他代码。当该状态发生时,它退出并执行以下代码,该代码不在循环中。你可能已经打算这样做了,或者你可能想把继电器脉冲20毫秒,这是我上面编码的。在这种情况下,在循环中使用一行代码,则不需要撑杆。但是请注意在“后”语句之后删除分号。
以上来自于百度翻译 以下为原文 The problem is that you placed a semicolon immediately after the "while" statement, so it creates a loop with no code other than the check for button state. When that state occurs, it exits and performs the following code, which is not in a loop. You may have intended this: while(IO_RC1_PORT == 0) { //opening brace for loop IO_RB4_LAT = 1; __delay_ms(20); } //closing brace for loop IO_RB4_SetLow(); // executes when loop condition is no longer satisfied Or you may have wanted to pulse the relay for 20 mSec, which is what I coded above. In that case, with a single line of code in the loop, the braces are not needed. But note the removal of the semicolon after the "while" statement. |
|
|
|
保罗,谢谢你的回复。这确实让事情有所好转。是的,我正在尝试接力。在按钮按压时,我希望它能将继电器线圈连接到RB4,只有20MS,即使按钮仍然被按下。然后,当再次按下同一个按钮时,仅将连接到RA4的继电器线圈的相反侧脉冲仅为20MS。这是一个闩锁继电器,它实际上只需要2M的电力施加无论哪种方式。
以上来自于百度翻译 以下为原文 Paul, thank you for the reply. That does clear things up a bit. Yes I am trying to pulse the relay. On the button press I want it to pulse the relay coil connected to RB4 for 20ms only, even if the button is still depressed. Then when the same button is pressed again, pulse the opposite side of the relay coil connected to RA4 for 20ms only. It's a latching relay and it really only needs 2ms of power applied to throw it either way. |
|
|
|
只有小组成员才能发言,加入小组>>
5166 浏览 9 评论
2000 浏览 8 评论
1928 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3174 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2226 浏览 5 评论
733浏览 1评论
615浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
505浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
631浏览 0评论
528浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 08:49 , Processed in 1.257949 second(s), Total 82, Slave 66 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号