|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#ifndef __24L01_H
#define __24L01_H
#include "sys.h"
//NRF24L01¼Ä´æÆ÷²Ù×÷ÃüÁî
#define NRF_READ_REG 0x00 //¶ÁÅäÖüĴæÆ÷,µÍ5λΪ¼Ä´æÆ÷µØÖ·
#define NRF_WRITE_REG 0x20 //дÅäÖüĴæÆ÷,µÍ5λΪ¼Ä´æÆ÷µØÖ·
#define RD_RX_PLOAD 0x61 //¶ÁRXÓÐЧÊý¾Ý,1~32×Ö½Ú
#define WR_TX_PLOAD 0xA0 //дTXÓÐЧÊý¾Ý,1~32×Ö½Ú
#define FLUSH_TX 0xE1 //Çå³ýTX FIFO¼Ä´æÆ÷.·¢ÉäģʽÏÂÓÃ
#define FLUSH_RX 0xE2 //Çå³ýRX FIFO¼Ä´æÆ÷.½ÓÊÕģʽÏÂÓÃ
#define REUSE_TX_PL 0xE3 //ÖØÐÂʹÓÃÉÏÒ»°üÊý¾Ý,CEΪ¸ß,Êý¾Ý°ü±»²»¶Ï·¢ËÍ.
#define NOP 0xFF //¿Õ²Ù×÷,¿ÉÒÔÓÃÀ´¶Á״̬¼Ä´æÆ÷
//SPI(NRF24L01)¼Ä´æÆ÷µØÖ·
#define CONFIG 0x00 //ÅäÖüĴæÆ÷µØÖ·;bit0:1½ÓÊÕģʽ,0·¢Éäģʽ;bit1:µçÑ¡Ôñ;bit2:CRCģʽ;bit3:CRCʹÄÜ;
//bit4:ÖжÏMAX_RT(´ïµ½×î´óÖØ·¢´ÎÊýÖжÏ)ʹÄÜ;bit5:ÖжÏTX_DSʹÄÜ;bit6:ÖжÏRX_DRʹÄÜ
#define EN_AA 0x01 //ʹÄÜ×Ô¶¯Ó¦´ð¹¦ÄÜ bit0~5,¶ÔӦͨµÀ0~5
#define EN_RXADDR 0x02 //½ÓÊÕµØÖ·ÔÊÐí,bit0~5,¶ÔӦͨµÀ0~5
#define SETUP_AW 0x03 //ÉèÖõØÖ·¿í¶È(ËùÓÐÊý¾ÝͨµÀ):bit1,0:00,3×Ö½Ú;01,4×Ö½Ú;02,5×Ö½Ú;
#define SETUP_RETR 0x04 //½¨Á¢×Ô¶¯ÖØ·¢;bit3:0,×Ô¶¯ÖØ·¢¼ÆÊýÆ÷;bit7:4,×Ô¶¯ÖØ·¢ÑÓʱ 250*x+86us
#define RF_CH 0x05 //RFͨµÀ,bit6:0,¹¤×÷ͨµÀƵÂÊ;
#define RF_SETUP 0x06 //RF¼Ä´æÆ÷;bit3:´«ÊäËÙÂÊ(0:1Mbps,1:2Mbps);bit2:1,·¢É书ÂÊ;bit0:µÍÔëÉù·Å´óÆ÷ÔöÒæ
#define STATUS 0x07 //״̬¼Ä´æÆ÷;bit0:TX FIFOÂú±êÖ¾;bit3:1,½ÓÊÕÊý¾ÝͨµÀºÅ(×î´ó:6);bit4,´ïµ½×î¶à´ÎÖØ·¢
//bit5:Êý¾Ý·¢ËÍÍê³ÉÖжÏ;bit6:½ÓÊÕÊý¾ÝÖжÏ;
#define MAX_TX 0x10 //´ïµ½×î´ó·¢ËÍ´ÎÊýÖжÏ
#define TX_OK 0x20 //TX·¢ËÍÍê³ÉÖжÏ
#define RX_OK 0x40 //½ÓÊÕµ½Êý¾ÝÖжÏ
#define OBSERVE_TX 0x08 //·¢Ëͼì²â¼Ä´æÆ÷,bit7:4,Êý¾Ý°ü¶ªÊ§¼ÆÊýÆ÷;bit3:0,ÖØ·¢¼ÆÊýÆ÷
#define CD 0x09 //Ôز¨¼ì²â¼Ä´æÆ÷,bit0,Ôز¨¼ì²â;
#define RX_ADDR_P0 0x0A //Êý¾ÝͨµÀ0½ÓÊÕµØÖ·,×î´ó³¤¶È5¸ö×Ö½Ú,µÍ×Ö½ÚÔÚÇ°
#define RX_ADDR_P1 0x0B //Êý¾ÝͨµÀ1½ÓÊÕµØÖ·,×î´ó³¤¶È5¸ö×Ö½Ú,µÍ×Ö½ÚÔÚÇ°
#define RX_ADDR_P2 0x0C //Êý¾ÝͨµÀ2½ÓÊÕµØÖ·,×îµÍ×Ö½Ú¿ÉÉèÖÃ,¸ß×Ö½Ú,±ØÐëͬRX_ADDR_P1[39:8]ÏàµÈ;
#define RX_ADDR_P3 0x0D //Êý¾ÝͨµÀ3½ÓÊÕµØÖ·,×îµÍ×Ö½Ú¿ÉÉèÖÃ,¸ß×Ö½Ú,±ØÐëͬRX_ADDR_P1[39:8]ÏàµÈ;
#define RX_ADDR_P4 0x0E //Êý¾ÝͨµÀ4½ÓÊÕµØÖ·,×îµÍ×Ö½Ú¿ÉÉèÖÃ,¸ß×Ö½Ú,±ØÐëͬRX_ADDR_P1[39:8]ÏàµÈ;
#define RX_ADDR_P5 0x0F //Êý¾ÝͨµÀ5½ÓÊÕµØÖ·,×îµÍ×Ö½Ú¿ÉÉèÖÃ,¸ß×Ö½Ú,±ØÐëͬRX_ADDR_P1[39:8]ÏàµÈ;
#define TX_ADDR 0x10 //·¢Ë͵ØÖ·(µÍ×Ö½ÚÔÚÇ°),ShockBurstTMģʽÏÂ,RX_ADDR_P0Óë´ËµØÖ·ÏàµÈ
#define RX_PW_P0 0x11 //½ÓÊÕÊý¾ÝͨµÀ0ÓÐЧÊý¾Ý¿í¶È(1~32×Ö½Ú),ÉèÖÃΪ0Ôò·Ç·¨
#define RX_PW_P1 0x12 //½ÓÊÕÊý¾ÝͨµÀ1ÓÐЧÊý¾Ý¿í¶È(1~32×Ö½Ú),ÉèÖÃΪ0Ôò·Ç·¨
#define RX_PW_P2 0x13 //½ÓÊÕÊý¾ÝͨµÀ2ÓÐЧÊý¾Ý¿í¶È(1~32×Ö½Ú),ÉèÖÃΪ0Ôò·Ç·¨
#define RX_PW_P3 0x14 //½ÓÊÕÊý¾ÝͨµÀ3ÓÐЧÊý¾Ý¿í¶È(1~32×Ö½Ú),ÉèÖÃΪ0Ôò·Ç·¨
#define RX_PW_P4 0x15 //½ÓÊÕÊý¾ÝͨµÀ4ÓÐЧÊý¾Ý¿í¶È(1~32×Ö½Ú),ÉèÖÃΪ0Ôò·Ç·¨
#define RX_PW_P5 0x16 //½ÓÊÕÊý¾ÝͨµÀ5ÓÐЧÊý¾Ý¿í¶È(1~32×Ö½Ú),ÉèÖÃΪ0Ôò·Ç·¨
#define NRF_FIFO_STATUS 0x17 //FIFO״̬¼Ä´æÆ÷;bit0,RX FIFO¼Ä´æÆ÷¿Õ±êÖ¾;bit1,RX FIFOÂú±êÖ¾;bit2,3,±£Áô
//bit4,TX FIFO¿Õ±êÖ¾;bit5,TX FIFOÂú±êÖ¾;bit6,1,Ñ»··¢ËÍÉÏÒ»Êý¾Ý°ü.0,²»Ñ»·;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//24L01²Ù×÷Ïß
#define NRF24L01_CE PAout(4) //24L01ƬѡÐźÅ
#define NRF24L01_CSN PCout(4) //SPIƬѡÐźÅ
#define NRF24L01_IRQ PAin(1) //IRQÖ÷»úÊý¾ÝÊäÈë
//24L01·¢ËͽÓÊÕÊý¾Ý¿í¶È¶¨Òå
#define TX_ADR_WIDTH 5 //5×ֽڵĵØÖ·¿í¶È
#define RX_ADR_WIDTH 5 //5×ֽڵĵØÖ·¿í¶È
#define TX_PLOAD_WIDTH 32 //32×Ö½ÚµÄÓû§Êý¾Ý¿í¶È
#define RX_PLOAD_WIDTH 32 //32×Ö½ÚµÄÓû§Êý¾Ý¿í¶È
void NRF24L01_Init(void);//³õʼ»¯
void NRF24L01_RX_Mode(void);//ÅäÖÃΪ½ÓÊÕģʽ
void NRF24L01_TX_Mode(void);//ÅäÖÃΪ·¢ËÍģʽ
u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 u8s);//дÊý¾ÝÇø
u8 NRF24L01_Read_Buf(u8 reg, u8 *pBuf, u8 u8s);//¶ÁÊý¾ÝÇø
u8 NRF24L01_Read_Reg(u8 reg); //¶Á¼Ä´æÆ÷
u8 NRF24L01_Write_Reg(u8 reg, u8 value);//д¼Ä´æÆ÷
u8 NRF24L01_Check(void);//¼ì²é24L01ÊÇ·ñ´æÔÚ
u8 NRF24L01_TxPacket(u8 *txbuf);//·¢ËÍÒ»¸ö°üµÄÊý¾Ý
u8 NRF24L01_RxPacket(u8 *rxbuf);//½ÓÊÕÒ»¸ö°üµÄÊý¾Ý
#endif
#include "24l01.h"
#include "delay.h"
#include "spi.h"
const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //·¢Ë͵ØÖ·
const u8 RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //·¢Ë͵ØÖ·
//³õʼ»¯24L01µÄIO¿Ú
void NRF24L01_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //ÉÏÀÊäÈë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);
SPI1_Init(); //³õʼ»¯SPI
SPI_Cmd(SPI1, DISABLE); //
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //Ñ¡ÔñÁË´®ÐÐʱÖÓµÄÎÈ̬:ʱÖÓÐü¿ÕµÍµçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //Êý¾Ý²¶»ñÓÚµÚÒ»¸öʱÖÓÑØ
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI1, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
NRF24L01_CE=0; //ʹÄÜ24L01
NRF24L01_CSN=1; //SPIƬѡȡÏû
}
//¼ì²â24L01ÊÇ·ñ´æÔÚ
//·µ»ØÖµ:0£¬³É¹¦;1£¬Ê§°Ü
u8 NRF24L01_Check(void)
{
u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
u8 i;
SPI1_SetSpeed(SPI_BaudRatePrescaler_8); //spiËÙ¶ÈΪ9Mhz£¨24L01µÄ×î´óSPIʱÖÓΪ10Mhz£©
NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);//дÈë5¸ö×ֽڵĵØÖ·.
NRF24L01_Read_Buf(TX_ADDR,buf,5); //¶Á³öдÈëµÄµØÖ·
for(i=0;i<5;i++)if(buf!=0XA5)break;
if(i!=5)return 1;//¼ì²â24L01´íÎó
return 0; //¼ì²âµ½24L01
}
//SPIд¼Ä´æÆ÷
//reg:Ö¸¶¨¼Ä´æÆ÷µØÖ·
//value:дÈëµÄÖµ
u8 NRF24L01_Write_Reg(u8 reg,u8 value)
{
u8 status;
NRF24L01_CSN=0; //ʹÄÜSPI´«Êä
status =SPI1_ReadWriteByte(reg);//·¢ËͼĴæÆ÷ºÅ
SPI1_ReadWriteByte(value); //дÈë¼Ä´æÆ÷µÄÖµ
NRF24L01_CSN=1; //½ûÖ¹SPI´«Êä
return(status); //·µ»Ø״ֵ̬
}
//¶ÁÈ¡SPI¼Ä´æÆ÷Öµ
//reg:Òª¶ÁµÄ¼Ä´æÆ÷
u8 NRF24L01_Read_Reg(u8 reg)
{
u8 reg_val;
NRF24L01_CSN = 0; //ʹÄÜSPI´«Êä
SPI1_ReadWriteByte(reg); //·¢ËͼĴæÆ÷ºÅ
reg_val=SPI1_ReadWriteByte(0XFF);//¶ÁÈ¡¼Ä´æÆ÷ÄÚÈÝ
NRF24L01_CSN = 1; //½ûÖ¹SPI´«Êä
return(reg_val); //·µ»Ø״ֵ̬
}
//ÔÚÖ¸¶¨Î»ÖöÁ³öÖ¸¶¨³¤¶ÈµÄÊý¾Ý
//reg:¼Ä´æÆ÷(λÖÃ)
//*pBuf:Êý¾ÝÖ¸Õë
//len:Êý¾Ý³¤¶È
//·µ»ØÖµ,´Ë´Î¶Áµ½µÄ״̬¼Ä´æÆ÷Öµ
u8 NRF24L01_Read_Buf(u8 reg,u8 *pBuf,u8 len)
{
u8 status,u8_ctr;
NRF24L01_CSN = 0; //ʹÄÜSPI´«Êä
status=SPI1_ReadWriteByte(reg);//·¢ËͼĴæÆ÷Öµ(λÖÃ),²¢¶Áȡ״ֵ̬
for(u8_ctr=0;u8_ctr<len;u8_ctr++)pBuf[u8_ctr]=SPI1_ReadWriteByte(0XFF);//¶Á³öÊý¾Ý
NRF24L01_CSN=1; //¹Ø±ÕSPI´«Êä
return status; //·µ»Ø¶Áµ½µÄ״ֵ̬
}
//ÔÚÖ¸¶¨Î»ÖÃдָ¶¨³¤¶ÈµÄÊý¾Ý
//reg:¼Ä´æÆ÷(λÖÃ)
//*pBuf:Êý¾ÝÖ¸Õë
//len:Êý¾Ý³¤¶È
//·µ»ØÖµ,´Ë´Î¶Áµ½µÄ״̬¼Ä´æÆ÷Öµ
u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 len)
{
u8 status,u8_ctr;
NRF24L01_CSN = 0; //ʹÄÜSPI´«Êä
status = SPI1_ReadWriteByte(reg);//·¢ËͼĴæÆ÷Öµ(λÖÃ),²¢¶Áȡ״ֵ̬
for(u8_ctr=0; u8_ctr<len; u8_ctr++)SPI1_ReadWriteByte(*pBuf++); //дÈëÊý¾Ý
NRF24L01_CSN = 1; //¹Ø±ÕSPI´«Êä
return status; //·µ»Ø¶Áµ½µÄ״ֵ̬
}
//Æô¶¯NRF24L01·¢ËÍÒ»´ÎÊý¾Ý
//txbuf:´ý·¢ËÍÊý¾ÝÊ×µØÖ·
//·µ»ØÖµ:·¢ËÍÍê³É×´¿ö
u8 NRF24L01_TxPacket(u8 *txbuf)
{
u8 sta;
SPI1_SetSpeed(SPI_BaudRatePrescaler_8);//spiËÙ¶ÈΪ9Mhz£¨24L01µÄ×î´óSPIʱÖÓΪ10Mhz£©
NRF24L01_CE=0;
NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//дÊý¾Ýµ½TX BUF 32¸ö×Ö½Ú
NRF24L01_CE=1;//Æô¶¯·¢ËÍ
while(NRF24L01_IRQ!=0);//µÈ´ý·¢ËÍÍê³É
sta=NRF24L01_Read_Reg(STATUS); //¶Áȡ״̬¼Ä´æÆ÷µÄÖµ
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //Çå³ýTX_DS»òMAX_RTÖжϱêÖ¾
if(sta&MAX_TX)//´ïµ½×î´óÖØ·¢´ÎÊý
{
NRF24L01_Write_Reg(FLUSH_TX,0xff);//Çå³ýTX FIFO¼Ä´æÆ÷
return MAX_TX;
}
if(sta&TX_OK)//·¢ËÍÍê³É
{
return TX_OK;
}
return 0xff;//ÆäËûÔÒò·¢ËÍʧ°Ü
}
//Æô¶¯NRF24L01·¢ËÍÒ»´ÎÊý¾Ý
//txbuf:´ý·¢ËÍÊý¾ÝÊ×µØÖ·
//·µ»ØÖµ:0£¬½ÓÊÕÍê³É£»ÆäËû£¬´íÎó´úÂë
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
u8 sta;
SPI1_SetSpeed(SPI_BaudRatePrescaler_8); //spiËÙ¶ÈΪ9Mhz£¨24L01µÄ×î´óSPIʱÖÓΪ10Mhz£©
sta=NRF24L01_Read_Reg(STATUS); //¶Áȡ״̬¼Ä´æÆ÷µÄÖµ
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //Çå³ýTX_DS»òMAX_RTÖжϱêÖ¾
if(sta&RX_OK)//½ÓÊÕµ½Êý¾Ý
{
NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//¶ÁÈ¡Êý¾Ý
NRF24L01_Write_Reg(FLUSH_RX,0xff);//Çå³ýRX FIFO¼Ä´æÆ÷
return 0;
}
return 1;//ûÊÕµ½ÈκÎÊý¾Ý
}
//¸Ãº¯Êý³õʼ»¯NRF24L01µ½RXģʽ
//ÉèÖÃRXµØÖ·,дRXÊý¾Ý¿í¶È,Ñ¡ÔñRFƵµÀ,²¨ÌØÂʺÍLNA HCURR
//µ±CE±ä¸ßºó,¼´½øÈëRXģʽ,²¢¿ÉÒÔ½ÓÊÕÊý¾ÝÁË
void NRF24L01_RX_Mode(void)
{
NRF24L01_CE=0;
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH);//дRX½ÚµãµØÖ·
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_AA,0x01); //ʹÄÜͨµÀ0µÄ×Ô¶¯Ó¦´ð
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01); //ʹÄÜͨµÀ0µÄ½ÓÊÕµØÖ·
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,40); //ÉèÖÃRFͨÐÅƵÂÊ
NRF24L01_Write_Reg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//Ñ¡ÔñͨµÀ0µÄÓÐЧÊý¾Ý¿í¶È
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f); //ÉèÖÃTX·¢Éä²ÎÊý,0dbÔöÒæ,2Mbps,µÍÔëÉùÔöÒ濪Æô
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG, 0x0f); //ÅäÖûù±¾¹¤×÷ģʽµÄ²ÎÊýWR_UP,EN_CRC,16BIT_CRC,½ÓÊÕģʽ
NRF24L01_CE = 1; //CEΪ¸ß,½øÈë½ÓÊÕģʽ
}
//¸Ãº¯Êý³õʼ»¯NRF24L01µ½TXģʽ
//ÉèÖÃTXµØÖ·,дTXÊý¾Ý¿í¶È,ÉèÖÃRX×Ô¶¯Ó¦´ðµÄµØÖ·,Ìî³äTX·¢ËÍÊý¾Ý,Ñ¡ÔñRFƵµÀ,²¨ÌØÂʺÍLNA HCURR
//PWR_UP,CRCʹÄÜ
//µ±CE±ä¸ßºó,¼´½øÈëRXģʽ,²¢¿ÉÒÔ½ÓÊÕÊý¾ÝÁË
//CEΪ¸ß´óÓÚ10us,ÔòÆô¶¯·¢ËÍ.
void NRF24L01_TX_Mode(void)
{
NRF24L01_CE=0;
NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);//дTX½ÚµãµØÖ·
NRF24L01_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH); //ÉèÖÃTX½ÚµãµØÖ·,Ö÷ҪΪÁËʹÄÜACK
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_AA,0x01); //ʹÄÜͨµÀ0µÄ×Ô¶¯Ó¦´ð
NRF24L01_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01); //ʹÄÜͨµÀ0µÄ½ÓÊÕµØÖ·
NRF24L01_Write_Reg(NRF_WRITE_REG+SETUP_RETR,0x1a);//ÉèÖÃ×Ô¶¯ÖØ·¢¼ä¸ôʱ¼ä:500us + 86us;×î´ó×Ô¶¯ÖØ·¢´ÎÊý:10´Î
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_CH,40); //ÉèÖÃRFͨµÀΪ40
NRF24L01_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f); //ÉèÖÃTX·¢Éä²ÎÊý,0dbÔöÒæ,2Mbps,µÍÔëÉùÔöÒ濪Æô
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e); //ÅäÖûù±¾¹¤×÷ģʽµÄ²ÎÊýWR_UP,EN_CRC,16BIT_CRC,½ÓÊÕģʽ,¿ªÆôËùÓÐÖжÏ
NRF24L01_CE=1;//CEΪ¸ß,10usºóÆô¶¯·¢ËÍ
}
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "key.h"
#include "spi.h"
#include "24l01.h"
int main(void)
{
u8 key,mode;
u16 t=0;
u8 tmp_buf[33];
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// ÉèÖÃÖжÏÓÅÏȼ¶·Ö×é2
uart_init(9600); //´®¿Ú³õʼ»¯Îª9600
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
LCD_Init(); //³õʼ»¯LCD
KEY_Init(); //°´¼ü³õʼ»¯
NRF24L01_Init(); //³õʼ»¯NRF24L01
POINT_COLOR=RED;//ÉèÖÃ×ÖÌåΪºìÉ«
LCD_ShowString(60,50,200,16,16,"Mini STM32");
LCD_ShowString(60,70,200,16,16,"NRF24L01 TEST");
LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(60,110,200,16,16,"2014/3/12");
while(NRF24L01_Check()) //¼ì²éNRF24L01ÊÇ·ñÔÚλ.
{
LCD_ShowString(60,130,200,16,16,"NRF24L01 Error");
delay_ms(200);
LCD_Fill(60,130,239,130+16,WHITE);
delay_ms(200);
}
LCD_ShowString(60,130,200,16,16,"NRF24L01 OK");
while(1)//Ôڸò¿·ÖÈ·¶¨½øÈëÄĸöģʽ!
{
key=KEY_Scan(0);
if(key==KEY0_PRES)
{
mode=0;
break;
}else if(key==KEY1_PRES)
{
mode=1;
break;
}
t++;
if(t==100)LCD_ShowString(10,150,230,16,16,"KEY0:RX_Mode KEY1:TX_Mode"); //ÉÁ˸ÏÔʾÌáʾÐÅÏ¢
if(t==200)
{
LCD_Fill(10,150,230,150+16,WHITE);
t=0;
}
delay_ms(5);
}
LCD_Fill(10,150,240,166,WHITE);//Çå¿ÕÉÏÃæµÄÏÔʾ
POINT_COLOR=BLUE;//ÉèÖÃ×ÖÌåΪÀ¶É«
if(mode==0)//RXģʽ
{
LCD_ShowString(60,150,200,16,16,"NRF24L01 RX_Mode");
LCD_ShowString(60,170,200,16,16,"Received DATA:");
NRF24L01_RX_Mode();
while(1)
{
if(NRF24L01_RxPacket(tmp_buf)==0)//Ò»µ©½ÓÊÕµ½ÐÅÏ¢,ÔòÏÔʾ³öÀ´.
{
tmp_buf[32]=0;//¼ÓÈë×Ö·û´®½áÊø·û
LCD_ShowString(0,190,239,32,16,tmp_buf);
}else delay_us(100);
t++;
if(t==10000)//´óÔ¼1sÖӸıäÒ»´Î״̬
{
t=0;
LED0=!LED0;
}
};
}else//TXģʽ
{
LCD_ShowString(60,150,200,16,16,"NRF24L01 TX_Mode");
NRF24L01_TX_Mode();
mode=' ';//´Ó¿Õ¸ñ¼ü¿ªÊ¼
while(1)
{
if(NRF24L01_TxPacket(tmp_buf)==TX_OK)
{
LCD_ShowString(60,170,239,32,16,"Sended DATA:");
LCD_ShowString(0,190,239,32,16,tmp_buf);
key=mode;
for(t=0;t<32;t++)
{
key++;
if(key>('~'))key=' ';
tmp_buf[t]=key;
}
mode++;
if(mode>'~')mode=' ';
tmp_buf[32]=0;//¼ÓÈë½áÊø·û
}else
{
LCD_ShowString(60,170,239,32,16,"Send Failed ");
LCD_Fill(0,188,240,218,WHITE);//Çå¿ÕÉÏÃæµÄÏÔʾ
};
LED0=!LED0;
delay_ms(1500);
};
}
} |
|