串口通信需要三个函数
// Initialize UART at the startup
//-------------------------------------------------------------------
void halUartInit(uint32 baud);
//-------------------------------------------------------------------
// Read a buffer from the UART
//-------------------------------------------------------------------
extern uint16 halUartRead(uint8 *pBuffer, uint16 length);
//-------------------------------------------------------------------
// Write a buff to the uart
//-------------------------------------------------------------------
extern uint16 halUartWrite(uint8 *pBuffer, uint16 length);
代码
#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_int.h"
#include "hal_mcu.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include "hal_uart.h"
#include "sensor_drv/sensor.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
/*****点对点通讯地址设置******/
#define RF_CHANNEL 11 // 频道 11~26
#define PAN_ID 0x1111 //网络id
#define MY_ADDR 0xAAAA //本机模块地址
#define SEND_ADDR 0xBBBB //发送地址
/**************************************************/
#define LED1 P1_0
#define LED2 P1_1
#define SW1 P1_2
static basicRfCfg_t basicRfConfig;
// 无线RF初始化
void ConfigRf_Init(void)
{
basicRfConfig.panId = PAN_ID;
basicRfConfig.channel = RF_CHANNEL;
basicRfConfig.myAddr = MY_ADDR;
basicRfConfig.ackRequest = TRUE;
while(basicRfInit(&basicRfConfig) == FAILED);
basicRfReceiveOn();
}
/********************MAIN************************/
void main(void)
{
halBoardInit();//选手不得在此函数内添加代码
ConfigRf_Init();//选手不得在此函数内添加代码
halUartInit(57600);
P1DIR |= 0X03;
LED2 = 0;
LED1 = 1;
halMcuWaitMs(3000);
LED1 = 0;
uint8 buf[1];
while(1)
{
/* user code start */
if(halUartRead(buf,1)){ //如果有数据,把数据读到buf里面
if(buf[0]==0xE1){
LED1 = 1;
halUartWrite("The LED1 is Open!\n",strlen("The LED1 is Open!\n"));
}
if(buf[0]==0xE2){
LED1 = 0;
halUartWrite("The LED1 is Closed!\n",strlen("The LED1 is Closed!\n"));
}
if(buf[0]==0xF1){
LED2 = 1;
halUartWrite("The LED2 is Open!\n",strlen("The LED2 is Open!\n"));
}
if(buf[0]==0xF2){
LED2 = 0;
halUartWrite("The LED2 is Closed!\n",strlen("The LED1 is Closed!\n"));
}
}
/* user code end */
}
}