Advertisement
microrobotics

FM62429 interfaced with ESP32

May 11th, 2023
1,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. 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.
  5. */
  6.  
  7. #define DI_PIN 26
  8. #define CLK_PIN 25
  9. #define CS_PIN 27
  10.  
  11. #define FM62429_MAX_VOL 78 // Maximum volume level
  12. #define FM62429_MIN_VOL 0  // Minimum volume level
  13.  
  14. void setup() {
  15.   // Initialize serial communication for debugging
  16.   Serial.begin(115200);
  17.  
  18.   // Set pin modes
  19.   pinMode(DI_PIN, OUTPUT);
  20.   pinMode(CLK_PIN, OUTPUT);
  21.   pinMode(CS_PIN, OUTPUT);
  22.  
  23.   // Set initial volume to maximum
  24.   setVolume(FM62429_MAX_VOL);
  25. }
  26.  
  27. void loop() {
  28.   // Decrease volume every second from maximum to minimum
  29.   for (int volume = FM62429_MAX_VOL; volume >= FM62429_MIN_VOL; volume--) {
  30.     setVolume(volume);
  31.     delay(1000);
  32.   }
  33.  
  34.   delay(5000); // Wait for 5 seconds
  35.  
  36.   // Increase volume every second from minimum to maximum
  37.   for (int volume = FM62429_MIN_VOL; volume <= FM62429_MAX_VOL; volume++) {
  38.     setVolume(volume);
  39.     delay(1000);
  40.   }
  41.  
  42.   delay(5000); // Wait for 5 seconds
  43. }
  44.  
  45. void setVolume(int volume) {
  46.   // The FM62429 receives data in MSB format, with D8-D11 as control bits (set to 1011 for volume control)
  47.   // and D0-D7 as volume level (in 2's complement form, 0 = max volume, 78 = min volume)
  48.   uint16_t data = 0b101100000000 | ((~volume + 1) & 0x7F);
  49.  
  50.   digitalWrite(CS_PIN, HIGH); // Set CS high
  51.   shiftOut(DI_PIN, CLK_PIN, MSBFIRST, data >> 8); // Shift out the high byte
  52.   shiftOut(DI_PIN, CLK_PIN, MSBFIRST, data); // Shift out the low byte
  53.   digitalWrite(CS_PIN, LOW); // Set CS low to latch the data
  54.  
  55.   Serial.println("Volume set to " + String(volume));
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement