#include<reg52.h>
#include<usually.h>
#include<intrins.h>
/****************************************************************/
/*====================无线模块引脚定义==========================*/
/****************************************************************/
sbit CE=P1^2; //芯片的模式控制线。在CSN为低的情况下,CE协同NRF24L01的CONFIG寄NRF24L01的状态。
sbit SCK=P1^1; // SPI时钟信号线
sbit MISO=P1^0; //数据线,(主入从出)
sbit CSN=P1^3; //芯片的片选线,CSN置低时芯片工作
sbit MOSI=P1^4; //数据线,(主出从入)
sbit IRQ=P1^5; //中断信号
//无线模块初始化
void init_nRF24L01(void)
{
IRQ=1; //
CSN=1; //芯片不工作
SCK=0; //拉低SPI时钟线,方便以后操作
CE=0; //
}
// nRF24L01 写一个字节
uint8 nRF24L01_write_byte(uint8 x)
{
uint8 a;
for(a=0;a<8;a++)
{
MOSI=(x&0x80);
x=x<<1;
SCK=1;
x|=MISO;
SCK=0;
}
return x;
}
//nRF24L01 写寄存器的值
//先写寄存器地址,再写要写入的寄存器值
void nRF24L01write_register(uint8 x,uint8 y)
{
uint8 a;
CSN=0;
a=nRF24L01_write_byte(x);
nRF24L01_write_byte(y);
CSN=1;
}
//nRF24L01 向NRF24L01中写入类似地址 等的数据
void nRF24L01write_data(uint8 x,uint8 *y,uint8 z)
{
uint8 a;
CSN=0;
nRF24L01_write_byte(x);
for(a=0;a<z;a++)
nRF24L01_write_byte(*(y++));
CSN=1;
}
//nRF24L01 读一个字节
uint8 nRF24L01read_byte(uint8 x)
{
uint8 y;
CSN=0; //使能SPI
nRF24L01_write_byte(x);
y=nRF24L01_write_byte(0);
CSN=1;
return y; //disable SPI
}
//nRF24L01 读数据,读出RX_BUF中的数据
uint8 nRF24L01read_data(uint8 x,uint8 *y,uint8 z)
{
uint8 a;
CSN=0;
nRF24L01_write_byte(x);
for(a=0;a<z;a++)
{
y[a]=nRF24L01_write_byte(0);
}
CSN=1;
return a;
}
/*********************************************/
/*========nRF24L01寄存器申明=================*/
/*********************************************/
#define RX_ADDR_P0 0X0A //通道地址设置寄存器
#define RX_ADDR_P1 0X0B
#define RX_ADDR_P2 0X0C
#define RX_ADDR_P3 0X0D
#define EN_AA 0X01 //AUTO ACKNOWLEDGEMENT RESIGTER ENABLE
#define EN_RXADDR 0X02 //RX ADDRES ENABLE
#define RF_CH 0X05 //RF CHANNEL
#define RX_PW_P0 0X11 //Number of bytes in RX payload in data pipe
#define RX_PW_P1 0X12
#define RX_PW_P2 0X13
#define RX_PW_P3 0X14
#define RF_SETUP 0X06 //RF Setup Register LIKE DATA RATE
#define CONFIG 0X00 //Configuration Register
#define W_register 0x20 //基地址
#define r_register 0x00
#define STATUS 0X07 //状态寄存器
#define R_RX_PLOAD 0X61 //READ RX-PAYLOAD
#define FLUSH_RX 0XE2 //清空接受寄存器指令
// //接收地址
uint8 code table[4][5]={{0x34,0x43,0x10,0x10,0x01},
{0xb3,0xb4,0xb5,0xb6,0xe1},
{0xb3,0xb4,0xb5,0xb6,0xe2},
{0xb3,0xb4,0xb5,0xb6,0xe3}};
//通道地址寄存器
uint8 code RX_ADDR[]={RX_ADDR_P0,RX_ADDR_P1,RX_ADDR_P2,RX_ADDR_P3};
//通道自动应答寄存器设置值
uint8 code autoack[]={0x01,0x02,0x04,0x08};
//接收通道载荷设置寄存器
uint8 code RX_PLOAD[]={RX_PW_P0,RX_PW_P1,RX_PW_P2,RX_PW_P3};
//nRF24L01 接收模式设置设置
void nRF24L01_rx_mode_setup(uint8 y,uint8 *x,uint8 z,uint8 w)
{
CE=0; //配置寄存器时CE一定要拉低
nRF24L01write_data(W_register+y,x,5); //设置RX 节点的地址
nRF24L01write_register(W_register+EN_AA,0X01); //允许通道0自动应答
nRF24L01write_register(W_register+EN_RXADDR,z); //设置允许通道
nRF24L01write_register(W_register+RF_CH,0); //设置通信频率
nRF24L01write_register(W_register+w,4); //设置有效数据宽度 为8字节
nRF24L01write_register(W_register+RF_SETUP,0X07); //1 Mbps 0 dbm
nRF24L01write_register(W_register+CONFIG,0X0F); //中断不在IRQ引脚表现,ENBALE CRC,CRC 2 BYTES,上电,配置为接收模式
CE=1;
}
uint8 data1[4][4]; //接收后数据存放数组
uint8 bdata STA; //存放STATUS 的值
sbit RX_DR=STA^6; //接收标志
sbit led=P3^0; //LED申明
sbit buzzer=P2^3; //蜂鸣器申明
//nRF24L01数据接收,及数据接收后的处理
void nRF24L01receieve(uint8 *x)
{
STA=nRF24L01read_byte(r_register+STATUS);
if(RX_DR)
{
CE=0;
nRF24L01read_data(R_RX_PLOAD,x,4); //读出数据
nRF24L01write_register(W_register+STATUS,0XFF); //清除RX_DR
CSN=0; //ENBALE SPI
nRF24L01_write_byte(FLUSH_RX); //FLUSHE RX FIFO
CSN=1;
buzzer=0;
delayms(10);
buzzer=1;
}
}
/***************************************/
/*========12864引脚申明================*/
/***************************************/
sbit RS = P2^4; //H:DATA L:INSTRUCTION
sbit SID = P2^5; //H:READ L:WRITE
sbit SCLK= P2^6; //ENBALE SIGNAL
sbit PSB = P2^7; //H:PARALLEL MODE L:SERIAL MODE
uint8 code r[]={"0123456789.%"}; //数字显示
//12864 WRITE A BYTE(DATA OR INSTRUCTION)
void _12864_write(uint8 x,uint8 y)
{
if(x==0)
RS=0;
else
RS=1;
P0=y;
delayms(1);
SCLK=1;
delayms(1);
SCLK=0;
delayms(1);
}
//INITIALISE 12864
void _12864_init(void)
{
PSB=1; //PARALLEL MODE
SID=0; //WRITE
SCLK=0;
_12864_write(0,0x30); //基本指令集
_12864_write(0,0x0c); //整体显示开
_12864_write(0,0x01); //清除显示
}
/****************************************************/
/*==================按键声明========================*/
/****************************************************/
sbit key_fuction=P1^6;
sbit key_add =P3^6;
sbit key_minus =P3^5;
sbit key_affirm =P3^4;
//各个按键的标志
uint8 key_fuction_flag,key_add_flag,key_minus_flag,key_affirm_flag;
void keyscan(void)
{
if(key_fuction==0)
{
delayms(10);
if(key_fuction==0)
{
key_fuction_flag=1;
while(key_fuction==0);
}
}
if(key_add==0)
{
delayms(10);
if(key_add==0)
{
key_add_flag=1;
while(key_add==0);
}
}
if(key_minus==0)
{
delayms(10);
if(key_minus==0)
{
key_minus_flag=1;
while(key_minus==0);
}
}
if(key_affirm==0)
{
delayms(10);
if(key_affirm==0)
{
key_affirm_flag=1;
while(key_affirm==0);
}
}
}
uint8 code welcome1[]={"温度湿度"};
uint8 code welcome2[]={"无线测量系统"};
uint8 code display_mode_setup[]={"模式设置"};
uint8 code display_mode1[]={"模式一"};
uint8 code display_mode2[]={"模式二"};
uint8 code ensure[]={"确认"};
uint8 code sec[]={"退出"};
uint8 display_mode_flag,cursor_position_flag,_flag; //显示模式标志
uint8 code cursor_position[2][2]={{0x92,0x93},{0x95,0x96}};
uint8 code temperture[4][16]={{"Temp A"},{"Temp B"},{"Temp C"},{"Temp D"}};
uint8 code humidity[4][5]={{"Hum A"},{"Hum B"},{"Hum C"},{"Hum D"}};
uint8 _flag0,_flag1,_flag2,_flag3;
/**********************************************/
/*====显示模式一——显示四点的温度值或者湿度值==*/
/**********************************************/
void display_mode1_display(uint8 *x,uint8 *y,uint8 *z,uint8 *w)
{
uint8 a;
_12864_write(0,0x01);
_12864_write(0,0x80);
for(a=0;a<6;a++)
_12864_write(1,x[a]);
_12864_write(0,0x90);
for(a=0;a<6;a++)
_12864_write(1,y[a]);
_12864_write(0,0x88);
for(a=0;a<6;a++)
_12864_write(1,z[a]);
_12864_write(0,0x98);
for(a=0;a<6;a++)
_12864_write(1,w[a]);
}
void display_mode1_display2(uint8 *x,uint8 *y,uint8 *z,uint8 *w)
{
uint8 a;
_12864_write(0,0x01);
_12864_write(0,0x80);
for(a=0;a<5;a++)
_12864_write(1,x[a]);
_12864_write(0,0x90);
for(a=0;a<5;a++)
_12864_write(1,y[a]);
_12864_write(0,0x88);
for(a=0;a<5;a++)
_12864_write(1,z[a]);
_12864_write(0,0x98);
for(a=0;a<5;a++)
_12864_write(1,w[a]);
}
/*********************************************************/
/*=========显示模式二——显示一点的温湿度值================*/
/*********************************************************/
void display_mode2_display(uint8 *x,uint8 *y)
{
uint8 a;
_12864_write(0,0x01);
_12864_write(0,0x80);
for(a=0;a<6;a++)
{
_12864_write(1,x[a]);
}
_12864_write(0,0x90);
fo