新手入门
- 积分
- 14
- 金钱
- 14
- 注册时间
- 2016-3-22
- 在线时间
- 4 小时
|
1金钱
int main(void)
{
u8 key,mode;
u16 t=0;
u8 tmp_buf[8];
//u8 tmp_buf1[3];
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃÖжÏÓÅÏȼ¶·Ö×éΪ×é2£º2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
uart_init(115200); //´®¿Ú³õʼ»¯Îª115200
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
KEY_Init(); //³õʼ»¯°´¼ü
LCD_Init(); //³õʼ»¯LCD
NRF24L01_Init(); //³õʼ»¯NRF24L01
POINT_COLOR=RED; //ÉèÖÃ×ÖÌåΪºìÉ«
while(NRF24L01_Check())
{
LCD_ShowString(30,130,200,16,16,"NRF24L01 Error");
delay_ms(200);
LCD_Fill(30,130,239,130+16,WHITE);
delay_ms(200);
}
LCD_ShowString(30,130,200,16,16,"NRF24L01 OK");
while(1)
{
NRF24L01_RX_Mode();
if(NRF24L01_RxPacket(tmp_buf)==0)
{
LCD_Clear(WHITE);
LCD_ShowString(30,200,200,24,24,tmp_buf);
delay_ms(3000);
}
}
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_GPIOB|RCC_APB2Periph_GPIOG, ENABLE); //ʹÄÜB,G¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PB12ÉÏÀ ·ÀÖ¹W25XµÄ¸ÉÈÅ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //³õʼ»¯Ö¸¶¨IO
GPIO_SetBits(GPIOB,GPIO_Pin_12);//ÉÏÀ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8; //PG8 7 ÍÆÍì
GPIO_Init(GPIOG, &GPIO_InitStructure);//³õʼ»¯Ö¸¶¨IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PG6 ÊäÈë
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_ResetBits(GPIOG,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);//PG6,7,8ÉÏÀ
SPI2_Init(); //³õʼ»¯SPI
SPI_Cmd(SPI2, DISABLE); // SPIÍâÉ費ʹÄÜ
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //SPIÖ÷»ú
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //·¢ËͽÓÊÕ8λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //ʱÖÓÐü¿ÕµÍ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //Êý¾Ý²¶»ñÓÚµÚ1¸öʱÖÓÑØ
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÈí¼þ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI2, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
SPI_Cmd(SPI2, ENABLE); //ʹÄÜSPIÍâÉè
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;
SPI2_SetSpeed(SPI_BaudRatePrescaler_4); //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[i]!=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 =SPI2_ReadWriteByte(reg);//·¢ËͼĴæÆ÷ºÅ
SPI2_ReadWriteByte(value); //дÈë¼Ä´æÆ÷µÄÖµ
NRF24L01_CSN=1; //½ûÖ¹SPI´«Êä
return(status); //·µ»Ø״ֵ̬
}
//¶ÁÈ¡SPI¼Ä´æÆ÷Öµ
//reg:Òª¶ÁµÄ¼Ä´æÆ÷
u8 NRF24L01_Read_Reg(u8 reg)
{
u8 reg_val;
NRF24L01_CSN = 0; //ʹÄÜSPI´«Êä
SPI2_ReadWriteByte(reg); //·¢ËͼĴæÆ÷ºÅ
reg_val=SPI2_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=SPI2_ReadWriteByte(reg);//·¢ËͼĴæÆ÷Öµ(λÖÃ),²¢¶Áȡ״ֵ̬
for(u8_ctr=0;u8_ctr<len;u8_ctr++)pBuf[u8_ctr]=SPI2_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 = SPI2_ReadWriteByte(reg);//·¢ËͼĴæÆ÷Öµ(λÖÃ),²¢¶Áȡ״ֵ̬
for(u8_ctr=0; u8_ctr<len; u8_ctr++)SPI2_ReadWriteByte(*pBuf++); //дÈëÊý¾Ý
NRF24L01_CSN = 1; //¹Ø±ÕSPI´«Êä
return status; //·µ»Ø¶Áµ½µÄ״ֵ̬
}
//Æô¶¯NRF24L01·¢ËÍÒ»´ÎÊý¾Ý
//txbuf:´ý·¢ËÍÊý¾ÝÊ×µØÖ·
//·µ»ØÖµ:·¢ËÍÍê³É×´¿ö
u8 NRF24L01_TxPacket(u8 *txbuf)
{
u8 sta;
SPI2_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;
SPI2_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¼Ä´æÆ÷ 0xff
//NRFWriteReg(FLUSH_TX,0x00);
//NRF24WriteReg(FLUSH_RX,0x00);
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ͨÐÅƵÂÊ 40
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ºóÆô¶¯·¢ËÍ
}
}
|
|