#define F_CPU 8000000UL #include<avr/io.h> #include<avr/interrupt.h> #include<util/delay.h> #define INT8U unsigned char #define INT16U unsigned int #define BAUDRATE 9600
struct { INT8U Buf_Array[100]; INT8U Buf_Len;
} Receive_Buffer;
INT8U Clear_Buffer_Flag=0; const INT8U SEG_CODE[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x00};//数码管断码表显示0123456789 char*s="只是AVR向PC机发送的字符串!\n",*p;
void Init_USART() { UCSRC=(1<<URSEL)|0x60; UBRRL=(F_CPU/BAUDRATE/16-1)%256; UBRRH=(F_CPU/BAUDRATE/16-1)/256; UCSRA=0X00; UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN); }
void PutChar(char c) {
if(c=='\n')PutChar('\r'); UDR=c; while(!(UCSRA &_BV(UDRE))); }
void show_Received_Digits() { INT8U i; for(i=0;i<Receive_Buffer.Buf_Len;i++) { PORTC=SEG_CODE[Receive_Buffer.Buf_Array]; _delay_ms(400);
}
int main() { Receive_Buffer.Buf_Len=0; DDRB=0X00; PORTB=0XFF; DDRC=0XFF; PORTC=0X00; DDRD=0X02; PORTD=0XFF; MCUCR=0X08; GICR =_BV(INT1); Init_USART(); sei(); while(1)show_Received_Digits(); }
ISR (USART_RXC_vect) { INT8U c=UDR; if(c=='\r'||c=='\n')Clear_Buffer_Flag=1; if(c>='0'&&c<='9') { if(Clear_Buffer_Flag==1) { Receive_Buffer.Buf_Len=0; Clear_Buffer_Flag=0; } Receive_Buffer.Buf_Array[Receive_Buffer.Buf_Len]=c-'0'; if(Receive_Buffer.Buf_Len<100) { Receive_Buffer.Buf_Len++; } } }
ISR (INT1_vect) { INT8U i=0; while(s!='\0') { PutChar(s[i++]); } }
|