为测试CC2530串口是否通信正常,做的基础测试。
本次内容主要涉及到:
CC2530串口发送数据,中断接收方式接收数据并打印出来,通过串口助手可查看数据。
此次测试串口配置为:
// uart1
// p0_4 - TX, p0_5 - RX
如果需要使用其他串口,可自行修改串口初始化里的端口配置即可。
具体测试代码如下,仅供参考。
#include <iocc2530.h>
#include "hal_types.h"
#include "hal_mcu.h"
#include "string.h"
#define LED P2_0
uint8 string[] = "uart R&T test!";//
//此数据是用来复制到内存的其他区域
uint8 data[] = "CC2530 UART test";
//数据长度
#define DATA_AMOUNT sizeof(data)
//用来保存串口接收来的数据区域
uint8 uartRxBuff[DATA_AMOUNT];
void InitClockTo32M(void)
{
CLKCONCMD &= ~0x40; //设置系统时钟源为 32MHZ晶振
while(!(CLKCONSTA & 0x40)); //等待晶振稳定
CLKCONCMD &= ~0x47; //设置系统主时钟频率为 32MHZ
}
/*
// uart1
// p0_4 - TX, p0_5 - RX
// PERCFG &= ~1<<1; //bit0 = 0
// P0SEL |= (1<<4 | 1<<5); //bit4-5 = 1
//
// p1_6 p1_7
// PERCFG |= 1<<1; //bit0 = 1
// P1SEL |= (1<<6 | 1<<7); //bit6-7 = 1
*/
void Uart1_Init(void)
{
// uart1 init config
PERCFG &= ~1<<1; //bit1 = 0, uart1 -- postion 1 p0_4, p0_5, bit1 = 1, postion 2 p1_6, p1_7
P0SEL |= (1<<4 | 1<<5);//uart1 p0_4, p0_5:bit4,5 = 1
P2DIR &= ~1<<7; //bit[7-6] = 01, at P0 port, uart1 get a higher priority than uart0
P2DIR |= 1<<6;
U1CSR |= 1<<7; //bit7=0 uartMode;1 spiMode
U1GCR |= 11; //U0GCR and U0BAUD set baud rate.
U1BAUD |= 216; //115200
UTX1IF = 0; //clear uart0 TX interrupt flag. //we does not use it.
URX1IF = 0; //clear uart0 RX interrupt flag.
URX1IE = 1; //Enable rx interrupt
U1CSR |= 0X40; //Enable rx
EA=1;
}
void Uart1_Send_String(uint8 *Data,uint8 len)
{
int j;
for(j=0; j<len; j++)
{
U1DBUF=*Data++;
while(UTX1IF==0);
UTX1IF=0;
}
}
void Init_Led(void)
{
P2DIR |= 0x01;
P2_0 = 0;
}
void main(void)
{
InitClockTo32M();
Init_Led();
Uart1_Init();
Uart1_Send_String(string,sizeof(string));
while(1)
{
if(*uartRxBuff != 0)
{
Uart1_Send_String(uartRxBuff,DATA_AMOUNT);
memset(uartRxBuff,0,DATA_AMOUNT);
}
LED = ~LED;
halMcuWaitMs(1000);
}
}
//串口接收中断服务程序入口:
#pragma vector = URX1_VECTOR
__interrupt void UART1_ISR(void)
{
static uint8 i=0;
URX1IF = 0;
if(i < DATA_AMOUNT-1)
{
uartRxBuff[i] = U1DBUF;
i++;
}
else
{
i = 0;
}
}
2.串口助手配置。
按以上代码测试时要注意:串口助手发送数据末尾记得加个空格。
仿真图如下,sizeof计算数组长度末尾会加个空格。