Arduino Forum

Products => Arduino Due => Topic started by: vaggabond on Mar 23, 2018, 11:35 pm

Title: Direct port manipulation using variables
Post by: vaggabond on Mar 23, 2018, 11:35 pm
Hi -

What im trying to do:
Define a precise timing output on some DUE pins, using direct port manipulation to drive some SK6182 RGBW 32 bit LEDs. I am trying to drive 10 strings in parallel.

Per SK6182 datasheet,
"1" is defined as 900ns high, 300ns low.
"0" is defined as 300ns high, 900ns low.

If I write the port values directly, all works fine.
For example, a "1" defined like this

Code: [Select]

#define NOP40 __asm__ __volatile__ ("nop\n\t"...40 times)
#define NOP20 __asm__ __volatile__ ("nop\n\t"...20 times)
      REG_PIOC_ODSR = 0xFFFFFFFF; NOP20; //300ns high
      REG_PIOC_ODSR = 0xFFFFFFFF; NOP40; //600ns high
      REG_PIOC_ODSR = 0x00000000; NOP20;//300ns low

Above works ok, and im getting a clean 900ns high and 300ns low signal on DUE pin.

However, if I change above code to this

Code: [Select]
unsigned long m=0x0xFFFFFFFF;
      REG_PIOC_ODSR = 0xFFFFFFFF; NOP20;  //300ns high
      REG_PIOC_ODSR = m; NOP40;                //600ns high
      REG_PIOC_ODSR = 0x00000000; NOP20; //300ns low

The timing gets inaccurate, and high time jitters between 900~1100ns.
I disabled the interrupts using noInterrupts();

Any idea as why this happens and more important what is causing the 200ns jitter?
I do understand that MCU needs extra 1-2clocks (might be way off on this on this) to take the "m" variable and write it into the register, but I don't understand why the delay is not consistent.

Any suggestions appreciated. Thanks:)

Title: Re: Direct port manipulation using variables
Post by: MasterT on Mar 24, 2018, 02:18 am
Instead of
    unsigned long m=0x0xFFFFFFFF;

   unsigned long m=0x0xFFFFFFFF;