Created
March 30, 2016 16:43
-
-
Save GeorgeHahn/e87373fc6c57afa3cd9d0fbc409ca648 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
ADC128S022 | |
12 bits: 0.806 mV/lsb (3.3v), 1.22 mV/lsb (5v) | |
Bandwidth: 8MHz (3.3v), 11MHz (5v) | |
ESP8266 SPI pins are hardcoded to the following: | |
SS – D8, MOSI – D7, MISO – D6, SCK – D5 | |
*/ | |
#include "SPI.h" | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("ADC begin"); | |
// Chip select pin needs to be an output | |
pinMode(SS, OUTPUT); | |
SPI.begin(); | |
SPI.setFrequency(1600000); // 1.6 MHz | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setDataMode(SPI_MODE2); // Mode=2 CPOL=1, CPHA=0 | |
} | |
// Channel map for our analog shield, pre-shifted for ADC register | |
const uint8_t channelmap[] = { 3 << 3, 2 << 3, 1 << 3, 0, 5 << 3, 6 << 3, 7 << 3, 4 << 3, }; | |
// Slow read | |
int readADC(byte chan) | |
{ | |
// Power up chip | |
digitalWrite(SS, LOW); | |
// Bits 3-5 are channel address (and the channels on our board are out of order) | |
int hi = SPI.transfer(channelmap[chan]); | |
int lo = SPI.transfer(0); | |
// Power | |
digitalWrite(SS, HIGH); | |
return (hi << 8) | lo; | |
} | |
void readADCFastBegin() | |
{ | |
// Power up chip | |
digitalWrite(SS, LOW); | |
} | |
void readADCFastEnd() | |
{ | |
// Power down chip | |
digitalWrite(SS, HIGH); | |
} | |
int readADCFast(uint8_t chan) | |
{ | |
// Bits 3-5 are channel address (and the channels on our board are out of order) | |
int hi = SPI.transfer(channelmap[chan]); // to run even faster, premap this | |
int lo = SPI.transfer(0); | |
return (hi << 8) | lo; | |
} | |
void loop() | |
{ | |
for (int i=0; i<=7; i++) { | |
int val = readADC(i); | |
Serial.print(val); | |
Serial.print("\t"); | |
} | |
Serial.println(); | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment