Created
January 17, 2018 13:05
-
-
Save Tomokatsu-Sakamoto/a61ce5a3d4ee469de4525773c2a68211 to your computer and use it in GitHub Desktop.
ESP-WROOM-02 + MCP3204 でA/D変換
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-WROOM-02 + MCP3204 でA/D変換 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <SPI.h> | |
#define CS 15 | |
void setup( ) | |
{ | |
ESP.eraseConfig(); | |
delay( 500 ); | |
Serial.begin( 115200 ); | |
Serial.println( ); | |
Serial.printf("Serial is %d bps", Serial.baudRate( ) ); | |
pinMode( CS, OUTPUT ); | |
SPI.begin( ); | |
//----- SET SPI MODE ----- | |
//SPI_MODE_0 (0,0) CPOL=0 (Clock Idle low level), CPHA=0 (SDO transmit/change edge active to idle) | |
//SPI_MODE_1 (0,1) CPOL=0 (Clock Idle low level), CPHA=1 (SDO transmit/change edge idle to active) | |
//SPI_MODE_2 (1,0) CPOL=1 (Clock Idle high level), CPHA=0 (SDO transmit/change edge active to idle) | |
//SPI_MODE_3 (1,1) CPOL=1 (Clock Idle high level), CPHA=1 (SDO transmit/change edge idle to active) | |
SPI.setDataMode( SPI_MODE0 ); // データシート 図6.1 | |
SPI.setBitOrder( MSBFIRST ); // most-significant bit first | |
} | |
int getMCP3204( int iCh ) | |
{ | |
int highByte, lowByte; | |
// Reading a data from MCP3204 | |
digitalWrite( CS, LOW ); | |
highByte = SPI.transfer( 0x06 | ( iCh >> 2 ) ); | |
highByte = SPI.transfer( iCh << 6 ); | |
lowByte = SPI.transfer( 0x00 ); | |
digitalWrite( CS, HIGH ); | |
// Serial.printf("MCP3204(%d) %02X-%02X ", iCh, highByte, lowByte ); | |
return ( highByte & 0x0F ) * 256 + lowByte; | |
} | |
void loop( ) | |
{ | |
Serial.printf("MCP3204 Ch0=%4d, Ch1=%4d, Ch2=%4d, Ch3=%4d \n", | |
getMCP3204( 0 ) , getMCP3204( 1 ) , getMCP3204( 2 ) , getMCP3204( 3 ) ); | |
delay( 500 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment