無論學(xué)習(xí)哪款MUC串口對于我們進(jìn)行實驗調(diào)試都是非常方便實用的,可以把程序中涉及的某些中間量或者其他程序狀態(tài)信息打印出來顯示在電腦上進(jìn)行調(diào)試,許多MUC和PC機通信都是通過串口來進(jìn)行的。
CC2530有兩個USB轉(zhuǎn)串口,分別是USART0和USART1。USART0和USART1是串行通信接口,它們能夠分別運行于異步UART模式或者同步SPI模式。兩個USART具體同樣的功能,可以設(shè)置在單獨的I/O引腳。
/*************************************************************************************************************
* 文件名: uart.c
* 功能: CC2530 串口相關(guān)函數(shù)
* 詳細(xì): 串口相關(guān)函數(shù)
串口最大時鐘為系統(tǒng)時鐘的1/16
*************************************************************************************************************/
#include “system.h”
#include “uart.h”
//相關(guān)UART狀態(tài)結(jié)構(gòu)
typedef struct
{
u8 BuffFull; //接收Buff滿
u8 *RxBuff; //接收Buff指針
u16 RxBuffSize; //接收緩沖區(qū)大小,一幀數(shù)據(jù)大小
u16 UartRxCnt; //接收數(shù)據(jù)計數(shù)器
} UartRx_TypeDef;
static UartRx_TypeDef UART_RX[2];
static const u8 BAUD_M[11] = {59, 59, 59, 216, 59, 216, 59, 216, 59, 216, 216}; //32MHZ系統(tǒng)時鐘對應(yīng)的分頻器小數(shù)部分
static const u8 BAUD_E[11] = {6, 7 ,8, 8, 9, 9, 10, 10, 11, 11, 12}; //32MHZ系統(tǒng)時鐘對應(yīng)的分頻器指數(shù)部分
/*************************************************************************************************************************
*函數(shù) : void UART_Init(UART_CH ch, USART_BAUD Baud, FunctionalState RxIntEn)
*功能 : 串口初始化
*參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
Baud:波特率控制,見USART_BAUD
RxIntEn:ENABLE:使能串口接收中斷
*返回 : 無
*依賴 : 底層宏定義
*說明 : 一個起始位,8個數(shù)據(jù)位,一個停止位,無奇偶校驗
需要開啟全局中斷
*************************************************************************************************************************/
void UART_Init(UART_CH ch, USART_BAUD Baud, FunctionalState RxIntEn)
{
switch(ch)
{
case UART_CH0:
{
U0CSR = BIT7 + BIT6; //UART模式,使能接收
U0UCR = BIT1; //無流控,無奇偶校驗,8bit,1個停止位,停止位高電平,起始低電平
U0GCR = BAUD_E[Baud]; //波特率分頻器指數(shù)部分
U0BAUD = BAUD_M[Baud]; //波特率分頻器小數(shù)部分
P0SEL |= BIT2 + BIT3; //P0.3 TXD,P0.2 RXD
IEN2 &= ~(1 《《 3); //關(guān)閉發(fā)送中斷
URX0IF = 0; //清除串口接收中斷標(biāo)志
UTX0IF = 0; //清除串口發(fā)送中斷標(biāo)志
URX0IE = (RxIntEn == ENABLE) ? 1 : 0; //使能串口接收中斷
}break;
case UART_CH1:
{
U1CSR = BIT7 + BIT6; //UART模式,使能接收
U1UCR = BIT1; //無流控,無奇偶校驗,8bit,1個停止位,停止位高電平,起始低電平
U1GCR = BAUD_E[Baud]; //波特率分頻器指數(shù)部分
U1BAUD = BAUD_M[Baud]; //波特率分頻器小數(shù)部分
P1SEL |= BIT4 + BIT5; //P0.5 TXD,P0.4 RXD
URX1IF = 0; //清除串口接收中斷標(biāo)志
UTX1IF = 0; //清除串口發(fā)送中斷標(biāo)志
URX1IE = (RxIntEn == ENABLE) ? 1 : 0; //使能串口接收中斷
}break;
default : return;
}
UART_SetRxBuff(ch, NULL, 0); //初始化串口緩沖區(qū)無效
}
/*************************************************************************************************************************
*函數(shù) : void UART_SendByte(UART_CH ch, u8 data)
*功能 : UART字節(jié)發(fā)送函數(shù)
*參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
data:需要發(fā)送的數(shù)據(jù)
*返回 : 無
*依賴 : 底層宏定義
*說明 : 無
*************************************************************************************************************************/
void UART_SendByte(UART_CH ch, u8 data)
{
switch(ch)
{
case UART_CH0:
{
U0DBUF = data; //發(fā)送字節(jié)數(shù)據(jù)
while(?。║0CSR & BIT1)); //等待發(fā)送數(shù)據(jù)寄存器為空
U0CSR &= ~BIT1;
}break;
case UART_CH1:
{
U1DBUF = data; //發(fā)送字節(jié)數(shù)據(jù)
while(!(U1CSR & BIT1)); //等待發(fā)送數(shù)據(jù)寄存器為空
U1CSR &= ~BIT1;
}break;
default : break;
}
}
/*************************************************************************************************************************
*函數(shù) : void UART2_SendData(u8 *pbuff, u16 len)
*功能 : 串口發(fā)送任意長度數(shù)據(jù)
*參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
pbuff:數(shù)據(jù)緩沖區(qū)指針,len:數(shù)據(jù)長度
*返回 : 無
*依賴 : 底層宏定義
*說明 : 無
*************************************************************************************************************************/
void UART_SendData(UART_CH ch, u8 *pbuff, u16 len)
{
u16 i;
switch(ch)
{
case UART_CH0:
{
for(i = 0;i 《 len;i ++)
{
U0DBUF = pbuff[i]; //發(fā)送字節(jié)數(shù)據(jù)
while(!(U0CSR & BIT1)); //等待發(fā)送數(shù)據(jù)寄存器為空
U0CSR &= ~BIT1;
}
}break;
case UART_CH1:
{
for(i = 0;i 《 len;i ++)
{
U1DBUF = pbuff[i]; //發(fā)送字節(jié)數(shù)據(jù)
while(?。║1CSR & BIT1)); //等待發(fā)送數(shù)據(jù)寄存器為空
U1CSR &= ~BIT1;
}
}break;
default : break;
}
}
/*************************************************************************************************************************
* 函數(shù) : void UART2_SendString(UART_CH ch, const char *pStr)
* 功能 : UART發(fā)送字符串
* 參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
pStr:字符串指針
* 返回 : 無
* 依賴 : 底層宏定義
* 說明 : 遇到‘0\’后停止發(fā)送
*************************************************************************************************************************/
void UART_SendString(UART_CH ch, const char *pStr)
{
while(*pStr != ‘\0’)
{
UART_SendByte(ch, *pStr ++);
}
}
/*************************************************************************************************************************
* 函數(shù) : void UART_RxEnable(UART_CH ch, FunctionalState Enable)
* 功能 : UART接收使能
* 參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
Enable:ENABLE:使能接收,DISABLE:取消接收
* 返回 : 無
* 依賴 : 底層宏定義
* 說明 : 無
*************************************************************************************************************************/
void UART_RxEnable(UART_CH ch, FunctionalState Enable)
{
switch(ch)
{
case UART_CH0:
{
U0CSR = (Enable == ENABLE) ? (U0CSR|BIT6) : (U0CSR&(~BIT6)); //使能接收
}break;
case UART_CH1:
{
U1CSR = (Enable == ENABLE) ? (U1CSR|BIT6) : (U1CSR&(~BIT6)); //使能接收
}break;
default : break;
}
}
//UART0中斷服務(wù)程序
#pragma vector=URX0_VECTOR
__interrupt void UART0_IRQHandler(void)
{
if(UART_RX[0].RxBuffSize 》 0)
{
UART_RX[0].RxBuff[UART_RX[0].UartRxCnt ++] = U0DBUF;
if(UART_RX[0].UartRxCnt == UART_RX[0].RxBuffSize)
{
UART_RX[0].UartRxCnt = 0;
UART_RX[0].BuffFull = 1;
}
}
else
{
URX0IF = 0; //清除串口接收中斷標(biāo)志
}
}
//UART1中斷服務(wù)程序
#pragma vector=URX1_VECTOR
__interrupt void UART1_IRQHandler(void)
{
if(UART_RX[1].RxBuffSize 》 0)
{
UART_RX[1].RxBuff[UART_RX[1].UartRxCnt ++] = U1DBUF;
if(UART_RX[1].UartRxCnt == UART_RX[1].RxBuffSize)
{
UART_RX[1].UartRxCnt = 0;
UART_RX[1].BuffFull = 1;
}
}
else
{
URX1IF = 0; //清除串口接收中斷標(biāo)志
}
}
/*************************************************************************************************************************
* 函數(shù) : bool UART_GetNewData(UART_CH ch, u8 *pData)
* 功能 : 獲取串口新數(shù)據(jù)
* 參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
pData:數(shù)據(jù)緩沖區(qū)指針
* 返回 : TRUE:有新數(shù)據(jù),F(xiàn)ALSE:無新數(shù)據(jù)
* 依賴 : 底層宏定義
* 說明 : 用于非中斷模式下獲取串口新數(shù)據(jù)
*************************************************************************************************************************/
bool UART_GetNewData(UART_CH ch, u8 *pData)
{
switch(ch)
{
case UART_CH0:
{
if(U0CSR & BIT2)
{
*pData = U0DBUF;
return TRUE;
}
return FALSE;
}break;
case UART_CH1:
{
if(U1CSR & BIT2)
{
*pData = U1DBUF;
return TRUE;
}
return FALSE;
}break;
default : return FALSE;
}
}
/*************************************************************************************************************************
* 函數(shù) : bool UART_GetRxBuffFullFlag(UART_CH ch)
* 功能 : 獲取串口接收緩沖區(qū)滿標(biāo)志
* 參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
* 返回 : TRUE:滿,F(xiàn)ALSE:沒有滿
* 依賴 : 底層宏定義
* 說明 : 用于判斷接收緩沖區(qū)是否滿,會清除標(biāo)志
*************************************************************************************************************************/
bool UART_GetRxBuffFullFlag(UART_CH ch)
{
if(UART_RX[ch].BuffFull) //緩沖區(qū)已滿
{
UART_RX[ch].BuffFull = 0; //清除滿標(biāo)志
return TRUE;
}
return FALSE;
}
/*************************************************************************************************************************
* 函數(shù) : void UART_SetRxBuff(UART_CH ch, u8 *pRxBuff, u16 BuffSize)
* 功能 : 設(shè)置串口接收緩沖區(qū)
* 參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
pRxBuff:緩沖區(qū)指針,BuffSize:緩沖區(qū)大小
* 返回 : 無
* 依賴 : 底層宏定義
* 說明 : 用于中斷接收
*************************************************************************************************************************/
void UART_SetRxBuff(UART_CH ch, u8 *pRxBuff, u16 BuffSize)
{
UART_RX[ch].RxBuffSize = BuffSize; //設(shè)置緩沖區(qū)大小
UART_RX[ch].RxBuff = pRxBuff; //設(shè)置緩沖區(qū)指針
UART_RX[ch].UartRxCnt = 0; //計數(shù)器清零
}
/*************************************************************************************************************************
* 函數(shù) : u16 UART_GetRxCnt(UART_CH ch)
* 功能 : 獲取串口接收數(shù)據(jù)計數(shù)器
* 參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
* 返回 : 接收到的數(shù)據(jù)數(shù)量
* 依賴 : 底層宏定義
* 說明 : 無
*************************************************************************************************************************/
u16 UART_GetRxCnt(UART_CH ch)
{
return UART_RX[ch].UartRxCnt; //返回計數(shù)值
}
/*************************************************************************************************************************
* 函數(shù) : void UART_ClearRxCnt(UART_CH ch)
* 功能 : 清除串口接收數(shù)據(jù)計數(shù)器
* 參數(shù) : ch:通道選擇,UART_CH0,UART_CH1
* 返回 : 無
* 依賴 : 底層宏定義
* 說明 : 無
*************************************************************************************************************************/
void UART_ClearRxCnt(UART_CH ch)
{
UART_RX[ch].UartRxCnt = 0; //計數(shù)器清零
}
uart.h
[cpp] view plain copy/*************************************************************************************************************
* 文件名: uart.h
* 功能: CC2530 串口相關(guān)函數(shù)
* 詳細(xì): 串口相關(guān)函數(shù)
*************************************************************************************************************/
#ifndef _UART_H_
#define _UART_H_
#include “system.h”
#include “stdio.h”
//串口波特率定義
typedef enum
{
BAUD_2400 = 0, //2400
BAUD_4800 = 1, //4800
BAUD_9600 = 2, //9600
BAUD_14400 = 3, //14400
BAUD_19200 = 4, //19200
BAUD_28800 = 5, //28800
BAUD_38400 = 6, //38400
BAUD_57600 = 7, //57600
BAUD_76800 = 8, //76800
BAUD_115200 = 9, //115200
BAUD_230400 = 10, //230400
} USART_BAUD;
//串口通道選擇
typedef enum
{
UART_CH0 = 0, //通道0,串口0
UART_CH1 = 1, //通道1,串口1
} UART_CH;
//UAR
void UART_Init(UART_CH ch, USART_BAUD Baud, FunctionalState RxIntEn); //UART初始化
void UART_SendByte(UART_CH ch, u8 data);
void UART_SendData(UART_CH ch, u8 *pbuff, u16 len); //串口發(fā)送任意長度數(shù)據(jù)
void UART_SendString(UART_CH ch, const char *pStr); //UART發(fā)送字符串
void UART_RxEnable(UART_CH ch, FunctionalState Enable); //UART接收使能
bool UART_GetNewData(UART_CH ch, u8 *pData); //獲取串口新數(shù)據(jù)
bool UART_GetRxBuffFullFlag(UART_CH ch); //獲取串口接收緩沖區(qū)滿標(biāo)志
void UART_SetRxBuff(UART_CH ch, u8 *pRxBuff, u16 BuffSize); //設(shè)置串口接收緩沖區(qū)
u16 UART_GetRxCnt(UART_CH ch); //獲取串口接收數(shù)據(jù)計數(shù)器
void UART_ClearRxCnt(UART_CH ch); //清除串口接收數(shù)據(jù)計數(shù)器
#endif //_UART_H_
//重定義printf到串口
[cpp] view plain copy#if _PRINTF_EN_
#include “uart.h”
#include “stdio.h”
//#define __CODE_MODEL__ = __CM_BANKED__
__near_func int putchar(int ch)
{
UART_SendByte(UART_CH0, ch);
return ch;
}
#endif
初始化
?。踓pp] view plain copy//主函數(shù)
int main(void)
{
SYS_ClockInit();
UART_Init(UART_CH0, BAUD_115200,ENABLE);
LED_Init();
clock_init();
SYS_EnableInt();
process_init();
process_start(&etimer_process, NULL);
autostart_start(autostart_processes);
printf(“Processes running\n”);
while(1)
{
do
{
} while(process_run()》 0);
SYS_PowerIdle(); //空閑模式
}
}
評論