#include <iom16v.h>
#include <macros.h>
typedef unsigned char uint8_t;
#define DF_Config_Uart0_BaudRate 9600
//UART0 初始化
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
// 配置波特率
#if DF_Config_Uart0_BaudRate==9600
//----------11.0592M 9600kbps:
UBRRL = 0x47; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
#else if DF_Config_Uart0_BaudRate==19200
//----------11.0592M 19200kbps:
UBRRL = 0x23; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
#endif
UCSRB = 0x98;
}
// 發送一個字節
void uart0_sendByte(uint8_t dat)
{
while(!(UCSRA&(1<<UDRE))); //判斷UDR是否為空
UDR=dat; //發送數據
}
// 發送多個字節數據
void uart0_sendData(uint8_t *pDat,uint8_t nCount)
{
uint8_t i;
uint8_t *p = pDat;
for (i=0; i<nCount; i++)
{
uart0_sendByte(*p++);
}
}
// 發送字符串
void uart0_sendString(uint8_t *pDat)
{
uint8_t *p = pDat;
while(*p)
{
uart0_sendByte(*p++);
}
}
// 串口接收中斷
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
//uart has received a character in UDR
#if 1 // For test
uint8_t dat=UDR;
uart0_sendByte(dat);
#else
#endif
}
/*-----------------------------------------------------------------
函數名稱: void uart_receive(void)
函數功能: 查詢方式,接收數據
參 數:
返 回 值: 無
-----------------------------------------------------------------*/
uint8_t uart0_receive(void) //定義返回值類型,否則出錯
{
while(!(UCSRA&(1<<RXC))); //判斷是否有數據未讀出
return UDR; //獲取并返回接收數據
}
/*