2

我在 AVR 芯片上运行 C 程序。每当听到串行信号时,它就会运行串行中断 ISR (USART_RX_vect)。在这种方法中,它应该打开 change to = 1;。然后在我的主while循环中,它应该清除 LCD 并显示它,然后change = 0再次设置。

这是为了阻止它继续进行计算,并在 LCD 上每分钟显示一百万次结果。

但是,当中断方法将 change 变量更改为 1 时,它似乎并没有“全局”更改它,并且在 main 方法中它始终为 0..

这里有一些用于调试目的的东西。

/* LCD DEFINES */
#define LED PB5
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)

/* UART SERIAL DEFINES */
#define F_CPU 16000000UL
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1

#define STARTCHAR 'R'
#define ENDCHAR 'E'

char reading;
char inputBuffer[12];
char readStatus;
uint8_t position;
int change;

char output;
int result;

struct Axis
{
    uint8_t axisNumber;
    uint16_t position;
    uint16_t oldPosition;

} axis1, axis2, axis3;


/* SETUP UART */

void USART_Init( unsigned int ubrr)
{
   /*Set baud rate */
   UBRR0H = (unsigned char)(ubrr>>8);
   UBRR0L = (unsigned char)ubrr;

  /*Enable receiver and transmitter */
   UCSR0B = (1<<RXEN0)|(1<<TXEN0);

   /* Set frame format: 8data, 2stop bit */
   UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

void USART_Transmit( unsigned char data )
{
    UDR0 = data;
}

unsigned char USART_Receive( void )
{
   return UDR0;
}

/*****************************************************************/

int main(void)
{
    /* INITALISE SERIAL */
    USART_Init(MYUBRR);

    /* Turn on Receive Complete Interrupt */
    UCSR0B |= (1 << RXCIE0);

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

    position = 0;
    change = 0;

    /* Initialise LCD */
    lcd_init(LCD_DISP_ON);  /* Initialize display, cursor off. */
    lcd_clrscr();
    lcd_puts("READY");

    //Turn on LED 13
    set_output(PORTB,LED);
    output_low(PORTB,LED);

    while (1)               /* Loop forever */
    {
        if (change == 1)
        {
            //If not reading, display the result on the LCD display.
            axis1.position  = (inputBuffer[0]<< 8) | inputBuffer[1];
            axis2.position  = (inputBuffer[2]<< 8) | inputBuffer[3];
            axis3.position  = (inputBuffer[4]<< 8) | inputBuffer[5];

            char axis1Printout[12];
            char axis2Printout[12];
            char axis3Printout[12];

            sprintf(axis1Printout,"%u ", axis1.position);
            sprintf(axis2Printout,"%u ", axis2.position);
            sprintf(axis3Printout,"%u ", axis3.position);

            char output[40] = "";
            strcat(output, axis1Printout);
            strcat(output, axis2Printout);
            //strcat(output, axis3Printout);

            lcd_clrscr();  /* Clear the screen*/
            lcd_puts(output);
            _delay_ms(300);
            change = 0;
        }
    }
}

/* INTERRUPTS */

ISR (USART_RX_vect)
{
    change = 1;
    unsigned char input = USART_Receive();

    if (input == 'R')
    {
        readStatus = 0; //Reading
        position = 0;
    }
    else if ((input != 'E') && (position < 12) && (position > -1))
    {
        inputBuffer[position] = input;
        position++;
    }
    else if (input == 'E')
    {
        readStatus = 1; //Stop Reading
        position = -1;
        output_high(PORTB,LED);
    }
}
4

3 回答 3

7

您需要使用 volatile 关键字声明更改:

volatile int change;

这告诉两个“线程”(主执行循环和您的 ISR 代码)不要“缓存”寄存器中的值,而是始终从内存中检索它。

编辑:代码还有另一个问题 - 在您的主循环中,当您设置更改为 0 时,您可能已经有另一个中断应该触发您的循环再次运行。简单但不保证的解决方法是在您检查后立即设置更改为 0。正确的方法是使用锁 - 但根据您的情况,第一个选项可能会做。

于 2012-02-10T13:24:48.117 回答
3

使变量声明volatile以确保更改的值立即写入内存中的变量。

于 2012-02-10T13:24:59.717 回答
0

由中断处理程序和应用程序代码共享的对象应符合volatile声明中的条件。

如果没有限定符,实现可以假设对象不会在应用程序代码中意外更改,并且可以在执行应用程序代码时缓存变量(例如在寄存器中)以进行优化。

于 2012-02-10T13:51:57.060 回答