144 lines
2.7 KiB
C
144 lines
2.7 KiB
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>
|
|
/*****点对点通讯地址设置****A**/
|
|
#define RF_CHANNEL 25 // 频道 11~26
|
|
#define PAN_ID 0xFF //网络id
|
|
#define MY_ADDR 0xA1 //本机模块地址
|
|
#define SEND_ADDR 0xA2 //发送地址
|
|
|
|
/**
|
|
* 无线通信实验-A板
|
|
*
|
|
*/
|
|
//LED & SW
|
|
#define LED1 P1_0
|
|
#define LED2 P1_1
|
|
#define LED3 P1_3
|
|
#define LED4 P1_4
|
|
#define SW P1_2
|
|
|
|
//长按标志
|
|
uint8 Long_click = 0;
|
|
//返回按键状态
|
|
uint8 key_down_status = 0;
|
|
//发送数据
|
|
uint8 send_data = 0x00;
|
|
//接收数据
|
|
uint8 recv_data = 0x00;
|
|
//长按计数
|
|
unsigned int time = 0;
|
|
//跑马标志位
|
|
unsigned char sign = 0;
|
|
|
|
|
|
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();
|
|
}
|
|
void initial_gpio(){
|
|
|
|
P1SEL &=~0X1F;
|
|
P1DIR |=0X1B;
|
|
P1DIR &=~0X04;
|
|
P1INP &=~0X04;
|
|
P2INP &=~0X40;
|
|
P1 &=~0X1B;
|
|
}
|
|
|
|
uint8 key_down(){
|
|
//判断是是否长按
|
|
if(Long_click == 1){
|
|
//松开返回
|
|
if(SW){
|
|
Long_click = 0;
|
|
return 11;
|
|
}
|
|
}
|
|
if(SW == 0){
|
|
halMcuWaitUs(20);
|
|
if(SW == 0){
|
|
time = 0;
|
|
while(!SW){
|
|
halMcuWaitMs(1);
|
|
time++;
|
|
if(time>1000){
|
|
Long_click = 1;
|
|
return 22;
|
|
}
|
|
}
|
|
return 33;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
//跑马灯
|
|
void light(){
|
|
LED2 = 1;
|
|
halMcuWaitMs(500);
|
|
LED1 = 1;
|
|
halMcuWaitMs(500);
|
|
LED4 = 1;
|
|
halMcuWaitMs(500);
|
|
LED3 = 1;
|
|
halMcuWaitMs(500);
|
|
LED2 = 0;
|
|
halMcuWaitMs(500);
|
|
LED1 = 0;
|
|
halMcuWaitMs(500);
|
|
LED4 = 0;
|
|
halMcuWaitMs(500);
|
|
LED3 = 0;
|
|
halMcuWaitMs(500);
|
|
}
|
|
/********************MAIN************************/
|
|
void main(void)
|
|
{
|
|
halBoardInit();//选手不得在此函数内添加代码
|
|
ConfigRf_Init();//选手不得在此函数内添加代码
|
|
initial_gpio();
|
|
while(1)
|
|
{
|
|
key_down_status = key_down();
|
|
if(key_down_status == 22){
|
|
|
|
//发送呼吸开始数据
|
|
send_data = 0x1F;
|
|
basicRfSendPacket(SEND_ADDR,&send_data,1);
|
|
}
|
|
if(key_down_status == 11){
|
|
|
|
//发送呼吸结束数据
|
|
send_data = 0x2F;
|
|
basicRfSendPacket(SEND_ADDR,&send_data,1);
|
|
}
|
|
//接收判断
|
|
if(basicRfPacketIsReady()){
|
|
basicRfReceive(&recv_data,1,NULL);
|
|
//接收跑马数据
|
|
if(recv_data == 0x1D){
|
|
sign = !sign;
|
|
}
|
|
}
|
|
if(sign){
|
|
light();
|
|
}
|
|
}
|
|
} |