中级会员
- 积分
- 411
- 金钱
- 411
- 注册时间
- 2014-4-18
- 在线时间
- 94 小时
|
5金钱
用扫描中断引脚可以正常接收(如同扫描时候同时打开中断也可以正常进入中断)
但是改成中断后读取就无法进入中断。
中断引脚:PB0
nRF初始化:
[mw_shl_code=c,true]void SPI_NRF_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*开启相应IO端口的时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB
|RCC_APB2Periph_AFIO
,ENABLE);
/*使能SPI1时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*配置 SPI_NRF_SPI的 SCK,MISO,MOSI引脚,GPIOA^5,GPIOA^6,GPIOA^7 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用功能
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*配置SPI_NRF_SPI的CE引脚,和SPI_NRF_SPI的 CSN 引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*配置SPI_NRF_SPI的IRQ引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*中断配置*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* 配置中断源 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line0);
/* 拉高csn引脚,NRF进入空闲状态 */
NRF_CSN_HIGH();
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式
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_4; //4分频,6MHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}[/mw_shl_code]
进入接收模式:
[mw_shl_code=c,true]void NRF_RX_Mode(void)
{
NRF_CE_LOW();
SPI_NRF_WriteBuf(NRF_WRITE_REG+RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH);//写RX节点地址
//SPI_NRF_WriteReg(NRF_WRITE_REG+EN_AA,0x01); //使能通道0的自动应答
//SPI_NRF_WriteReg(NRF_WRITE_REG+EN_RXADDR,0x01); //使能通道0的接收地址
SPI_NRF_WriteReg(NRF_WRITE_REG+RF_CH,CHANAL); //设置RF通信频率
SPI_NRF_WriteReg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
SPI_NRF_WriteReg(NRF_WRITE_REG+RF_SETUP,0x27); //设置TX发射参数,0db增益,250kbps,低噪声增益开启
SPI_NRF_WriteReg(NRF_WRITE_REG+CONFIG, 0x0f); //配置基本工作模式的参数WR_UP,EN_CRC,16BIT_CRC,接收模式
/*CE拉高,进入接收模式*/
NRF_CE_HIGH();
}[/mw_shl_code]
数据接收:
[mw_shl_code=c,true]u8 NRF_Rx_Dat(u8 *rxbuf)
{
u8 state;
u8 err;
//while(NRF_Read_IRQ()!=0);
state=SPI_NRF_ReadReg(STATUS);
if(state&RX_DR)
{
NRF_CE_LOW();
SPI_NRF_ReadBuf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);
//SPI_NRF_WriteReg(FLUSH_RX,NOP);
err=RX_DR;
}
else
err= ERROR;
SPI_NRF_WriteReg(NRF_WRITE_REG+STATUS,state);
return err;
}[/mw_shl_code]
中断处理函数:
[mw_shl_code=c,true]void EXTI0_IRQHandler(void)
{
PCout(13)=1;
if(EXTI_GetITStatus(EXTI_Line0)!=RESET) //确保是否产生了EXTI Line中断
{
if(NRF_Rx_Dat(RX_BUF)==RX_DR)GetData=1;
PBout(2)=!PBout(2);
EXTI_ClearITPendingBit(EXTI_Line0); //清除中断标志位
}
}[/mw_shl_code]
|
|