一、准备
最近研究一下LORA,因为LLCC68是阉割版本的SX126X,所以就选用这颗芯片了
- 模块:随便选一家都可以
- 我简单做了个测试板,接口如下:只需要注意DIO配置成中断引脚即可
二、程序流程
-
RX TX初始化
#define RF_FREQUENCY 433000000 // Hz bool EnableMaster = false; //主从选择 #define TX_OUTPUT_POWER 20 // 22 dBm #define RX_TIMEOUT_VALUE 5000 #define LORA_BANDWIDTH 2 // [0: 125 kHz, \ // 1: 250 kHz, \ // 2: 500 kHz, \ // 3: Reserved] #define LORA_SPREADING_FACTOR 8 // [SF7..SF12] #define LORA_CODINGRATE 2 // [1: 4/5, \ // 2: 4/6, \ // 3: 4/7, \ // 4: 4/8] #define LORA_PREAMBLE_LENGTH 2048 // 1049.66ms //计算发射时间通过此网站进入:https://loratools.nl/#/airtime #define LORA_SYMBOL_TIMEOUT 200 // Symbols #define LORA_FIX_LENGTH_PAYLOAD_ON false #define LORA_IQ_INVERSION_ON false
lora_gpio_init();//lora gpio init RadioEvents.TxDone = OnTxDone; RadioEvents.RxDone = OnRxDone; RadioEvents.TxTimeout = OnTxTimeout; RadioEvents.RxTimeout = OnRxTimeout; RadioEvents.RxError = OnRxError; RadioEvents.CadDone = OnCadDone; Radio.Init(&RadioEvents); Radio.SetChannel(RF_FREQUENCY); //设置频率 // HAL_Delay_nMS(100); Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, true, 0, 0, LORA_IQ_INVERSION_ON, 3000); Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, true, 0, 0, LORA_IQ_INVERSION_ON, true); //true 连续接收,false 单次接收
-
CAD初始化:
关于CAD的配置,可以参考这里:https://blog.csdn.net/u011638175/article/details/121899630
在这里,我建议就选用推荐的SF12的最大配置,对于功耗影响其实并不大#define CAD_SYMBOL_NUM LORA_CAD_04_SYMBOL //建议用04 可以兼容多个 #define CAD_DET_PEAK 28 #define CAD_DET_MIN 10 #define CAD_TIMEOUT_MS 100 #define RX_TIMEOUT_VALUE 1000
SX126xSetDioIrqParams(IRQ_RADIO_ALL, IRQ_RADIO_ALL, IRQ_RADIO_NONE, IRQ_RADIO_NONE); SX126xConfigureCad(CAD_SYMBOL_NUM, CAD_DET_PEAK, CAD_DET_MIN, CAD_TIMEOUT_MS); // Configure the CAD, CAD_ONLY 模式 Radio.StartCad();
-
主程序中,周期调用中断事件
不需要丢IO中断里面去,只要配置了中断,唤醒后跑主循环即可Radio.IrqProcess(); // Process Radio IRQ
重新进入CAD,调用此函数即可
Radio.StartCad(); // 启动cad
检测到CAD或者超时,都会进入此回调
void OnCadDone(bool channelActivityDetected) { // printf("OnCadDone\r\n"); Radio.Standby(); if (channelActivityDetected == true) //检测到信号 { printf("channelActivityDetected\r\n"); TickCounter = 0; CadRx = CAD_SUCCESS; Radio.Rx(RX_TIMEOUT_VALUE); State = RX;//进入RX状态 } else //没检测到信号 { TickCounter = 0; CadTime = 0; CadRx = CAD_FAIL; Radio.Sleep(); //射频休眠 Lpt_Run();//开始定时器唤醒 Lpm_GotoLpmMode();//休眠 State = START_CAD;//重新进入CAD状态 } }
数据接收成功,会进入此回调
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { Radio.Standby(); // printf("OnRxDone\r\n"); BufferSize = size; memcpy(RX_Buffer, payload, BufferSize); //获取用户数据 RssiValue = rssi; SnrValue = snr; PacketReceived = true; CadTime = 0; /* { printf("OnRxDone[%d]:",size); uint8_t i; for(i=0; i< size;i++) { printf(" %02x",payload[i]); } printf("\r\n"); } */ State = RX; }
== 接收超时/失败/成功;发送成功等回调我就不贴了==
-
需要和SX1278进行互通的话,需要注意以下几点:
具体可以参考一下http://bbs.lierda.com/forum.php?mod=viewthread&tid=11400&extra=●中心频率 ●扩频因子(SF) ●调制带宽(BW) ●纠错率(CR) ●CRC是否开启 ●报头显隐性格式 ●低速率优化(Low Data Rate Optimize) ●IQ信号极性 ●前导码长度 ●数据包长度
三、CAD的功耗
配置如下:
- BW:500K
- CR:4/6
- SF8
- CAD_SYMBOL_NUM LORA_CAD_04_SYMBOL
- CAD_DET_PEAK 28
- CAD_DET_MIN 10
- 1S唤醒一次