Grove-Lora Radio的使用详见:http://wiki.seeedstudio.com/cn/Grove_LoRa_Radio/
但是网站中提供的Grove_LoRa_433MHz_and_915MHz_RF-master中的示例代码只能修改RF的频率,不能修改LoRa扩频因子、带宽等参数。研究了一下午,终于搞明白怎么修改库函数,特在此记录。
注意!必须找到与Grove_LoRa_433MHz_and_915MHz_RF-master库对应的 RH_RF95.h和 RH_RF95.cpp文件!
1. 修改扩频因子,添加如下代码段
① RH_RF95.cpp,添加
template <typename T>
void RH_RF95<T>::setSpreadingFactor(int8_t sf)
{if (sf < 6) {sf = 6;} else if (sf > 12) {sf = 12;}if (sf == 6) {this->write(RH_RF95_REG_31_DETECTION_OPTIMIZE, 0xc5);this->write(RH_RF95_REG_37_DETECTION_THRESHOLD, 0x0c);} else {this->write(RH_RF95_REG_31_DETECTION_OPTIMIZE, 0xc3);this->write(RH_RF95_REG_37_DETECTION_THRESHOLD, 0x0a);}this->write(RH_RF95_REG_1E_MODEM_CONFIG2, (this->read(RH_RF95_REG_1E_MODEM_CONFIG2) & 0x0f) | ((sf << 4) & 0xf0));
}
② RH_RF95.h,添加
#define RH_RF95_REG_31_DETECTION_OPTIMIZE 0x31
#define RH_RF95_REG_37_DETECTION_THRESHOLD 0x37
void setSpreadingFactor(int8_t sf);
2. 修改带宽
① RH_RF95.cpp,添加
template <typename T>
void RH_RF95<T>::setSignalBandwidth(long sbw)
{int bw;if (sbw <= 7.8E3) {bw = 0;} else if (sbw <= 10.4E3) {bw = 1;} else if (sbw <= 15.6E3) {bw = 2;} else if (sbw <= 20.8E3) {bw = 3;} else if (sbw <= 31.25E3) {bw = 4;} else if (sbw <= 41.7E3) {bw = 5;} else if (sbw <= 62.5E3) {bw = 6;} else if (sbw <= 125E3) {bw = 7;} else if (sbw <= 250E3) {bw = 8;} else /*if (sbw <= 250E3)*/ {bw = 9;}this->write(RH_RF95_REG_1D_MODEM_CONFIG1, (this->read(RH_RF95_REG_1D_MODEM_CONFIG1) & 0x0f) | (bw << 4));
}
② RH_RF95.h,添加
void setSignalBandwidth(long sbw);
3. 修改码率
① RH_RF95.cpp,添加
template <typename T>
void RH_RF95<T>::setCodingRate4(int8_t denominator)
{if (denominator < 5) {denominator = 5;} else if (denominator > 8) {denominator = 8;}int cr = denominator - 4;this->write(RH_RF95_REG_1D_MODEM_CONFIG1, (this->read(RH_RF95_REG_1D_MODEM_CONFIG1) & 0xf1) | (cr << 1));
}
② RH_RF95.h,添加
void setCodingRate4(int8_t denominator);