不晓得怎么回事,一直发送失败,有没有nrf24l01的例子给我参考一下,或者大神帮我看看问题在哪
- u8 NRF_WriteReg(u8 reg,u8 val)
- {
- u8 temp;
- NRF24L01_CSN_0;
- temp=SPI3_SendReceiveByte(reg);
- SPI3_SendReceiveByte(val);
- NRF24L01_CSN_1;
- return temp;
- }
- u8 NRF_ReadReg(u8 reg)
- {
- u8 val;
- NRF24L01_CSN_0;
- SPI3_SendReceiveByte(reg);
- val=SPI3_SendReceiveByte(0xff);
- NRF24L01_CSN_1;
- return val;
- }
- u8 NRF_ReadBuf(u8 reg,u8 *buff,u8 num)
- {
- u8 i,val;
- NRF24L01_CSN_0;
- val=SPI3_SendReceiveByte(reg);
- for(i=0;i<num;i++)
- *buff++=SPI3_SendReceiveByte(0xff);
- NRF24L01_CSN_1;
- return val;
- }
- u8 NRF_WriteBuf(u8 reg,u8 *buff,u8 num)
- {
- u8 val,i;
- NRF24L01_CSN_0;
- val=SPI3_SendReceiveByte(reg);
- for(i=0;i<num;i++)
- SPI3_SendReceiveByte(*buff++);
- NRF24L01_CSN_1;
- return val;
- }
- u8 NRF_SendPacket(u8 *TxBuf)
- {
- u8 val;
- NRF24L01_CE_0;
- NRF_WriteBuf(WR_TX_PLOAD,TxBuf,TX_PLOAD_WIDTH);
- NRF24L01_CE_1;
- delay_ms(5);
- while(NRF24L01_IRQ!=0);
- val=NRF_ReadReg(STATUS);
- NRF_WriteReg(WRITE_REG,val);
- if(val&MAX_TX)
- {
- NRF_WriteReg(FLUSH_TX,0xff);
- return MAX_TX;
- }
- if(val&TX_OK)
- return TX_OK;
- return 0xff;
- }
复制代码
此帖出自
小平头技术问答
- void SPI3_Init()
- {
- P10SEL|=BIT1+BIT2+BIT3; //设置P10.1~P10.3外设功能
- P10SEL&=~(BIT0+BIT4+BIT5);
- P10DIR|=BIT0+BIT1+BIT3+BIT4; //设置CS,MOSI,CLK,CE,为输出方向
- P10DIR&=~(BIT2+BIT5); //设置MISO为输入方向
- P10DS|=BIT0+BIT1+BIT3+BIT4;
- P10REN|=BIT2+BIT5; //设置MISO上拉
- P10OUT|=BIT1+BIT2+BIT5;
- P10OUT&=~BIT0+BIT3;
- UCB3CTL1|=UCSWRST;
- UCB3CTL0 =UCCKPH+UCMSB+UCMST+UCMODE_0+UCSYNC; //3线SPI主机模式,时钟极性高,先发送MSB
- UCB3CTL1|=UCSSEL_2; ///选择SMCLK
- UCB3BR0=5; //25M/5=5M <10M
- UCB3BR1=0;
- UCB3CTL1&=~UCSWRST;
- UCB3IE &= ~(UCRXIE+UCTXIE); //不开中断
- UCB3IFG&=~UCRXIFG;
- }
- //连接方式
- //P10.0->CSN P10.1->MOSI
- //P10.2->MISO P10.3->CLK
- //P10.4->CE P10.5->IRQ
- u8 SPI3_SendReceiveByte(u8 byte)
- {
- u8 val;
- while (!(UCB3IFG&UCTXIFG)); //等待TXBUF为空
- UCB3TXBUF=byte;
- while((UCB3STAT&UCBUSY)&&(!(UCB3IFG&UCRXIFG))); //等待发送跟接收完成
- // while (!(UCB3IFG&UCRXIFG)); //等待接收完
- val=UCB3RXBUF;
- return val;
- }
复制代码一周热门 更多>