#include <ioCC2530.h>
#include <string.h>
//declaration of function
void init_uartsend(void);
void uartTx_send_String(char *data, int len);
char txData[] = "Please input \n";
//initial the uart
void init_uartsend(void)
{
CLKCONCMD &= ~0x47; //System clock-source select:32MHz,Timer ticks output setting:250kHz
while(CLKCONSTA & 0x47); //wait for CLKCONCMD selected
PERCFG = 0x00; //alternative 1 location
P0SEL = 0x3c; //P0_2:RX P0_2:TX P0_2:CT P0_2:RT
P2DIR &= ~0xc0; //set port 0 1st priority: USART 0 2nd priority: USART 1 3rd priority: Timer 1
U0CSR |= 0xc0; //usart mode select : UART mode; UART receiver enabled
U0GCR |= 9;
U0BAUD |= 59; //set baud rate:19200
UTX0IF = 0; //USART 0 TX interrupt flag : interrupt not pending
}
//send data
void uartTx_send_String(char *data, int len)
{
int j;
for(j = 0; j < len; j++)
{
U0DBUF = data[j];
while(UTX0IF == 0);
UTX0IF = 0;
}
}
void main()
{
init_uartsend();
uartTx_send_String(txData, sizeof(txData));
while(1)
{
while(!(U0CSR & (1<<2)));
U0DBUF = U0DBUF;
while(!(U0CSR & (1<<1)));
UTX0IF = 0;
}
}
该程序功能:实现接收数据,并将接收到的数据原封不动的发送回去。
测试:使用串口调试助手,波特率设置为19200,复位开发板,接收到从开发板发来的数据:Please input,发送"test text",收到"test text"。