中级会员
- 积分
- 212
- 金钱
- 212
- 注册时间
- 2015-8-1
- 在线时间
- 7 小时
|
楼主 |
发表于 2015-8-22 10:57:33
|
显示全部楼层
<p>
实验思路:<br />
1、在 main 中每两秒发射一次数据,调试输出发射的数据内容。<br />
2、在发射模块的 IRQ 中断(PC8)中,对发射情况进行调试输出。<br />
3、在接收模块的 IRQ 中断(PC9)中,对接收情况进行调试输出,并调试输出接收的内容。<br />
经过调试输出判断,NRF24L01 先进行发射,如果成功,会先在接收端产生 RX_OK 中断,然后是发送方产生 TX_OK 中断。如果发送不成功,仅在发送端产生 MAX_TX 中断。
</p>
<p>相关代码
</p>
<div style="background-color:#E8E8E8;">
[mw_shl_code=c,true]int main(void)
{
_Init();
while (1)
{
static u32 i = 0;
u8 lTxBuffer[33];
i++;
sprintf((char *)lTxBuffer, "%032d", i);
oDebug("RF1 => [%s]\r\n", lTxBuffer);
WIRELESS_SendPacket(WIRELESS1, lTxBuffer);
LED1 = !LED1;
Wait_s(2);
}
}[/mw_shl_code]
</div>
<p>
NRF24L01 中为配合中断方式,做了些调整,代码如下
</p>
<div style="background-color:#E8E8E8;">
[mw_shl_code=c,true]static bool WIRELESS1_IsIrqHandled;
static bool WIRELESS2_IsIrqHandled;
...
void WIRELESS_IrqHandled(u8 aWhichWireless)
{
switch(aWhichWireless)
{
case WIRELESS1: WIRELESS1_IsIrqHandled = TRUE; break;
case WIRELESS2: WIRELESS2_IsIrqHandled = TRUE; break;
}
}
void WIRELESS_SendPacket(u8 aWhichWireless, u8 *aBuffer)
{
switch(aWhichWireless)
{
case WIRELESS1: WIRELESS1_CE = 0; WIRELESS1_IsIrqHandled = FALSE; break;
case WIRELESS2: WIRELESS2_CE = 0; WIRELESS2_IsIrqHandled = FALSE; break;
}
WIRELESS_WriteBuffer(aWhichWireless, NFR_WRITE_TX_PAYLOAD, aBuffer, WIRELESS_TX_PAYLOAD_WIDTH);
switch(aWhichWireless)
{
case WIRELESS1: WIRELESS1_CE = 1; do { Wait_us(10);} while(WIRELESS1_IsIrqHandled == FALSE); break;
case WIRELESS2: WIRELESS2_CE = 1; do { Wait_us(10);} while(WIRELESS2_IsIrqHandled == FALSE); break;
}
return;
}[/mw_shl_code]
</div>
<p>
中断处理代码如下:
</p>
<div style="background-color:#E8E8E8;">
[mw_shl_code=c,true]void EXTI9_5_IRQHandler(void)
{
// WIRELESS1 触发的中断
if(EXTI_GetITStatus(EXTI_Line8) == SET)
{
u8 lStatus = WIRELESS_ReadReg(WIRELESS1, NRF_READ_REG + NFR_STATUS); // 读取状态寄存器的值
oDebug("RF1 => ");
if(lStatus & NFR_MAX_TX) // 达到最大重发次数
{
WIRELESS_WriteReg(WIRELESS1, NFR_FLUSH_TX, 0xFF); // 清除TX FIFO寄存器
oDebug("MAX_TX\r\n");
}
if(lStatus & NFR_TX_OK) // 发送完成
{
oDebug("TX_OK\r\n");
}
if(lStatus & NFR_RX_OK) // 接收完成
{
oDebug("RX_OK\r\n");
}
// 清除 RX_DS,TX_DS 或 MAX_RT 中断标志
WIRELESS_WriteReg(WIRELESS1, NRF_WRITE_REG + NFR_STATUS, lStatus);
WIRELESS_IrqHandled(WIRELESS1);
EXTI_ClearITPendingBit(EXTI_Line8);
}
if(EXTI_GetITStatus(EXTI_Line9) == SET)
{
u8 lStatus = WIRELESS_ReadReg(WIRELESS2, NRF_READ_REG + NFR_STATUS); // 读取状态寄存器的值
oDebug("RF2 => ");
if(lStatus & NFR_MAX_TX) // 达到最大重发次数
{
WIRELESS_WriteReg(WIRELESS2, NFR_FLUSH_TX, 0xFF); // 清除TX FIFO寄存器
oDebug("MAX_TX\r\n");
}
if(lStatus & NFR_TX_OK) // 发送完成
{
oDebug("TX_OK\r\n");
}
if(lStatus & NFR_RX_OK) // 接收完成
{
u8 lRxBuffer[33];
oDebug("RX_OK");
WIRELESS_ReadBuffer(WIRELESS2, NFR_READ_RX_PAYLOAD, lRxBuffer, WIRELESS_RX_PAYLOAD_WIDTH); // 读取数据
WIRELESS_WriteReg(WIRELESS2, NFR_FLUSH_RX, 0xFF); // 清除RX FIFO寄存器
lRxBuffer[32] = 0;
oDebug("[%s]\r\n", lRxBuffer);
}
// 清除 RX_DS,TX_DS 或 MAX_RT 中断标志
WIRELESS_WriteReg(WIRELESS2, NRF_WRITE_REG + NFR_STATUS, lStatus);
WIRELESS_IrqHandled(WIRELESS2);
EXTI_ClearITPendingBit(EXTI_Line9);
}
}[/mw_shl_code]
</div>
<p>
<br />
</p> |
|