|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
基于51 单片机的无线温湿度传输
//main函数
#include <reg52.h>
#include "nrf.h"
#include "temp.h"
#include "dht11.h"
uchar TX_DATA[8]={'a',0,0,0,0,0,0,0};
uchar TX_DATA2[8]={'b',0,0,0,0,0,0,0};
uchar b=0,a,flag = 0;
uint c=0;
//void InitTimer0(void)
//{
// TMOD = 0x00;
// TH0 = 0x1C;
// TL0 = 0x18;
// EA = 1;
// ET0 = 1;
// TR0 = 1;
//}
//void UART_INIT() //9600
//{
// TMOD = 0x20;
// SCON = 0x50;
// TH1 = 0xFD;
// TL1 = TH1;
// PCON = 0x00;
// TR1 = 1;
//}
//void send_UART(unsigned char i)
//{
// TI = 0;
// SBUF = i;
// while(TI ==0);
// TI = 0;
//}
void temp_trans( int temp )
{
float tp;
if(temp< 0) //μ±Î¶èÖμÎa¸oêy
{
//òòÎa¶áè¡μÄζèêÇêμ¼êζèμÄ21Â룬ËùòÔ¼õ1£¬Ôùè¡·′Çó3öÔ-Âë
TX_DATA[1] = 'm';
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//áôὸöD¡êyμã¾í*100£¬+0.5êÇËÄéáÎåè룬òòÎaCóïÑÔ¸¡μãêy×a»»ÎaÕûDíμÄê±oò°ÑD¡êyμã
//oóÃæμÄêy×Ô¶ˉè¥μô£¬2»1üêÇ·ñ′óóú0.5£¬¶ø+0.5Ö®oó′óóú0.5μľíêǽø1áË£¬D¡óú0.5μľí
//ËãóéÏ0.5£¬»1êÇÔúD¡êyμãoóÃæ¡£
}
else
{
TX_DATA[1] = 'n';
tp=temp;//òòÎaêy¾Y′|àíóDD¡êyμãËùòÔ½«Î¶è¸3¸øò»¸ö¸¡μãDí±äá¿
//èç1ûζèêÇÕyμÄÄÇÃ′£¬ÄÇÃ′ÕyêyμÄÔ-Âë¾íêÇ21ÂëËü±¾éí
temp=tp*0.0625*100+0.5;
//áôὸöD¡êyμã¾í*100£¬+0.5êÇËÄéáÎåè룬òòÎaCóïÑÔ¸¡μãêy×a»»ÎaÕûDíμÄê±oò°ÑD¡êyμã
//oóÃæμÄêy×Ô¶ˉè¥μô£¬2»1üêÇ·ñ′óóú0.5£¬¶ø+0.5Ö®oó′óóú0.5μľíêǽø1áË£¬D¡óú0.5μľí
//Ëã¼óéÏ0.5£¬»1êÇÔúD¡êyμãoóÃæ¡£
}
TX_DATA[2] = (uchar)(temp / 10000);
TX_DATA[3] = (uchar)(temp % 10000 / 1000);
TX_DATA[4] = (uchar)(temp % 1000 / 100);
TX_DATA[5] = (uchar)(' ');
TX_DATA[6] = (uchar)(temp % 100 / 10);
TX_DATA[7] = (uchar)(temp % 10);
}
void main()
{
// UART_INIT(); //′®¿úéè3õê¼»ˉ
init_NRF24L01(); //nrf3õê¼»ˉ
// InitTimer0();
// if(NRF24L01_Check() == 0 )
// send_UART(0x66); //¼ì2aμ½nrf
// else
// send_UART(0x2b); //ûóD¼ì2aμ½nrf
while(1)
{
//·¢Ëíζè
nRF24L01_TxMode1();
temp_trans(Ds18b20ReadTemp());
for(a=0;a<8;a++)
NRF_TX_CheckEvent(TX_DATA+a, 1);
//·¢Ëíêa¶è
for(a=1;a<8;a++)
TX_DATA2[a] = RH();
nRF24L01_TxMode2();
for(a=0;a<8;a++)
NRF_TX_CheckEvent(TX_DATA2+a, 1);
// if(flag == 1)
// {
// flag = 0;
// for(a=1;a<8;a++)
// TX_DATA2[a] = RH();
// }
}
}
//void Timer0Interrupt(void) interrupt 1
//{
// TH0 = 0x1C;
// TL0 = 0x18;
// b++;c++;
// if(b > 5)
// {
// //·¢Ëíζè
// b=0;
// nRF24L01_TxMode1();
// temp_trans(Ds18b20ReadTemp());
// for(a=0;a<8;a++)
// NRF_TX_CheckEvent(TX_DATA+a, 1);
//
//
// //·¢Ëíêa¶è
// nRF24L01_TxMode2();
// for(a=0;a<8;a++)
// NRF_TX_CheckEvent(TX_DATA2+a, 1);
// }
// if(c > 2000)
// {
// c= 0;
// flag = 1;
// }
//}
#include "nrf.h"
//****************************************IO¶Ë¿ú¶¨òå***************************************
sbit CSN =P2^2;
sbit MOSI =P2^3;
sbit IRQ =P2^4;
sbit MISO =P2^5;
sbit SCK =P2^6;
sbit CE =P2^7;
const uchar TX_ADDRESS2[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //±¾μØμØÖ·
const uchar RX_ADDRESS2[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //½óêÕμØÖ·
const uchar TX_ADDRESS[TX_ADR_WIDTH]= {0x33,0x42,0x11,0x12,0x00}; //±¾μØμØÖ·
const uchar RX_ADDRESS[RX_ADR_WIDTH]= {0x33,0x42,0x11,0x12,0x00}; //½óêÕμØÖ·
/******************************************************************************************
/*Ñóê±oˉêy
/******************************************************************************************/
void delay10us(void)
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=1;a>0;a--);
}
//void delay10ms(void) //Îó2î -1.084010840108us
//{
// unsigned char a,b;
// for(b=21;b>0;b--)
// for(a=218;a>0;a--);
//}
//void delay500ms(void) //Îó2î -0.000000000127us
//{
// unsigned char a,b,c;
// for(c=122;c>0;c--)
// for(b=222;b>0;b--)
// for(a=7;a>0;a--);
// _nop_(); //if Keil,require use intrins.h
//}
//void delay100ms(void) //Îó2î -0.000000000021us
//{
// unsigned char a,b,c;
// for(c=35;c>0;c--)
// for(b=10;b>0;b--)
// for(a=130;a>0;a--);
//}
//****************************************************************************************
/*NRF24L013õê¼»ˉ
//***************************************************************************************/
void init_NRF24L01(void)
{
CE=0; // CEà-μí£¬¿éòÔ¶ÔD¾Æ¬D′èëêy¾Y£¬à-¸ß£¬¿aê¼·¢Ëí»ò½óêÕ
CSN=1; // Spi disable
IRQ = 1;
SCK=0; // sckDèòaÏèà-μí¡£2»è»spi¶áD′»á꧰ü
CE=0; //StandBy IÄ£ê½
SPI_Write_Buf(WRITE_REG + TX_ADDR, (uchar*)TX_ADDRESS, TX_ADR_WIDTH); // D′±¾μØμØÖ·
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,(uchar*)RX_ADDRESS, RX_ADR_WIDTH); // éèÖÃRX½úμãμØÖ·,Ö÷òaÎaáËê1ÄüACK
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Æμμà0×Ô¶ˉ ACKó|′eÔêDí
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // ÔêDí½óêÕμØÖ·Ö»óDÆμμà0£¬èç1ûDèòa¶àÆμμà¿éòÔ2ο¼age21
SPI_RW_Reg(WRITE_REG+SETUP_RETR,0x1a); //éèÖÃ×Ô¶ˉÖØ·¢¼ä¸ôê±¼ä:500us;×î′ó×Ô¶ˉÖØ·¢′Îêy:10′Î
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //éèÖýóêÕêy¾Y3¤¶è£¬±¾′ÎéèÖÃÎa32×Ö½ú
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // éèÖÃDÅμà1¤×÷Îa2.4GHZ£¬êÕ·¢±ØDëò»ÖÂ
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //éèÖ÷¢éäËùÂêÎa1MHZ£¬·¢éä1|ÂêÎa×î′óÖμ0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQêÕ·¢íê3éÖD¶ÏÏìó|£¬16λCRC£¬Ö÷·¢Ëí
SPI_RW(0x50);
SPI_RW(0x73);
SPI_RW_Reg(WRITE_REG+0x1c,0x01);
SPI_RW_Reg(WRITE_REG+0x1d,0x06);
CE=1; //ÖøßCE£¬¼¤·¢êy¾Y·¢Ëí
}
/****************************************************************************************************
/*oˉêy£ouint SPI_RW(uint uchar)
/*1|Äü£oNRF24L01μÄSPID′ê±Dò
/****************************************************************************************************/
uint SPI_RW(uint uchar)
{
uint bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI = (uchar & 0x80); // output 'uchar', MSB to MOSI
uchar = (uchar << 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
uchar |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(uchar); // return read uchar
}
/****************************************************************************************************
/*oˉêy£ouchar SPI_Read(uchar reg)
/*1|Äü£oNRF24L01μÄSPIê±Dò
/****************************************************************************************************/
uchar SPI_Read(uchar reg)
{
uchar reg_val;
CSN = 0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN = 1; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/****************************************************************************************************/
/*1|Äü£oNRF24L01¶áD′¼Ä′æÆ÷oˉêy
/****************************************************************************************************/
uint SPI_RW_Reg(uchar reg, uchar value)
{
uint status;
CSN = 0; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN = 1; // CSN high again
return(status); // return nRF24L01 status uchar
}
/****************************************************************************************************/
/*oˉêy£ouint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
/*1|Äü: óÃóú¶áêy¾Y£¬reg£oÎa¼Ä′æÆ÷μØÖ·£¬pBuf£oÎa′y¶á3öêy¾YμØÖ·£¬uchars£o¶á3öêy¾Yμĸöêy
///****************************************************************************************************/
//uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
//{
// uint status,uchar_ctr;
//
// CSN = 0; // Set CSN low, init SPI tranaction
// status = SPI_RW(reg); // Select register to write to and read status uchar
//
// for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
// pBuf[uchar_ctr] = SPI_RW(0); //
//
// CSN = 1;
//
// return(status); // return nRF24L01 status uchar
//}
/*********************************************************************************************************
/*oˉêy£ouint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
/*1|Äü: óÃóúD′êy¾Y£oÎa¼Ä′æÆ÷μØÖ·£¬pBuf£oÎa′yD′èëêy¾YμØÖ·£¬uchars£oD′èëêy¾Yμĸöêy
/*********************************************************************************************************/
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
uint status,uchar_ctr;
CSN = 0; //SPIê1Äü
status = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr<uchars; uchar_ctr++) //
SPI_RW(*pBuf++);
CSN = 1; //1رÕSPI
return(status); //
}
//¼ì2a24L01êÇ·ñ′æÔú
//·μ»ØÖμ:0£¬3é1|;1£¬ê§°ü
//uchar NRF24L01_Check(void)
//{
// uchar buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
// uchar i;
// SPI_Write_Buf(WRITE_REG + TX_ADDR,buf,5);//D′èë5¸ö×Ö½úμÄμØÖ·.
// SPI_Read_Buf(TX_ADDR,buf,5); //¶á3öD′èëμÄμØÖ·
// for(i=0;i<5;i++)if(buf!=0XA5)break;
// if(i!=5)return 1;//¼ì2a24L01′íÎó
// return 0; //¼ì2aμ½24L01
//}
void nRF24L01_TxMode1(void)
{
CE=0; //StandBy IÄ£ê½
SPI_Write_Buf(WRITE_REG + TX_ADDR, (uchar*)TX_ADDRESS, TX_ADR_WIDTH); // D′±¾μØμØÖ·
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,(uchar*)RX_ADDRESS, RX_ADR_WIDTH); // éèÖÃRX½úμãμØÖ·,Ö÷òaÎaáËê1ÄüACK
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Æμμà0×Ô¶ˉ ACKó|′eÔêDí
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // ÔêDí½óêÕμØÖ·Ö»óDÆμμà0£¬èç1ûDèòa¶àÆμμà¿éòÔ2ο¼age21
SPI_RW_Reg(WRITE_REG+SETUP_RETR,0x1a); //éèÖÃ×Ô¶ˉÖØ·¢¼ä¸ôê±¼ä:500us;×î′ó×Ô¶ˉÖØ·¢′Îêy:10′Î
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //éèÖýóêÕêy¾Y3¤¶è£¬±¾′ÎéèÖÃÎa32×Ö½ú
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // éèÖÃDÅμà1¤×÷Îa2.4GHZ£¬êÕ·¢±ØDëò»ÖÂ
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //éèÖ÷¢éäËùÂêÎa1MHZ£¬·¢éä1|ÂêÎa×î′óÖμ0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQêÕ·¢íê3éÖD¶ÏÏìó|£¬16λCRC£¬Ö÷·¢
SPI_RW_Reg(FLUSH_TX,0xff);
SPI_RW_Reg(FLUSH_RX,0xff);
SPI_RW(0x50);
SPI_RW(0x73);
SPI_RW_Reg(WRITE_REG+0x1c,0x01);
SPI_RW_Reg(WRITE_REG+0x1d,0x06);
CE=1; //ÖøßCE£¬¼¤·¢êy¾Y·¢Ëí
delay10us();
}
void nRF24L01_TxMode2(void)
{
CE=0; //StandBy IÄ£ê½
SPI_Write_Buf(WRITE_REG + TX_ADDR, (uchar*)TX_ADDRESS2, TX_ADR_WIDTH); // D′±¾μØμØÖ·
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,(uchar*)RX_ADDRESS2, RX_ADR_WIDTH); // éèÖÃRX½úμãμØÖ·,Ö÷òaÎaáËê1ÄüACK
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Æμμà0×Ô¶ˉ ACKó|′eÔêDí
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // ÔêDí½óêÕμØÖ·Ö»óDÆμμà0£¬èç1ûDèòa¶àÆμμà¿éòÔ2ο¼age21
SPI_RW_Reg(WRITE_REG+SETUP_RETR,0x1a); //éèÖÃ×Ô¶ˉÖØ·¢¼ä¸ôê±¼ä:500us;×î′ó×Ô¶ˉÖØ·¢′Îêy:10′Î
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //éèÖýóêÕêy¾Y3¤¶è£¬±¾′ÎéèÖÃÎa32×Ö½ú
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // éèÖÃDÅμà1¤×÷Îa2.4GHZ£¬êÕ·¢±ØDëò»ÖÂ
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //éèÖ÷¢éäËùÂêÎa1MHZ£¬·¢éä1|ÂêÎa×î′óÖμ0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQêÕ·¢íê3éÖD¶ÏÏìó|£¬16λCRC£¬Ö÷·¢
SPI_RW_Reg(FLUSH_TX,0xff);
SPI_RW_Reg(FLUSH_RX,0xff);
SPI_RW(0x50);
SPI_RW(0x73);
SPI_RW_Reg(WRITE_REG+0x1c,0x01);
SPI_RW_Reg(WRITE_REG+0x1d,0x06);
CE=1; //ÖøßCE£¬¼¤·¢êy¾Y·¢Ëí
delay10us();
}
//void NRF_TxPacket(uchar *tx_buf, uchar len)
//{
// CE = 0;
//// SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQêÕ·¢íê3éÖD¶ÏÏìó|£¬16λCRC£¬Ö÷·¢Ëí
// SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,(uchar*)RX_ADDRESS, RX_ADR_WIDTH); // éèÖÃRX½úμãμØÖ·,Ö÷òaÎaáËê1ÄüACK
// SPI_Write_Buf(WR_TX_PLOAD, tx_buf, len); //½«òa·¢ËíμÄêy¾YD′èëμ½·¢Ëí»o3å
// CE = 1;
//// delay10us();
//}
void NRF_TX_CheckEvent(uchar *tx_buf, uchar len)
{
unsigned char sta;
CE = 0;
// SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQêÕ·¢íê3éÖD¶ÏÏìó|£¬16λCRC£¬Ö÷·¢Ëí
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,(uchar*)RX_ADDRESS, RX_ADR_WIDTH); // éèÖÃRX½úμãμØÖ·,Ö÷òaÎaáËê1ÄüACK
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, len); //½«òa·¢ËíμÄêy¾YD′èëμ½·¢Ëí»o3å
CE = 1;
delay10us();
while(IRQ == 1);
sta=SPI_Read(STATUS); //¶áè¡×′쬼Ä′æÆ÷μÄÖμ
if((sta & MAX_TX)||(sta & 0x01))//′ïμ½×î′óÖØ·¢′Îêy
{
SPI_RW_Reg(FLUSH_TX,0xff);//Çå3yTX FIFO¼Ä′æÆ÷
}
if(sta & TX_OK)//·¢Ëííê3é
{
SPI_RW_Reg(FLUSH_TX,0xff);//Çå3yTX FIFO¼Ä′æÆ÷
// return TX_OK;
}
SPI_RW_Reg(WRITE_REG+STATUS,sta); //Çå3yTX_DS»òMAX_RTÖD¶Ï±êÖ¾
}
#include <reg52.h>
#include "intrins.h"
#ifndef __NRF_H_
#define __NRF_H_
typedef unsigned char uchar;
typedef unsigned char uint;
//*********************************************NRF24L01*************************************
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 32 // 20 uints TX payload
#define RX_PLOAD_WIDTH 32 // 20 uints TX payload
//***************************************NRF24L01¼Ä′æÆ÷Ö¸áî*******************************************************
#define READ_REG 0x00 // ¶á¼Ä′æÆ÷Ö¸áî
#define WRITE_REG 0x20 // D′¼Ä′æÆ÷Ö¸áî
#define R_RX_PL_WID 0x60 //¶áè¡êy¾Y¿í¶è
#define RD_RX_PLOAD 0x61 // ¶á衽óêÕêy¾YÖ¸áî
#define WR_TX_PLOAD 0xA0 // D′′y·¢êy¾YÖ¸áî
#define FLUSH_TX 0xE1 // 3åÏ′·¢Ëí FIFOÖ¸áî
#define FLUSH_RX 0xE2 // 3åÏ′½óêÕ FIFOÖ¸áî
#define REUSE_TX_PL 0xE3 // ¶¨òåÖظ′×°ÔØêy¾YÖ¸áî
#define NOP 0xFF // 񜠓
//*************************************SPI(nRF24L01)¼Ä′æÆ÷μØÖ·****************************************************
#define CONFIG 0x00 // ÅäÖÃêÕ·¢×′쬣¬CRCD£ÑéÄ£ê½òÔ¼°êÕ·¢×′ì¬Ïìó|·½ê½
#define EN_AA 0x01 // ×Ô¶ˉó|′e1|ÄüéèÖÃ
#define EN_RXADDR 0x02 // ¿éóÃDÅμàéèÖÃ
#define SETUP_AW 0x03 // êÕ·¢μØÖ·¿í¶èéèÖÃ
#define SETUP_RETR 0x04 // ×Ô¶ˉÖØ·¢1|ÄüéèÖÃ
#define RF_CH 0x05 // 1¤×÷ÆμÂêéèÖÃ
#define RF_SETUP 0x06 // ·¢éäËùÂê¡¢1|oÄ1|ÄüéèÖÃ
#define STATUS 0x07 // ×′쬼Ä′æÆ÷
#define OBSERVE_TX 0x08 // ·¢Ëí¼à2a1|Äü
#define CD 0x09 // μØÖ·¼ì2a
#define RX_ADDR_P0 0x0A // Æμμà0½óêÕêy¾YμØÖ·
#define RX_ADDR_P1 0x0B // Æμμà1½óêÕêy¾YμØÖ·
#define RX_ADDR_P2 0x0C // Æμμà2½óêÕêy¾YμØÖ·
#define RX_ADDR_P3 0x0D // Æμμà3½óêÕêy¾YμØÖ·
#define RX_ADDR_P4 0x0E // Æμμà4½óêÕêy¾YμØÖ·
#define RX_ADDR_P5 0x0F // Æμμà5½óêÕêy¾YμØÖ·
#define TX_ADDR 0x10 // ·¢ËíμØÖ·¼Ä′æÆ÷
#define RX_PW_P0 0x11 // ½óêÕÆμμà0½óêÕêy¾Y3¤¶è
#define RX_PW_P1 0x12 // ½óêÕÆμμà0½óêÕêy¾Y3¤¶è
#define RX_PW_P2 0x13 // ½óêÕÆμμà0½óêÕêy¾Y3¤¶è
#define RX_PW_P3 0x14 // ½óêÕÆμμà0½óêÕêy¾Y3¤¶è
#define RX_PW_P4 0x15 // ½óêÕÆμμà0½óêÕêy¾Y3¤¶è
#define RX_PW_P5 0x16 // ½óêÕÆμμà0½óêÕêy¾Y3¤¶è
#define FIFO_STATUS 0x17 // FIFOÕ»èëÕ»3ö×′쬼Ä′æÆ÷éèÖÃ
//**************************************************************************************
#define RX_OK 0x40 //êÕμ½êy¾YÖD¶Ï
#define TX_OK 0x20 //·¢Ëíêy¾YÖD¶Ï
#define MAX_TX 0x10 //′ïμ½×î′óÖØ·¢′ÎêyÖD¶Ï
/*****************************************************************************************/
void init_NRF24L01(void);
void delay10us(void);
//void delay10ms(void);
//void delay100ms(void);
//void delay500ms(void);
uint SPI_RW(uint uchar);
uchar SPI_Read(uchar reg);
uint SPI_RW_Reg(uchar reg, uchar value);
//uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars);
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars);
//uchar NRF24L01_Check(void);
void nRF24L01_TxMode1(void);
void nRF24L01_TxMode2(void);
void NRF_TX_CheckEvent(uchar *tx_buf, uchar len);
//void NRF_TxPacket();
#endif
#include"temp.h"
/*******************************************************************************
* oˉ êy Ãû : Delay1ms
* oˉêy1|Äü : Ñóê±oˉêy
* êä èë : ÎT
* êä 3ö : ÎT
*******************************************************************************/
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
/*******************************************************************************
* oˉ êy Ãû : Ds18b20Init
* oˉêy1|Äü : 3õê¼»ˉ
* êä èë : ÎT
* êä 3ö : 3õê¼»ˉ3é1|·μ»Ø1£¬ê§°ü·μ»Ø0
*******************************************************************************/
uchar Ds18b20Init()
{
uchar i;
DSPORT = 0; //½«×üÏßà-μí480us~960us
i = 70;
while(i--); //Ñóê±642us
DSPORT = 1; //è»oóà-¸ß×üÏߣ¬èç1ûDS18B20×ö3ö·′ó|»á½«Ôú15us~60usoó×üÏßà-μí
i = 0;
while(DSPORT) //μè′yDS18B20à-μí×üÏß
{
i++;
if(i>5)//μè′y>5MS
{
return 0;//3õê¼»ˉ꧰ü
}
Delay1ms(1);
}
return 1;//3õê¼»ˉ3é1|
}
/*******************************************************************************
* oˉ êy Ãû : Ds18b20WriteByte
* oˉêy1|Äü : Ïò18B20D′èëò»¸ö×Ö½ú
* êä èë : com
* êä 3ö : ÎT
*******************************************************************************/
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0; //ÿD′èëò»Î»êy¾Y֮ǰÏè°Ñ×üÏßà-μí1us
i++;
DSPORT = dat & 0x01; //è»oóD′èëò»¸öêy¾Y£¬′ó×îμíλ¿aê¼
i=6;
while(i--); //Ñóê±68us£¬3ÖDøê±¼ä×îéù60us
DSPORT = 1; //è»oóêí·Å×üÏߣ¬Öáéù1us¸ø×üÏ߻ָ′ê±¼ä2ÅÄü½ó×ÅD′èëμú¶t¸öêyÖμ
dat >>= 1;
}
}
/*******************************************************************************
* oˉ êy Ãû : Ds18b20ReadByte
* oˉêy1|Äü : ¶áè¡ò»¸ö×Ö½ú
* êä èë : com
* êä 3ö : ÎT
*******************************************************************************/
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DSPORT = 0;//Ï轫×üÏßà-μí1us
i++;
DSPORT = 1;//è»oóêí·Å×üÏß
i++;
i++;//Ñóê±6usμè′yêy¾YÎ趨
bi = DSPORT; //¶áè¡êy¾Y£¬′ó×îμíλ¿a꼶áè¡
/*½«byte×óòÆò»Î»£¬è»oóóëéÏóòòÆ7λoóμÄbi£¬×¢òaòƶˉÖ®oóòÆμôÄÇλ210¡£*/
byte = (byte >> 1) | (bi << 7);
i = 4; //¶áè¡íêÖ®oóμè′y48usÔù½ó×Ŷáè¡ÏÂò»¸öêy
while(i--);
}
return byte;
}
/*******************************************************************************
* oˉ êy Ãû : Ds18b20ChangTemp
* oˉêy1|Äü : èÃ18b20¿aê¼×a»»Î¶è
* êä èë : com
* êä 3ö : ÎT
*******************************************************************************/
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //ìø1yROM2ù×÷Ãüáî
Ds18b20WriteByte(0x44); //ζè×a»»Ãüáî
Delay1ms(100); //μè′y×a»»3é1|£¬¶øèç1ûÄãêÇò»Ö±Ë¢×ÅμÄ»°£¬¾í2»óÃÕa¸öÑóê±áË
}
/*******************************************************************************
* oˉ êy Ãû : Ds18b20ReadTempCom
* oˉêy1|Äü : ·¢Ëí¶áè¡Î¶èÃüáî
* êä èë : com
* êä 3ö : ÎT
*******************************************************************************/
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //ìø1yROM2ù×÷Ãüáî
Ds18b20WriteByte(0xbe); //·¢Ëí¶áè¡Î¶èÃüáî
}
/*******************************************************************************
* oˉ êy Ãû : Ds18b20ReadTemp
* oˉêy1|Äü : ¶áè¡Î¶è
* êä èë : com
* êä 3ö : ÎT
*******************************************************************************/
int Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp(); //ÏèD′èë×a»»Ãüáî
Ds18b20ReadTempCom(); //è»oóμè′y×a»»íêoó·¢Ëí¶áè¡Î¶èÃüáî
tml = Ds18b20ReadByte(); //¶áè¡Î¶èÖμ1216룬Ïè¶áμí×Ö½ú
tmh = Ds18b20ReadByte(); //Ôù¶á¸ß×Ö½ú
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
#ifndef __TEMP_H_
#define __TEMP_H_
#include<reg52.h>
//---Öض¨òå1ؼü′ê---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
//--¶¨òåê1óÃμÄIO¿ú--//
sbit DSPORT=P1^0;
//--éùÃ÷諾Öoˉêy--//
void Delay1ms(uint );
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar com);
uchar Ds18b20ReadByte();
void Ds18b20ChangTemp();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
#endif
#include "dht11.h"
U8 U8FLAG,k;
U8 U8count,U8temp;
U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8 U8comdata;
U8 outdata[5]; //¶¨òå·¢ËíμÄ×Ö½úêy
U8 indata[5];
U8 count, count_r=0;
U8 str[5]={"RS232"};
U16 U16temp1,U16temp2;
//void SendData(U8 *a)
//{
// outdata[0] = a[0];
// outdata[1] = a[1];
// outdata[2] = a[2];
// outdata[3] = a[3];
// outdata[4] = a[4];
// count = 1;
// SBUF=outdata[0];
//}
void Delay(U16 j)
{
U8 i;
for(;j>0;j--)
{
for(i=0;i<27;i++);
}
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void COM(void)
{
U8 i;
for(i=0;i<8;i++)
{
U8FLAG=2;
while((!P2_0)&&U8FLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
U8FLAG=2;
while((P2_0)&&U8FLAG++);
//3¬ê±Ôòìø3öforÑ-»·
if(U8FLAG==1)break;
//ÅD¶Ïêy¾YλêÇ0»1êÇ1
// èç1û¸ßμçƽ¸ß1yÔ¤¶¨0¸ßμçƽÖμÔòêy¾YλÎa 1
U8comdata<<=1;
U8comdata|=U8temp; //0
}//rof
}
U8 RH(void)
{
//Ö÷»úà-μí18ms
P2_0=0;
Delay(180);
P2_0=1;
//×üÏßóééÏà-μç×èà-¸ß Ö÷»úÑóê±20us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
//Ö÷»úéèÎaêäèë ÅD¶Ï′ó»úÏìó|DÅoÅ
P2_0=1;
//ÅD¶Ï′ó»úêÇ·ñóDμíμçƽÏìó|DÅoÅ èç2»Ïìó|Ôòìø3ö£¬Ïìó|ÔòÏòÏÂÔËDD
if(!P2_0) //T !
{
U8FLAG=2;
//ÅD¶Ï′ó»úêÇ·ñ·¢3ö 80us μÄμíμçƽÏìó|DÅoÅêÇ·ñ½áêø
while((!P2_0)&&U8FLAG++);
U8FLAG=2;
//ÅD¶Ï′ó»úêÇ·ñ·¢3ö 80us μĸßμçƽ£¬èç·¢3öÔò½øèëêy¾Y½óêÕ×′ì¬
while((P2_0)&&U8FLAG++);
//êy¾Y½óêÕ×′ì¬
COM();
U8RH_data_H_temp=U8comdata;
COM();
U8RH_data_L_temp=U8comdata;
COM();
U8T_data_H_temp=U8comdata;
COM();
U8T_data_L_temp=U8comdata;
COM();
U8checkdata_temp=U8comdata;
P2_0=1;
//êy¾YD£Ñé
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
{
U8RH_data_H=U8RH_data_H_temp;
U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}//fi
}//fi
return U8RH_data_H_temp;
}
#include <reg52.h>
#ifndef __DHT11_H_
#define __DHT11_H_
sbit P2_0 = P3^5 ;
typedef unsigned char U8; /* defined for unsigned 8-bits integer variable ÎT·ûoÅ8λÕûDí±äá¿ */
typedef unsigned int U16; /* defined for unsigned 16-bits integer variable ÎT·ûoÅ16λÕûDí±äá¿ */
#define Data_0_time 4
//void SendData(U8 *a);
void Delay(U16 j);
void Delay_10us(void);
void COM(void);
U8 RH(void);
#endif
|
|