?? uart.c
字號:
/////////////////////////////////////
//070410
//在發(fā)送函數(shù)中加上超時處理
//防止發(fā)送失敗進入死循環(huán)
/////////////////////////////////////
#define _UART_C_
#define BAUD 9600
#define CRYSTAL 11059200
#define BAUD_SETTING (unsigned int)((unsigned long)CRYSTAL/(16*(unsigned long)BAUD)-1)
#define BAUD_H (unsigned char)(BAUD_SETTING>>8)
#define BAUD_L (unsigned char)(BAUD_SETTING)
#include <includes.h>
// 初始化串口
void uartInit(uint8 uartNum)
{
OS_ENTER_CRITICAL();
if(1==uartNum)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = BAUD_L; //set baud rate lo
UBRR1H = BAUD_H; //set baud rate hi
UCSR1B = 0x98; // 接收、發(fā)送使能,接收中斷使能
//共有3個中斷源, 接受, 發(fā)送, 寄存器空, 對應此寄存器的高3位
inRxd1Buf = Rxd1Buf;
outRxd1Buf = Rxd1Buf;
}
else
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06; //set frame format, 8 bits, 2stop]
UBRR0H = BAUD_H;
UBRR0L = BAUD_L;
UCSR0B = 0x98; //0x18為允許接收和發(fā)關,0x98時全能接收中斷,允許接收.
inRxd0Buf = Rxd0Buf;
outRxd0Buf = Rxd0Buf;
}
OS_EXIT_CRITICAL();
}
// 發(fā)送1個字節(jié)的數(shù)據(jù)
uint8 putChar(uint8 num, uint8 dat)
{
if(1 == num)
{
while ( !( UCSR1A & (1<<UDRE1)) )
;
UDR1 = dat;
}
else if(0 == num)
{
while ( !( UCSR0A & (1<<UDRE0)) )
;
UDR0 = dat;
}
else
{
return 0;
}
return 1;
}
// 發(fā)送完整的字符串
uint8 putStr(uint8 num, uint8 *str, uint8 len)
{
uint8 i = 0;
//ENTER_CRITICAL();
while((*str != NULL) && (len > 0))
{
if(0 == putChar(num, *str) )
{
return i;
}
i++;
str++;
len--;
}
return i;
//EXIT_CRITICAL();
}
//發(fā)送一個字節(jié)的hex碼,分成兩個字節(jié)發(fā)。
uint8 const hex_[] = {"0123456789ABCDEF"};
uint8 putCharHex(uint8 num, uint8 dat)
{
uint8 ch;
uint8 flag;
ch = (dat >> 4) & 0x0F;
flag = putChar(num, hex_[ch]);
if (0 == flag)
{
return 0;
}
ch = dat & 0x0F;
flag = putChar(num, hex_[ch]);
if (0 == flag)
{
return 0;
}
return 1;
}
// 將字符串以HEX格式發(fā)送
uint8 putStrHex(uint8 num, uint8 *str, uint8 len)
{
uint8 i = 0;
while((*str != NULL) && (len > 0))
{
if (0 == putCharHex(num, *str))
{
return i;
}
str++;
i++;
len--;
}
return i;
}
// 從串口緩沖區(qū)中讀取1個字節(jié)的數(shù)據(jù)
int16 getChar(uint8 num)
{
uint8 temp=0;
uint8 timeout = 10;
if(1==num)
{
while(timeout)
{
if(Rxd1BufCnt != 0)
{
temp = *outRxd1Buf;
if(outRxd1Buf == &Rxd1Buf[Rxd1BufLen - 1])
{
outRxd1Buf = Rxd1Buf; // 地址到頂部后回到底部
}
else
{
outRxd1Buf++; // 輸出指針右移1位
}
Rxd1BufCnt--; // 緩沖區(qū)中數(shù)據(jù)個數(shù)減1
return (temp); // 讀到數(shù)據(jù),退出
}
else
{
timeout--; // 緩沖區(qū)中沒有數(shù)據(jù),計時減1
DelayMs(); // 緩沖區(qū)中沒有數(shù)據(jù),延時1ms
}
}
}
else if (0 == num)
{
while(timeout)
{
if(Rxd0BufCnt != 0)
{
temp = *outRxd0Buf;
if(outRxd0Buf == &Rxd0Buf[Rxd0BufLen - 1])
{
outRxd0Buf = Rxd0Buf; // 地址到頂部后回到底部
}
else
{
outRxd0Buf++; // 輸出指針右移1位
}
Rxd0BufCnt--; // 緩沖區(qū)中數(shù)據(jù)個數(shù)減1
return (temp); // 讀到數(shù)據(jù),退出
}
else
{
timeout--; // 緩沖區(qū)中沒有數(shù)據(jù),計時減1
DelayMs(); // 緩沖區(qū)中沒有數(shù)據(jù),延時1ms
}
}
}
else
{
return (-1);
}
return (-1);
}
// 從串口緩沖區(qū)中讀取n個字節(jié)的數(shù)據(jù)到dest中
uint8 getStrLong(uint8 num, uint8 len, uint8 *dest)
{
uint8 i = 0;
INT16S c = 0;
INT8U cnt = 0;
INT8U *pDest = dest;
for(i = 0; i < len; i++)
{
c = getChar(num);
if(c == -1)
{
return cnt;
//return pDest;
}
else
{
cnt++;
*dest++ = (INT8U)(c);
}
}
return cnt;
//return pDest;
}
// 從串口緩沖區(qū)中讀取某符號前的所有數(shù)據(jù)< Rxd0BufLev2Len 到dest中
uint8 getStr(uint8 num, uint8 c, uint8 *dest)
{
uint8 i = 0;
uint16 ch;
while((uint8)(ch = getChar(num))!= c)
{
if(-1 == ch)
{
return i;
}
*dest++ = (uint8)ch;
}
return i;
}
////////////////////////////////////////////////
//UART1 接受完成中斷
////////////////////////////////////////////////
#pragma interrupt_handler uart1_rx_isr:31
void uart1_rx_isr(void)
{
unsigned char temp;
//ENTER_CRITICAL();//uart has received a character in UDR
temp = UDR1;
if(Rxd1BufLen > Rxd1BufCnt)
{
*inRxd1Buf = temp;
Rxd1BufCnt++; // 緩沖區(qū)中數(shù)據(jù)計數(shù)器加1
if(inRxd1Buf == &Rxd1Buf[Rxd1BufLen -1])
{
inRxd1Buf = Rxd1Buf; // 地址到頂部后回到底部
}
else
{
inRxd1Buf++; // 指針右移1位
}
}
//putChar(0, temp);
//EXIT_CRITICAL();
}
////////////////////////////////////////////////
//UART0 接受完成中斷
////////////////////////////////////////////////
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RX //iv_USART0_RXC
void uart0_rx_isr(void)
{
unsigned char data;
//ENTER_CRITICAL();//uart has received a character in UDR
data = UDR0;
if(Rxd0BufLen > Rxd0BufCnt)
{
*inRxd0Buf = data;
Rxd0BufCnt++; // 緩沖區(qū)中數(shù)據(jù)計數(shù)器加1
if(inRxd0Buf == &Rxd0Buf[Rxd0BufLen -1])
{
inRxd0Buf = Rxd0Buf; // 地址到頂部后回到底部
}
else
{
inRxd0Buf++; // 指針右移1位
}
}
//putChar(0, (INT8U)getChar(0));
//EXIT_CRITICAL();
}
///////////////////////////////////////////////
//發(fā)送寄存器空中斷
///////////////////////////////////////////////
#pragma interrupt_handler uart1_udre_isr:32
void uart1_udre_isr(void)
{
//ENTER_CRITICAL();//character transferred to shift register so UDR is now empty
//EXIT_CRITICAL();
}
//////////////////////////////////////////////
//發(fā)送完成中斷
//////////////////////////////////////////////
#pragma interrupt_handler uart1_tx_isr:33
void uart1_tx_isr(void)
{
//ENTER_CRITICAL();;//character has been transmitted
//EXIT_CRITICAL();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -