ESP32でMCP3208でanalogread
1389 ワード
ESP-WROOM-32とMCP3208でanalogread
コードは下記のやつ。回路図は後ほど書きます。
#include <SPI.h>
#define SPI1_CLK 32
#define SPI1_MISO 33
#define SPI1_MOSI 25
#define SPI1_SS 26
// set SPI freqency 1MHz
#define SPI_CLK 1000000
//uninitalised pointers to SPI objects
SPIClass SPI1(HSPI);
SPISettings spiSettings = SPISettings(SPI_CLK, SPI_MSBFIRST, SPI_MODE1);
void setup() {
Serial.begin(115200);
SPI1.begin(SPI1_CLK, SPI1_MISO, SPI1_MOSI, SPI1_SS);
pinMode(SPI1_SS, OUTPUT);
pinMode(SPI1_CLK, OUTPUT);
pinMode(SPI1_MISO, INPUT);
pinMode(SPI1_MOSI, OUTPUT);
digitalWrite(SPI1_SS, HIGH) ;
}
void loop() {
//byte data = 0b01010101;
byte channel = 0;
Serial.println(adc_read(0));
delay(10);
}
int adc_read(uint8_t channel) {
SPI1.beginTransaction(spiSettings);
digitalWrite(SPI1_SS, LOW);
SPI1.transfer(0x06 | (channel >> 2));
int d1 = SPI1.transfer(channel << 6) ;
int d2 = SPI1.transfer(0x00) ;
digitalWrite(SPI1_SS, HIGH);
SPI1.endTransaction();
return ((d1 & 0x1F) * 256 + d2);
}
Author And Source
この問題について(ESP32でMCP3208でanalogread), 我々は、より多くの情報をここで見つけました https://qiita.com/crawd4274/items/79d3f13797db7ee1eb5c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .