简体   繁体   English

AVR C如何停止中断

[英]AVR C how to stop interrupt

I am programming a AVR MCU. 我正在编写AVR MCU。

It has a POT that reads off an analogue pin. 它具有读取模拟引脚的POT。 It seems that the interrupt is constantly called, and it must be called during a LCD_display method as it is messing with my LCD. 似乎该中断一直在被调用,并且必须在LCD_display方法期间调用该中断,因为它与我的LCD混乱了。

Is there a way to STOP the inturrupts until after the block is run? 有没有办法在程序块运行之后停止入侵?

int main(void)
{
/*Turn on ADC Interrupt */
ADCSRA |= (1 << ADIE); 

/*Turn On GLobal Interrupts*/
sei();


/* Intalise LCD */
lcd_init(LCD_DISP_ON);                /* initialize display, cursor off */
lcd_clrscr();
lcd_puts("READY");

DDRB &= ~(1 << PINB5);   //set input direction
ADC_Init(128, 0); //initalize ADC


while (1)                         
{

if (!bit_is_clear(PINB, 5))
{
_delay_ms(500);

if (!pressed)
{           
lcd_gotoxy(0,0);
lcd_clrscr();
lcd_puts("test");  //Doesnt work unless I dont comment out the last line of interrupt
pressed = 1;
}

}

/* INTERRUPTS */

//ADC INTERRUPT
ISR(ADC_vect) 
{ 

char adcResult[4];

uint8_t theLowADC = ADCL;
uint16_t theTenBitResults = ADCH<<8 | theLowADC;
itoa(theTenBitResults, adcResult, 10);
ADCSRA |= (1 << ADSC);  //next conversion  *if i comment out this line it works*


} 

If the interrupt handler behaves bad with your code, the reason could be you spend too much time in the interrupt handler. 如果中断处理程序的行为不佳,原因可能是您在中断处理程序上花费了太多时间。 You should only do critical work in the interrupt handler and defer the less critical work in the application code; 您只应在中断处理程序中执行关键工作,而在应用程序代码中延迟次要工作; use a volatile flag shared between the handler and the application code to let the application code know if it has work to do. 使用在处理程序和应用程序代码之间共享的volatile标志,以使应用程序代码知道它是否有工作要做。 In your example, you should defer the itoa call in the application code. 在您的示例中,您应该在应用程序代码中延迟itoa调用。

I believe, I am little late but still I had the same issue I solved it using the following method, 我相信,我迟到了,但仍然遇到了使用以下方法解决的相同问题,

Interrupts are enabled using two flags 1.A global interrupt flag 2.A module related interrupt flag (in your case ADC) 使用两个标志允许中断1.一个全局中断标志2.一个模块相关的中断标志(在您的ADC中)

You can have control over module related flag, in your case in the ADCSRA control register there is a flag named ADIE- ADC Interrupt Enable flag you can use that to control the Interrupts. 您可以控制与模块相关的标志,在ADCSRA控制寄存器中,有一个名为ADIE- ADC中断允许标志的标志,您可以使用它来控制中断。

For example, In main function you can enable the flag and in ISR disable the flag. 例如,在主要功能中,您可以启用该标志,而在ISR中则禁用该标志。

main()
{
    //enable global flag and ADC flag
    while(1)
    {
        //your logic
        // enable ADC flag
    }
}

ISR()
{
   //disable the ADC flag
}

I hope this solves the issue you are having. 我希望这能解决您遇到的问题。

Use cli(); 使用cli(); to disable interrupts and sei(); 禁用中断和sei(); to enable them again after you finished the display routine. 在完成显示例程后再次启用它们。 Which MCU are you using? 您正在使用哪个MCU? You should propably use a timer instead of a delay of 500ms. 您应该适当使用计时器,而不要使用500毫秒的延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM