Program to turn on VS1003 builtin MIC using NodeMCU

Upload the following program to your NodeMCU. Connect headphone and speak something through the MIC. Voice can be heard in the head phone.


#include <VS1003ESP12.h>
#include <ESP8266WiFi.h>
#include <SPI.h>

// VS1003 SCI Write Command byte is 0x02
#define VS_WRITE_COMMAND 0x02

// VS1003 SCI Read COmmand byte is 0x03
#define VS_READ_COMMAND  0x03

const int csPin = D1;
const int dcsPin = D0;
const int dreqPin = D2;
const int resetPin = D3;

// SCI Registers

const uint8_t SCI_MODE = 0x0;
const uint8_t SCI_STATUS = 0x1;
const uint8_t SCI_BASS = 0x2;
const uint8_t SCI_CLOCKF = 0x3;
const uint8_t SCI_DECODE_TIME = 0x4;
const uint8_t SCI_AUDATA = 0x5;
const uint8_t SCI_WRAM = 0x6;
const uint8_t SCI_WRAMADDR = 0x7;
const uint8_t SCI_HDAT0 = 0x8;
const uint8_t SCI_HDAT1 = 0x9;
const uint8_t SCI_AIADDR = 0xa;
const uint8_t SCI_VOL = 0xb;
const uint8_t SCI_AICTRL0 = 0xc;
const uint8_t SCI_AICTRL1 = 0xd;
const uint8_t SCI_AICTRL2 = 0xe;
const uint8_t SCI_AICTRL3 = 0xf;
const uint8_t SCI_num_registers = 0xf;

// SCI_MODE bits

const uint8_t SM_DIFF = 0;
const uint8_t SM_LAYER12 = 1;
const uint8_t SM_RESET = 2;
const uint8_t SM_OUTOFWAV = 3;
const uint8_t SM_EARSPEAKER_LO = 4;
const uint8_t SM_TESTS = 5;
const uint8_t SM_STREAM = 6;
const uint8_t SM_EARSPEAKER_HI = 7;
const uint8_t SM_DACT = 8;
const uint8_t SM_SDIORD = 9;
const uint8_t SM_SDISHARE = 10;
const uint8_t SM_SDINEW = 11;
const uint8_t SM_ADPCM = 12;
const uint8_t SM_ADCPM_HP = 13;
const uint8_t SM_LINE_IN = 14;


void control_mode_on(void)
{
  digitalWrite(dcsPin,HIGH);
  digitalWrite(csPin,LOW);
}

void await_data_request(void)
{
  while ( !digitalRead(dreqPin) );
}

void control_mode_off(void)
{
  digitalWrite(csPin,HIGH);
}

uint16_t read_register(uint8_t _reg)
{
  uint16_t result;
  control_mode_on();
  delayMicroseconds(1); // tXCSS
  SPI.transfer(VS_READ_COMMAND); // Read operation
  SPI.transfer(_reg); // Which register
  result = SPI.transfer(0xff) << 8; // read high byte
  result |= SPI.transfer(0xff); // read low byte
  delayMicroseconds(1); // tXCSH
  await_data_request();
  control_mode_off();
  return result;
}

void write_register(uint8_t _reg, uint16_t _value)
{
  control_mode_on();
  delayMicroseconds(1); // tXCSS
  SPI.transfer(VS_WRITE_COMMAND); // Write operation
  SPI.transfer(_reg); // Which register
  SPI.transfer(_value >> 8); // Send hi byte
  SPI.transfer(_value & 0xff); // Send lo byte
  delayMicroseconds(1); // tXCSH
  await_data_request();
  control_mode_off();
}

void data_mode_on(void)
{
  digitalWrite(csPin,HIGH);
  digitalWrite(dcsPin,LOW);
}

void data_mode_off(void)
{
  digitalWrite(dcsPin,HIGH);
}

void setVolume(uint8_t vol)
{
  uint16_t value = vol;
  value <<= 8;
  value |= vol;

  write_register(SCI_VOL,value); // VOL
}

void onMIC(void)
{
  // Keep the chip in reset until we are ready
  pinMode(resetPin,OUTPUT);
  pinMode(csPin,OUTPUT);
  pinMode(dcsPin,OUTPUT);
  pinMode(dreqPin, INPUT); 

  digitalWrite(resetPin, LOW);
  digitalWrite(csPin, HIGH);
  digitalWrite(dcsPin, HIGH);

  digitalWrite(resetPin, HIGH);

  //SPI.setDataMode(SPI_MODE0);
  //SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV64); 

  write_register(SCI_VOL, 0x0000);
  write_register(SCI_BASS, 0);
  write_register(SCI_CLOCKF, 0x4430);

  write_register(SCI_AICTRL0, 12);
  delay(100);
  /* Rec level: 1024 = 1. If 0, use AGC */
  write_register(SCI_AICTRL1, 1024);
  delay(100);
  /* Maximum AGC level: 1024 = 1. Only used if SCI_AICTRL1 is set to 0. */
  //write_register(SCI_AICTRL2, 0);
  //delay(100);
  /* Miscellaneous bits that also must be set before recording. */
  //write_register(SCI_AICTRL3, 0);

  //write_register(SCI_MODE, _BV(SM_ADPCM) | _BV(SM_RESET) | _BV(SM_ADCPM_HP) | _BV(SM_SDINEW));
  write_register(SCI_MODE, 0x1804);
}

void setup () {

  // initiate SPI
  SPI.begin();

    // initiate a serial port at 57600
    Serial.begin(115200);

    //configure();

    //setVolume(0x20);

    onMIC();
}

void loop() {

}
5 1 vote
Article Rating

Published by

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
X