串口0中断方式异步接收数据

串口0中断方式异步接收数据

#include "ioCC2530.h"
#include "string.h"
#define LED1  P1_0
#define LED2  P1_1
#define LED3  P1_4
#define RXBUFSIZE 50

void delay(unsigned x)
{
  for(unsigned i=x;i>0;i--)
    for(unsigned char j=240;j>0;j--);
}

void board_init()
{
  CLKCONCMD &= 0x80;
  
  P1SEL &= ~0x13;
  P1DIR |= 0x13;
  P1 |= 0x13;
  
  U0CSR |= 0x80;  //串口0选用异步通信模式
  P0SEL |= 0x3c;  //设置对应引脚为外设口
  U0BAUD = 216;
  U0GCR = 10;     //设置波特率57600
  U0UCR |= 0x80;  //无流控、无奇偶校验、8位数据位、1位停止位等参数设置
  URX0IF = 0;     //清除串口接收中断标志位
  U0CSR |= 0x40;  //允许接收数据(该语句要放在串口设置完成后,经常会被忽略)
  IEN0 |= 0x84;   //使能中断
}

unsigned char rxbuf[RXBUFSIZE],rxlen=0;
unsigned char *head=rxbuf, *tail=rxbuf;
unsigned char cmp[4]={0};
void main()
{
  board_init();
  while(1)
  {
    if(rxlen>0)
    {
      delay(200);
      rxlen=0;
      unsigned char u=0;
      while(head!=tail)
      {
        cmp[u++] = *head++;
        if(head==rxbuf+RXBUFSIZE)
          head = rxbuf;
      }
      if(strcmp(cmp,"#10")==0)LED1=1;
      else if(strcmp(cmp,"#11")==0)LED1=0;
      else if(strcmp(cmp,"#20")==0)LED2=1;
      else if(strcmp(cmp,"#21")==0)LED2=0;
      else if(strcmp(cmp,"#30")==0)LED3=1;
      else if(strcmp(cmp,"#31")==0)LED3=0;
      else LED1=LED2=LED3=1;
    }
  }
}

#pragma vector = URX0_VECTOR
__interrupt void urx0_proc(void)
{
  URX0IF = 0;
  *tail++ = U0DBUF;
  rxlen++;
  if(tail == rxbuf+RXBUFSIZE)
    tail = rxbuf;
}

后续利用PC机,通过串口助手向单片机发送字符串

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值