完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
我目前正在尝试编写一个简单的程序,用按钮输入来切换输出(比如说LED)。我使用的是PIC16F1829,我使用3.3V的稳压器来为PIC供电。可以看到我的示意图。一个LED连接到输出引脚RC6。我使用的是8MHz的内部振荡器。代码上传完美,闪烁代码也完美地执行。然而,当我尝试使用按钮来切换LED时,LED暂时停留,然后关闭。我的代码如下:对这个问题的任何帮助都将不胜感激。
以上来自于百度翻译 以下为原文 I am currently trying to write a simple program to toggle an output (say an LED) with a pushbutton input. I am using a PIC16F1829. I am using a 3.3V regulator to power the pic. My schematic can be seen attached. An LED is connected to the out pin RC6. I am using the internal oscillator at 8Mhz. The code uploads perfectly and a blink code also executes perfectly. However, when I try to use the pushbutton to toggle the LED the LED stays on momentarily and then turns off. My code is as follows: #include #include #include #include #define _XTAL_FREQ 8000000 #pragma config FOSC = INTOSC // Oscillator Selection->INTOSC oscillator: I/O function on CLKIN pin #pragma config WDTE = OFF // Watchdog Timer Enable->WDT disabled #pragma config PWRTE = OFF // Power-up Timer Enable->PWRT disabled #pragma config MCLRE = ON // MCLR Pin Function Select->MCLR/VPP pin function is MCLR #pragma config CP = OFF // Flash Program Memory Code Protection->Program memory code protection is disabled #pragma config CPD = OFF // Data Memory Code Protection->Data memory code protection is disabled #pragma config BOREN = OFF // Brown-out Reset Enable->Brown-out Reset disabled #pragma config CLKOUTEN = OFF // Clock Out Enable->CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin #pragma config IESO = ON // Internal/External Switchover->Internal/External Switchover mode is enabled #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable->Fail-Safe Clock Monitor is enabled // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection->Write protection off #pragma config PLLEN = ON // PLL Enable->4x PLL enabled #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable->Stack Overflow or Underflow will cause a Reset #pragma config BORV = LO // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (Vbor), low trip point selected. #pragma config LVP = ON // Low-Voltage Programming Enable->Low-voltage programming enabled char BTN_pressed = 0; char BTN_press = 0; char BTN_release = 0; char Bouncevalue = 500; void main(void) { OSCCON = 0x72; OSCTUNE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; TRISA = 0x3F; TRISB = 0xF0; TRISC = 0xBF; ANSELC = 0x8B; ANSELB = 0x30; ANSELA = 0x17; WPUB = 0xF0; WPUA = 0x3F; WPUC = 0xFB; OPTION_REGbits.nWPUEN = 0; APFCON1 = 0x00; APFCON0 = 0x00; while (1) { if (RC2 == 1) { if (RC6 == 0) { LATC6 = 1; __delay_ms(300); } if (RC6 == 1) { LATC6 = 0; __delay_ms(300); } } } return; } Any help with this issue would be greatly appreciated. Attached Image(s) |
|
相关推荐
14个回答
|
|
|
若要切换RC6,请执行此操作,而不是读取RC6。你可以阅读LATC6。l///gt;关闭LVP。
以上来自于百度翻译 以下为原文 To toggle RC6, do this LATC6 ^= 1; instead of reading RC6. Turn off LVP. |
|
|
|
|
|
谢谢你的回答。我试过你说的话,把LVP关了,用你的建议替换了代码。然而,这没有什么区别。每次循环通过时,LED都闪烁闪烁。然而,应该注意的是,如果在切换线之后添加延迟,则LED会随着延迟时间闪烁。似乎由于某些原因,按钮输入引脚被不断拉高。
以上来自于百度翻译 以下为原文 Thanks for the response. I tried what you said, turned LVP off and replaced the code with your suggestion. However, it makes no difference. The LED keeps blinking on and off each time the loop goes through. However, it should be noted that if I add a delay to after the toggle line the LED blinks with that delay period. It doesn't seem as if the pushbutton input pin is constantly pulled high for some reason too. |
|
|
|
|
|
当按钮被按下时,你在RC2上读取的电压是多少?
以上来自于百度翻译 以下为原文 What voltage are you reading on RC2 when the button is _not_ pressed? |
|
|
|
|
|
检测上下开关位置和一些标志,表示开关是下降/上升。你能使用改变通知吗?花300毫秒什么也不做并不好。{SWLAGM==0?{SWLAG=1 LED启用单触发定时器中断},否则SWFLAG=0 / /开关} ISR:关断LED
以上来自于百度翻译 以下为原文 Detect the up and down switch position and some flag to say the switch is down/up. Can you use change notify? Spending 300ms doing nothing is not good. { switch is down?{ swflag==0?{ swflag=1 led on enable one shot timer interrupt } } else swflag=0 //switch up } isr: turn led off |
|
|
|
|
|
“1”和“0”,在0和4MV之间拉销。当用万用表检查时,引脚似乎切换到3.3V和低电平。我似乎无法理解这一点。尝试了两种不同的PIC16F1829,并得到了相同的结果。
以上来自于百度翻译 以下为原文 @1and0, the pin is pulled down at between 0 and around 4mV. The pins seems to switch high to 3.3V and low when checked with a multimeter. I cant seem to figure this one out. Tried with two different PIC16F1829s and ended up with the same result. |
|
|
|
|
|
除了RC2之外,您已经启用了所有PoLTC引脚上的弱拉引线。在该引脚上是否有外部上拉?我用二进制来表示这一行,这样你就可以很快地看到哪些位是1或0。
以上来自于百度翻译 以下为原文 WPUC = 0xFB; You have enabled the weak pullups on all PORTC pins, except for RC2. Do you have an external pullup on that pin? I'd use binary for that line so you can quickly see which bits are 1 or 0 WPUC = 0b11111011; |
|
|
|
|
|
@ HBHBPIN RC2是按钮输入,它被拉下来。按钮按下带来电压到VCC(3.3V)。
以上来自于百度翻译 以下为原文 @qhb pin RC2 is the pushbutton input and it's pulled down. A button press brings the voltage up to VCC (3.3V). |
|
|
|
|
|
啊,是的,我在你的示意图中注意到了,并且很快就忘记了,因为这很常见。你尝试过另一个输入而不是RC2吗?
以上来自于百度翻译 以下为原文 Ah yes, I did notice that in your schematic, and promptly forgot as it's so common to go the other way. (there's a number of reasons why pulling down is more common). Sorry, I can't see anything else that should cause your problem. Have you tried another input rather than RC2? |
|
|
|
|
|
好的,刚才发生了一些奇怪的事情。我以前用MPLAB X 4.1运行代码,上面有这个问题。现在我试着用MPLAB X 3.15上传代码,代码工作得很好。然后我用MPLAB X 4.1再次运行同样的代码,它仍然有效。我很困惑。谁知道刚才发生了什么事?恐怕这个问题可能再次出现。
以上来自于百度翻译 以下为原文 Alright so something weird just happened. I was previously running the code with MPLAB X 4.1 with the above problem. I now tried uploading the code with MPLAB X 3.15 and the code worked perfectly. I then ran the same code again with MPLAB X 4.1 and it still worked. I'm very confused. Anyone have any idea what just happened? I'm afraid the problem might arise again out of the blue. |
|
|
|
|
|
你正在前往另一个维度…黄昏带;
以上来自于百度翻译 以下为原文 You are travelling to another dimension ... the Twilight Zone ;) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
前进1步,后退2步。(每一个新版本都会引入新的bug,但是旧错误没有得到修复),这就是操作方法。
以上来自于百度翻译 以下为原文 1 step forward, 2 steps back. (Every new version introduces new bugs but the old bugs don't get fixed) That is the modus operandi. |
|
|
|
|
|
TX充满了bug,这个关于“一个编程会话不同于下一个”的事情听起来很奇怪…
以上来自于百度翻译 以下为原文 Tho X is full of bugs, this thing about "one programming session different from the next" sounds strange... |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
473 浏览 0 评论
5793 浏览 9 评论
2334 浏览 8 评论
2224 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3530 浏览 3 评论
1121浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1095浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
872浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
473浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-1 18:40 , Processed in 1.174733 second(s), Total 100, Slave 83 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
276