|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
刚才是从机的
#include <reg52.h>
#include "nrf.h"
#include"lcd.h"
uchar RX_DATA[8]={0};
//void UART_INIT() //9600
//{
// TMOD = 0x20;
// SCON = 0x50;
// TH1 = 0xFD;
// TL1 = TH1;
// PCON = 0x00;
// TR1 = 1;
//}
void LcdDisplay1() //lcdÏÔê¾
{
uchar a,b;
a = RX_DATA[1]/10;
b = RX_DATA[1]%10;
LcdWriteCom(0x87);
LcdWriteData('0'+a);
LcdWriteCom(0x88);
LcdWriteData('0'+b);
}
void LcdDisplay2() //lcdÏÔê¾
{
if(RX_DATA[0] == 'm')
{
LcdWriteCom(0x80+0x40+5);
LcdWriteData('-');
}
if(RX_DATA[0] == 'n')
{
LcdWriteCom(0x80+0x40+5);
LcdWriteData('+');
}
LcdWriteCom(0x80+0x40+6);
LcdWriteData('0'+RX_DATA[1]);
LcdWriteCom(0x80+0x40+7);
LcdWriteData('0'+RX_DATA[2]);
LcdWriteCom(0x80+0x40+8);
LcdWriteData('0'+RX_DATA[3]);
LcdWriteCom(0x80+0x40+9);
LcdWriteData('.');
LcdWriteCom(0x80+0x4a);
LcdWriteData('0'+RX_DATA[5]);
LcdWriteCom(0x80+0x4b);
LcdWriteData('0'+RX_DATA[6]);
}
void main()
{
uint a;
LcdInit(); //16023õê¼»ˉ
// UART_INIT(); //′®¿úéè3õê¼»ˉ
init_NRF24L01(); //nrf3õê¼»ˉ
// if(NRF24L01_Check() == 0 )
// send_UART(0x66); //¼ì2aμ½nrf
// else
// send_UART(0x2b); //ûóD¼ì2aμ½nrf
while(1)
{
a=800;
nRF24L01_RxMode();
while(a--)
{
NRF_RX_CheckEvent(RX_DATA);
}
if( RX_DATA[7] == 'b' )
LcdDisplay1();
a=800;
nRF24L01_RxMode1();
while(a--)
{
NRF_RX_CheckEvent(RX_DATA);
}
if( RX_DATA[7] == 'a' )
LcdDisplay2();
/*′®¿úÏÔê¾½óêÕμ½μÄζè*/
// if( (RX_DATA[7] == 'a' )||(RX_DATA[7] == 'b') )
// for(a=0;a<7;a++)
// {
// SBUF = RX_DATA[a];
// while(TI ==0);
// TI = 0;
// }
}
}
#include "nrf.h"
//****************************************IO¶Ë¿ú¶¨òå***************************************
uchar num=0;
extern uchar RX_DATA[8];
const uchar TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //±¾μØμØÖ·
const uchar RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //½óêÕμØÖ·
const uchar TX_ADDRESS1[TX_ADR_WIDTH]= {0x33,0x42,0x11,0x12,0x00}; //±¾μØμØÖ·
const uchar RX_ADDRESS1[RX_ADR_WIDTH]= {0x33,0x42,0x11,0x12,0x00}; //½óêÕμØÖ·
/******************************************************************************************
/*Ñóê±oˉêy
/******************************************************************************************/
//void delay10ms(void) //Îó2î -1.084010840108us
//{
// unsigned char a,b;
// for(b=21;b>0;b--)
// for(a=218;a>0;a--);
//}
void delay130us(void)
{
unsigned char a,b;
for(b=19;b>0;b--)
for(a=2;a>0;a--);
}
//void delay1s(void) //Îó2î -1.084010840006us
//{
// unsigned char a,b,c;
// for(c=143;c>0;c--)
// for(b=208;b>0;b--)
// for(a=14;a>0;a--);
// _nop_(); //if Keil,require use intrins.h
//}
//****************************************************************************************
/*NRF24L013õê¼»ˉ
//***************************************************************************************/
void init_NRF24L01(void)
{
CE=0; // chip enable
CSN=1; // Spi disable
SCK=0; // Õa¸ö±ØDëòa¼ó£¬2»è»spií¨DÅ»á꧰ü
IRQ =1;
CE=0;
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 + RF_CH, 40); // éèÖÃDÅμà1¤×÷Îa2.4GHZ£¬êÕ·¢±ØDëò»ÖÂ
SPI_RW_Reg(WRITE_REG + RX_PW_P0, 32);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //éèÖ÷¢éäËùÂêÎa2MHZ£¬·¢éä1|ÂêÎa×î′óÖμ0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0F); // IRQêÕ·¢íê3éÖD¶ÏÏìó|£¬16λCRC£¬Ö÷½óêÕ
SPI_RW(0x50);//Æá±Î½óêÕÖD¶Ï
SPI_RW(0x73);//
SPI_RW_Reg(WRITE_REG+0x1c,0x01);
SPI_RW_Reg(WRITE_REG+0x1d,0x06);
CE=1;
delay130us();
}
/****************************************************************************************************
/*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
//}
/******************************************************************************************************/
/*oˉêy£ounsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
/*1|Äü£oêy¾Y¶áè¡oó·Åèçrx_buf½óêÕ»o3åÇøÖD
/******************************************************************************************************/
void nRF24L01_RxMode()
{
CE=0;
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 + RF_CH, 40); // éèÖÃDÅμà1¤×÷Îa2.4GHZ£¬êÕ·¢±ØDëò»ÖÂ
SPI_RW_Reg(WRITE_REG + RX_PW_P0, 32);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //éèÖ÷¢éäËùÂêÎa2MHZ£¬·¢éä1|ÂêÎa×î′óÖμ0dB
SPI_RW_Reg(FLUSH_TX,0xff);
SPI_RW_Reg(FLUSH_RX,0xff);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0F); // 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;
delay130us();
}
void nRF24L01_RxMode1(void)
{
CE=0; // chip enable
CSN=1; // Spi disable
SCK=0; // Õa¸ö±ØDëòa¼ó£¬2»è»spií¨DÅ»á꧰ü
IRQ =1;
CE=0;
SPI_Write_Buf(WRITE_REG + TX_ADDR, (uchar*)TX_ADDRESS1, TX_ADR_WIDTH); // D′±¾μØμØÖ·
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,(uchar*)RX_ADDRESS1, 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 + RF_CH, 40); // éèÖÃDÅμà1¤×÷Îa2.4GHZ£¬êÕ·¢±ØDëò»ÖÂ
SPI_RW_Reg(WRITE_REG + RX_PW_P0, 32);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //éèÖ÷¢éäËùÂêÎa2MHZ£¬·¢éä1|ÂêÎa×î′óÖμ0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0F); // IRQêÕ·¢íê3éÖD¶ÏÏìó|£¬16λCRC£¬Ö÷½óêÕ
SPI_RW(0x50);//Æá±Î½óêÕÖD¶Ï
SPI_RW(0x73);//
SPI_RW_Reg(WRITE_REG+0x1c,0x01);
SPI_RW_Reg(WRITE_REG+0x1d,0x06);
CE=1;
delay130us();
}
void NRF_RX_CheckEvent(unsigned char* rx_buf)
{
unsigned char sta;
sta=SPI_Read(STATUS); // ¶áè¡×′쬼Ä′æÆäà′ÅD¶Ïêy¾Y½óêÕ×′¿ö
SPI_RW_Reg(WRITE_REG+STATUS,sta); //½óêÕμ½êy¾YoóRX_DR,TX_DS,MAX_PT¶¼ÖøßÎa1£¬í¨1yD′1à′Çå3tÖD¶Ï±êÖ¾
if(sta & RX_OK) // ÅD¶ÏêÇ·ñ½óêÕμ½êy¾Y
{
// rx_len = SPI_Read(R_RX_PL_WID);
SPI_Read_Buf(RD_RX_PLOAD,rx_buf+num,1);//¶áè¡êy¾Y
// send_UART(rx_buf[0]);
num++;
if(rx_buf[0] == 'a' || rx_buf[0] == 'b' || num == 8)
num= 0;
}
if(sta & MAX_TX)
{
if(sta & 0x01) //TX FIFO FULL
{
SPI_RW_Reg(FLUSH_TX,0xff);
}
}
}
//void send_UART(unsigned char i)
//{
// ES = 0;
// TI = 0;
// SBUF = i;
// while(TI ==0);
// TI = 0;
// ES = 1;
//}
#include <reg52.h>
#include "intrins.h"
#ifndef __NRF_H_
#define __NRF_H_
sbit CSN =P2^2;
sbit MOSI =P2^3;
sbit IRQ =P2^4;
sbit MISO =P2^5;
sbit SCK =P2^6;
sbit CE =P2^7;
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 delay130us(void);
//void delay10ms(void);
void delay1s(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_RxMode(void);
void nRF24L01_RxMode1(void);
void NRF_RX_CheckEvent(unsigned char* rx_buf);
//void send_UART(unsigned char i);
#endif
#include"lcd.h"
/*******************************************************************************
* oˉ êy Ãû : Lcd1602_Delay1ms
* oˉêy1|Äü : Ñóê±oˉêy£¬Ñóê±1ms
* êä èë : c
* êä 3ö : ÎT
* Ëμ Ãû : ¸ÃoˉêyêÇÔú12MHZ¾§ÕñÏ£¬12·ÖÆμμ¥Ƭ»úμÄÑó걡£
*******************************************************************************/
void Lcd1602_Delay1ms(uint c) //Îó2î 0us
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
/*******************************************************************************
* oˉ êy Ãû : LcdWriteCom
* oˉêy1|Äü : ÏòLCDD′èëò»¸ö×Ö½úμÄÃüáî
* êä èë : com
* êä 3ö : ÎT
*******************************************************************************/
#ifndef LCD1602_4PINS //μ±Ã»óD¶¨òåÕa¸öLCD1602_4PINSê±
void LcdWriteCom(uchar com) //D′èëÃüáî
{
LCD1602_E = 0; //ê1Äü
LCD1602_RS = 0; //Ñ¡Ôñ·¢ËíÃüáî
LCD1602_RW = 0; //Ñ¡ÔñD′èë
LCD1602_DATAPINS = com; //·ÅèëÃüáî
Lcd1602_Delay1ms(1); //μè′yêy¾YÎ趨
LCD1602_E = 1; //D′èëê±Dò
Lcd1602_Delay1ms(5); //±£3Öê±¼ä
LCD1602_E = 0;
}
#else
void LcdWriteCom(uchar com) //D′èëÃüáî
{
LCD1602_E = 0; //ê1ÄüÇåáã
LCD1602_RS = 0; //Ñ¡ÔñD′èëÃüáî
LCD1602_RW = 0; //Ñ¡ÔñD′èë
LCD1602_DATAPINS = com; //óéóú4λμĽóÏßêǽóμݢ¿úμĸßËÄ룬ËùòÔ′«Ëí¸ßËÄλ2»óøÄ
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //D′èëê±Dò
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
// Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = com << 4; //·¢ËíμíËÄλ
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //D′èëê±Dò
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* oˉ êy Ãû : LcdWriteData
* oˉêy1|Äü : ÏòLCDD′èëò»¸ö×Ö½úμÄêy¾Y
* êä èë : dat
* êä 3ö : ÎT
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdWriteData(uchar dat) //D′èëêy¾Y
{
LCD1602_E = 0; //ê1ÄüÇåáã
LCD1602_RS = 1; //Ñ¡Ôñêäèëêy¾Y
LCD1602_RW = 0; //Ñ¡ÔñD′èë
LCD1602_DATAPINS = dat; //D′èëêy¾Y
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //D′èëê±Dò
Lcd1602_Delay1ms(5); //±£3Öê±¼ä
LCD1602_E = 0;
}
#else
void LcdWriteData(uchar dat) //D′èëêy¾Y
{
LCD1602_E = 0; //ê1ÄüÇåáã
LCD1602_RS = 1; //Ñ¡ÔñD′èëêy¾Y
LCD1602_RW = 0; //Ñ¡ÔñD′èë
LCD1602_DATAPINS = dat; //óéóú4λμĽóÏßêǽóμݢ¿úμĸßËÄ룬ËùòÔ′«Ëí¸ßËÄλ2»óøÄ
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //D′èëê±Dò
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
LCD1602_DATAPINS = dat << 4; //D′èëμíËÄλ
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //D′èëê±Dò
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* oˉ êy Ãû : LcdInit()
* oˉêy1|Äü : 3õê¼»ˉLCDÆá
* êä èë : ÎT
* êä 3ö : ÎT
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdInit() //LCD3õê¼»ˉ×ó3ìDò
{
LcdWriteCom(0x38); //¿aÏÔê¾
LcdWriteCom(0x0c); //¿aÏÔê¾2»ÏÔê¾1a±ê
LcdWriteCom(0x06); //D′ò»¸öÖ¸Õë¼ó1
LcdWriteCom(0x01); //ÇåÆá
LcdWriteCom(0x80); //éèÖÃêy¾YÖ¸ÕëÆeμã
}
#else
void LcdInit() //LCD3õê¼»ˉ×ó3ìDò
{
LcdWriteCom(0x32); //½«8λ×üÏß×aÎa4λ×üÏß
LcdWriteCom(0x28); //ÔúËÄλÏßÏÂμÄ3õê¼»ˉ
LcdWriteCom(0x0c); //¿aÏÔê¾2»ÏÔê¾1a±ê
LcdWriteCom(0x06); //D′ò»¸öÖ¸Õë¼ó1
LcdWriteCom(0x01); //ÇåÆá
LcdWriteCom(0x80); //éèÖÃêy¾YÖ¸ÕëÆeμã
LcdWriteCom(0x80);
LcdWriteData('H');
LcdWriteCom(0x81);
LcdWriteData(':');
LcdWriteCom(0x80+0x0c);
LcdWriteData('%');
LcdWriteCom(0x80+0x40+0);
LcdWriteData('T');
LcdWriteCom(0x80+0x40+1);
LcdWriteData(':');
LcdWriteCom(0x80+0x40+0x0c);
LcdWriteData('C');
}
#endif
#ifndef __LCD_H_
#define __LCD_H_
/**********************************
μ±ê1óÃμÄêÇ4λêy¾Y′«êäμÄê±oò¶¨ò壬
ê1óÃ8λè¡ÏûÕa¸ö¶¨òå
**********************************/
#define LCD1602_4PINS
/**********************************
°üo¬í·Îļt
**********************************/
#include<reg52.h>
//---Öض¨òå1ؼü′ê---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
/**********************************
PIN¿ú¶¨òå
**********************************/
#define LCD1602_DATAPINS P1
sbit LCD1602_E=P3^5;
sbit LCD1602_RW=P3^6;
sbit LCD1602_RS=P3^7;
/**********************************
oˉêyéùÃ÷
**********************************/
/*Ôú51μ¥Ƭ»ú12MHZê±ÖóÏÂμÄÑóê±oˉêy*/
void Lcd1602_Delay1ms(uint c); //Îó2î 0us
/*LCD1602D′èë8λÃüáî×óoˉêy*/
void LcdWriteCom(uchar com);
/*LCD1602D′èë8λêy¾Y×óoˉêy*/
void LcdWriteData(uchar dat) ;
/*LCD16023õê¼»ˉ×ó3ìDò*/
void LcdInit();
#endif |
|