Announcement

Collapse
No announcement yet.

Announcement

Collapse
No announcement yet.

Synchronization of generators

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Synchronization of generators

    Hi
    Maybe someone knows how to synchronize these two generators?
    ///////////////////////////////////////////////
    HTML Code:
    #define WAVEFORM_SAMPLES 400
    HTML Code:
    int f = 5000;
    ///////////////////////////////////////////////
    HTML Code:
    #include "driver/i2s.h"
    #include <driver/ledc.h>
    
    #define SAMPLE_RATE 1000000
    #define WAVEFORM_SAMPLES 400     // 5khz
    int f = 5000;
    //#define k  SAMPLE_RATE /f/2
    //#define WAVEFORM_SAMPLES  1000000 /2
    //#define WAVEFORM_SAMPLES k
    
    uint8_t stereo_buffer[WAVEFORM_SAMPLES * 2];
    uint8_t wave_data1[WAVEFORM_SAMPLES];
    uint8_t wave_data2[WAVEFORM_SAMPLES];
    
    void generateWaveforms() {
      for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
        wave_data1[i] = (uint8_t)((255 * i) / WAVEFORM_SAMPLES);
        wave_data2[i] = (uint8_t)(127.5 * (1 + sin(2 * 3.14159 * i / WAVEFORM_SAMPLES)));
      }
      for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
        stereo_buffer[i * 2] = wave_data1[i];      // DAC1
        stereo_buffer[i * 2 + 1] = wave_data2[i];  // DAC2
      }
    }
    
    void setup() {
      Serial.begin(115200);
      generateWaveforms();
    
      i2s_config_t i2s_config = {
        .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
        .sample_rate = SAMPLE_RATE,
        .bits_per_sample = I2S_BITS_PER_SAMPLE_8BIT,
        .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
        .communication_format = I2S_COMM_FORMAT_I2S_MSB,
        .intr_alloc_flags = 0,
        .dma_buf_count = 8,
        .dma_buf_len = WAVEFORM_SAMPLES * 2,
        .use_apll = false,
      };
    
      i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
      i2s_set_pin(I2S_NUM_0, NULL);
      i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
      i2s_set_sample_rates(I2S_NUM_0, i2s_config.sample_rate);
      
     ledc_timer_config_t timerConfig = {
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .duty_resolution = LEDC_TIMER_10_BIT,
        .timer_num = LEDC_TIMER_0,
        //.freq_hz = 10000,
        // .freq_hz = 5000,
        .freq_hz = f,
        .clk_cfg = LEDC_AUTO_CLK
      };
      ledc_timer_config(&timerConfig);
    
     
      ledc_channel_config_t channelConfig1 = {
        .gpio_num = 33, //
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .channel = LEDC_CHANNEL_0, //
        .timer_sel = LEDC_TIMER_0, //
        .duty = 512,
        .hpoint = 0
      };
      ledc_channel_config(&channelConfig1);
    
      
      ledc_channel_config_t channelConfig2 = {
        .gpio_num = 32, //
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .channel = LEDC_CHANNEL_1,
        .timer_sel = LEDC_TIMER_0,
        .duty = 512,
        .hpoint = 256
      };
      ledc_channel_config(&channelConfig2);
      
    
    void loop() {
      size_t bytes_written;
      i2s_write(I2S_NUM_0, stereo_buffer, sizeof(stereo_buffer), &bytes_written, portMAX_DELAY);
      Serial.print( "f  = "); Serial.print( f  );
     // Serial.print( "  W  = "); Serial.print(SAMPLE_RATE /f/2 );
        Serial.print( "  W  = "); Serial.print(SAMPLE_RATE /f/2 );
      Serial.println();
    }​
Working...
X