初级会员
- 积分
- 75
- 金钱
- 75
- 注册时间
- 2017-7-31
- 在线时间
- 29 小时
|
楼主 |
发表于 2018-5-21 00:46:00
|
显示全部楼层
void GPIO_Configuration(void)
{
//无线模块的IO口初始化
//MISO 读取数据 浮动输入,没有外部中断
GPIO_Init(NRF24L01_CE_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CE_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//ce
GPIO_Init(NRF24L01_CS_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CS_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//cs
GPIO_Init(NRF24L01_SCK_PORT, NRF24L01_SCK_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);//sck
GPIO_Init(NRF24L01_MOSI_PORT, NRF24L01_MOSI_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);//MOSI
GPIO_Init(NRF24L01_MISO_PORT, NRF24L01_MISO_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);//MISO
GPIO_Init(NRF24L01_IRQ_PORT, (GPIO_Pin_TypeDef)(NRF24L01_IRQ_PIN ), GPIO_MODE_IN_PU_NO_IT); //IRQ
}
void init_io(void)
{
delay_ms(100); //延时 100us
GPIO_WriteHigh(NRF24L01_CE_PORT, NRF24L01_CE_PIN);
GPIO_WriteHigh(NRF24L01_CS_PORT, NRF24L01_CS_PIN);
GPIO_WriteLow(NRF24L01_SCK_PORT, NRF24L01_SCK_PIN);
}
uchar TxBuf[TX_PLOAD_WIDTH]="abcds";
void main(void)
{
SystemClock_Init();
delay_init(16);
UART_Init(115200);
LED_Init();
GPIO_Configuration();
SPI_DeInit();//spi恢复默认设置
SPI_ResetCRC();
/* Initialize SPI in Slave mode */
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_8, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_HIGH,
SPI_CLOCKPHASE_2EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT, 0x07);
/* Enable the SPI*/
SPI_Cmd(ENABLE);
SPI_RW(0xff);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //16MHz高速内部RC振荡器(HSI)
init_io();
SPI_Cmd(DISABLE);
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_8, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_LOW,
SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT, 0x07);
SPI_Cmd(ENABLE);
GPIO_WriteLow(GPIOD, GPIO_PIN_3);//ce=0
GPIO_WriteHigh(GPIOA, GPIO_PIN_3);//csn=1
/* 前头检测 ------------------------------------------------------*/
if(NRF24L01_Check()==0)
{
printf("\n\r设备正常\n\r");
}
else printf("\n\r>>>请检查设备是否安装正确<<<\n\r");
while (1)
{
if(nRF24L01_TxPacket(TxBuf)==TX_OK)
{
printf("\n 发送成功 \n\r");
}
else printf("\n\r发送失败\n\r");
delay_ms(500);
}
}
|
|