Last active
June 10, 2016 19:37
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ESP_Digital_MIC_To_DAC | |
// by koichi kurahashi 2016-06-08 | |
// | |
// read SPM0405HD4H and write to MCP4921 | |
// | |
// Thanks: | |
// http://www.pwv.co.jp/~take/TakeWiki/index.php?arduino%2FDACを試す | |
// arduino/DACを試す | |
// | |
#include <Arduino.h> | |
#include <SPI.h> | |
extern "C" { | |
#include <user_interface.h> | |
} | |
// | |
// SPM0405 | |
// | |
#define kOutClock 5 // to CLK | |
#define kInSignal 4 // from DAT | |
// L/R is connected to GND -> down edge mode | |
// | |
// DAC MCP4921 | |
// | |
// GPIO 13 - Pin 5 SDI | |
// 14 - 4 SCK | |
// 15 - 3 !CS | |
// 16 - 8 LDAC | |
// 1, 9, 11, 13 - Vcc | |
// 12 - Gnd | |
// 1 - 0.1uF - 13 | |
// | |
const int kPinLatchDAC = 16; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(230400); | |
pinMode(kOutClock, OUTPUT); | |
pinMode(kInSignal, INPUT); | |
setupDac(); | |
} | |
// DAC init | |
// | |
void setupDac() { | |
// set pinMode | |
pinMode(kPinLatchDAC, OUTPUT); | |
pinMode(SS, OUTPUT); | |
// init SPI for DAC | |
SPI.begin(); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setClockDivider(SPI_CLOCK_DIV8); | |
SPI.setDataMode(SPI_MODE0); | |
} | |
// | |
// loop | |
// | |
const int kMaxLoop = 100; | |
const int kMaxDAC = 4095; | |
const int kMaxRate = 2000 / kMaxLoop; | |
const int kMaxOffset = kMaxDAC / 2; | |
const int kOutLoop = 10000 / kMaxLoop; | |
long a, b; // dummy wait | |
void loop() { | |
// int min = 32767; | |
// int max =-32767; | |
while (1) { | |
for (int i = 0; i < kOutLoop; i++) { | |
int count = 0; | |
for (int l = 0; l < kMaxLoop; l++) { // 25n | |
digitalWrite(kOutClock, LOW); // low 212.5n | |
a++; b++; | |
if (digitalRead(kInSignal) == HIGH) { | |
count++; | |
} | |
else { | |
count--; | |
} | |
if (l < kMaxLoop - 1) { | |
digitalWrite(kOutClock, HIGH); // high=225.0n | |
digitalWrite(kOutClock, HIGH); // high=225.0n | |
} | |
} | |
count = count * kMaxRate + kMaxOffset; | |
outputToDAC(count); | |
// if (count > max) max = count; | |
// if (count < min) min = count; | |
} | |
// Serial.print(min); | |
// Serial.print(","); | |
// Serial.println(max); | |
ESP.wdtFeed(); | |
} | |
} | |
void outputToDAC(uint16_t inData) { | |
inData = inData & 0xfff; | |
digitalWrite(kPinLatchDAC, HIGH); | |
digitalWrite(SS, LOW); | |
SPI.transfer16(inData | 0x3000); | |
digitalWrite(SS, HIGH); | |
digitalWrite(kPinLatchDAC, LOW) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment