0
\$\begingroup\$

I try to create periodic pulse train for the clock signal in application. I use an arduino nano board. But I noticed in scopes I see flickering pulse train behaviour. It is very hard to explain this by text, so I have uploaded to YouTube:

https://www.youtube.com/watch?v=G51mplB87Cw

As you can see in the video, the pulse train is not very stable it is flickering randomly. When I save the data as CSV or stop the screen I get the following pulse train sometimes I can catch it like below:

enter image description here

So at this particular point above scope shows some pulses are missing.

Here is the code I use to produce the pulses:

const int CLOCK_PIN = 5;

void setup() {
  pinMode(CLOCK_PIN, OUTPUT);

}

void loop() {      

  for (int i=0; i<25; i++) {    

    //digitalWrite(CLOCK_PIN,LOW);
    PORTD &= ~(1 << 5); // clock pin goes low    
    delayMicroseconds(2);    
    //digitalWrite(CLOCK_PIN,HIGH);
    PORTD |= (1 << 5); // lock pin goes high    
    delayMicroseconds(2); 
  }

    delay(50);
 }

I couldn't figure out whether this is scope artefact or related to the code. What could be the reason for this? This is happening both in Auto and Normal trigger mode.

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Interrupts are occurring while your code is running. The time required to execute the interrupts is significant compared to your desired delay of 2 microseconds. This disturbs your desired timing.

Your options include disabling interrupts while generating this clock signal, but that is likely to have undesired and unintended consequences. A better idea would be to find some way to generate the clock using only hardware resources rather than a delay loop.

\$\endgroup\$
3
  • \$\begingroup\$ What do you mean by hardware resources? Like using FPGA? \$\endgroup\$
    – ty_1917
    Commented Jan 6, 2020 at 13:36
  • \$\begingroup\$ FPGA would be an option BUT most microcontrollers including the ones Arduino use, have built in counter/timer peripherals to do the job. So first learn to program those, and see if you can get them to do what you want. \$\endgroup\$
    – user16324
    Commented Jan 6, 2020 at 14:26
  • \$\begingroup\$ @BrianDrummond I transferred the code the Atmel Studio and used _delay_ms and _delay_us. Seems I dont get that issue anymore. \$\endgroup\$
    – ty_1917
    Commented Jan 6, 2020 at 15:07
0
\$\begingroup\$

Don't use delayMicroseconds, it's not an accurate way to timing. Although they try, as you can see in the code.
See if you can make your signal with hardware timers.

\$\endgroup\$
0
\$\begingroup\$

I'm not sure if @ElliotAlderson has the correct answer, hence here my 2 cents. In the code you posted there shouldn't be running interrupts in the background as you simply don't use one. The delay-functions are blocking ones, not based on interrupt.

My guess is that the flickering is because you toggle for 25 times and then delay for 50ms. Repeating this cycle. Could it be that it is this 50ms you see? --> EDIT: never mind, the delay on the scope trace isn't that long.

As for a fix, I'd use a timer in PWM-mode so you can start it in setup() and let it run without additional code in loop() until it's not needed anymore.

\$\endgroup\$
7
  • \$\begingroup\$ But inside the for loop I get the data bits in to array. So each clock will cause a data bit. \$\endgroup\$
    – ty_1917
    Commented Jan 6, 2020 at 13:45
  • \$\begingroup\$ so PWM wouldnt work for me. \$\endgroup\$
    – ty_1917
    Commented Jan 6, 2020 at 13:46
  • \$\begingroup\$ It's arduino code, so it will have a millisecond timer interrups running in the background, causing the pauses, even if the user does not use interrupts. \$\endgroup\$
    – Justme
    Commented Jan 6, 2020 at 13:55
  • \$\begingroup\$ @ty_1917 you could put the data bits into a buffer in the interrupt handler \$\endgroup\$
    – Swedgin
    Commented Jan 6, 2020 at 13:56
  • \$\begingroup\$ @Justme If that's the case I'm glad I stopped using Arduino code for a while now. That piece of code should work. \$\endgroup\$
    – Swedgin
    Commented Jan 6, 2020 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.