完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我正在使用MPLABX V3.55和XC8开发C代码。我有一个TMR1和ADC的中断。当我启动代码时,TMR1会引起预期的中断。ADC没有启动-调试。出于某种原因,GIE在执行过程中被重置。这是在几秒钟的操作之后发生的。这是我的代码:调试器不会在最后一行设置GIE在永远循环中。我从来没有见过这样的东西。有什么想法吗?
以上来自于百度翻译 以下为原文 I am developing C code using MPLABX v3.55 and XC8. I have one interrupt for TMR1 and the ADC. When I start the code, TMR1 causes interrupts as expected. The ADC is not started - debugging. For some reason, GIE is getting reset during execution. This happens after a few seconds of operation. Here's my code: // PIC16F676 Configuration Bit Settings #pragma config FOSC = INTRCCLK // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config MCLRE = ON // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR) #pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled) #pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) /* * ROUTINE: timer_init * * DESCRIPTION: * Initialize main timer interrupt * * PARAMETERS: * Name Dir Description * ------------------------------------------------------- * None * * RETURNS: * None */ void timer_init(void) { T1CON = 0x31; // Internal clock (4 MHz /4) / 8 - 125 kHz / 8 uSec TMR1 = TMR1COUNT; // 100 mSec timer TMR1IE = TRUE; // Enable timer 0 IRQ timer = 0; second = SECOND; } /* * ROUTINE: adc_init * * DESCRIPTION: * Initialize ADC system * * PARAMETERS: * Name Dir Description * ------------------------------------------------------- * None * * RETURNS: * None */ void adc_init(void) { ADCON0 = 0x91; // Right justified result, AN4 active, enable ADC ADCON1 = 0x00; // 2 Tosc ADC clock clock ANSEL = 0x10; // AN4 enabled for(i = 0; i < READING_COUNT; i++) reading = 0; average = 0; ADIE = TRUE; // Enable ADC IRQ GO = TRUE; // Start first conversion } /* * ROUTINE: interrupt1 * * DESCRIPTION: * High priority interrupt * * PARAMETERS: * Name Dir Description * ------------------------------------------------------- * None * * RETURNS: * None */ void interrupt interrupt1(void) { if(TMR1IF) { TMR1 = TMR1COUNT; // Reset timer // GO = TRUE; // Start ADC if(second) // One second timer { second--; if(second == 0) { if(timer) timer--; second = SECOND; } } TMR1IF = FALSE; // clear IRQ flag TMR1IE = TRUE; // Reset interrupt } if(ADIF) { reading[i++] = (ADRESH << 8) + ADRESL; if(i < READING_COUNT) i = 0; for(j = 0; j < READING_COUNT; j++) average += reading[j]; average = average / READING_COUNT; // Maintain running average delta[0] = reading[4] - reading[3]; // Maintain rate of change delta[1] = reading[3] - reading[2]; ADIF = FALSE; // Clear IRQ flag } } void main(void) { timer_init(); adc_init(); PORTC = 0; // Initialize outputs TRISC = 0x10; // AD4 is input // Beep receiver and blink LED PORTC = 0x02; // Red LED and transmitter on PEIE = TRUE; // Enable peripheral interrupts GIE = TRUE; // enable IRQs timer = 1; // wait a second while(timer); PORTC = 0x01; // LED and transmitter off state = INIT; // Main loop while(1) { // State machine switch(state) { case INIT: // Set up for baseline reading timer = BASE_TIME; state = BASE; break; case BASE: // Take baseline reading if(!timer) { baseline = average; state = WAIT; PORTC |= 0x02; timer = 2; } break; case WAIT: // Maintain baseline, look for excursions if(!timer) { state = BASE; PORTC &= ~0x02; timer = 2; } break; case RISE: // Debounce excursion break; case CHANGE: // Debounced excursion seen break; case ALERT: // Alert driver to green light break; default: // Catchall - should never get here break; } GIE = TRUE; // enable IRQs } return; } The debugger will not break on the last line setting GIE in the forever loop. I've never seen anything like this. Any ideas? |
|
相关推荐
4个回答
|
|
“i”在哪里使用中断声明?(BTW,全局静态变量的一个非常坏的名称)同样,这可能是错误的:
以上来自于百度翻译 以下为原文 Where is "i" used inside Interrupt declared? (btw, a very bad name for a global static variable ) Also, this is possibly wrong: if(i < READING_COUNT) i = 0; |
|
|
|
你在哪里/如何声明变量,如“定时器”和“I”?如果在ISR和主代码中使用,它们应该是易失性的。
以上来自于百度翻译 以下为原文 Where/how are you declaring variables such as "timer" and "i"? If used in ISR and main code, they should be volatile. |
|
|
|
你是如何确定这个的?使用调试器吗?当你看到代码的时候,你在哪里?预期GIE在中断处理程序中是清晰的,而ISR正在做超出它应该做的事情,这可能意味着你花费了比预期的更多的时间(或者永远不会出去)。很可能问题在于你没有显示的代码。
以上来自于百度翻译 以下为原文 How are you determining this? With the debugger? Where are you in the code when you see it? It's expected for GIE to be clear in the interrupt handler, and your ISR is doing more than it should be, which could mean you're spending more time than expected there (or never getting out). And it's likely the problem is in the code you haven't shown. |
|
|
|
将I和J的声明移动到中断函数和其他使用的地方。修正了‘& lt’——肯定是错误的。在主循环的底部移除了GIE重置-不应该是必要的;只是把它作为一个诊断。不知道什么破了,但感谢提醒基本愚蠢的错误。过去两年来,我一直在做C代码以外的事情。我猜如果你不使用它,你就会失去它!
以上来自于百度翻译 以下为原文 Moved the declarations for i and j into the interrupt function and other places they were used. Fixed the '<' - definitely was wrong. Removed the GIE reset at the bottom of the main loop - should not have been necessary; just put it in as a diagnostic. And - it's running. No idea what broke, but thanks for the reminder about basic stupid mistakes. I've been doing everything but coding C for the last couple years. I guess if you don't use it, you lose it! |
|
|
|
只有小组成员才能发言,加入小组>>
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:26 , Processed in 1.257184 second(s), Total 84, Slave 68 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号