代码是移植的正点原子的,但是前面的spi配置部分是自己写的,大神帮忙看看哪里配置不对,硬件spi跟软件模拟spi都试过了
- void SPI3_Init()
- {
- #if HW_SPI
- 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
- UCB3CTL0&=~(UCCKPL);
- UCB3CTL1|=UCSSEL_2; ///选择SMCLK
- UCB3BR0=5; //25M/5=5M <10M
- UCB3BR1=0;
- UCB3CTL1&=~UCSWRST;
- UCB3IE &= ~(UCRXIE+UCTXIE); //不开中断
- // UCB3IFG&=~UCRXIFG;
- #elif !HW_SPI
- P10DIR|=BIT0+BIT1+BIT3+BIT4;
- P10DIR&=~(BIT2+BIT5);
- P10DS|=BIT0+BIT1+BIT3+BIT4;
- P10REN|=BIT2+BIT5;
- P10OUT|=BIT2+BIT5;
- P10OUT&=~BIT3;
- #endif
- }
- //连接方式
- //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;
- #if HW_SPI
- while (!(UCB3IFG&UCTXIFG)); //等待TXBUF为空
- UCB3TXBUF=byte;
- while((UCB3STAT&UCBUSY)&&(!(UCB3IFG&UCRXIFG))); //等待发送跟接收完成
- val=UCB3RXBUF;
- #elif !HW_SPI
- u8 i;
- for(i=0;i<8;i++)
- {
- if(byte&0x80)
- NRFMOSI_1;
- else
- NRFMOSI_0;
- byte<<=1;
- NRFCLK_1;
- if(NRFMISO)
- byte|=0x01;
- NRFCLK_0;
- }
- val=byte;
- #endif
- return val;
-
- }
复制代码
此帖出自
小平头技术问答
- #include "NRF24L01.h"
- //P9.0 -> CE
- //P9.4 -> CSN
- //P9.5 -> IRQ
- uint8_t TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //Send Addr
- uint8_t RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //Receive Addr
- void NRF_GPIO_Config()
- {
- P9SEL &= ~(BIT0 + BIT4);
- P9DIR |= BIT0 + BIT4;
- P9DIR &= ~BIT5;
- P9DS |= BIT0 + BIT4;
- }
- uint8_t NRF_WriteReg(uint8_t reg,uint8_t val)
- {
- uint8_t temp = 0;
- NRF_CSN_0;
- temp = SPI2_SendReceive_SingleByte(reg);
- SPI2_SendReceive_SingleByte(val);
- NRF_CSN_1;
- return temp;
- }
- uint8_t NRF_ReadReg(uint8_t reg)
- {
- uint8_t val = 0;
- NRF_CSN_0;
- SPI2_SendReceive_SingleByte(reg);
- val=SPI2_SendReceive_SingleByte(0xFF);
- NRF_CSN_1;
- return val;
- }
- uint8_t NRF_ReadBuf(uint8_t reg,uint8_t *buff,uint8_t num)
- {
- uint8_t i = 0,val = 0;
- NRF_CSN_0;
- val = SPI2_SendReceive_SingleByte(reg);
- for(i = 0;i < num;i++)
- buff[i] = SPI2_SendReceive_SingleByte(0xFF);
- NRF_CSN_1;
- return val;
- }
- uint8_t NRF_WriteBuf(uint8_t reg,uint8_t *buff,uint8_t num)
- {
- uint8_t val = 0,i = 0;
- NRF_CSN_0;
- val = SPI2_SendReceive_SingleByte(reg);
- for(i = 0;i < num;i++)
- SPI2_SendReceive_SingleByte(buff[i]);
- NRF_CSN_1;
- return val;
- }
- void Set_Rx_Mode()
- {
- NRF_CE_0;
- NRF_WriteReg(WRITE_REG+CONFIG, 0x0F);
- NRF_CE_1;
- delay_ms(1);
- }
- void Set_Tx_Mode()
- {
- NRF_CE_0;
- NRF_WriteReg(WRITE_REG+CONFIG, 0x0E);
- NRF_CE_1;
- delay_ms(1);
- }
- uint8_t NRF_SendPacket(uint8_t *TxBuf)
- {
- uint8_t val = 0;
- NRF_CE_0;
- NRF_WriteBuf(WR_TX_PLOAD,TxBuf,TX_PLOAD_WIDTH);
- Set_Tx_Mode();
- NRF_CE_1;
- delay_ms(1);
- // while(NRF_IRQ != 0);
- val=NRF_ReadReg(STATUS);
- NRF_WriteReg(WRITE_REG+STATUS,0xFF);
- if(val&TX_OK)
- return TX_OK;
- if(val&MAX_TX)
- {
- NRF_WriteReg(WRITE_REG+STATUS,0xff);
- NRF_WriteReg(FLUSH_TX,0xFF);
- return MAX_TX;
- }
- return 0xFF;
- }
- uint8_t NRF_ReceivePacket(uint8_t *RxBuf)
- {
- uint8_t val = 0;
- NRF_CE_0;
- Set_Rx_Mode();
- NRF_CE_1;
- delay_us(500);
- while(NRF_IRQ != 0);
- val=NRF_ReadReg(STATUS);
- if(val&RX_OK)
- {
- NRF_ReadBuf(RD_RX_PLOAD,RxBuf,RX_PLOAD_WIDTH);
- NRF_WriteReg(FLUSH_RX,0xff);
- return 0;
- }
- return 0xff;
- }
- void NRF_Config()
- {
- delay_us(1000);
- NRF_GPIO_Config();
- SPI2_Init();
- delay_us(250);
- NRF_CE_0;
- NRF_CSN_1;
- P9OUT &= ~BIT3;
- NRF_WriteBuf(WRITE_REG + TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);
- NRF_WriteBuf(WRITE_REG + RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH);
- NRF_WriteReg(WRITE_REG + EN_AA,0x01);
- NRF_WriteReg(WRITE_REG + EN_RXADDR,0x01);
- NRF_WriteReg(WRITE_REG + RF_CH,0);
- NRF_WriteReg(WRITE_REG + RX_PW_P0,RX_PLOAD_WIDTH);
- NRF_WriteReg(WRITE_REG + RF_SETUP,0x07);
- }
复制代码一周热门 更多>