|
基于51单片机的nRF24L01无线传感模块的接收个发送的程序。
单片机源程序如下:
- #include <reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit RS = P0^2; //定义端口
- sbit RW = P0^1;
- sbit EN = P0^0;
- sbit DQ = P0^3; //定义总线的I/O管脚
- bit crrent;
- uint value;
- uchar data disdat[6];
- uchar data setdat[4];
- #define TX_ADR_WIDTH 5
- #define TX_PLOAD_WIDTH 2
- uchar const TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x55,0x10,0x10,0x01};
- uchar rx_buf[TX_PLOAD_WIDTH];
- uchar tx_buf[TX_PLOAD_WIDTH];
- uchar distance_data[2];
- uchar flag;
- sbit CE=P2^5;
- sbit CSN=P2^0;
- sbit SCK=P2^4;
- sbit MOSI=P2^1;
- sbit MISO=P2^3;
- sbit IRQ=P2^2;
- uchar bdata sta;
- sbit RX_DR =sta^6;
- sbit TX_DS =sta^5;
- sbit MAX_RT =sta^4;
-
- unsigned char time;
- #define READ_REG 0x00
- #define WRITE_REG 0x20
- #define RD_RX_PLOAD 0x61
- #define WR_TX_PLOAD 0xA0
- #define FLUSH_TX 0xE1
- #define FLUSH_RX 0xE2
- #define REUSE_TX_PL 0xE3
- #define NOP 0xFF
- #define CONFIG 0x00
- #define EN_AA 0x01
- #define EN_RXADDR 0x02
- #define SETUP_AW 0x03
- #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
- #define RX_ADDR_P1 0x0B
- #define RX_ADDR_P2 0x0C
- #define RX_ADDR_P3 0x0D
- #define RX_ADDR_P4 0x0E
- #define RX_ADDR_P5 0x0F
- #define TX_ADDR 0x10
- #define RX_PW_P0 0x11
- #define RX_PW_P1 0x12
- #define RX_PW_P2 0x13
- #define RX_PW_P3 0x14
- #define RX_PW_P4 0x15
- #define RX_PW_P5 0x16
- #define FIFO_STATUS 0x17
- void init_io(void)
- {
- CE=0;
- CSN=1;
- SCK=0;
- }
- void delay_ms(unsigned int x)
- {
- unsigned int i,j;
- for(i=0;i<x;i++)
- {
- j=108;
- while(j--);
- }
- }
- uchar SPI_RW(uchar byte)
- {
- uchar bit_ctr;
- for(bit_ctr=0;bit_ctr<8;bit_ctr++)
- {
- MOSI = (byte&0x80);
- byte = (byte<<1);
- SCK = 1;
- byte|=MISO;
- SCK=0;
- }
- return(byte);
- }
- uchar SPI_RW_Reg(uchar reg, uchar value)
- {
- uchar status;
- CSN = 0;
- status = SPI_RW(reg);
- SPI_RW(value);
- CSN = 1;
- return(status);
- }
- uchar SPI_Read(uchar reg)
- {
- uchar reg_val;
- CSN = 0;
- SPI_RW(reg);
- reg_val = SPI_RW(0);
- CSN = 1;
- return(reg_val);
- }
- uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes)
- {
- uchar status,byte_ctr;
- CSN = 0;
- status = SPI_RW(reg);
- for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
- pBuf[byte_ctr] = SPI_RW(0);
- CSN = 1;
- return(status);
- }
- uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes)
- {
- uchar status,byte_ctr;
- CSN = 0;
- status = SPI_RW(reg);
- for(byte_ctr=0;
- byte_ctr<bytes; byte_ctr++)
- SPI_RW(*pBuf++);
- CSN = 1;
- return(status);
- }
- void TX_Mode(void)
- {
- CE=0;
- SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
- SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);
- SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);
- SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
- SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a);
- SPI_RW_Reg(WRITE_REG + RF_CH,40);
- SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
- SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);
- CE=1;
- }
- void checkflag()
- {
- sta=SPI_Read(STATUS);
- if(RX_DR)
- {
- SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
- flag=1;
- }
- if(MAX_RT)
- {
- SPI_RW_Reg(FLUSH_TX,0);
- }
- SPI_RW_Reg(WRITE_REG+STATUS,sta);
- }
- void SendByte(unsigned char dat);
- void zf_1602(unsigned char x,unsigned char y,unsigned dat);
- void Delay4us() //延时4us
- {
- ;
- }
- void Delay(unsigned char j) //一个循环15us
- {
- unsigned char i;
- while(j--)
- {
- i = 5;
- while (--i);
- }
- }
- bit d18b20_qs() //18b20 起始
- {
-
- DQ = 1; //DQ复位
- Delay4us();
- DQ = 0; //拉低总线
- Delay(35);
- DQ = 1;
- Delay(2);
- crrent= DQ;
- Delay(2);
- return crrent; //返回数值
- }
- void d18b20_x(unsigned char dat) //写 8 位 数 据
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- DQ = 0; //拉低总线
- DQ = dat & 0x01;
- Delay(3); //延时45us
- DQ = 1;
- dat >>= 1; //数据右移一位
- }
- }
- unsigned char d18b20_d() //读 8 位 数 据
- {
- unsigned char i,dat=0;
- for(i=0;i<8;i++) //8位计数器
- {
- DQ = 0; //拉低总线
- dat >>= 1; //数据右移一位
- DQ = 1;
- if(DQ)
- dat |= 0x80;
- Delay(4);
- }
- return dat; //返回读取到数据数据
- }
- unsigned int wd() //读取温度函数
- {
- unsigned char i = 0; //低8位数据
- unsigned char j = 0; //高8位数据
- unsigned int k = 0;
- d18b20_qs();
- d18b20_x(0xCC);
- d18b20_x(0x44);
- Delay(200);
-
- d18b20_qs();
- d18b20_x(0xCC);
- d18b20_x(0xBE);
- i = d18b20_d();
- j = d18b20_d();
- distance_data[0]=j;
- distance_data[1]=i;
- k = j;
- k <<= 8;
- k = k + i;
- return k; //返回读取到的16位数据
- }
- void zh(unsigned int i)
- {
- unsigned char x,z;
- x = i & 0x0f; //取出小数
- i >>=4;
- switch(x) //小数位转换
- {
- case 0: z=0;break;
- case 1: z=1;break;
- case 2: z=1;break;
- case 3: z=2;break;
- case 4: z=3;break;
- case 5: z=3;break;
- case 6: z=4;break;
- case 7: z=4;break;
- case 8: z=5;break;
- case 9: z=6;break;
- case 10: z=6;break;
- case 11: z=7;break;
- case 12: z=8;break;
- case 13: z=8;break;
- case 14: z=9;break;
- case 15: z=9;break;
- }
- z = z +48; //转换成ascii码
- zf_1602(8,1,z); //写入1602缓存
- z = i & 0xff; //取出整数
- x = z/10; //取出十位
- x= x+48;
- zf_1602(5,1,x);
- x = z%10;
- x= x+48;
- zf_1602(6,1,x);
- }
- void Delayus(unsigned char t)
- {
- while(--t);
- }
- void Delayms(unsigned char t)
- {
-
- while(t--)
- {
- //大致延时1mS
- Delayus(245);
- Delayus(245);
- }
- }
- bit m_1602() //判断1602是否忙
- {
- P1 = 0xFF;
- RS = 0;
- RW = 1;
- EN = 0;
- _nop_();
- EN = 1;
- return (bit)(P1 & 0x80);
- }
- void x_1602(bit i,unsigned char j) //参数一是写(0、写指令 1、写数据),参数二是写入的8位数据
- {
- while(m_1602())
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- RS = i;
- RW = 0;
- EN = 1;
- P1 = j;
- _nop_();
- EN = 0; //产生下降沿
- }
- void qp_1602() //清屏函数
- {
- x_1602(0,0x01); //第一个参数是:写入的类型(0、写指令 1、写数据),第一个参数是:写入的数据
- Delayms(5);
- }
- //显示字符
- void zf_1602(unsigned char x,unsigned char y,unsigned dat) //参数一是显示的列,参数二是显示的行,参数三是显示的数据
- {
- if(y==0)
- {
- x_1602(0,(0x80+x)); //第一行
- }
- else
- {
- x_1602(0,(0xc0+x)); //第二行
- }
- x_1602(1,dat); //写入数据
- }
- void zfc_1602(unsigned char x,unsigned char y,unsigned char *dat)
- {
- if(y==0)
- {
- x_1602(0,(0x80+x));
- }
- else
- {
- x_1602(0,(0xc0+x));
- }
- while(*dat) //&:取地址 *:取值
- {
- x_1602(1,*dat);
- dat ++;
- }
- }
- void csh_1602() //初始化1602
- {
- x_1602(0,0x38);
- Delayms(5);
- x_1602(0,0x38);
- Delayms(5);
- x_1602(0,0x38);
- Delayms(5);
- x_1602(0,0x38);
- x_1602(0,0x08);
- x_1602(0,0x01);
- x_1602(0,0x06);
- Delayms(5);
- x_1602(0,0x0c);
- }
- void Lcd_User_Chr(void) // 1602 自定义字符(摄氏度的小点) 自定义字符
- {
- x_1602(0,0x48); //"01 001 000" 第1行地址
- x_1602(1,0x03); //"XXX 00001" 第1行数据
- x_1602(0,0x49); //"01 001 001" 第2行地址
- x_1602(1,0x03); //"XXX 11011" 第2行数据
- x_1602(0,0x4a); //"01 001 010" 第3行地址
- x_1602(1,0x00); //"XXX 11101" 第3行数据
- x_1602(0,0x4b); //"01 001 011" 第4行地址
- x_1602(1,0x00); //"XXX 11001" 第4行数据
- x_1602(0,0x4c); //"01 001 100" 第5行地址
- x_1602(1,0x00); //"XXX 11101" 第5行数据
- x_1602(0,0x4d); //"01 001 101" 第6行地址
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
无线收发代码.rar
(5 KB, 下载次数: 48)
|
|