新手上路
- 积分
- 24
- 金钱
- 24
- 注册时间
- 2021-11-12
- 在线时间
- 11 小时
|
如题,发送端通过串口中断触发发送数据,接收端采用中断接收收据。发送端如下
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //
}
switch(Res)
{
case '1':
tmp_buf[0] = '1';
tmp_buf[1]=0;
NRF24L01_TxPacket(tmp_buf);
order_music = 0;
state = 2;
break;
case '2':
tmp_buf[0] = '2';
tmp_buf[1]=0;
NRF24L01_TxPacket(tmp_buf);
order_music = 0;
state = 3;
break;
......
}
接收端
void EXTI9_5_IRQHandler(void)
{
u8 istatus;
if (EXTI_GetITStatus(EXTI_Line8)!=RESET)
{
NRF24L01_CE=0;
delay_ms(1);
istatus=NRF24L01_Read_Reg(STATUS); // 读取状态寄存其来判断数据接收状况
if(istatus&RX_OK)//bit6:数据接收中断
{
NRF24L01_Read_Buf(RD_RX_PLOAD,tmp_buf,RX_PLOAD_WIDTH);//读取数据
NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器
flag_Tx = 1;
}
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,istatus);//清除状态寄存器
NRF24L01_CE=1;
}
EXTI_ClearITPendingBit(EXTI_Line8); //清除标志
}
通过LCD打印tmp_buf[0],无论发送端发‘1’还是发‘2’,在接收端都显示‘X’,
但是不采用中断接收,将接收放进循环中,就可以正常接收到数据,代码如下,LCD屏显示‘1’或‘2’
while(1)
{
if (NRF24L01_RxPacket(tmp_buf)==0)
{
LCD_Clear(BLACK);
tmp_buf[1] = 0;
LCD_ShowChar(30,50,tmp_buf[0],24,0);
flag_Tx = 0;
}
}
nrf初始化如下
void NRF24L01_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOG, ENABLE);//使能GPIOB,G时钟
//GPIOB14初始化设置:推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化PB14
//GPIOG6,7推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//上拉
GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化PG6,7
//GPIOG.8上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输入
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化PG8
GPIO_SetBits(GPIOB,GPIO_Pin_14);//PB14输出1,防止SPI FLASH干扰NRF的通信
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOG, EXTI_PinSource8);
EXTI_InitStructure.EXTI_Line=EXTI_Line8;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line8);
SPI1_Init(); //初始化SPI1
NRF24L01_SPI_Init();//针对NRF的特点修改SPI的设置
NRF24L01_CE=0; //使能24L01
NRF24L01_CSN=1; //SPI片选取消
}
|
|