ISR Timer Interrupt Output and void loop() Output not synchronous

osesp32
Posts: 5
Joined: Thu May 31, 2018 7:04 pm

ISR Timer Interrupt Output and void loop() Output not synchronous

Postby osesp32 » Tue Feb 25, 2020 3:28 pm

Hello,
I want to realize a serial synchronous data exchange with GPIOs "by hand". My CLOCK is in a ISR-Timer Interrupt Service Routine and GPIO23 with LED1 at 1Hz. The DATA line is on GPIO22 with LED2 and is performed in void loop() with a pattern stored in an array.
Starting the programm the following happens:
CLK: 0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-
DAT: 1-1-1-0-0-0-0-1-1-1-1-0-0-0-0-1-1-1-1-
Why does the CLOCK in the ISR start with LOW?
After the 0-1-0 sequence at the beginning everything works well.

It should be like this:
CLK: 1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-1-0-
DAT: 1-1-1-1-0-0-0-0-1-1-1-1-0-0-0-0-1-1-1-1-

#define TaktLED 23
#define DatenLED 22
byte stateTaktLED = 0;
byte TaktZaehler = 0;
byte Daten[] = {1,1,0,0,1,1,0,0}; //8 bit Array
byte DatenWert = 0; //Value of Arrays
hw_timer_t * timer = NULL;

void Sendertaktsignal () //ISR
{
if(stateTaktLED == 0)
{
stateTaktLED = 1;
digitalWrite(TaktLED, HIGH);
TaktZaehler ++;
if (TaktZaehler == 8)
{
TaktZaehler = 0;
}
}
else
{
stateTaktLED = 0;
digitalWrite (TaktLED, LOW);
}
}

void setup()
{
pinMode(TaktLED, OUTPUT); //Clock OUT
pinMode(DatenLED, OUTPUT); //DATA IN

timer = timerBegin(0, 80, true); // Prescaler microsec
timerAttachInterrupt(timer, &Sendertaktsignal, true);
timerAlarmWrite(timer, 1000000, true); //Call every 1000ms
timerAlarmEnable(timer);
}

void loop()
{
DatenWert = Daten[TaktZaehler]; //Read Array
digitalWrite(DatenLED,DatenWert); //Output Array
}

Who is online

Users browsing this forum: Google [Bot] and 41 guests