利用basic RF库实现串口通信

1导入库

2编写调用库

主函数C代码

#include "hal_defs.h"        //硬件抽象宏定义 
#include "hal_cc8051.h"      //8051硬件抽象层 
#include "hal_int.h"         //抽象层中断控制文件 
#include "hal_mcu.h"         //mcu库 
#include "hal_board.h"       //板库 
#include "hal_led.h"         //led 
#include "hal_rf.h"          //rf
#include "basic_rf.h"        //基本rf库 
#include "hal_uart.h"        //uart 
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
uint8 uRxdata [128];//Rx缓冲
uint8 uTxdata [128];//Tx缓冲
int uRxlen; 
int r_UartLen;
void MyByteCopy(uint8 *dst, int dststart, uint8 *src, int srcstart, int len)//取出数据
{
    int i;
    for(i=0; i<len; i++)
    {
        *(dst+dststart+i)=*(src+srcstart+i);//实现数组上的数据交换
    }
}
uint16 RecvUartData(void)
{   
    uint16 r_UartLen = 0;
    uint8 r_UartBuf[128]; 
    uRxlen=0; 
    r_UartLen = halUartRxLen();//获取Rx的长度
    while(r_UartLen > 0)
    {
        r_UartLen = halUartRead(r_UartBuf, sizeof(r_UartBuf));//取出串口的数据
        MyByteCopy(uRxdata, uRxlen, r_UartBuf, 0, r_UartLen);//将数据给Tx
        uRxlen += r_UartLen;//获取rx的长度给Tx     且Rx+1
        halMcuWaitMs(5);   //这里的延迟非常重要,因为这是串口连续读取数据时候需要有一定的时间间隔
        r_UartLen = halUartRxLen();  
    }   
    return uRxlen;
}
int main(void){
    halBoardInit();//板初始化
    halLedSet(1);//led灯1
    halLedSet(2);//led灯2
    
    while(1){
        int len=0;
        len=RecvUartData();//看Rx是否有数据
        if(len>0){
          halUartWrite(uRxdata,len);//串口输出
            //len=0;
        }
    }
    
}

小白的一天的成果感谢支持

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值