|
本帖最后由 机灵的兔子 于 2018-9-4 09:25 编辑
Tag1: xdata uint8_t RX_ADDRESS[5] = {0xE7,0xE7,0xE7,0xE7,0xE7}; // RX address
xdata uint8_t TX_ADDRESS[5] = {0xE1,0xE7,0xE7,0xE7,0xE7}; // TX address
void RfCofig(void)
{
RFCKEN = 1; //使能时钟
hal_nrf_close_pipe(HAL_NRF_ALL);
hal_nrf_open_pipe(HAL_NRF_PIPE0,0); // 通道0.
hal_nrf_open_pipe(HAL_NRF_PIPE1,0); // 通道1
hal_nrf_set_operation_mode(HAL_NRF_PRX); // 接收
hal_nrf_set_rf_channel(48); // RF信道:48
hal_nrf_set_datarate(HAL_NRF_250KBPS); // RF速率
hal_nrf_set_output_power(HAL_NRF_0DBM); //0DB
hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);
hal_nrf_set_address(HAL_NRF_TX,TX_ADDRESS); //设置发射机地址
hal_nrf_set_address(HAL_NRF_PIPE0,RX_ADDRESS); //设置通道0接收机地址
hal_nrf_set_address(HAL_NRF_PIPE1,TX_ADDRESS); //设置通道1接收机地址
hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, 6); //设置通道0接收长度
hal_nrf_set_rx_payload_width(HAL_NRF_PIPE1, 6); //设置通道0接收长度
hal_nrf_set_power_mode(HAL_NRF_PWR_UP);
RF = 1;
EA = 1;
CE_HIGH();
}
void rf_irq() interrupt INTERRUPT_RFIRQ
{
uint8_t irq_flags,i;
irq_flags = hal_nrf_get_clear_irq_flags();
if(irq_flags & (1<<HAL_NRF_RX_DR))
{
while(!hal_nrf_rx_fifo_empty())
{
PipeAndLen = hal_nrf_read_rx_payload(RxPayload);
if((PipeAndLen&0xFF) == RX_PAYLOAD_LEN)
{
for(i=0;i<(6);i++)ID_BufTmp = RxPayload;
RF_Recv_Flag = 1;
}
hal_nrf_flush_rx();
}
}
if(irq_flags & ((1<<HAL_NRF_TX_DS)))
{
radio_busy = false;
hal_nrf_flush_tx();
}
if(irq_flags & ((1<<HAL_NRF_MAX_RT)))
{
radio_busy = false;
hal_nrf_flush_tx();
}
}
void SendToTag(void)
{
TxPayload[0] = 0x11;
TxPayload[1] = 0x11;
TxPayload[2] =0x33;
TxPayload[3] = 0x44;
TxPayload[4] = 0x55;
TxPayload[5] = 0x66;
CE_LOW();
hal_nrf_set_operation_mode(HAL_NRF_PTX); //模式:发送 //一句切换
hal_nrf_write_tx_payload(TxPayload,6);
CE_PULSE();
radio_busy = true;
while(radio_busy) ;
hal_nrf_set_operation_mode(HAL_NRF_PRX); //模式:接收
CE_HIGH();
}
*******
main() 里的
P0DIR = 0xF6;
P1DIR = 0xFF;
hal_clk_set_16m_source(HAL_CLK_XOSC16M); //使用外部16MHz晶振
RfCofig();
hal_uart_init(UART_BAUD_9K6);
while(hal_clk_get_16m_source() != HAL_CLK_XOSC16M) //等待时钟稳定
;
while(1)
{
if(RF_Recv_Flag )
{
RF_Recv_Flag = 0;
for(beep=0;beep<6;beep++)
hal_uart_putchar(ID_BufTmp[beep]);
}
if(S1 == 0)
{
delay_ms(10);
if(S1 == 0)
{
D1 = 0;
while(S1 == 0); // 等待按键释放
D1 = 1;
SendToTag();
}
}
}
Tag2:
uint8_t TX_ADDRESS[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
uint8_t RX_ADDRESS[5] = {0xE1,0xE7,0xE7,0xE7,0xE7};
void RfCofig(void)
{
RFCKEN = 1; //使能时钟
hal_nrf_close_pipe(HAL_NRF_ALL); //关所有通道
hal_nrf_open_pipe(HAL_NRF_PIPE0,0); // 开通道0
hal_nrf_open_pipe(HAL_NRF_PIPE1,0); // 开通道1
hal_nrf_set_rf_channel(48); // RF信道:48
hal_nrf_set_datarate(HAL_NRF_250KBPS); // RF速率
hal_nrf_set_output_power(HAL_NRF_0DBM);
hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);
hal_nrf_set_address(HAL_NRF_TX,TX_ADDRESS); //设置发射机地址
hal_nrf_set_auto_retr(0,1500);
hal_nrf_set_operation_mode(HAL_NRF_PRX); // 接收
hal_nrf_set_address(HAL_NRF_PIPE0,TX_ADDRESS ); //设置接收机地址
hal_nrf_set_address(HAL_NRF_PIPE1,RX_ADDRESS); //设置接收机地址
hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, 6); //设置通道0接收长度
hal_nrf_set_rx_payload_width(HAL_NRF_PIPE1, 6); //设置通道1接收长度
hal_nrf_set_power_mode(HAL_NRF_PWR_UP);
RF = 1;
EA = 1;
CE_HIGH();
}
void rf_irq() interrupt INTERRUPT_RFIRQ
{
uint8_t irq_flags;
uint8_t i;
irq_flags = hal_nrf_get_clear_irq_flags();
if(irq_flags & (1<<HAL_NRF_RX_DR))
{
while(!hal_nrf_rx_fifo_empty())
{
PipeAndLen = hal_nrf_read_rx_payload(RxPayload);
for(i=0;i<6;i++)ID_BufTmp = RxPayload;
RF_Recv_Flag = 1;
}
}
if(irq_flags & ((1<<HAL_NRF_TX_DS)))
{
hal_uart_putchar(0x22) ;
radio_busy = false;
}
if(irq_flags & ((1<<HAL_NRF_MAX_RT)))
{
hal_uart_putchar(0x33) ;
radio_busy = false;//
hal_nrf_flush_tx();
}
}
void SentDataTest(void)
{
TxPayload[0] = 0x99;
TxPayload[1] = 0x88;
TxPayload[2] =0x77;
TxPayload[3] = 0x66;
TxPayload[4] = 0x55;
TxPayload[5] = 0x44;
CE_LOW();
hal_nrf_set_operation_mode(HAL_NRF_PTX); //模式:发送
hal_nrf_write_tx_payload(TxPayload,6);
CE_PULSE();
radio_busy = true;
while(radio_busy) ;
hal_nrf_set_operation_mode(HAL_NRF_PRX); //模式:接收
CE_HIGH();
}
*******
main() 里的
P0DIR = 0xF6;
P1DIR = 0xFF;
hal_clk_set_16m_source(HAL_CLK_XOSC16M); //使用外部16MHz晶振
RfCofig();
hal_uart_init(UART_BAUD_9K6);
while(hal_clk_get_16m_source() != HAL_CLK_XOSC16M) //等待时钟稳定
;
while(1)
{
if(RF_Recv_Flag )
{
RF_Recv_Flag = 0;
for(beep=0;beep<6;beep++)
hal_uart_putchar(ID_BufTmp[beep]);
}
if(S1 == 0)
{
delay_ms(10);
if(S1 == 0)
{
D1 = 0;
while(S1 == 0); // 等待按键释放
D1 = 1;
SentDataTest();
}
}
}
需要实现的功能,按键按下对应节点Tag1 或Tag2 ,另一个节点会收到 6个事先定好的字节,并打印到串口显示。
新手,还没能调通,帮看看看,卡了好些天了, 程序调试结果 是 Tag2 按下按键,能正常发数据给Tag1 ,但 T ag2按下按键 Tag1 进入无线接收中断后卡死 ; 程序里,
是无限参数错了?还是 无线的中断函数写错了?
|
|