2026年4月1日水曜日

Pico - PCM5102 三角波出力

 
Raspberry Pi Pico を使って PCM5102A I2S DAC を下記サイトのスケッチをそのまま使用して三角波を出力しました。 

接続
Pico   PCM5102
GP22 ー DIN
GP21 ー LCK
GP20 ー BCK
3.3V      ー VIN
GND      ー    GND





参考サイト
●Raspberry Pi PicoとPCM5102AでI2S再生



PCM5102A - 32ビットDAC
アマゾン購入
【SCK】ブリッジ(内部クロック)












【H1L】(FLT)      L側 ブリッジ
【H2L】(DEMP) L側 ブリッジ
【H3L】(XSMT) H側 ブリッジ
【H4L】(FMT)    L側 ブリッジ







プログラム  Arduino IDE【ボード:Raspberry Pi Pico】
#include <I2S.h>

#define PIN_I2S_BCLK 20
#define PIN_I2S_LRCLK (PIN_I2S_BCLK + 1)
#define PIN_I2S_DOUT 22
#define PIN_I2S_MUTE 19

#define BUFFER_SIZE 256

I2S i2s(OUTPUT);
const int32_t sampleRate = 48000;

uint32_t phase       = 0;
uint32_t phase_delta = 0;

int16_t triangle(uint32_t phase) {
  phase += (1 << 30);
  return ((phase < (uint32_t)(1 << 31)) ? (phase >> 16) : ((1 << 16) - (phase >> 16) - 1)) - 16383;
}

void generate_triangle(int16_t *buffer, size_t size, uint32_t *phase, uint32_t phase_delta) {
  for (size_t i = 0; i < size; i++) {
    buffer[i] = triangle(*phase);
    *phase += phase_delta;
  }
}

void setup() {
  pinMode(PIN_I2S_MUTE, OUTPUT);

  i2s.setBCLK(PIN_I2S_BCLK);
  i2s.setDATA(PIN_I2S_DOUT);
  i2s.setBitsPerSample(16);
  i2s.begin(sampleRate);

  phase_delta = 440.0 * (float)(1ULL << 32) / sampleRate;

  digitalWrite(PIN_I2S_MUTE, HIGH);
}

void loop() {
  static size_t buffer_index = 0;

  if (buffer_index == BUFFER_SIZE) {
    generate_triangle(buffer, BUFFER_SIZE, &phase, phase_delta);

    buffer_index = 0;
  }

  while (buffer_index < BUFFER_SIZE) {
    i2s.write(buffer[buffer_index]);  // L
    i2s.write(buffer[buffer_index]);  // R
    buffer_index++;
  }
}

0 件のコメント:

コメントを投稿

ESP32 - Bluepad32 ロボカー PS4コントローラー操作

  Bluetooth (Classic / BLE)対応の Bluepad32ライブラリー使用して、 無印E SP32制御ロボットカーを純正PS4コントローラーで操作しました。 (Xboxコントローラーに入替て操作出来ます) 同ブログ【 ESP32 - Bluepad32 PS...