本例得安装VSPD,虚拟串口。当按下k1时会向PC串口发送数据。
以下是Studio6.2编译通过的截图:
程序清单:
/*
* GccApplication15.c
*
* Created: 2014-11-12 20:55:37
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include<avr/delay.h>
#include <avr/interrupt.h>
#include <stdint.h>
struct
{
uint8_t Buf_Aray[100];
uint8_t Buf_Len;
}Receive_Buffer;
uint8_t Clear_Buffer_Flag = 0;
const uint8_t SEG_CODE[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
char *s = "this is AVR computer!\n",*P;
void Init_USART()
{
UCSRB = _BV(RXEN)|_BV(TXEN)|_BV(RXCIE);
UCSRC = _BV(URSEL)|_BV(UCSZ1)|_BV(UCSZ0);
UBRRL = (F_CPU/9600/16-1)% 256;
UBRRH = (F_CPU/9600/16-1)/256;
}
void PutChar(char c)
{
if(c=='\n')PutChar('\r');
UDR = c;
while(!(UCSRA & _BV(UDRE)));
}
void Show_Received_Digits()
{
uint8_t i;
for(i=0;i<Receive_Buffer.Buf_Len;i++)
{
PORTC = SEG_CODE[Receive_Buffer.Buf_Aray[i]];
_delay_ms(400);
}
}
int main(void)
{
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)
{
uint8_t 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_Aray[Receive_Buffer.Buf_Len]= c-'0';
if(Receive_Buffer.Buf_Len<100)Receive_Buffer.Buf_Len++;
}
}
ISR(INT1_vect)
{
uint8_t i=0;
while(s[i]!='\0')PutChar(s[i++]);
}
|