CC2530开发试题(一)
试题
注意
分为两个项目便于管理(当然如果开发经验颇深,不分的话小试题也不会乱)
基于BasicRF无线点对点框架简单完成试题
主要部分
第一个项目ZigbeeA
rf_set.c
#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 20 // 频道 11~26
#define PAN_ID 0x1234 //网络id
#define MY_ADDR 0x1111 //本机模块地址
#define SEND_ADDR 0x2222 //发送地址
/**************************************************/
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();//选手不得在此函数内添加代码
uint8 key_recv, key_send;
P1SEL &= ~0xFF;
P1DIR |= 0xFF;
P1DIR &= ~0x04;
P1 = 0xFF;
uint8 horseRaceFG = 0, horseRaceCount = 0;
uint8 ledArray[4] = {0x02, 0x01, 0x10, 0x08};
while(1)
{
/* user code start */
int count = 0;
if(P1_2 == 0)
{
halMcuWaitMs(10);
if(P1_2 == 0)
{
while(P1_2 == 0);
for(int time = 500; time > 0; time--)
{
halMcuWaitMs(1);
if(P1_2 == 0)
{
halMcuWaitMs(10);
if(P1_2 == 0)
{
count = 2;
//P1_2 = 1; //断开,此处很重要 //因无法直接控制高低电压,所以,只能加循环,
while(P1_2 == 0);
break;
}
}
if(time == 1) count = 1;
}
}
}
else count = 0;
if(count == 1)
{
key_send = 0xD4;
basicRfSendPacket(SEND_ADDR, &key_send, 1);
}
else if(count == 2)
{
key_send = 0xD5;
basicRfSendPacket(SEND_ADDR, &key_send, 1);
}
if(basicRfPacketIsReady())
{
basicRfReceive(&key_recv, 1, NULL);
if(key_recv == 0xDD)
{
horseRaceFG = horseRaceFG == 1 ? 0 : 1;
}
}
if(horseRaceFG == 1)
{
P1 = ledArray[horseRaceCount];
halMcuWaitMs(380);
if(horseRaceCount < 4) ++horseRaceCount;
else horseRaceCount = 0;
}
else P1 = 0;
/* user code end */
}
}
第二个项目ZigbeeB
rf_set.c
#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 20 // 频道 11~26
#define PAN_ID 0x1234 //网络id
#define MY_ADDR 0x2222 //本机模块地址
#define SEND_ADDR 0x1111 //发送地址
/**************************************************/
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();//选手不得在此函数内添加代码
uint8 key_recv, key_send;
P1SEL &= ~0xFF;
P1DIR |= 0xFF;
P1DIR &= ~0x04;
P1 = 0xFF;
uint8 D5FG = 0;
while(1)
{
/* user code start */
if(basicRfPacketIsReady())
{
basicRfReceive(&key_recv, 1, NULL);
if(key_recv == 0xD4) P1_1 = ~P1_1;
else if(key_recv == 0xD5) D5FG = D5FG == 1 ? 0 : 1;
}
if(P1_2 == 0)
{
halMcuWaitMs(10);
if(P1_2 == 0)
{
key_send = 0xDD;
basicRfSendPacket(SEND_ADDR, &key_send, 1);
while(P1_2 == 0);
basicRfSendPacket(SEND_ADDR, &key_send, 1);
}
}
if(D5FG == 1)
{
int32 pwm_cycle = 1000, pwm = 0;
for(pwm = 0; pwm < pwm_cycle; pwm++)
{
P1_3 = 0;
halMcuWaitUs(pwm);
P1_3 = 1;
halMcuWaitUs(pwm_cycle - pwm);
}
for(pwm = pwm_cycle - 1; pwm > 0; pwm--)
{
P1_3 = 0;
halMcuWaitUs(pwm);
P1_3 = 1;
halMcuWaitUs(pwm_cycle - pwm);
}
}
/* user code end */
}
}
视频演示
点击打开
自此本文告一段落。。。