#include <reg52.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
//----------------------------------------------------------------------------------------------------------------
#define uint unsigned int
#define uchar unsigned char
//----------------------------------------------------------------------------------------------------------------
#define BYTE_BIT0 0x01
#define BYTE_BIT7 0x80
//----------------------------------------------------------------------------------------------------------------
bdata unsigned char DATA_BUF;
#define DATA7 ((DATA_BUF&BYTE_BIT7) != 0)
#define DATA0 ((DATA_BUF&BYTE_BIT0) != 0)
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
//---------------------------------------------------发送数据缓冲区-------------------------------------------------
#define TxRxBuf_Len 4
unsigned char TxRxBuf[TxRxBuf_Len]={0x29,0x30,0x31,0x32,};
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
char tf;
uchar H_temp=40;
uint L_temp=3000;
uchar set_flag=0;
bit beep_flag=0;
bit beep1=0;
//----------------------------------------NRF905工作模式控制端口------------------------------------------------------
sbit TXEN=P1^4;
sbit TRX_CE=P3^2;
sbit PWR=P1^3;
//----------------------------------------LED显示端口---------------------------------------------------
//sbit LED=P3^5;
//----------------------------------------NRF905 数据交换端口---------------------------------------------------
sbit MISO=P1^6;
sbit MOSI=P1^1;
sbit SCK=P1^5;
sbit CSN=P1^0;
//----------------------------------------nrf905状态端口---------------------------------------------------------
sbit AM=P1^7;
sbit DR=P3^3;
sbit CD=P1^2;
//---------------------------------------------------------------------------------------------------------------
//----------------------------------------按键端口-------------------------------------------------------
sbit KEY1=P3^5;
sbit KEY2=P3^6;
sbit KEY3=P3^7;
sbit BEEP=P3^4;
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2;
//sbit LED4=P2^3;
//----------------------------------------nrf905控制指令-------------------------------------------
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
//------------------------------------------------NRF905寄存器配置------------------------------------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在430MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为4字节
0xCC,0xCC,0xCC,0xCC, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
//================================================延时===========================================================
void nrf905_Delay(int n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
//=================================================SPI读函数=======================================================
unsigned char SpiRead(void)
{
unsigned char j;
for (j=0;j<8;j++)
{
DATA_BUF=DATA_BUF<<1;
SCK=1;
if (MISO) //读取最高位,保存至最末尾,通过左移位完成整个字节
{
DATA_BUF|=BYTE_BIT0;
}
else
{
DATA_BUF&=~BYTE_BIT0;
}
SCK=0;
}
return DATA_BUF;
}
//===========================================SPI写函数===============================================================
void SpiWrite(unsigned char send)
{
unsigned char i;
DATA_BUF=send;
for (i=0;i<8;i++)
{
if (DATA7) //总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
DATA_BUF=DATA_BUF<<1;
SCK=0;
}
}
//------------------------------------------------------初始化nRF905---------------------------------------------
void nRF905Init(void)
{
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
DR=1; // Init DR for input
AM=1; // Init AM for input
CD=1; // Init CD for input
PWR=1; // nRF905 power on
TRX_CE=0; // Set nRF905 in standby mode
TXEN=0; // set radio in Rx mode
}
//-----------------------------------------------------初始化寄存器-----------------------------------------------
void Config905(void)
{
uchar i;
CSN=0; // Spi enable for write a spi command
//SpiWrite(WC); // Write config command写放配置命令
for (i=0;i<11;i++) // Write configration words 写放配置字
{
SpiWrite(RFConf[i]);
}
CSN=1; // Disable Spi
}
//-----------------------------------------------------发送数据打包---------------------------------------------------
void TxPacket(uchar *TxRxBuf)
{
uchar i;
//Config905();
CSN=0;
SpiWrite(WTP); // Write payload command
for (i=0;i<4;i++)
{
SpiWrite(TxRxBuf[i]); // Write 32 bytes Tx data
}
CSN=1;
nrf905_Delay(1); // Spi disable
CSN=0; // Spi enable for write a spi command
SpiWrite(WTA); // Write address command
for (i=0;i<4;i++) // 写入与对方地址一样的地址
{
SpiWrite(TxAddress[i]);
}
CSN=1; // Spi disable
TRX_CE=1; // Set TRX_CE high,start Tx data transmission
nrf905_Delay(1); // while (DR!=1);
TRX_CE=0; // Set TRX_CE low
}
//----------------------------------------------设置发送初始状态---------------------------------------------
void SetTxMode(void)
{
TRX_CE=0;
TXEN=1;
nrf905_Delay(1); // nrf905_Delay for mode change(>=650us)
}
//-----------------------------------------------设置接收初始化---------------------------------------------------
void SetRxMode(void)
{
TXEN=0;
TRX_CE=1;
nrf905_Delay(1); // nrf905_Delay for mode change(>=650us)
}
//-------------------------------------------------判断数据接收状态-----------------------------------------------------
unsigned char CheckDR(void) //检查是否有新数据传入 Data Ready
{
DR=1;
//通过对端口写1,可以使端口为输入状态,这51的 特性。不熟悉者可以参阅51相关书籍作证(将DR端口设置为输入状态。)
if (DR==1)
{
DR=0;
return 1;
}
else
{
return 0;
}
}
//----------------------------------------------------读NRF905接收数据------------------------------------------------------------
void RxPacket(void)
{
uchar i;
nrf905_Delay(1);
// TRX_CE=0; // Set nRF905 in standby mode
nrf905_Delay(100);
TRX_CE=0;
CSN=0; // Spi enable for write a spi command
nrf905_Delay(1);
SpiWrite(RRP);
for (i = 0 ;i < 4 ;i++)
{
TxRxBuf[i]=SpiRead(); // Read data and save to buffer
}
CSN=1;
nrf905_Delay(10);
TRX_CE=1;
}
//--------------------------------------------------------数据接收------------------------------------------------
#include"LCD1602.H"
void RX(void)
{
SetRxMode();
//while (CheckDR()==0); //
nrf905_Delay(10);
RxPacket();
if(TxRxBuf[0]==0x29)
{
display();
TxRxBuf[0]=0;
}
}
void key()
{
uchar i;
if(KEY1==0)
{
nrf905_Delay(8);
if(KEY1==0)
{
set_flag++;
if(set_flag>2)
{
WRITE_LCD1602_COM(0x80);
for(i=0;i<16;i++)
WRITE_LCD1602_DAT(DIS_TAB1[i]);
WRITE_LCD1602_COM(0x80+0x40);
for(i=0;i<16;i++)
WRITE_LCD1602_DAT(DIS_TAB2[i]);
set_flag=0;
}
else if(set_flag==1)
{
WRITE_LCD1602_COM(0x80);
for(i=0;i<16;i++)
WRITE_LCD1602_DAT(DIS_TAB3[i]);
WRITE_LCD1602_COM(0x80+0x40);
for(i=0;i<16;i++)
WRITE_LCD1602_DAT(DIS_TAB4[i]);
}
while(!KEY1);
}
}
switch(set_flag)
{
case 1:
if(KEY2==0)
{
nrf905_Delay(8);
if(KEY2==0)
{
if(H_temp<99)
H_temp++;
while(!KEY2);
}
}
else if(KEY3==0)
{
nrf905_Delay(8);
if(KEY3==0)
{
if(H_temp>0)
H_temp--;
while(!KEY3);
}
>行者<
- 粉丝: 247
- 资源: 135
最新资源
- Centos7.x通过RPM包升级OpenSSH9.6最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos7.x通过RPM包升级OpenSSH9.9最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos7.x通过RPM包升级OpenSSH9.8最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos7.x通过RPM包升级OpenSSH9.7最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- 机器人开发的操作案例练习
- Centos6.x通过RPM包升级OpenSSH9.7最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos6.x通过RPM包升级OpenSSH9.8最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- Centos6.x通过RPM包升级OpenSSH9.9最新版 升级有风险,前务必做好快照,以免升级后出现异常影响业务
- 软考冲刺的基本内容和操作
- Centos8.x通过RPM包升级OpenSSH9.8(openssl-3.0) 升级有风险,前务必做好快照,以免升级后出现异常影响业务
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈