高中生
最后登录1970-1-1
在线时间 小时
注册时间2014-4-19
|
例程没有使用中断接收,稍微改了一下,改成中断后读取数据,但是如果仅加入下降沿中断,在中断里加一个LED反转,其他跟例程一样使用while(NRF_Read_IRQ()!=0);查询到中断后读取;可以发现LED接收到后闪烁。
如果不使用while(NRF_Read_IRQ()!=0);,中断后直接在中断函数中读取,就无法进入中断,这是什么问题造成的呢?
初始化:
- 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);
- }
复制代码 接收:
- u8 NRF_Rx_Dat(u8 *rxbuf)
- {
- u8 state;
- u8 err;
- 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;
- }
复制代码 中断:
- void EXTI0_IRQHandler(void)
- {
- if(EXTI_GetITStatus(EXTI_Line0)!=RESET) //确保是否产生了EXTI Line中断
- {
- NRF_Rx_Dat(RX_BUF);
- PBout(2)=!PBout(2);
- EXTI_ClearITPendingBit(EXTI_Line0); //清除中断标志位
- }
- }
复制代码
|
|