本人在调试一个AVR系统时发现一个百思不得其解的问题,后经仔细分析后才找到答案,原来是GCC在优化时未考虑变量的“非顺序性”改变(如被中断程序修改),造成了优化后程序执行结果的不同。下面给出一个典型的测试程序:
// 以下程序用AVR Studio v4.12 service Pack4 + WINAVR 20060421 编译 // 其用意是将串口输入的八位数据从 PORT A 口输出。
/////////////////////////////////////// // TEST GCC for AVR optimization ///////////////////////////////////////
#include <avr/io.h> #include <avr/interrupt.h>
unsigned char Buffer;
void Init( void ); void USART_Init ( unsigned int baud ); unsigned char USART_Receive( void );
int main ( void ) { Init();
while ( 1 ) { PORTA = Buffer; } }
void Init( void ) { // set PORTA as output port DDRA = 0xFF;
// initialize UART USART_Init( 71 ); // set B.R. 9600 for 11.0592 MHz
// enable interupt sei(); }
void USART_Init ( unsigned int baud ) { // set BAUD RATE UBRRH = (unsigned char) (baud >> 8); UBRRL = (unsigned char) baud;
// enable receiver and transmitter and RXC interrupt UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
// set frame format: 8 data bit, 1 stop bit UCSRC = (1 << URSEL) | (3 << UCSZ0); }
unsigned char USART_Receive( void ) { /* Wait for data to be received */ while ( !(UCSRA & (1 << RXC)) ) ;
/* Get and return received data from buffer */ return UDR; }
// ISR for UART ISR ( USART_RXC_vect ) { Buffer = USART_Receive(); }
|