完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
我正在学习中断,似乎偶然发现了一些非常奇怪的行为。至少在我的设置中,它的行为很奇怪。我已经删除了我所有的其他代码,并将其缩小到以下条件。在我的设置中,我有2个LED连接到RB4&RB5。我的伪代码基本上是这样做的,打开两个LED。等待X秒。打开一个LED,等待Y秒。在我的代码中,如果我使用值为Y-YelayaysMs()最多1000,一切似乎都很好,表现正常。如果我开始增加值,保持X和Y的相同,“ON”时间似乎是正确的,但是“off”时间大大减少到1/5秒。我开始以100ms的增量增加值直到1900毫秒,然后LED似乎永远保持不变。根据文档(我使用XC8 1.42)-“延迟参数必须是常数,小于50463240”任何可能导致这一点的想法,我的完整代码如下。
以上来自于百度翻译 以下为原文 I was learning about interrupts and seem to have stumbled across some really weird behaviour of the __delay_ms() function. At least on my setup it's behaving odd. I've stripped out all my other code and narrowed it down to the following conditions. In my setup I have 2 LEDs connected to RB4 & RB5. My pseudo code basically does this; Turn both LEDs on. Wait x seconds. Turn one of the LEDs off Wait y seconds. In my code if I use values up for __delay_ms() up to 1000, everything seems fine and behaves apparently normally. Howevef if I start increasing the values, keeping the same for x & y the "on" time seems correct but the "off" time is greatly reduced to what seems like 1/5 second. I started increasing values in increments of 100ms up to 1900ms and then the LEDs seem to stay on permanently. According to the documentation (I'm using XC8 1.42) - "The delay argument must be a constant and less than 50,463,240" Any ideas what could be causing this, my full code is below. // PIC16F87 Configuration Bit Settings // 'C' source line config statements // CONFIG1 #pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin) #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off) #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off) #pragma config CCPMX = RB0 // CCP1 Pin Selection bit (CCP1 function on RB0) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) // CONFIG2 #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled) #pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode enabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include // We're going to be using the __delay_ms() functions so we need to define the clock frequency we've set. #define _XTAL_FREQ 4000000 void main(void) { // Set up the clock frequency. Make sure the _XTAL_FREQ definition matches. // // 000 = 31.25 kHz // 001 = 125 kHz // 010 = 250 kHz // 011 = 500 kHz // 100 = 1 MHz // 101 = 2 MHz // 110 = 4 MHz // 111 = 8 MHz // OSCCONbits.IRCF = 0b110; // Set up our ports. TRISBbits.TRISB5 = 0; TRISBbits.TRISB4 = 0; // Make sure all LEDs are off. PORTBbits.RB5 = 0; PORTBbits.RB4 = 0; // Enter inifinite loop. while(1) { // Turn 2 pins on. PORTBbits.RB4 = 1; PORTBbits.RB5 = 1; // Wait 3 seconds. #ifndef __DEBUG __delay_ms(1000); #endif // Turn off one of the LEDS. PORTBbits.RB5 = 0; // Wait 2 seconds. #ifndef __DEBUG __delay_ms(1000); #endif } return; } |
|
相关推荐
3个回答
|
|
对于长延时,你可以做一个功能,包括在一个循环中击中看门狗:一个4 MHz的时钟,5000的延迟变成5.185秒。如果需要的话,有提高精度的方法。[De]我注意到每次我编辑这个代码时,都会在代码中添加一个额外的行空间。高飞!
以上来自于百度翻译 以下为原文 For long delays you can make a function that includes hitting the watchdog in a loop: void delay_msec(unsigned long msec) { while( msec > 0 ) { __delay_ms(1); msec--; CLRWDT(); } } With a 4 MHz clock a delay of 5000 becomes 5.185 seconds. There are ways to improve accuracy if needed. [edit] I just noticed that each time I edit this, an extra line space is added to the code. Goofy! |
|
|
|
用于在延迟期间杀死狗。
以上来自于百度翻译 以下为原文 Use __delaywdt_ms(); to |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
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 06:29 , Processed in 1.160058 second(s), Total 80, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号