Last active
September 28, 2023 07:51
-
-
Save SimedruF/c2486d0dd1270c60a4fd5b82b70611bf to your computer and use it in GitHub Desktop.
ESP_SPI_Master.ino
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
/* | |
Florin Simedru | |
Complete project details at https://blog.automatic-house.ro | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
/* | |
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:esp32doit-devkit-v1] | |
platform = espressif32 | |
board = esp32doit-devkit-v1 | |
framework = arduino | |
upload_port = COM12 | |
monitor_port = COM12 | |
monitor_speed = 115200 | |
*/ | |
#include <Arduino.h> | |
#include <SPI.h> | |
// Define ALTERNATE_PINS to use non-standard GPIO pins for SPI bus | |
#ifdef ALTERNATE_PINS | |
#define HSPI_MISO 26 | |
#define HSPI_MOSI 27 | |
#define HSPI_SCLK 25 | |
#define HSPI_SS 32 | |
#else | |
#define SPI_MISO MISO | |
#define SPI_MOSI MOSI | |
#define SPI_SCLK SCK | |
#define SPI_SS SS | |
#endif | |
void spi_send_command(uint8_t data); | |
static const int spiClk = 1000000; // 1 MHz | |
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6) | |
const int potPin = 34; | |
// variable for storing the potentiometer value | |
int potValue = 0; | |
// uninitalised pointers to SPI objects | |
SPIClass *hspi = NULL; | |
SPIClass *vspi = NULL; | |
void setup() | |
{ | |
// initialise instance of the SPIClass attached to HSPI | |
hspi = new SPIClass(HSPI); | |
vspi = new SPIClass(VSPI); | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
Serial.print("MOSI: "); | |
Serial.println(MOSI); | |
Serial.print("MISO: "); | |
Serial.println(MISO); | |
Serial.print("SCK: "); | |
Serial.println(SCK); | |
Serial.print("SS: "); | |
Serial.println(SS); | |
// clock miso mosi ss | |
#ifndef ALTERNATE_PINS | |
// initialise hspi with default pins | |
// SCLK = 14, MISO = 12, MOSI = 13, SS = 15 | |
vspi->begin(SPI_SCLK, SPI_MISO, SPI_MOSI, SPI_SS); | |
#else | |
// alternatively route through GPIO pins | |
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); // SCLK, MISO, MOSI, SS | |
#endif | |
// set up slave select pins as outputs as the Arduino API | |
// doesn't handle automatically pulling SS low | |
pinMode(SPI_SS, OUTPUT); // SPI SS | |
} | |
// the loop function runs over and over again until power down or reset | |
void loop() | |
{ | |
potValue = analogRead(potPin); | |
Serial.print("Analog Data: "); | |
Serial.println(potValue); | |
uint8_t val = map(potValue, 0,4095, 0, 255); | |
spi_send_command(val); | |
delay(100); | |
} | |
void spi_send_command(uint8_t data) | |
{ | |
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0)); | |
digitalWrite(SPI_SS, LOW); | |
vspi->transfer(data); | |
Serial.print("SPI Data: "); | |
Serial.println(data); | |
digitalWrite(SPI_SS, HIGH); | |
vspi->endTransaction(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment