此程序為NRF24L01無線模塊的接收程序,RevTempDate是用來存放接收到的數(shù)據(jù),最后一位存放結(jié)束碼。#include #define unchar unsigned char#define unint unsigned int/*******************************************************/#define TX_ADDR_WITDH 5//發(fā)送地址寬度設置為5個字節(jié)#define RX_ADDR_WITDH 5//接收地址寬度設置為5個字節(jié)#define TX_DATA_WITDH 4//發(fā)送數(shù)據(jù)寬度1個字節(jié)#define RX_DATA_WITDH 4//接收數(shù)據(jù)寬度1個字節(jié)/*******************命令寄存器***************************/#define R_REGISTER 0x00//讀取配置寄存器#define W_REGISTER 0x20//寫配置寄存器#define R_RX_PAYLOAD 0x61//讀取RX有效數(shù)據(jù)#define W_TX_PAYLOAD 0xa0//寫TX有效數(shù)據(jù)#define FLUSH_TX 0xe1//清除TXFIFO寄存器#define FLUSH_RX 0xe2//清除RXFIFO寄存器#define REUSE_TX_PL 0xe3//重新使用上一包有效數(shù)據(jù)#define NOP 0xff//空操作/******************寄存器地址****************************/#define CONFIG 0x00//配置寄存器#define EN_AA 0x01//使能自動應答#define EN_RXADDR 0x02//接收通道使能0-5個通道#define SETUP_AW 0x03//設置數(shù)據(jù)通道地址寬度3-5#define SETUP_RETR 0x04//建立自動重發(fā)#define RF_CH 0x05//射頻通道設置#define RF_SETUP 0x06//射頻寄存器#define STATUS 0x07//狀態(tài)寄存器#define OBSERVE_TX 0x08//發(fā)送檢測寄存器#define CD 0x09//載波#define RX_ADDR_P0 0x0a//數(shù)據(jù)通道0接收地址#define RX_ADDR_P1 0x0b//數(shù)據(jù)通道1接收地址#define RX_ADDR_P2 0x0c//數(shù)據(jù)通道2接收地址#define RX_ADDR_P3 0x0d//數(shù)據(jù)通道3接收地址#define RX_ADDR_P4 0x0e//數(shù)據(jù)通道4接收地址#define RX_ADDR_P5 0x0f//數(shù)據(jù)通道5接收地址#define TX_ADDR 0x10//發(fā)送地址#define RX_PW_P0 0x11//P0通道數(shù)據(jù)寬度設置#define RX_PW_P1 0x12//P1通道數(shù)據(jù)寬度設置#define RX_PW_P2 0x13//P2通道數(shù)據(jù)寬度設置#define RX_PW_P3 0x14//P3通道數(shù)據(jù)寬度設置#define RX_PW_P4 0x15//P4通道數(shù)據(jù)寬度設置#define RX_PW_P5 0x16//P5通道數(shù)據(jù)寬度設置#define FIFO_STATUS 0x17//FIFO狀態(tài)寄存器/*******************相關函數(shù)聲明**************************/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();unchar sta;/******************SPI口定義*******************************/sbit CE=P2^0; //RX/TX模式選擇端sbit IRQ=P2^5; //可屏蔽中斷端sbit CSN=P2^3; //SPI片選端//就是SSsbit MOSI=P2^4;//SPI主機輸出從機輸入端sbit MISO=P2^2;//SPI主機輸出從機輸出端sbit SCLK=P2^1;//SPI時鐘端unchar RevTempDate[5];//最后一位用來存放結(jié)束標志unchar code TxAddr[]={0x34,0x43,0x10,0x10,0x01};//發(fā)送地址/*****************狀態(tài)標志*****************************************/unchar bdata sta; //狀態(tài)標志sbit RX_DR=sta^6;sbit TX_DS=sta^5;sbit MAX_RT=sta^4;/*****************SPI時序函數(shù)******************************************/unchar NRFSPI(unchar date){unchar i;for(i=0;i<8;i++) // 循環(huán)8次{if(date&0x80)MOSI=1;elseMOSI=0; // byte最高位輸出到MOSIdate<<=1; // 低一位移位到最高位SCLK=1;if(MISO) // 拉高SCK,nRF24L01從MOSI讀入1位數(shù)據(jù),同時從MISO輸出1位數(shù)據(jù)date|=0x01; // 讀MISO到byte最低位SCLK=0; // SCK置低}return(date); // 返回讀出的一字節(jié)}/**********************NRF24L01初始化函數(shù)*******************************/void NRF24L01Int(){Delay(2);//讓系統(tǒng)什么都不干CE=0; //待機模式1 CSN=1; SCLK=0;IRQ=1;}/*****************SPI讀寄存器一字節(jié)函數(shù)*********************************/unchar NRFReadReg(unchar RegAddr){unchar BackDate;CSN=0;//啟動時序NRFSPI(RegAddr);//寫寄存器地址BackDate=NRFSPI(0x00);//寫入讀寄存器指令 CSN=1;return(BackDate); //返回狀態(tài)}
評論