|
24 | 24 | 2nd September 2021
|
25 | 25 | Lucas Saavedra Vaz (lucasssvaz)
|
26 | 26 | 22nd December 2023
|
| 27 | +anon |
| 28 | +10nd February 2025 |
27 | 29 | */
|
28 | 30 |
|
29 | 31 | #include <ESP_I2S.h>
|
30 | 32 |
|
| 33 | +// The GPIO pins are not fixed, most other pins could be used for the I2S function. |
| 34 | +#define I2S_LRC 25 |
| 35 | +#define I2S_BCLK 5 |
| 36 | +#define I2S_DIN 26 |
| 37 | + |
31 | 38 | const int frequency = 440; // frequency of square wave in Hz
|
32 | 39 | const int amplitude = 500; // amplitude of square wave
|
33 | 40 | const int sampleRate = 8000; // sample rate in Hz
|
@@ -36,17 +43,19 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT;
|
36 | 43 | i2s_mode_t mode = I2S_MODE_STD;
|
37 | 44 | i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO;
|
38 | 45 |
|
39 |
| -const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave |
| 46 | +const unsigned int halfWavelength = sampleRate / frequency / 2; // half wavelength of square wave |
40 | 47 |
|
41 | 48 | int32_t sample = amplitude; // current sample value
|
42 |
| -int count = 0; |
| 49 | +unsigned int count = 0; |
43 | 50 |
|
44 | 51 | I2SClass i2s;
|
45 | 52 |
|
46 | 53 | void setup() {
|
47 | 54 | Serial.begin(115200);
|
48 | 55 | Serial.println("I2S simple tone");
|
49 | 56 |
|
| 57 | +i2s.setPins(I2S_BCLK, I2S_LRC, I2S_DIN); |
| 58 | + |
50 | 59 | // start I2S at the sample rate with 16-bits per sample
|
51 | 60 | if (!i2s.begin(mode, sampleRate, bps, slot)) {
|
52 | 61 | Serial.println("Failed to initialize I2S!");
|
@@ -60,8 +69,13 @@ void loop() {
|
60 | 69 | sample = -1 * sample;
|
61 | 70 | }
|
62 | 71 |
|
63 |
| -i2s.write(sample); // Right channel |
64 |
| -i2s.write(sample); // Left channel |
| 72 | +// Left channel, the low 8 bits then high 8 bits |
| 73 | +i2s.write(sample); |
| 74 | +i2s.write(sample >> 8); |
| 75 | + |
| 76 | +// Right channel, the low 8 bits then high 8 bits |
| 77 | +i2s.write(sample); |
| 78 | +i2s.write(sample >> 8); |
65 | 79 |
|
66 | 80 | // increment the counter for the next sample
|
67 | 81 | count++;
|
|
0 commit comments