一.前言
上次我们学习了串口的发送,今天我们要学习串口的接收,要实现的功能是接收电脑发来的数据,控制LED 灯闪烁,而且将收到的数据发回给电脑显示出来。而且要采用不占用cpu资源的中断。
二原理与分析
由于要采用中断处理的方式,所以我们要开接收中断和总中断,具体相关的寄存器配置如下:
U0CSR|=0x40是因为要选择模式和允许接收使能。除此之外都跟上个接收程序一样
三.程序
主函数
#include <ioCC2530.h>
#include "Uart.h"
#define uint unsigned int
#define uchar unsigned char
#define LED1 P1_0 //P1_0连接LED1
#define LED2 P1_1 //P1_1连接LED2
#define LED3 P1_4 //P1_4连接LED3
//函数声明
void Send_ReceicedData(void);
void Initial_IO(void);
void Delayms(uint xms);
//定义待发送的数据 char Txdata0[]={"欢迎您\n"}; //存放"欢迎您"
char Txdata1[]={"This is a Uart test.\n"}; char Txdata2[]={"The data CC2530 has received is: "}; char Txdata3[]={"\n"};
char Rxdata = '\0';
/*********** 函 数 名 : main 功能描述 : USART测试(USART0接收电脑发过来的数据) 输入参数 : NONE 输出参数 : NONE 返 回 值 : NONE **********/ void main(void)
{
SetSysClock(); //设置系统时钟为32MHz
InitUART(); //调用初始化函数
Initial_IO(); //调用初始化函数
UartSend_String(Txdata0,sizeof("秉火团队欢迎您\n"));
Delayms(500);
UartSend_String(Txdata1,sizeof("This is a Uart test.\n"));
while(1) { Send_ReceicedData(); } }
/******************** 函 数 名 : UART0_ISR 功能描述 : USART接收中断 输入参数 : NONE 输出参数 : Rxdata 返 回 值 : NONE **********/
#pragma vector = URX0_VECTOR
__interrupt void UART0_ISR(void)
{ Rxdata = U0DBUF; //接收数据并存于Rxdata
URX0IF = 0; // 清中断标志 }
/************************* 函 数 名 : Send_ReceicedData 功能描述 : 将接收到的数据发回给电脑 输入参数 : NONE 输出参数 : NONE 返 回 值 : NONE **************/
void Send_ReceicedData(void)
{
if ( Rxdata != '\0' )
{
UartSend_String(Txdata2,sizeof("The data CC2530 has received is: ")); //提示接收到数据
Send_char(Rxdata);
UartSend_String(Txdata3,sizeof("\n"));
if (Rxdata=='1')
{ LED1=0; Delayms(500); LED1=1; }
if (Rxdata=='2') { LED2=0; Delayms(500); LED2=1; }
if (Rxdata=='3') { LED3=0; Delayms(500); LED3=1; }
Rxdata='\0'; }
}
/****************** 函 数 名 : Initial_IO 功能描述 : IO初始化函数 输入参数 : NONE 输出参数 : NONE 返 回 值 : NONE ****************/
void Initial_IO(void)
{ P1DIR |= 0xff; //P1为输出
P1=0x1f; //P1_4口为高,让LED3灯指示工作状态 }
/************** 函 数 名 : Delayms 功能描述 : 毫秒延时 输入参数 : xms:延时时间,如 i=xms 即延时i/2毫秒 输出参数 : none 返 回 值 : none ************/ void Delayms(uint xms) { uint i,j; for(i=xms;i>0;i--) for(j=587;j>0;j--); }
Uart.c
#include "Uart.h"
#define uint unsigned int
#define uchar unsigned char
/****************** 函 数 名 : InitUART 功能描述 : CC2530串口初始化配置 输入参数 : NONE 输出参数 : NONE 返 回 值 : NONE ******************/ void InitUART(void)
{
PERCFG = 0x00; //位置1 P0口
P0SEL = 0x0c; //P0_2,P0_3 用作串口,第二功能
P2DIR &= ~0XC0; //P0 优先作为UART0 ,优先级
U0CSR |= 0x80; //UART 方式
U0GCR |= 11; //U0GCR与U0BAUD配合
U0BAUD |= 216; //波特率设为115200
UTX0IF = 0; //UART0 TX 中断标志清0
U0CSR |= 0X40; //允许接收 URX0IE=1; //接收中断
EA=1; //开总中断, }
/************************************************** 函 数 名 : Send_char 功能描述 : 串口向电脑发送字节 输入参数 : c---所要发送的数据 输出参数 : NONE 返 回 值 : NONE ***************************************************/
void Send_char(uchar c)
{ U0DBUF = c;
while(UTX0IF == 0); //发送完成标志位
UTX0IF = 0; }
/******************************** 函 数 名 : UartSend_String 功能描述 : 串口向电脑发送字符串 输入参数 : *Data---字符串首地址 len---字符串长度 输出参数 : NONE 返 回 值 : NONE ******************************/
void UartSend_String(char *Data,int len)
{ int j;
for(j=0;j<len;j++)
{ Send_char(*Data++); } }
/********************** 函 数 名 : SetSysClock 功能描述 : 设置系统时钟为32MHz 输入参数 : NONE 输出参数 : NONE 返 回 值 : NONE ******************/
void SetSysClock(void)
{
CLKCONCMD &= ~0x40; //选择系统时钟源为外部32MHz crystal oscillator
while(CLKCONSTA & 0x40); //等待晶振稳定为32MHz
CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ
}
四.总结
Send_ReceicedData(void)这个函数,用来根据接收的字符来作出反应,让对应的灯亮,需要注意的是使用了串口中断,所以我们要把相关的串口中断打开,并在中断函数里清除中断标志位。