1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ESP32でMCP3208でanalogread

Posted at

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);
}

IMG_3112.JPG

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?