microrobotics icon

FM62429 interfaced with ESP32

microrobotics | PRO | 05/11/23 11:23:17 AM UTC | 0 ⭐ | 3050 👁️ | Never ⏰ | []
Arduino |

1.98 KB

|

None

|

0 👍

/

0 👎

/*
Here is a simple example of how to use the FM62429 with an ESP32. We will use the shiftOut function to send data to the FM62429. Let's assume you've connected the FM62429's DI, CLK and CS pins to GPIOs 26, 25, and 27 respectively.
 
This example sets the volume to the maximum level at startup, then gradually decreases it to the minimum level, waits for 5 seconds, then gradually increases it back to the maximum level, and repeats this process. Please adjust the code according to your needs.
*/
 
#define DI_PIN 26
#define CLK_PIN 25
#define CS_PIN 27
 
#define FM62429_MAX_VOL 78 // Maximum volume level
#define FM62429_MIN_VOL 0  // Minimum volume level
 
void setup() {
  // Initialize serial communication for debugging
  Serial.begin(115200);
 
  // Set pin modes
  pinMode(DI_PIN, OUTPUT);
  pinMode(CLK_PIN, OUTPUT);
  pinMode(CS_PIN, OUTPUT);
 
  // Set initial volume to maximum
  setVolume(FM62429_MAX_VOL);
}
 
void loop() {
  // Decrease volume every second from maximum to minimum
  for (int volume = FM62429_MAX_VOL; volume >= FM62429_MIN_VOL; volume--) {
    setVolume(volume);
    delay(1000);
  }
  
  delay(5000); // Wait for 5 seconds
 
  // Increase volume every second from minimum to maximum
  for (int volume = FM62429_MIN_VOL; volume <= FM62429_MAX_VOL; volume++) {
    setVolume(volume);
    delay(1000);
  }
 
  delay(5000); // Wait for 5 seconds
}
 
void setVolume(int volume) {
  // The FM62429 receives data in MSB format, with D8-D11 as control bits (set to 1011 for volume control)
  // and D0-D7 as volume level (in 2's complement form, 0 = max volume, 78 = min volume)
  uint16_t data = 0b101100000000 | ((~volume + 1) & 0x7F);
 
  digitalWrite(CS_PIN, HIGH); // Set CS high
  shiftOut(DI_PIN, CLK_PIN, MSBFIRST, data >> 8); // Shift out the high byte
  shiftOut(DI_PIN, CLK_PIN, MSBFIRST, data); // Shift out the low byte
  digitalWrite(CS_PIN, LOW); // Set CS low to latch the data
 
  Serial.println("Volume set to " + String(volume));
}
 

Comments