Hi i'm a little bit new programming on arduino. I am programming an esp32-s2 to control the energy consumption of a motor and move the motor at the same time. To do that i want to read the adc (ADS131M03IRUKT). That adc meassures voltage, current and 1 encoder that determinate the position of the motor using 3 channels. This adc sends data using SPI. I want to read the SPI each ms to integrate the multiplication of current and voltage and meassure the energy each second. At the same time i want to move the motor to meassure the difference on consumption when the motor is working or not.
I have developed one routine to read the SPI using digitalwrite() to modified the value of sclk and cs to read values on miso when drdy is active and data is ready to get readed. But i think i can read SPI using SPI master in esp32-s2-SOLO-N4R2 in an easier way. The problem is that i don't know how i can configurate the SPI master and which are the pins that i can use for the signals (MOSI,MISO,SCLK,CS,SYNC/RESET and DRDY). Also i dont know how can i generate the pulses to move my step by step motor and the pulses for reading SPI at the same time.
Here is my code:
//Variables globales
int i=0;
long ReadData[3];
long TData;
int EnergiaLeido = 0;
int EnergiaEnvio = 0;
int MuestrasEnergia = 0;
int Multiplicacion = 0;
int Energia = 0;
double Total = 0;
int velocidad = 120;
//Configuración del motor
const int MODE= 6;
const int MODE1= 7;
const int STEP= 8;
const int MODE0= 15;
const int ENABLE= 16;
const int CW_CCW= 17;
const int RESET= 18;
const int pasomaximo = 5000; //max number of steps
//Pines de los sensores
const int CS_ENERGIA = 3; //CS_ENERGIA
const int DIN = 9; //MOSI
const int CS = 10; //CS
const int DOUT = 11; //MISO
const int SCLK = 12; //SCLK
const int CS2 = 21; //CS2
const int DRDY = 35; //DRDY
const int SYNC_RESET = 36; //SYNC/RESET
//////////////////////////////////////////////////////////////////////////////
void leerADS131M03(){
int bitleido = 0;
digitalWrite(SCLK,LOW);
digitalWrite(CS,HIGH);
while(digitalRead(DRDY)==0){
//Serial.println("Esperando datos");
}
digitalWrite(CS_ENERGIA,LOW);
for(int p=0;p<3;p++){
ReadData[p] = 0;
i = 24;
while (i > 0) {
delayMicroseconds(2);
digitalWrite(SCLK,HIGH);
delayMicroseconds(2);
digitalWrite(SCLK,LOW);
bitleido=digitalRead(DOUT);
if (i>1){
if (bitleido == 1) {
bitSet(ReadData[p],(i-1));
}
}
i--;
}
//Serial.println(ReadData[p]);
}
digitalWrite(CS_ENERGIA,HIGH);
}
//////////////////////////////////////////////////////////////////////////////////
void leerMAX6682MUAT(){
int negativo = 0;
int bitleido = 0;
digitalWrite(SCLK,LOW);
digitalWrite(CS_ENERGIA,HIGH);
digitalWrite(CS,LOW);
TData = 0;
i = 11;
while (i > 0) {
delayMicroseconds(10);
digitalWrite(SCLK,HIGH);
delayMicroseconds(10);
digitalWrite(SCLK,LOW);
bitleido=digitalRead(DOUT);
if (i==11 && bitleido == 1){
negativo = 1;
}
if (i>1 && i<11){
if(negativo == 1){
if (bitleido == 0) {
bitSet(TData,(i-1));
}
}
else{
if (bitleido == 1) {
bitSet(TData,(i-1));
}
}
}
i--;
}
if (negativo == 1){
TData = TData * (-1);
}
//Serial.println(TData*0.1+17);
digitalWrite(CS,HIGH);
}
////////////////////////////////////////////////////////////////////////////
void desplazaalasalida() {
digitalWrite (RESET, LOW);
digitalWrite (ENABLE, HIGH); //enciendo el motor
for (int i=0; i<3; i++) {
digitalWrite(STEP,HIGH);
delayMicroseconds(velocidad);
digitalWrite(STEP,LOW);
delayMicroseconds(velocidad);
}
digitalWrite (RESET, HIGH);
for (int i=0; i<2; i++) {
digitalWrite(STEP,HIGH);
delayMicroseconds(velocidad);
digitalWrite(STEP,LOW);
delayMicroseconds(velocidad);
}
delay (200);
digitalWrite (MODE0, HIGH); //
digitalWrite (MODE1, HIGH); //
digitalWrite (MODE, LOW); //
delay (200);
digitalWrite (CW_CCW, LOW);
for (int i=0; i<pasomaximo; i++){ //Forward pasomaximo steps o hasta detectar que ha llegado al fin***
digitalWrite(STEP,HIGH);
delayMicroseconds(velocidad);
digitalWrite(STEP,LOW);
delayMicroseconds(velocidad);
}
vuelvealcomienzo();
}
//////////////////////////////////////////////////////////////////////////////////
bool vuelvealcomienzo() {
digitalWrite (CW_CCW, HIGH);
delay (200);
for (int i=0; i<pasomaximo; i++){
digitalWrite(STEP,HIGH);
delayMicroseconds(velocidad);
digitalWrite(STEP,LOW);
delayMicroseconds(velocidad);
}
para();
return false;
}
//////////////////////////////////////////////////////////////////////////////////
bool para() {
digitalWrite (RESET, LOW); //Posición inicial
digitalWrite (ENABLE, HIGH); //Posición inicial final
delay(100);
digitalWrite (RESET, LOW);
digitalWrite(ENABLE,LOW); //apago el motor
digitalWrite (MODE0, LOW); //
digitalWrite (MODE1, LOW); //
digitalWrite (MODE, LOW); //
return true;
}
/////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
pinMode(ENABLE, OUTPUT);
pinMode(CW_CCW, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(MODE0, OUTPUT);
pinMode(MODE1, OUTPUT);
pinMode(MODE, OUTPUT);
digitalWrite (MODE, LOW); //Esta combinacion pone el motor en full step
digitalWrite (ENABLE, HIGH); //enciendo el motor
digitalWrite (RESET, LOW); //Posición inicial
delay(100);
digitalWrite (ENABLE, LOW); //apago el motor
pinMode(DOUT, INPUT);
pinMode(DRDY, INPUT);
pinMode(DIN, OUTPUT);
pinMode(SYNC_RESET, OUTPUT);
digitalWrite(SYNC_RESET,HIGH);
pinMode(CS, OUTPUT);
digitalWrite(CS,HIGH);
pinMode(CS2, OUTPUT);
digitalWrite(CS2,HIGH);
pinMode(CS_ENERGIA, OUTPUT);
digitalWrite(CS_ENERGIA,HIGH);
pinMode(SCLK, OUTPUT);
digitalWrite(SCLK,LOW);
digitalWrite(SYNC_RESET,LOW);
delayMicroseconds(300);
digitalWrite(SYNC_RESET,HIGH);
digitalWrite(CS_ENERGIA,LOW);
//desplazaalasalida();
}
/////////////////////////////////////////////////////////////////////////////
void loop() {
if(millis() - EnergiaLeido >= 1 && millis() - EnergiaEnvio < 1000){
EnergiaLeido=millis();
leerADS131M03();
Multiplicacion = ReadData[0] * ReadData[1];
Total = Total + Multiplicacion;
MuestrasEnergia++;
}
if (millis() - EnergiaEnvio > 1000){
EnergiaLeido=millis();
leerADS131M03();
Multiplicacion = ReadData[0] * ReadData[1];
Total = Total + Multiplicacion;
MuestrasEnergia++;
Energia = Total / MuestrasEnergia;
Serial.println(Energia);
Serial.println(MuestrasEnergia);
Total = 0;
MuestrasEnergia = 0;
delayMicroseconds(50);
leerMAX6682MUAT();
EnergiaEnvio=millis();
}
desplazaalasalida();
}
Thanks you for your time and assistance. Any help is wellcomed.