Videos
Do I have to write instructions in assembly? Im using Arduino IDE and im trying to sample a signal at 44.1khz. I come from a PIC Micro background where I would just use a TMR. Whats the play here in Arduino Land?
I am controlling a stepper motor driver with my ESP32 and so I need to have my loop execution time to be low, because otherwise it can´t send enough signals a second. And generally it does that but every 2 seconds it doesn´t, even tough there is nothing changing. There is nothing more being executed by my code. Could that be caused by some feature of the ESP32 C3 and if yes how can I avoid it?
I have already turned of WIFI and Bluetooth and now Hardwareinterrupts are calles, as they only are buttons which I dont press and thus the LCD isn´t updated either.
Code:
#include <AccelStepper.h>
#include <LiquidCrystal.h>
#include <esp_wifi.h>
#include <esp_bt.h>
LiquidCrystal lcd(19, 3, 4, 5, 6, 7);
//Stepper Motor
AccelStepper stepper1(1, 1, 0); // (Type of driver: with 2 pins, STEP, DIR)
boolean plusPressed = false;
boolean minusPressed = false;
boolean switcherPressed = false;
boolean changeStep = false;
boolean oldChangeStep = false;
int curSpeed = 0;
int oldCurSpeed = 0;
int stepSize = 100;
int oldStepSize = 100;
unsigned long lastButtonPress = millis();
void setup()
{
// Disable Wi-Fi
esp_wifi_stop();
// Disable Bluetooth
esp_bt_controller_disable();
esp_bt_controller_deinit();
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(18, INPUT);
//Stepper Motor
stepper1.setAcceleration(500);
stepper1.moveTo(2147483647);
// Display
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(fillupTo16("Speed: " + String(curSpeed)));
lcd.setCursor(0, 1);
lcd.print(fillupTo16("Step Size: " + String(stepSize)));
if(changeStep) {
lcd.setCursor(15, 1);
lcd.print("X");
lcd.setCursor(15, 0);
lcd.print(" ");
}
else {
lcd.setCursor(15, 0);
lcd.print("X");
lcd.setCursor(15, 1);
lcd.print(" ");
}
// Hardware Interrupts
attachInterrupt(digitalPinToInterrupt(18), increasePressed, RISING);
attachInterrupt(digitalPinToInterrupt(10), decreasePressed, RISING);
attachInterrupt(digitalPinToInterrupt(2), changePressed, RISING);
}
void increasePressed() {
if(changeStep) {
stepSize += 50;
lcd.setCursor(0, 1);
lcd.print(fillupTo16("Step Size: " + String(stepSize)));
}
else {
curSpeed += stepSize;
lcd.setCursor(0, 0);
lcd.print(fillupTo16("Speed: " + String(curSpeed)));
}
}
void decreasePressed() {
if(changeStep) {
stepSize -= 50;
if(stepSize < 0) {
stepSize = 0;
}
lcd.setCursor(0, 1);
lcd.print(fillupTo16("Step Size: " + String(stepSize)));
}
else {
curSpeed -= stepSize;
if(curSpeed < 0) {
curSpeed = 0;
}
lcd.setCursor(0, 0);
lcd.print(fillupTo16("Speed: " + String(curSpeed)));
}
}
void changePressed() {
if(changeStep) {
changeStep = false;
lcd.setCursor(15, 1);
lcd.print(" ");
lcd.setCursor(15, 0);
lcd.print("X");
}
else {
changeStep = true;
lcd.setCursor(15, 0);
lcd.print(" ");
lcd.setCursor(15, 1);
lcd.print("X");
}
}
String fillupTo16(String input) {
for(int i = input.length(); i < 15; i++) {
input += " ";
}
return input;
}
void loop()
{
// Stepper Motor Control
stepper1.setMaxSpeed(curSpeed);
stepper1.run();
}Hi guys,
I'm trying to implement something that will generate two triggers at regular intervals where the two triggers are separated by a configurable delay. The delay has to be configurable to sub microsecond resolution. Ideally, 500ns or less.
I chose the ESP32 because of its 240MHz clock, but it still seems to be having trouble.
I'm messing around with the following code:
#define cyclespermicro 240
#define microcycles(n) (n*cyclespermicro)
uint32_t startCounter, counter, cpu_cycles;
int cyclediff, totalcycles, corrected;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
startCounter = ESP.getCycleCount();
totalcycles = startCounter + microcycles(2);
//delayMicroseconds(1);
while (ESP.getCycleCount() < totalcycles) {
__asm__ __volatile__ ("nop");
}
counter = ESP.getCycleCount();
cpu_cycles = counter - startCounter;
corrected = cpu_cycles - 262;
Serial.print("StartCounter: ");
Serial.print(startCounter);
Serial.print(" counter: ");
Serial.print(counter);
Serial.print(" cpu_cycles: ");
Serial.print(cpu_cycles);
Serial.print(" corrected: ");
Serial.println(corrected);
}
void loop() {
}The problem is that I get the same number of clock cycles for 1 us as I do for 2 us. I'm guessing this is because the while loop overhead is overwhelming the 1 us delay.
The problem is I need to have the amount of delay configurable and I don't know the best way to do this. Anyone have any ideas how to efficiently create configurable sub-microsecond delays?
EDIT: I am also noticing, when I get rid of the while loop entirely, and just straight up copy in NOP lines, it works as expected for like 3 NOPs, but once I hit 5 NOPS, the overhead jumps to like 500 cycles. What is going on here?