1

I am using the receiver RDA5807m, but I do NOT need to switch the frequency and other parameters during operation. enter image description here

I noticed that the module continues to work when the programming pins are disconnected during its operation, that is, it remembers the settings until the supply voltage is removed.

Therefore, I want to set the frequency and volume at the beginning of work, so as not to occupy the resources of the microcontroller, but I just understand the registers. Tell me how to write code, the simplest of all possible configurations for this purpose.

Now I am using the following sketch, it works, but it takes a lot of resources, and I don't need it. Help simplify it and free it if possible "void loop()"

#include <Wire.h>

#define RDA5807M_RANDOM_ACCESS_ADDRESS 0x11
// registers
#define RDA5807M_REG_CONFIG 0x02
#define RDA5807M_REG_TUNING 0x03
#define RDA5807M_REG_VOLUME 0x05
#define RDA5807M_REG_RSSI   0x0B
// flags
#define RDA5807M_FLG_DHIZ 0x8000
#define RDA5807M_FLG_DMUTE 0x4000
#define RDA5807M_FLG_BASS 0x1000
#define RDA5807M_FLG_ENABLE word(0x0001)
#define RDA5807M_FLG_TUNE word(0x0010)
//masks
#define RDA5807M_CHAN_MASK 0xFFC0
#define RDA5807M_CHAN_SHIFT 6
#define RDA5807M_VOLUME_MASK word(0x000F)
#define RDA5807M_VOLUME_SHIFT 0
#define RDA5807M_RSSI_MASK 0xFE00
#define RDA5807M_RSSI_SHIFT 9

uint8_t volume = 15; // 0..15
uint16_t freq = 1073; // 107.3FM
uint16_t reg02h, reg03h, reg05h, reg0Bh;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  // Register 02h - enable, settings
  reg02h = RDA5807M_FLG_ENABLE | RDA5807M_FLG_DHIZ | RDA5807M_FLG_DMUTE;
  setRegister(RDA5807M_REG_CONFIG, reg02h);
  
  // And then we decided to further strengthen the bass:
  reg02h |= RDA5807M_FLG_BASS;
  setRegister(RDA5807M_REG_CONFIG, reg02h);
  
  // Register 03h - radio station selection
  // After reset in register 03h, the default value is 0
  // Thus BAND = 00 (87..108MHz), STEP = 00 (100kHz). Let's leave them as they are
  reg03h = (freq - 870) << RDA5807M_CHAN_SHIFT; // chan = (freq - band) / space
  setRegister(RDA5807M_REG_TUNING, reg03h | RDA5807M_FLG_TUNE);
  
  // Register 05h. Set the volume, do not change the rest of the beats
  reg05h = getRegister(RDA5807M_REG_VOLUME); // read the current value
  reg05h &= ~RDA5807M_VOLUME_MASK; // Resetting the VOLUME bits
  reg05h |= volume << RDA5807M_VOLUME_SHIFT; // Set a new volume
  setRegister(RDA5807M_REG_VOLUME, reg05h);
}

void loop() {
  // Register 0Bh - status
  reg0Bh = getRegister(RDA5807M_REG_RSSI);
  uint8_t RSSI = (reg0Bh & RDA5807M_RSSI_MASK) >> RDA5807M_RSSI_SHIFT;
  Serial.print("RSSI = ");
  Serial.print(RSSI);
  Serial.println(" (0-min, 127-max)");
  delay(500);
}

void setRegister(uint8_t reg, const uint16_t value) {
  Wire.beginTransmission(0x11);
  Wire.write(reg);
  Wire.write(highByte(value));
  Wire.write(lowByte(value));
  Wire.endTransmission(true);
}

uint16_t getRegister(uint8_t reg) {
  uint16_t result;
  Wire.beginTransmission(RDA5807M_RANDOM_ACCESS_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission(false);
  Wire.requestFrom(0x11, 2, true);
  result = (uint16_t)Wire.read() << 8;
  result |= Wire.read();
  return result;
}

I found a really short sketch that allows you to set the frequency. It remains to set the volume in the same way.

#include <Wire.h>
float f = 104.7; 

void setFrequency(float frequency)
{
  unsigned int frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  byte frequencyH = frequencyB >>8;
  byte frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0X60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write(0x00);
  Wire.endTransmission();
}

void setup() {
  Wire.begin();
  setFrequency(f);
}

void loop()
{
}
8
  • What resources do you mean? RAM? Flash? Computing time? I don't see anything, that seems really bad here
    – chrisl
    Jan 24, 2021 at 9:21
  • I do not need the receiver to interfere with the microcontroller during operation. I noticed a significant slowdown in other code when I use it in conjunction with this code. So I need to keep the program as simple as possible
    – Антон
    Jan 24, 2021 at 9:29
  • 1
    Without seeing the complete code, that slows down, I cannot be sure. But in the code above the biggest slowdown is the delay. You should not use any delay calls. Also: do you really need to read the RSSI continuously? If not, then just don't do it.
    – chrisl
    Jan 24, 2021 at 9:48
  • It seems to me, that you want to optimize for code length (not size). If you compare the two codes in your question, you see two things: The second code does less things (setting only one register instead of 4) and it doesn't use any defines. The first one is the easiest to optimize (only do things, that you need to do). The second one is an antipattern (meaning not a good coding practice). The code uses hexadecimal values without any description. Defines can give these values names without taking up any extra space in the final program (since they are substituted before compilation)
    – chrisl
    Jan 24, 2021 at 11:03
  • 1
    I need to set the frequency and volume once; I don't need to change them during operation. As I understand it, I just need to remove everything from the loop of the first given program. Since I did not create it myself, but took it ready, so there is a lot of unnecessary things.
    – Антон
    Jan 24, 2021 at 13:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.