This is my first practical project with an Arduino UNO, and the truth is that I have not touched anything easy :( I need to convert my Arduino into a 14-bit encoder driver for this I need to generate a 14-pulse train to A fixed frequency greater than 30 Khz and to establish between each train a dead time of 50 microseconds, or until a little more. In all the variants that I have realized I have stumbled on my oscilloscope with an annoying jitter or phase shift in the wave, which should be as clean as possible. This was my first crude variant:
void setup() {
pinMode(11, OUTPUT);
}
void loop() {
for (int i=0; i<15; i++){
digitalWrite(11, HIGH);
delayMicroseconds(12);
digitalWrite(11, LOW);
delayMicroseconds(12);
}
delayMicroseconds(50);
}
Then I tried to solve it using the timer to make the wave, and there seems to be a time offset product to stop and summarize the timer to make up the dead time. I use the TimerOne library which I download at: https://github.com/PaulStoffregen/TimerOne
#include <TimerOne.h>
const byte CLOCKOUT = 11;
volatile long counter=0;
void setup() {
Timer1.initialize(15); //Cada 15 microsegundos cambio el estado del pin en la funcion onda dando un periodo
Timer1.attachInterrupt(Onda); //de 30 microsegundos
pinMode (CLOCKOUT, OUTPUT);
digitalWrite(CLOCKOUT,HIGH);
}
void loop() {
if (counter>=29){ //con 29 cambios logro los pulsos que necesito
Timer1.stop(); //Aqui creo el tiempo muerto, el cual esta debe estar en HIGH
digitalWrite(CLOCKOUT,HIGH);
counter=0;
delayMicroseconds(50);
Timer1.resume();
}
}
void Onda(){
digitalWrite(CLOCKOUT, digitalRead(CLOCKOUT) ^ 1); //Cambio el estado del pin
counter+=1;
}
I found a better solution using timer2 in CTC mode