【问题标题】:Breaking a while loop and restarting code打破一个while循环并重新启动代码
【发布时间】:2016-04-22 08:12:01
【问题描述】:

我想知道是否可以在不使用 MCU 复位引脚上的外部复位按钮的情况下中断 while 循环并从特定位置重新启动代码。

下面是当“if”语句为真时我想打破的while循环,我正在使用LCD并想返回到我的代码中显示文本的特定部分(模仿主页) .

事实上,当“if”语句为真时,while 循环被中断并且代码结束。

int main(void)
{
    /***************************************** BUTTON CONFIGURATION ********************************/

    DDRA &= ~((1<<PINA0) | (1<<PINA1) | (1<<PINA2) | (1<<PINA3));   // Config pins as inputs (ADC3 - Matching with ADMUX assignment below in ADC configuration)

    DDRC = 0xFF;        // Output pins for LEDs

    PORTA |= (1<< PINA0) | (1<<PINA1) | (1<<PINA2); // Three pins for three push buttons

    /****************************************** ADC CONFIGURATION **********************************/

    ADMUX |= (1<<MUX0) | (1<<MUX1) | (1<<REFS0);        // ADC3 and Internal voltage as reference voltage

    MCUCR &= ~((1<<ADTS2) | (1<<ADTS1) | (1<<ADTS0));   // Free running mode

    ADCSRA |= (1<<ADEN) | (1<<ADATE) | (1<<ADIE);   // ADC, Auto trigger source enable and start conversion

    sei();  // Enable global interrupts

    /***************************************** LCD CONFIGURATION ***********************************/

    LCD_Data_DDRB |= (1<<LCD_D7) | (1<<LCD_D6) | (1<<LCD_D5) | (1<<LCD_D4);     // Set output lines for lower 4 bits 

    LCD_Data_DDRD |= (1<<LCD_B3) | (1<<LCD_B2) | (1<<LCD_B1) | (1<<LCD_B0);     // Set output lines for upper 4 bits 

    LCD_Control_DDRB |= (1<<RS) | (1<<RW) | (1<<EN);                // Set RS, RW & EN output lines

    /******************************************** START CODE **************************************/

    LCD_Initialise();   // Run function to initialize the LCD

    LCD_startup();      // Run function which displays default start up text

    ADCSRA |= (1<<ADSC);    // Start conversion

    LCD_Send_Command(DISP_CL);

    while(1)
    {   

      if(Default > Final) 
      {
        LCD_Send_Command(DISP_CL);
        LCD_Send_Command(DISP_CS | LINE_1);
        LCD_Send_String(" text would go here"); 
        break;
      }

      else
      {
          ;
      }

    }

}

【问题讨论】:

  • @unwind 为您的问题提供了安全的解决方案。我不熟悉 Atmel 控制器的细节,但我认为退出 main 函数会导致 main 函数重新进入。这就是我在微芯片控制器上看到的。实际上,您可以自己轻松尝试。在特定位置切换一些引脚并用示波器观察它们。
  • 感谢您的评论,我会调查并试一试!

标签: c atmel lcd


【解决方案1】:

这有点难以理解,因为您没有显示要“重新启动”的代码。

也许您可以使用另一个循环围绕您显示的循环:

while(1)
{
  code_that_is_restarted();
  while(1)
  {
    if(Default > Final) /* Very bad variable names */
    {
      break;  /* Exits the inner loop only. */
    }
  }
}

break; 将仅退出最内层循环,因此将在code_that_is_restarted(); 中继续执行。

【讨论】:

  • 感谢您的快速回复,我会尝试一下。我还添加了我的代码的简化版本,只是为了显示我在中断后尝试重新输入的位置。
猜你喜欢
  • 1970-01-01
  • 2020-10-21
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 2011-09-06
  • 2020-03-28
相关资源
最近更新 更多