AVR 串口使用
#define uchar unsigned char
#define uint unsigned int
#define BUFFER_LENGTH 11
unsignedcharReceive_Calc=0;
unsignedcharUART_Receive_Buffer[11]={0};
unsignedcharUART_Send_Buffer[]={w,F,0,0,V,0,A,0,0,R,0};
//###########################################################
/*串口初始化函數(shù)*/
voidUart_Init(void)
{
UCSRA=(1<
UBRRH=0x00;// 設置波特率寄存器低位字節(jié)
UBRRL=95;//9600 (7.3728MHz) // 設置波特率寄存器高位字節(jié)
SREG_I=1;// 開總中斷
DDRD_Bit1=1;// 配置TX 為輸出(很重要)
}
//###########################################################
/*發(fā)送一個字符數(shù)據(jù),查詢方式*/
voidUart_Transmit_Char(unsignedchardata)
{
while(!(UCSRA&(1<
UDR=data;// 發(fā)送數(shù)據(jù)
}
//###########################################################
/*發(fā)送字符串*/
voidUart_Transmit_String(unsignedchar*ptr)
{
unsignedchari;
i=BUFFER_LENGTH;
while(i--)
{
Uart_Transmit_Char(*ptr++);
}
}
//###########################################################
/*中斷接收*/
#pragma vector=USART_RXC_vect
__interruptvoidUSART_RXC_Server(void)
{
UART_Receive_Buffer[Receive_Calc++]=UDR;// 將收到的值賦值給變量
if(Receive_Calc>=11)
{
Receive_Calc=0;
}
}
//###########################################################
/*主函數(shù)*/
voidmain(void)
{
Uart_Init();
while(1)
{
Uart_Transmit_Char(0x0d);
Uart_Transmit_Char(0x0a);
}
}
評論