c - 打破 while 循环并重新启动代码

标签 c atmel lcd

我想知道是否可以在不使用 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
      {
          ;
      }

    }

最佳答案

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

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

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(); 中继续。

关于c - 打破 while 循环并重新启动代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36788428/

相关文章:

c - 如何通过采用结构的函数来解决这种循环依赖问题?

c - 子进程向管道写入内容后无法从管道读取

c - PEM_read_RSAPrivateKey : Getting RSA key public modulus and exponent

c - LUA 在没有的地方显示错误

c - 用C语言初始化LCD显示屏PIC16F1829?

c - 将数据写入以 2 帧发送的套接字

c++ - Atmel 微芯片 CDC 的标准 COM 端口命令

microcontroller - 处理器架构寄存器与 MCU 寄存器

c++ - 指针列表出错

c - sprintf 在 C 中毫无理由地将 FF 放在前面