|
首先,,說(shuō)一下情況:
參考STM32F0開(kāi)發(fā)板的例程中的推挽輸出(參照GPIO),上拉輸入(參照EXTI),,復(fù)用輸出(參照UART),,然后照著正點(diǎn)原子的ALIENTEK MINISTM32 實(shí)驗(yàn) 無(wú)線通信實(shí)驗(yàn)完成了連線以及配置,但是,,無(wú)法使這兩個(gè)板子通訊,,經(jīng)過(guò)檢測(cè),發(fā)現(xiàn)F0的板子的NRF24L01_Check()一直無(wú)法過(guò)去,,while在那里,,現(xiàn)在傳上代碼,請(qǐng)達(dá)人們給予指點(diǎn),,謝謝了
程序很簡(jiǎn)單,,就是初始化24L01及檢測(cè)它的存在
主函數(shù)部分
int main(void)
{
NRF24L01_Init(); //初始化NRF24L01
while(NRF24L01_Check());//檢測(cè)24L01,一直死在這里,,很不解
while (1)
{
}
}
24L01函數(shù)部分
const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //發(fā)送地址
const u8 RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //發(fā)送地址
//初始化24L01的IO口
void NRF24L01_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT ; //推挽輸出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT ; //推挽輸出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN ; //上拉輸入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
SPIx_Init(); //初始化SPI
Clr_NRF24L01_CE; //使能24L01 NRF24L01_CE
Set_NRF24L01_CSN; //SPI片選取消 NRF24L01_CSN
}
void SPIx_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE );
GPIO_DeInit(GPIOA);
/* Connect pin to Periph */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
/* Configure pins as AF pushpull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //設(shè)置SPI單向或者雙向的數(shù)據(jù)模式:SPI設(shè)置為雙線雙向全雙工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //設(shè)置SPI工作模式:設(shè)置為主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //設(shè)置SPI的數(shù)據(jù)大小:SPI發(fā)送接收8位幀結(jié)構(gòu)
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //選擇了串行時(shí)鐘的穩(wěn)態(tài):時(shí)鐘懸空低電平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //數(shù)據(jù)捕獲于第一個(gè)時(shí)鐘沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信號(hào)由硬件(NSS管腳)還是軟件(使用SSI位)管理:內(nèi)部NSS信號(hào)有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定義波特率預(yù)分頻的值:波特率預(yù)分頻值為256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定數(shù)據(jù)傳輸從MSB位還是LSB位開(kāi)始:數(shù)據(jù)傳輸從MSB位開(kāi)始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值計(jì)算的多項(xiàng)式
SPI_Init(SPI1, &SPI_InitStructure); //根據(jù)SPI_InitStruct中指定的參數(shù)初始化外設(shè)SPIx寄存器
SPI_Cmd(SPI1, ENABLE); //使能SPI外設(shè)
SPIx_ReadWriteByte(0xff);//啟動(dòng)傳輸
}
//檢測(cè)24L01是否存在
//返回值:0,,成功;1,失敗
u8 NRF24L01_Check(void)
{
u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
u8 i;
SPIx_SetSpeed(SPI_BaudRatePrescaler_8); //spi速度為9Mhz(24L01的最大SPI時(shí)鐘為10Mhz)
NRF24L01_Write_Buf(NRF24L01_WRITE_REG+TX_ADDR,buf,5);//寫(xiě)入5個(gè)字節(jié)的地址.
NRF24L01_Read_Buf(TX_ADDR,buf,5); //讀出寫(xiě)入的地址
for(i=0;i<5;i++)if(buf[i]!=0XA5)break;
if(i!=5)return 1;//檢測(cè)24L01錯(cuò)誤
return 0; //檢測(cè)到24L01
} |
|
|
|
//SPIx 讀寫(xiě)一個(gè)字節(jié)
//TxData:要寫(xiě)入的字節(jié)
//返回值:讀取到的字節(jié)
u8 SPIx_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //檢查指定的SPI標(biāo)志位設(shè)置與否:發(fā)送緩存空標(biāo)志位
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData16(SPI1, TxData); //通過(guò)外設(shè)SPIx發(fā)送一個(gè)數(shù)據(jù)
retry=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //檢查指定的SPI標(biāo)志位設(shè)置與否:接受緩存非空標(biāo)志位
{
retry++;
if(retry>200)return 0;
}
return SPI_I2S_ReceiveData16(SPI1); //返回通過(guò)SPIx最近接收的數(shù)據(jù)
}
紅色部分和原子的不一樣,,因?yàn)镕0的SPI部分有
void SPI_SendData8(SPI_TypeDef* SPIx, uint8_t Data)
uint8_t SPI_ReceiveData8(SPI_TypeDef* SPIx) |
|
|
|
|
|