#include #include //---- Deklaration of the used pins for SPI0 ---- const uint8_t SPI_MISO = 16; const uint8_t SPI_CS = 17; const uint8_t SPI_SCK = 18; const uint8_t SPI_MOSI = 19; SPISettings parSPI(2000000, MSBFIRST, SPI_MODE0); void setup() { Serial.begin(115200); while(!Serial) delay(10); delay(1000); Serial.println("SPI-Test with MCP3008"); //---- Define the used SPI pins ------- bool xOK = SPI.setRX(SPI_MISO); xOK &= SPI.setTX(SPI_MOSI); xOK &= SPI.setSCK(SPI_SCK); xOK &= SPI.setCS(SPI_CS); if (xOK) SPI.begin(true); // OK -> run else { //---- Error with setXX(...) ----- Serial.println("Fehler bei serXX"); pinMode(25, OUTPUT); while(true) { //---- signal the error with integrated LED digitalWrite(25,HIGH); delay(250); digitalWrite(25,LOW); delay(250); } } } void loop() { uint16_t reading = mcp3008_read(0); Serial.println(reading); delay(500); } //==== Read a value from MCP3008 ===== // channel = # of analogue channel // return (analogue value) //------------------------------------- uint16_t mcp3008_read(uint8_t channel) { SPI.beginTransaction(parSPI); uint8_t temp = SPI.transfer(0x01); uint8_t msb = SPI.transfer(0x80 + (channel << 4)); uint8_t lsb = SPI.transfer(0x00); SPI.endTransaction(); Serial.printf("%d,%d,%d,",temp,msb,lsb); // For debug only return ((msb & 0x03) << 8) + lsb; } SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)); SPI_BAUDRATE 1,000,000 SPI_POLARITY 0 SPI_PHASE 0 SPI_BITS 8 SPI_FIRSTBIT MSB SPI0_SCK Pin 6 SPI0_MOSI Pin 7 SPI0_MISO Pin 4 SPI1_SCK Pin 10 SPI1_MOSI Pin 11 SPI1_MISO Pin 8 bool setRX(pin_size_t pin); bool setCS(pin_size_t pin); bool setSCK(pin_size_t pin); bool setTX(pin_size_t pin);