无线广告屏——无线模块
NRF24L01无线模块原理图:
无线NRF24L01例程:
NRF24L01发送模块:
Allhead.h
#ifndef _ALLHEAD_H_
#define _ALLHEAD_H_
/******************************************/
#include"pubile.h"
#include"Delay.h"
#include"NRF24L01.h"
#include"DS18N02.h"
/*****************************************/
#endif
Delay.c
#include"reg52.h"
#include"Allhead.h"
/***************************延时函数****************/
void Delay(unint t)
{
unintx,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
/**********************************************
DS18B20专用延时子函数
**********************************************/
void DDelay(unint t)
{
unint n;
n=0;
while(n<t)
{
n++;
}
}
Delay.h
#ifndef _DELAY_H_
#define _DELAY_H_
/*****************************************/
void Delay(unint t);
void DDelay(unint t);
/*******************************************/
#endif
DS18B20.c
#include"reg52.h"
#include"allhead.h"
sbit DQ=P1^0;//数据端
unint tempL,tempH;
/*********************************************************
DS18B20复位函数
**********************************************************/
void Reset()
{
DQ=1;
DDelay(8);
DQ=0;
DDelay(85);
DQ=1;
DDelay(14);
}
/***********************************************************
DS18B20写一个字节函数
*********************************************************/
void WriteChar(unchar dat)
{
unint i;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
DDelay(5);
DQ=1;
dat>>=1;
}
DDelay(4);
}
/****************************************************
DS18B20读一个字节函数
****************************************************/
unint ReadChar()
{
unchar i,dat=0;
for (i=8;i>0;i--)
{
DQ=1;
DDelay(1);
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat=dat|0x80;
DDelay(4);
}
return(dat);
}
/***************************************************
DS18B20读温度程序
****************************************************/
unint ReadTemperature()
{
unint temperature;
Reset();
WriteChar(0xcc);
WriteChar(0x44);
DDelay(125);
Reset();
WriteChar(0xcc);
WriteChar(0xbe);
tempL=ReadChar();
tempH=ReadChar();
temperature=((tempH*256)+tempL)*0.0625*10;
DDelay(200);
return(temperature); //返回的是0123格式
}
DS18B20.h
#ifndef _DS18B20_H_
#define _DS18B20_H_
/*****************************************/
extern void Reset();
extern void WriteChar(unchar dat);
extern unint ReadChar();
extern unint ReadTemperature();
/*****************************************/
#endif
NRF14L01.c
#include"reg52.h"
#include"Allhead.h"
sbit CE=P2^0; //RX/TX模式选择端
sbit IRQ=P2^5; //可屏蔽中断端
sbit CSN=P2^3; //SPI片选端//就是SS
sbit MOSI=P2^4; //SPI主机输出从机输入端
sbit MISO=P2^2; //SPI主机输出从机输出端
sbit SCLK=P2^1; //SPI时钟端
unchar code TxAddr[]={0x34,0x43,0x10,0x10,0x01};//发送地址
/*****************状态标志*****************************************/
unchar bdata sta; //状态标志
sbit RX_DR=sta^6;
sbit TX_DS=sta^5;
sbit MAX_RT=sta^4;
/*****************SPI时序函数******************************************/
unchar NRFSPI(unchar date)
{
unchar i;
for(i=0;i<8;i++) // 循环8次
{
if(date&0x80)
MOSI=1;
else
MOSI=0; // byte最高位输出到MOSI
date<<=1; // 低一位移位到最高位
SCLK=1;
if(MISO) // 拉高SCK,nRF24L01从MOSI读入1位数据,同时从MISO输出1位数据
date|=0x01; // 读MISO到byte最低位
SCLK=0; // SCK置低
}
return(date); // 返回读出的一字节
}
/**********************NRF24L01初始化函数*******************************/
void NRF24L01Int()
{
Delay(2);//让系统什么都不干
CE=0; //待机模式1
CSN=1;
SCLK=0;
IRQ=1;
}
/*****************SPI读寄存器一字节函数*********************************/
unchar NRFReadReg(unchar RegAddr)
{
unchar BackDate;
CSN=0;//启动时序
NRFSPI(RegAddr);//写寄存器地址
BackDate=NRFSPI(0x00);//写入读寄存器指令
CSN=1;
return(BackDate); //返回状态
}
/*****************SPI写寄存器一字节函数*********************************/
unchar NRFWriteReg(unchar RegAddr,unchar date)
{
unchar BackDate;
CSN=0;//启动时序
BackDate=NRFSPI(RegAddr);//写入地址
NRFSPI(date);//写入值
CSN=1;
return(BackDate);
}
/*****************SPI读取RXFIFO寄存器的值********************************/
unchar NRFReadRxDate(unchar RegAddr,unchar *RxDate,unchar DateLen)
{ //寄存器地址//读取数据存放变量//读取数据长度//用于接收
unchar BackDate,i;
CSN=0;//启动时序
BackDate=NRFSPI(RegAddr);//写入要读取的寄存器地址
for(i=0;i<DateLen;i++) //读取数据
{
RxDate[i]=NRFSPI(0);
}
CSN=1;
return(BackDate);
}
/*****************SPI写入TXFIFO寄存器的值**********************************/
unchar NRFWriteTxDate(unchar RegAddr,unchar *TxDate,unchar DateLen)
{ //寄存器地址//写入数据存放变量//读取数据长度//用于发送
unchar BackDate,i;
CSN=0;
BackDate=NRFSPI(RegAddr);//写入要写入寄存器的地址
for(i=0;i<DateLen;i++)//写入数据
{
NRFSPI(*TxDate++);
}
CSN=1;
return(BackDate);
}
/*****************NRF设置为发送模式并发送数据******************************/
void NRFSetTxMode(unchar *TxDate)
{//发送模式
CE=0;
NRFWriteTxDate(W_REGISTER+TX_ADDR,TxAddr,TX_ADDR_WITDH);//写寄存器指令+接收地址使能指令+接收地址+地址宽度
NRFWriteTxDate(W_REGISTER+RX_ADDR_P0,TxAddr,TX_ADDR_WITDH);//为了应答接收设备,接收通道0地址和发送地址相同
NRFWriteTxDate(W_TX_PAYLOAD,TxDate,TX_DATA_WITDH);//写入数据
/******下面有关寄存器配置**************/
NRFWriteReg(W_REGISTER+EN_AA,0x01); // 使能接收通道0自动应答
NRFWriteReg(W_REGISTER+EN_RXADDR,0x01); // 使能接收通道0
NRFWriteReg(W_REGISTER+SETUP_RETR,0x0a); // 自动重发延时等待250us+86us,自动重发10次
NRFWriteReg(W_REGISTER+RF_CH,0x40); // 选择射频通道0x40
NRFWriteReg(W_REGISTER+RF_SETUP,0x07); // 数据传输率1Mbps,发射功率0dBm,低噪声放大器增益
NRFWriteReg(W_REGISTER+CONFIG,0x0e); // CRC使能,16位CRC校验,上电
CE=1;
Delay(5);//保持10us秒以上
}
/*****************NRF设置为接收模式并接收数据******************************/
//主要接收模式
void NRFSetRXMode()
{
CE=0;
NRFWriteTxDate(W_REGISTER+RX_ADDR_P0,TxAddr,TX_ADDR_WITDH); // 接收设备接收通道0使用和发送设备相同的发送地址
NRFWriteReg(W_REGISTER+EN_AA,0x01); // 使能接收通道0自动应答
NRFWriteReg(W_REGISTER+EN_RXADDR,0x01); // 使能接收通道0
NRFWriteReg(W_REGISTER+RF_CH,0x40); // 选择射频通道0x40
NRFWriteReg(W_REGISTER+RX_PW_P0,TX_DATA_WITDH); // 接收通道0选择和发送通道相同有效数据宽度
NRFWriteReg(W_REGISTER+RF_SETUP,0x07); // 数据传输率1Mbps,发射功率0dBm,低噪声放大器增益*/
NRFWriteReg(W_REGISTER+CONFIG,0x0f); // CRC使能,16位CRC校验,上电,接收模式
CE = 1;
Delay(5);//保持10us秒以上
}
/****************************检测应答信号******************************/
unchar CheckACK()
{ //用于发射
sta=NRFReadReg(R_REGISTER+STATUS); // 返回状态寄存器
if(TX_DS||MAX_RT) //发送完毕中断
{
NRFWriteReg(W_REGISTER+STATUS,0xff); // 清除TX_DS或MAX_RT中断标志
CSN=0;
NRFSPI(FLUSH_TX);//用于清空FIFO !!关键!!不然会出现意想不到的后果!!!大家记住!!
CSN=1;
return(0);
}
else
return(1);
}
/******************判断是否接收收到数据,接到就从RX取出*********************/
//用于接收模式
unchar NRFRevDate(unchar *RevDate)
{
unchar RevFlags=0;
sta=NRFReadReg(R_REGISTER+STATUS);//发送数据后读取状态寄存器
if(RX_DR) // 判断是否接收到数据
{
CE=0; //SPI使能
NRFReadRxDate(R_RX_PAYLOAD,RevDate,RX_DATA_WITDH);// 从RXFIFO读取数据
RevFlags=1; //读取数据完成标志
}
NRFWriteReg(W_REGISTER+STATUS,0xff); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标
return(RevFlags);
}
NRF24L01.h
#ifndef _NRF24L01_H_
#define _NRF24L01_H_
/*******************************************************/
#define TX_ADDR_WITDH 5 //发送地址宽度设置为5个字节
#define RX_ADDR_WITDH 5 //接收地址宽度设置为5个字节
#define TX_DATA_WITDH 4//发送数据宽度1个字节
#define RX_DATA_WITDH 4//接收数据宽度1个字节
/*******************命令寄存器***************************/
#define R_REGISTER 0x00//读取配置寄存器
#define W_REGISTER 0x20//写配置寄存器
#define R_RX_PAYLOAD 0x61//读取RX有效数据
#define W_TX_PAYLOAD 0xa0//写TX有效数据
#define FLUSH_TX 0xe1//清除TXFIFO寄存器
#define FLUSH_RX 0xe2//清除RXFIFO寄存器
#define REUSE_TX_PL 0xe3//重新使用上一包有效数据
#define NOP 0xff//空操作
/******************寄存器地址****************************/
#define CONFIG 0x00//配置寄存器
#define EN_AA 0x01//使能自动应答
#define EN_RXADDR 0x02//接收通道使能0-5个通道
#define SETUP_AW 0x03//设置数据通道地址宽度3-5
#define SETUP_RETR 0x04//建立自动重发
#define RF_CH 0x05//射频通道设置
#define RF_SETUP 0x06//射频寄存器
#define STATUS 0x07//状态寄存器
#define OBSERVE_TX 0x08//发送检测寄存器
#define CD 0x09//载波
#define RX_ADDR_P0 0x0a//数据通道0接收地址
#define RX_ADDR_P1 0x0b//数据通道1接收地址
#define RX_ADDR_P2 0x0c//数据通道2接收地址
#define RX_ADDR_P3 0x0d//数据通道3接收地址
#define RX_ADDR_P4 0x0e//数据通道4接收地址
#define RX_ADDR_P5 0x0f//数据通道5接收地址
#define TX_ADDR 0x10//发送地址
#define RX_PW_P0 0x11//P0通道数据宽度设置
#define RX_PW_P1 0x12//P1通道数据宽度设置
#define RX_PW_P2 0x13//P2通道数据宽度设置
#define RX_PW_P3 0x14//P3通道数据宽度设置
#define RX_PW_P4 0x15//P4通道数据宽度设置
#define RX_PW_P5 0x16//P5通道数据宽度设置
#define FIFO_STATUS 0x17//FIFO状态寄存器
/*******************相关函数声明**************************/
unchar NRFACK();
unchar NRFSPI(unchar date);
unchar NRFReadReg(unchar RegAddr);
unchar NRFWriteReg(unchar RegAddr,unchar date);
unchar NRFReadRxDate(unchar RegAddr,unchar *RxDate,unchar DateLen);
unchar NRFWriteTxDate(unchar RegAddr,unchar *TxDate,unchar DateLen);
unchar NRFRevDate(unchar *RevDate);
void NRFSetTxMode(unchar *TxDate);
void NRF24L01Int();
void NRFSetRXMode();
unchar CheckACK();
extern unchar bdata sta;
/*********************************************************/
#endif
public.h
#ifndef _PUBLIC_H_
#define _PUBLIC_H_
/****************************************/
#define unchar unsigned char
#define unint unsigned int
/*****************************************/
#endif
send.c
#include"reg52.h"
#include"Allhead.h"
/*********************主函数***************************/
sbit Key=P1^0;
void main()
{
unint ReadTempDate;//读取温度
char TxDate[4];
NRF24L01Int();
while(1)
{
ReadTempDate=169;
TxDate[0]=ReadTempDate/100+'0';
TxDate[1]=ReadTempDate%100/10+'0';
TxDate[2]='.';
TxDate[3]=ReadTempDate%100%10+'0';
NRFSetTxMode(TxDate);//发送温度
while(CheckACK()); //检测是否发送完毕
}
}
NRF24L01接收模块:
Allhead.h
#ifndef _ALLHEAD_H_
#define _ALLHEAD_H_
/*************************************************/
#include"pubile.h"
#include"DELAY.h"
//#include"LCD1602.h"
#include"NRF24L01.h"
#include"MAX232.h"
#include"LCD12864.h"
/*************************************************/
#endif
CODE.h
// ------------------ 汉字字模的数据结构定义 ------------------------ //
typedef struct typFNT_GB12 // 汉字字模数据结构
{
signed char Index[2]; // 汉字内码索引
char Msk[24]; // 点阵码数据
};
/
// 汉字字模表 //
// 汉字库: 宋体12.dot 纵向取模下高位,数据排列:从左到右从上到下 //
/
struct typFNT_GB12 code GB_12[] = // 数据表
{
"当", 0x00,0x20,0xA4,0xA8,0xA0,0xBF,0xA0,0xB0,
0xA8,0xA6,0xF0,0x20,0x00,0x04,0x04,0x04,
0x04,0x04,0x04,0x04,0x04,0x04,0x0F,0x00,
"前", 0x04,0xF4,0x54,0x55,0x56,0xF4,0x04,0xF6,
0x05,0x04,0xF6,0x04,0x00,0x0F,0x01,0x01,
0x09,0x0F,0x00,0x03,0x04,0x08,0x07,0x00,
"温", 0x88,0xD1,0x22,0x80,0xBF,0x95,0x95,0x95,
0x95,0xBF,0x80,0x00,0x00,0x0F,0x08,0x0F,
0x08,0x0F,0x08,0x08,0x0F,0x08,0x0F,0x08,
"度", 0x00,0x00,0xFE,0x0A,0x8A,0xBE,0xAB,0xAA,
0xBE,0x0A,0x0A,0x00,0x0C,0x02,0x09,0x08,
0x04,0x05,0x02,0x05,0x04,0x08,0x08,0x08
};
// 汉字表:
// 当前温度:
// ------------------ ASCII字模的数据表 ------------------------ //
// 码表从0x20~0x7e //
// 字库: E:\单片机专用\lcd汉字取模软件\Asc8X16E.dat 纵向取模下高位//
// -------------------------------------------------------------- //
unsigned char code nAsciiDot816[][16]= // ASCII
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // - -
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x38,0xFC,0xFC,0x38,0x00,0x00, // -!-
0x00,0x00,0x00,0x0D,0x0D,0x00,0x00,0x00,
0x00,0x0E,0x1E,0x00,0x00,0x1E,0x0E,0x00, // -"-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x20,0xF8,0xF8,0x20,0xF8,0xF8,0x20,0x00, // -#-
0x02,0x0F,0x0F,0x02,0x0F,0x0F,0x02,0x00,
0x38,0x7C,0x44,0x47,0x47,0xCC,0x98,0x00, // -$-
0x03,0x06,0x04,0x1C,0x1C,0x07,0x03,0x00,
0x30,0x30,0x00,0x80,0xC0,0x60,0x30,0x00, // -%-
0x0C,0x06,0x03,0x01,0x00,0x0C,0x0C,0x00,
0x80,0xD8,0x7C,0xE4,0xBC,0xD8,0x40,0x00, // -&-
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0x00,0x10,0x1E,0x0E,0x00,0x00,0x00,0x00, // -'-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xF0,0xF8,0x0C,0x04,0x00,0x00, // -(-
0x00,0x00,0x03,0x07,0x0C,0x08,0x00,0x00,
0x00,0x00,0x04,0x0C,0xF8,0xF0,0x00,0x00, // -)-
0x00,0x00,0x08,0x0C,0x07,0x03,0x00,0x00,
0x80,0xA0,0xE0,0xC0,0xC0,0xE0,0xA0,0x80, // -*-
0x00,0x02,0x03,0x01,0x01,0x03,0x02,0x00,
0x00,0x80,0x80,0xE0,0xE0,0x80,0x80,0x00, // -+-
0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // -,-
0x00,0x00,0x10,0x1E,0x0E,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00, // ---
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // -.-
0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0xC0,0x60,0x30,0x00, // -/-
0x0C,0x06,0x03,0x01,0x00,0x00,0x00,0x00,
0xF8,0xFC,0x04,0xC4,0x24,0xFC,0xF8,0x00, // -0-
0x07,0x0F,0x09,0x08,0x08,0x0F,0x07,0x00,
0x00,0x10,0x18,0xFC,0xFC,0x00,0x00,0x00, // -1-
0x00,0x08,0x08,0x0F,0x0F,0x08,0x08,0x00,
0x08,0x0C,0x84,0xC4,0x64,0x3C,0x18,0x00, // -2-
0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0C,0x00,
0x08,0x0C,0x44,0x44,0x44,0xFC,0xB8,0x00, // -3-
0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,
0xC0,0xE0,0xB0,0x98,0xFC,0xFC,0x80,0x00, // -4-
0x00,0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,
0x7C,0x7C,0x44,0x44,0xC4,0xC4,0x84,0x00, // -5-
0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,
0xF0,0xF8,0x4C,0x44,0x44,0xC0,0x80,0x00, // -6-
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x0C,0x0C,0x04,0x84,0xC4,0x7C,0x3C,0x00, // -7-
0x00,0x00,0x0F,0x0F,0x00,0x00,0x00,0x00,
0xB8,0xFC,0x44,0x44,0x44,0xFC,0xB8,0x00, // -8-
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x38,0x7C,0x44,0x44,0x44,0xFC,0xF8,0x00, // -9-
0x00,0x08,0x08,0x08,0x0C,0x07,0x03,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00, // -:-
0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00, // -;-
0x00,0x00,0x08,0x0E,0x06,0x00,0x00,0x00,
0x00,0x80,0xC0,0x60,0x30,0x18,0x08,0x00, // -<-
0x00,0x00,0x01,0x03,0x06,0x0C,0x08,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00, // -=-
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,
0x00,0x08,0x18,0x30,0x60,0xC0,0x80,0x00, // ->-
0x00,0x08,0x0C,0x06,0x03,0x01,0x00,0x00,
0x18,0x1C,0x04,0xC4,0xE4,0x3C,0x18,0x00, // -?-
0x00,0x00,0x00,0x0D,0x0D,0x00,0x00,0x00,
0xF0,0xF8,0x08,0xC8,0xC8,0xF8,0xF0,0x00, // -@-
0x07,0x0F,0x08,0x0B,0x0B,0x0B,0x01,0x00,
0xE0,0xF0,0x98,0x8C,0x98,0xF0,0xE0,0x00, // -A-
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0x04,0xFC,0xFC,0x44,0x44,0xFC,0xB8,0x00, // -B-
0x08,0x0F,0x0F,0x08,0x08,0x0F,0x07,0x00,
0xF0,0xF8,0x0C,0x04,0x04,0x0C,0x18,0x00, // -C-
0x03,0x07,0x0C,0x08,0x08,0x0C,0x06,0x00,
0x04,0xFC,0xFC,0x04,0x0C,0xF8,0xF0,0x00, // -D-
0x08,0x0F,0x0F,0x08,0x0C,0x07,0x03,0x00,
0x04,0xFC,0xFC,0x44,0xE4,0x0C,0x1C,0x00, // -E-
0x08,0x0F,0x0F,0x08,0x08,0x0C,0x0E,0x00,
0x04,0xFC,0xFC,0x44,0xE4,0x0C,0x1C,0x00, // -F-
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xF0,0xF8,0x0C,0x84,0x84,0x8C,0x98,0x00, // -G-
0x03,0x07,0x0C,0x08,0x08,0x07,0x0F,0x00,
0xFC,0xFC,0x40,0x40,0x40,0xFC,0xFC,0x00, // -H-
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0x00,0x00,0x04,0xFC,0xFC,0x04,0x00,0x00, // -I-
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x00,0x00,0x00,0x04,0xFC,0xFC,0x04,0x00, // -J-
0x07,0x0F,0x08,0x08,0x0F,0x07,0x00,0x00,
0x04,0xFC,0xFC,0xC0,0xF0,0x3C,0x0C,0x00, // -K-
0x08,0x0F,0x0F,0x00,0x01,0x0F,0x0E,0x00,
0x04,0xFC,0xFC,0x04,0x00,0x00,0x00,0x00, // -L-
0x08,0x0F,0x0F,0x08,0x08,0x0C,0x0E,0x00,
0xFC,0xFC,0x38,0x70,0x38,0xFC,0xFC,0x00, // -M-
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0xFC,0xFC,0x38,0x70,0xE0,0xFC,0xFC,0x00, // -N-
0x0F,0x0F,0x00,0x00,0x00,0x0F,0x0F,0x00,
0xF0,0xF8,0x0C,0x04,0x0C,0xF8,0xF0,0x00, // -O-
0x03,0x07,0x0C,0x08,0x0C,0x07,0x03,0x00,
0x04,0xFC,0xFC,0x44,0x44,0x7C,0x38,0x00, // -P-
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xF8,0xFC,0x04,0x04,0x04,0xFC,0xF8,0x00, // -Q-
0x07,0x0F,0x08,0x0E,0x3C,0x3F,0x27,0x00,
0x04,0xFC,0xFC,0x44,0xC4,0xFC,0x38,0x00, // -R-
0x08,0x0F,0x0F,0x00,0x00,0x0F,0x0F,0x00,
0x18,0x3C,0x64,0x44,0xC4,0x9C,0x18,0x00, // -S-
0x06,0x0E,0x08,0x08,0x08,0x0F,0x07,0x00,
0x00,0x1C,0x0C,0xFC,0xFC,0x0C,0x1C,0x00, // -T-
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0xFC,0xFC,0x00,0x00,0x00,0xFC,0xFC,0x00, // -U-
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0xFC,0xFC,0x00,0x00,0x00,0xFC,0xFC,0x00, // -V-
0x01,0x03,0x06,0x0C,0x06,0x03,0x01,0x00,
0xFC,0xFC,0x00,0x80,0x00,0xFC,0xFC,0x00, // -W-
0x03,0x0F,0x0E,0x03,0x0E,0x0F,0x03,0x00,
0x0C,0x3C,0xF0,0xC0,0xF0,0x3C,0x0C,0x00, // -X-
0x0C,0x0F,0x03,0x00,0x03,0x0F,0x0C,0x00,
0x00,0x3C,0x7C,0xC0,0xC0,0x7C,0x3C,0x00, // -Y-
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x1C,0x0C,0x84,0xC4,0x64,0x3C,0x1C,0x00, // -Z-
0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0E,0x00,
0x00,0x00,0xFC,0xFC,0x04,0x04,0x00,0x00, // -[-
0x00,0x00,0x0F,0x0F,0x08,0x08,0x00,0x00,
0x38,0x70,0xE0,0xC0,0x80,0x00,0x00,0x00, // -\-
0x00,0x00,0x00,0x01,0x03,0x07,0x0E,0x00,
0x00,0x00,0x04,0x04,0xFC,0xFC,0x00,0x00, // -]-
0x00,0x00,0x08,0x08,0x0F,0x0F,0x00,0x00,
0x08,0x0C,0x06,0x03,0x06,0x0C,0x08,0x00, // -^-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // -_-
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x00,0x00,0x03,0x07,0x04,0x00,0x00,0x00, // -`-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xA0,0xA0,0xA0,0xE0,0xC0,0x00,0x00, // -a-
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0x04,0xFC,0xFC,0x20,0x60,0xC0,0x80,0x00, // -b-
0x08,0x0F,0x07,0x08,0x08,0x0F,0x07,0x00,
0xC0,0xE0,0x20,0x20,0x20,0x60,0x40,0x00, // -c-
0x07,0x0F,0x08,0x08,0x08,0x0C,0x04,0x00,
0x80,0xC0,0x60,0x24,0xFC,0xFC,0x00,0x00, // -d-
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0xC0,0xE0,0xA0,0xA0,0xA0,0xE0,0xC0,0x00, // -e-
0x07,0x0F,0x08,0x08,0x08,0x0C,0x04,0x00,
0x40,0xF8,0xFC,0x44,0x0C,0x18,0x00,0x00, // -f-
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0xC0,0xE0,0x20,0x20,0xC0,0xE0,0x20,0x00, // -g-
0x27,0x6F,0x48,0x48,0x7F,0x3F,0x00,0x00,
0x04,0xFC,0xFC,0x40,0x20,0xE0,0xC0,0x00, // -h-
0x08,0x0F,0x0F,0x00,0x00,0x0F,0x0F,0x00,
0x00,0x00,0x20,0xEC,0xEC,0x00,0x00,0x00, // -i-
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0xEC,0xEC,0x00, // -j-
0x00,0x30,0x70,0x40,0x40,0x7F,0x3F,0x00,
0x04,0xFC,0xFC,0x80,0xC0,0x60,0x20,0x00, // -k-
0x08,0x0F,0x0F,0x01,0x03,0x0E,0x0C,0x00,
0x00,0x00,0x04,0xFC,0xFC,0x00,0x00,0x00, // -l-
0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x00,
0xE0,0xE0,0x60,0xC0,0x60,0xE0,0xC0,0x00, // -m-
0x0F,0x0F,0x00,0x0F,0x00,0x0F,0x0F,0x00,
0x20,0xE0,0xC0,0x20,0x20,0xE0,0xC0,0x00, // -n-
0x00,0x0F,0x0F,0x00,0x00,0x0F,0x0F,0x00,
0xC0,0xE0,0x20,0x20,0x20,0xE0,0xC0,0x00, // -o-
0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,
0x20,0xE0,0xC0,0x20,0x20,0xE0,0xC0,0x00, // -p-
0x40,0x7F,0x7F,0x48,0x08,0x0F,0x07,0x00,
0xC0,0xE0,0x20,0x20,0xC0,0xE0,0x20,0x00, // -q-
0x07,0x0F,0x08,0x48,0x7F,0x7F,0x40,0x00,
0x20,0xE0,0xC0,0x60,0x20,0x60,0xC0,0x00, // -r-
0x08,0x0F,0x0F,0x08,0x00,0x00,0x00,0x00,
0x40,0xE0,0xA0,0x20,0x20,0x60,0x40,0x00, // -s-
0x04,0x0C,0x09,0x09,0x0B,0x0E,0x04,0x00,
0x20,0x20,0xF8,0xFC,0x20,0x20,0x00,0x00, // -t-
0x00,0x00,0x07,0x0F,0x08,0x0C,0x04,0x00,
0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00, // -u-
0x07,0x0F,0x08,0x08,0x07,0x0F,0x08,0x00,
0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00, // -v-
0x00,0x03,0x07,0x0C,0x0C,0x07,0x03,0x00,
0xE0,0xE0,0x00,0x00,0x00,0xE0,0xE0,0x00, // -w-
0x07,0x0F,0x0C,0x07,0x0C,0x0F,0x07,0x00,
0x20,0x60,0xC0,0x80,0xC0,0x60,0x20,0x00, // -x-
0x08,0x0C,0x07,0x03,0x07,0x0C,0x08,0x00,
0xE0,0xE0,0x00,0x00,0x00,0xE0,0xE0,0x00, // -y-
0x47,0x4F,0x48,0x48,0x68,0x3F,0x1F,0x00,
0x60,0x60,0x20,0xA0,0xE0,0x60,0x20,0x00, // -z-
0x0C,0x0E,0x0B,0x09,0x08,0x0C,0x0C,0x00,
0x00,0x40,0x40,0xF8,0xBC,0x04,0x04,0x00, // -{-
0x00,0x00,0x00,0x07,0x0F,0x08,0x08,0x00,
0x00,0x00,0x00,0xBC,0xBC,0x00,0x00,0x00, // -|-
0x00,0x00,0x00,0x0F,0x0F,0x00,0x00,0x00,
0x00,0x04,0x04,0xBC,0xF8,0x40,0x40,0x00, // -}-
0x00,0x08,0x08,0x0F,0x07,0x00,0x00,0x00,
0x08,0x0C,0x04,0x0C,0x08,0x0C,0x04,0x00, // -~-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0xC0,0x60,0x30,0x60,0xC0,0x80,0x00, // --
0x07,0x07,0x04,0x04,0x04,0x07,0x07,0x00,
};
DELAY.c
#include"reg52.h"
#include"Allhead.h"
/***************************延时函数*********************/
void LCDDelay(unchar t)
{
unchar x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
/**********************延时函数**************************/
void NRFDelay(unint t)
{
unint x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
DELAY.h
#ifndef _DELAY_H_
#define _DELAY_H_
/*******************************************/
extern void LCDDelay(unchar t);
extern void NRFDelay(unint t);
/*******************************************/
#endif
LCD1602.c
#include"reg52.h"
#include"Allhead.h"
sbit RS=P0^0;
sbit RW=P0^1;
sbit EN=P0^2;
#define DBPort P2
/***************************LCD写命令函数********************/
void LCDWriteCmd(unchar cmd)
{
DBPort=cmd;
RS=0;
RW=0;
LCDDelay(5);
EN=1;
EN=0;
}
/************************LCD写数据函数**************************/
void LCDWriteDate(unchar date)
{
DBPort=date;
RS=1;
RW=0;
LCDDelay(5);
EN=1;
EN=0;
}
/***************************定位函数************************/
void SetXY(unchar x,unchar y)
{
if(x==0)
LCDWriteCmd(0x80+y); //第一行
else
LCDWriteCmd(0x80+0x40+y); //第二行
}
/***********************显示函数****************************/
void LcdShow(unchar x,unchar y,unchar *str)
{
SetXY(x,y);
while(*str!='\0')
{
LCDWriteDate(*str);
str++;
}
}
/**********************LCD初始化函数***********************/
void LCDInt()
{
LCDWriteCmd(0x38);//设置模式
LCDWriteCmd(0x0c);//不显示光标
LCDWriteCmd(0x06);//设置指针自加
LCDWriteCmd(0x01);//清屏
}
LCD1602.h
#ifndef _LCD1602_H_
#define _LCD1602_H_
/*********************************************/
extern void LCDWriteCmd(unchar cmd);
void LCDWriteDate(unchar date);
void LcdShow(unchar x,unchar y,unchar *str);
void SetXY(unchar x,unchar y);
extern void LCDInt();
/********************************************/
#endif
LCD12864.c
#include"reg52.h"
#include"code.h"
#include"Allhead.h"
#include"math.h"
#define DPOT P2
sbit RS=P0^0;
sbit RW=P0^1;
sbit EN=P0^2;
sbit CS1=P0^3;
sbit CS2=P0^4;
sbit RES=P0^5;
/*************LCD片选函数**************************************/
void LcdCS(unchar num)
{
switch(num)
{
case 0:CS1=1;CS2=1;break;//全屏
case 1:CS1=1;CS2=0;break;//左屏
case 2:CS1=0;CS2=1;break;//右屏
case 3:CS1=0;CS2=0;break;//关屏
default:break;
}
}
/********LCD写命令函数*****************************************/
void LcdWriteCmd(unchar num,unchar cmd)
{
RS=0;
RW=0;
LcdCS(num); //选屏写入命令
EN=1;
DPOT=cmd;
EN=0;
}
/*********LCD写数据函数****************************************/
void LcdWriteDate(unchar num,unchar date)
{
RS=1;
RW=0;
LcdCS(num);//选择屏幕写入数据
EN=1;
DPOT=date;
EN=0;
}
/**********LCD读屏幕函数****************************************/
unchar LcdReadDate(unchar num)
{
unchar date;
DPOT=0xff; //让数据口置1后才能读端口
RS=1;
RW=1;
LcdCS(num);//选择屏幕读入数据
EN=1;
date=DPOT;
EN=0;
return(date);
}
/**********X设置函数*************************************************/
void PageSet(unchar num,unchar page)
{
LcdWriteCmd(num,page|0xb8);
}
/**********Y设置函数************************************************/
void YSet(unchar num,unchar y)
{
LcdWriteCmd(num,y|0x40);
}
/************指定的位置写入数据***********************************/
void LcdWriteToSet(unchar x,unchar y,unchar date)
{
if(y<64)
{
PageSet(1,x);
YSet(1,y);
LcdWriteDate(1,date);
}
else
{
PageSet(2,x);
YSet(2,y-64);
LcdWriteDate(2,date);
}
}
/************指定位置读出数据****************************************/
unchar LcdReadFromSet(unchar x,unchar y)
{
unchar date;
if(y<64)
{
PageSet(1,x);
YSet(1,y);
date=LcdReadDate(1);
date=LcdReadDate(1);
return(date);
}
else
{
PageSet(2,x);
YSet(2,y-64);
date=LcdReadDate(2);
date=LcdReadDate(2);
return(date);
}
}
/************画点基本函数********************************************/
void LcdWriteDot(unchar x,unchar y,unchar attr)
{
unchar yaddr,tempdate1,tempdate2;
yaddr=6-(y>>3); //计算出所在页
tempdate1=LcdReadFromSet(yaddr,x);//在指定的x,y读出一个字节的数据
tempdate2=0x80>>((y&0x07)-1); //计算出页中点
if(attr)
tempdate1|=tempdate2; //画点
else
tempdate1&=~tempdate2; //消点
LcdWriteToSet(yaddr,x,tempdate1);//指定位置画点
}
/*******指定位置写入12点阵汉字*******************************************/
void LcdWriteCh12(unchar x,unchar y,unchar *string)
{
unchar i;
for(i=0;i<12;i++)
LcdWriteToSet(x,y++,string[i]);
y-=12;
for(i=12;i<24;i++)
LcdWriteToSet(x+1,y++,string[i]);
}
/**********指定位置写入一个5*8ASCII字模*******************/
/*void LcdWriteAsc58(unchar x,unchar y,unchar string)
{
unchar i;
for(i=0;i<5;i++) //ASCII码上部分
LcdWriteToSet(x,y++,nAsciiDot58[string-32][i]);
} */
/**********指定位置写入一个8*16ASCII字模*******************/
void LcdWriteAsc816(unchar x,unchar y,unchar string)
{
unchar i;
for(i=0;i<8;i++) //ASCII码上部分
LcdWriteToSet(x,y++,nAsciiDot816[string-32][i]);
y-=8;//返回初始的y地址
for(i=8;i<16;i++) //ASCII码下部分
LcdWriteToSet(x+1,y++,nAsciiDot816[string-32][i]);
}
/*********指定位置写入一串汉字或者ASCII码********************************/
//采用内码搜索
void LcdWriteChStr(unchar x,unchar y,unchar *string)
{
unchar ch1,ch2,i,j=0,length;
for(length=0;string[length]!=0;length++);
do
{
ch1=string[j];
ch2=string[j+1];
if(ch1<0x80)//小于0x80为ASCII码
{
LcdWriteAsc816(x,y,ch1);
j++;
y+=8; //看字体来设置
}
else
{
for(i=0;i<sizeof(GB_12)/sizeof(GB_12[0]);i++) //汉字内码搜索
{
if((ch1==GB_12[i].Index[0])&&(ch2==GB_12[i].Index[1]))
break;
}
LcdWriteCh12(x,y,GB_12[i].Msk);
j+=2;
y+=12;//看字体来设置
}
}while(j<length);
}
/************指定位置图片显示函数*******************************************/
/*void LcdShowPicture(unchar x,unchar y,unchar high,unchar wide,unchar *img)
{ //页/Y地址/图片高/图片宽/图片编码
//在显示图片时要特别小心,注意图片长宽时多少!
//设置的Y和page参数不要超过能显示这副图片的最高参数!
unchar i,j;
for(i=0;i<(high>>3);i++) //判断这幅画多少个页,竖向8个像素为一页
for(j=0;j<(wide-y);j++) //设置Y地址时要减掉前面空格,就是设置Y后第一页还可以显示多少个字节!
LcdWriteToSet(x+i,y+j,img[i*wide+j]);
}
/*********************LCD清屏******************************************/
void LcdClear()
{
unchar x,y;
for(x=0;x<8;x++)
{
for(y=0;y<128;y++)
LcdWriteToSet(x,y,0x00);
}
}
/************LCD初始化函数*********************************************/
void LcdInt()
{
RES=0;
RES=1;
RW=0;
RS=0;
EN=0;
LcdCS(3);
LcdWriteCmd(1,0x3f);
LcdWriteCmd(1,0xc0);
LcdCS(3);
LcdWriteCmd(2,0x3f);
LcdWriteCmd(2,0xc0);
}
LCD12864.h
/********************************************************************
创建人:陈志强
时间:2010.5.17
功能:LCD12864头文件
******************************************************************/
#ifndef _LCD12864_H_
#define _LCD12864_H_
/***************************************************/
void LcdCS(unchar num);
void LcdWriteCmd(unchar num,unchar cmd);
void LcdWriteDate(unchar num,unchar date);
unchar LcdReadDate(unchar num);
void PageSet(unchar num,unchar page);
void YSet(unchar num,unchar y);
void LcdWriteToSet(unchar x,unchar y,unchar date);
unchar LcdReadFromSet(unchar x,unchar y);
void LcdWriteDot(unchar x,unchar y,unchar attr);
void LcdWriteCh12(unchar x,unchar y,unchar *string);
void LcdWriteAsc58(unchar x,unchar y,unchar string);
void LcdWriteAsc816(unchar x,unchar y,unchar string);
extern void LcdWriteChStr(unchar x,unchar y,unchar *string);
extern void LcdClear();
extern void LcdInt();
/***************************************************/
#endif
MAX232.c
#include"reg52.h"
#include"allhead.h"
unchar RevFlags;
/*************************串口初始化函数**************/
void Max232Int()
{
PCON=0x00; //SMOD=0
TMOD=0x20; //设置T1为工作方式2
TH1=0xfd; //9600
TL1=0xfd;
SM0=0;
SM1=1;//方式1,8位
EA=1;
ES=1;
TR1=1;//定时器1允许
REN=1;//允许接收
}
/*************************串口发送一个字节函数********************/
void Max232SendChar(unchar date)
{
ES=0;
SBUF=date;
while(!TI);
TI=0;
ES=1;
}
/**********************发送一个字符串******************************/
void Max232SendAChar(unchar *string)
{
while(*string!='\0')
{
Max232SendChar(*string);
string++;
}
}
/**********************串口中断接收函数****************************/
void ExInt() interrupt 4
{
ES=0;
if(RI)
{
RI=0;
RevFlags=SBUF;
}
ES=1;
}
/*********************发送NRF24L01采集到的数据********************/
void MAX232SendDate()
{
if(RevFlags=='S')
{
RevFlags=0;
Max232SendAChar(RevTempDate);
Max232SendAChar("12.3");
Max232SendAChar("32.6");
Max232SendAChar("56.7");
Max232SendAChar("36.9");
Max232SendAChar("89.4");
}
//Max232SendChar('E');//发送结束标志
}
MAX232.h
#ifndef _MAX232_H_
#define _MAX232_H_
/*********************************************/
void Max232Int();
void Max232SendChar(unchar date);
void Max232SendAChar(unchar *string);
extern void MAX232SendDate();
/********************************************/
#endif
NRF24L01.c
#include"reg52.h"
#include"Allhead.h"
sbit CE=P1^2; //RX/TX模式选择端
sbit IRQ=P1^3; //可屏蔽中断端
sbit CSN=P1^4; //SPI片选端//就是SS
sbit MOSI=P1^5; //SPI主机输出从机输入端
sbit MISO=P1^6; //SPI主机输出从机输出端
sbit SCLK=P1^7; //SPI时钟端
unchar RevTempDate[5];//最后一位用来存放结束标志
unchar code TxAddr[]={0x34,0x43,0x10,0x10,0x01};//发送地址
/*****************状态标志*****************************************/
unchar bdata sta; //状态标志
sbit RX_DR=sta^6;
sbit TX_DS=sta^5;
sbit MAX_RT=sta^4;
/*****************SPI时序函数******************************************/
unchar NRFSPI(unchar date)
{
unchar i;
for(i=0;i<8;i++) // 循环8次
{
if(date&0x80)
MOSI=1;
else
MOSI=0; // byte最高位输出到MOSI
date<<=1; // 低一位移位到最高位
SCLK=1;
if(MISO) // 拉高SCK,nRF24L01从MOSI读入1位数据,同时从MISO输出1位数据
date|=0x01; // 读MISO到byte最低位
SCLK=0; // SCK置低
}
return(date); // 返回读出的一字节
}
/**********************NRF24L01初始化函数*******************************/
void NRF24L01Int()
{
NRFDelay(2);//让系统什么都不干
CE=0;
CSN=1;
SCLK=0;
IRQ=1;
}
/*****************SPI读寄存器一字节函数*********************************/
unchar NRFReadReg(unchar RegAddr)
{
unchar BackDate;
CSN=0;//启动时序
NRFSPI(RegAddr);//写寄存器地址
BackDate=NRFSPI(0x00);//写入读寄存器指令
CSN=1;
return(BackDate); //返回状态
}
/*****************SPI写寄存器一字节函数*********************************/
unchar NRFWriteReg(unchar RegAddr,unchar date)
{
unchar BackDate;
CSN=0;//启动时序
BackDate=NRFSPI(RegAddr);//写入地址
NRFSPI(date);//写入值
CSN=1;
return(BackDate);
}
/*****************SPI读取RXFIFO寄存器的值********************************/
unchar NRFReadRxDate(unchar RegAddr,unchar *RxDate,unchar DateLen)
{ //寄存器地址//读取数据存放变量//读取数据长度//用于接收
unchar BackDate,i;
CSN=0;//启动时序
BackDate=NRFSPI(RegAddr);//写入要读取的寄存器地址
for(i=0;i<DateLen;i++) //读取数据
{
RxDate[i]=NRFSPI(0);
}
CSN=1;
return(BackDate);
}
/*****************SPI写入TXFIFO寄存器的值**********************************/
unchar NRFWriteTxDate(unchar RegAddr,unchar *TxDate,unchar DateLen)
{ //寄存器地址//写入数据存放变量//读取数据长度//用于发送
unchar BackDate,i;
CSN=0;
BackDate=NRFSPI(RegAddr);//写入要写入寄存器的地址
for(i=0;i<DateLen;i++)//写入数据
{
NRFSPI(*TxDate++);
}
CSN=1;
return(BackDate);
}
/*****************NRF设置为发送模式并发送数据******************************/
void NRFSetTxMode(unchar *TxDate)
{ //发送模式
CE=0;
NRFWriteTxDate(W_REGISTER+TX_ADDR,TxAddr,TX_ADDR_WITDH);//写寄存器指令+P0地址使能指令+发送地址+地址宽度
NRFWriteTxDate(W_REGISTER+RX_ADDR_P0,TxAddr,TX_ADDR_WITDH);//为了应答接收设备,接收通道0地址和发送地址相同
NRFWriteTxDate(W_TX_PAYLOAD,TxDate,TX_DATA_WITDH);//写入数据
/******下面有关寄存器配置**************/
NRFWriteReg(W_REGISTER+EN_AA,0x01); // 使能接收通道0自动应答
NRFWriteReg(W_REGISTER+EN_RXADDR,0x01); // 使能接收通道0
NRFWriteReg(W_REGISTER+SETUP_RETR,0x0a); // 自动重发延时等待250us+86us,自动重发10次
NRFWriteReg(W_REGISTER+RF_CH,0x40); // 选择射频通道0x40
NRFWriteReg(W_REGISTER+RF_SETUP,0x07); // 数据传输率1Mbps,发射功率0dBm,低噪声放大器增益
NRFWriteReg(W_REGISTER+CONFIG,0x0e); // CRC使能,16位CRC校验,上电
CE=1;
NRFDelay(5);//保持10us秒以上
}
/*****************NRF设置为接收模式并接收数据******************************/
//接收模式
void NRFSetRXMode()
{
CE=0;
NRFWriteTxDate(W_REGISTER+RX_ADDR_P0,TxAddr,TX_ADDR_WITDH); // 接收设备接收通道0使用和发送设备相同的发送地址
NRFWriteReg(W_REGISTER+EN_AA,0x01); // 使能接收通道0自动应答
NRFWriteReg(W_REGISTER+EN_RXADDR,0x01); // 使能接收通道0
NRFWriteReg(W_REGISTER+RF_CH,0x40); // 选择射频通道0x40
NRFWriteReg(W_REGISTER+RX_PW_P0,TX_DATA_WITDH); // 接收通道0选择和发送通道相同有效数据宽度
NRFWriteReg(W_REGISTER+RF_SETUP,0x07); // 数据传输率1Mbps,发射功率0dBm,低噪声放大器增益
NRFWriteReg(W_REGISTER+CONFIG,0x0f); // CRC使能,16位CRC校验,上电,接收模式
CE = 1;
NRFDelay(5);
}
/****************************检测是否有接收到数据******************************/
void CheckACK()
{ //用于发射模式接收应答信号
sta=NRFReadReg(R_REGISTER+STATUS); // 返回状态寄存器
if(TX_DS)
NRFWriteReg(W_REGISTER+STATUS,0xff); // 清除TX_DS或MAX_RT中断标志
}
/*************************接收数据*********************************************/
void GetDate()
{
sta=NRFReadReg(R_REGISTER+STATUS);//发送数据后读取状态寄存器
if(RX_DR) // 判断是否接收到数据
{
CE=0;//待机
NRFReadRxDate(R_RX_PAYLOAD,RevTempDate,RX_DATA_WITDH);// 从RXFIFO读取数据 接收4位即可,后一位位结束位
LcdWriteChStr(2,65,RevTempDate);//LCD12864液晶显示
MAX232SendDate();//发送数据到上位机
NRFWriteReg(W_REGISTER+STATUS,0xff); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标
CSN=0;
NRFSPI(FLUSH_RX);//用于清空FIFO !!关键!!不然会出现意想不到的后果!!!大家记住!!
CSN=1;
}
//NRFWriteReg(W_REGISTER+STATUS,0xff); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标
}
NRF24L01.h
#ifndef _NRF24L01_H_
#define _NRF24L01_H_
/*******************************************************/
#define TX_ADDR_WITDH 5 //发送地址宽度设置为5个字节
#define RX_ADDR_WITDH 5 //接收地址宽度设置为5个字节
#define TX_DATA_WITDH 4//发送数据宽度1个字节
#define RX_DATA_WITDH 4//接收数据宽度1个字节
/*******************命令寄存器***************************/
#define R_REGISTER 0x00//读取配置寄存器
#define W_REGISTER 0x20//写配置寄存器
#define R_RX_PAYLOAD 0x61//读取RX有效数据
#define W_TX_PAYLOAD 0xa0//写TX有效数据
#define FLUSH_TX 0xe1//清除TXFIFO寄存器
#define FLUSH_RX 0xe2//清除RXFIFO寄存器
#define REUSE_TX_PL 0xe3//重新使用上一包有效数据
#define NOP 0xff//空操作
/******************寄存器地址****************************/
#define CONFIG 0x00//配置寄存器
#define EN_AA 0x01//使能自动应答
#define EN_RXADDR 0x02//接收通道使能0-5个通道
#define SETUP_AW 0x03//设置数据通道地址宽度3-5
#define SETUP_RETR 0x04//建立自动重发
#define RF_CH 0x05//射频通道设置
#define RF_SETUP 0x06//射频寄存器
#define STATUS 0x07//状态寄存器
#define OBSERVE_TX 0x08//发送检测寄存器
#define CD 0x09//载波
#define RX_ADDR_P0 0x0a//数据通道0接收地址
#define RX_ADDR_P1 0x0b//数据通道1接收地址
#define RX_ADDR_P2 0x0c//数据通道2接收地址
#define RX_ADDR_P3 0x0d//数据通道3接收地址
#define RX_ADDR_P4 0x0e//数据通道4接收地址
#define RX_ADDR_P5 0x0f//数据通道5接收地址
#define TX_ADDR 0x10//发送地址
#define RX_PW_P0 0x11//P0通道数据宽度设置
#define RX_PW_P1 0x12//P1通道数据宽度设置
#define RX_PW_P2 0x13//P2通道数据宽度设置
#define RX_PW_P3 0x14//P3通道数据宽度设置
#define RX_PW_P4 0x15//P4通道数据宽度设置
#define RX_PW_P5 0x16//P5通道数据宽度设置
#define FIFO_STATUS 0x17//FIFO状态寄存器
/*******************相关函数声明**************************/
unchar NRFACK();
unchar NRFSPI(unchar date);
unchar NRFReadReg(unchar RegAddr);
unchar NRFWriteReg(unchar RegAddr,unchar date);
unchar NRFReadRxDate(unchar RegAddr,unchar *RxDate,unchar DateLen);
unchar NRFWriteTxDate(unchar RegAddr,unchar *TxDate,unchar DateLen);
void NRFSetTxMode(unchar *TxDate);
void NRF24L01Int();
void NRFSetRXMode();
void CheckACK();
extern void GetDate();
extern unchar bdata sta;
extern unchar RevTempDate[5];
/*********************************************************/
#endif
public.h
#ifndef _PUBILE_H_
#define _PUBILE_H_
/****************************************/
#define unchar unsigned char
#define unint unsigned int
/*****************************************/
#endif
receive.c
#include"reg52.h"
#include"Allhead.h"
/************************主函数********************/
void main()
{
//LCD1602Int();
*(RevTempDate+4)='\0';
LcdInt(); //12864液晶初始化
LcdClear(); //12864液晶清屏
Max232Int();//串口初始化
NRF24L01Int();
LcdWriteChStr(2,10,"当前温度:");
while(1)
{
NRFSetRXMode();//设置为接收模式
GetDate();//开始接受数
}
}
转载自原文链接, 如需删除请联系管理员。
原文链接:无线广告屏]无线模块,转载请注明来源!