+ Post New Thread
Results 1 to 14 of 14
  1. #1
    Member level 2
    Points: 1,009, Level: 7

    Join Date
    Sep 2015
    Posts
    42
    Helped
    0 / 0
    Points
    1,009
    Level
    7

    100 kHz interleaved signal generation?

    I am generating a 100 kHz interleaved signal (image attached below) with 80 % duty cycle using the following code for Arduino UNO. However, when I measure the signal frequency with oscilloscope, I get 48 kHz only. If I try to reduce the delayMicroseconds() the maximum achievable frequency is 61 kHz.

    Why this is happening? Either with UNO I cannot get more than 61 kHz or the oscilloscope measurements are incorrect... I could figure out the reason. Please help me with this issue.

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    void setup()
    {
      pinMode(11, OUTPUT);
      pinMode(13, OUTPUT);
     
      digitalWrite(11, HIGH);
      digitalWrite(13, HIGH);
    }
     
    void loop()
    {
      digitalWrite(13, LOW);
      delayMicroseconds(2);
      digitalWrite(13, HIGH);
      delayMicroseconds(3);
      digitalWrite(11, LOW);
      delayMicroseconds(2);
      digitalWrite(11, HIGH);
      delayMicroseconds(3);
    }

    Click image for larger version. 

Name:	signal.png 
Views:	3 
Size:	23.0 KB 
ID:	151731

  2. #2
    Advanced Member level 5
    Points: 24,699, Level: 38
    barry's Avatar
    Join Date
    Mar 2005
    Location
    California, USA
    Posts
    4,734
    Helped
    1050 / 1050
    Points
    24,699
    Level
    38

    Re: 100 kHz interleaved signal generation?

    Is your clock frequency correct?

    Also, from the Arduino reference:
    "This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times."
    I don't know anything about the Arduino, but this is a very scary statement to me. This is a digital system and everything SHOULD be deterministic, right?

    What is the overhead of the digitalWrite statement? Is it a single clock-cycle? 100 clock cycles?



    •   AltAdvertisement

        
       

  3. #3
    Member level 2
    Points: 1,009, Level: 7

    Join Date
    Sep 2015
    Posts
    42
    Helped
    0 / 0
    Points
    1,009
    Level
    7

    Re: 100 kHz interleaved signal generation?

    Quote Originally Posted by barry View Post
    What is the overhead of the digitalWrite statement? Is it a single clock-cycle? 100 clock cycles?
    digitalWrite takes 3 us on UNO... thats what the problem is. Any solution?



  4. #4
    Super Moderator
    Points: 29,994, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,947
    Helped
    1134 / 1134
    Points
    29,994
    Level
    42
    Blog Entries
    9

    Re: 100 kHz interleaved signal generation?

    Yes, have a search on the Web for some way to configure compilation to generate output listing of disassembled code, so that you can precisely determine how long each microprocessor instruction take from the core.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  5. #5
    Advanced Member level 4
    Points: 7,977, Level: 21
    Achievements:
    7 years registered

    Join Date
    Jan 2015
    Posts
    1,117
    Helped
    350 / 350
    Points
    7,977
    Level
    21

    Re: 100 kHz interleaved signal generation?

    I've not done this myself on the Uno but doing a little Googling I found https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM that mentions the MCU in the Uno has 3 hardware PWMs and shows how to manipulate the registers directly. Using the hardware is generally a better approach than bit-banging when timing is important.
    Susan


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 5
    Points: 24,699, Level: 38
    barry's Avatar
    Join Date
    Mar 2005
    Location
    California, USA
    Posts
    4,734
    Helped
    1050 / 1050
    Points
    24,699
    Level
    38

    Re: 100 kHz interleaved signal generation?

    48 clocks to set a bit. Pffft. This apparently not very good for realtime applications.

    Maybe you have to write some subroutines in assembler to get any kind of speed out of this.



  7. #7
    Advanced Member level 4
    Points: 7,977, Level: 21
    Achievements:
    7 years registered

    Join Date
    Jan 2015
    Posts
    1,117
    Helped
    350 / 350
    Points
    7,977
    Level
    21

    Re: 100 kHz interleaved signal generation?

    Don't forget that everything is done via library code written in C++ on Arduinos. That and the library code tries to handle all of the variants of processor that can make up an Arduino.
    Susan


    1 members found this post helpful.

  8. #8
    Member level 2
    Points: 1,009, Level: 7

    Join Date
    Sep 2015
    Posts
    42
    Helped
    0 / 0
    Points
    1,009
    Level
    7

    Re: 100 kHz interleaved signal generation?

    With some examples I have managed to get one of the required signals with the following code using Time 1. How I can get the other phase-shifted signal?

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    void setup()
    {
      // Set PB1 to be an output (Pin9 Arduino UNO)
      DDRB |= (1 << PB1);
     
      // Clear Timer/Counter Control Registers
      TCCR1A = 0;
      TCCR1B = 0;
     
      // Set inverting mode
      TCCR1A |= (1 << COM1A1);
      TCCR1A |= (1 << COM1A0);
     
      // Set PWM Phase Correct, Mode 10
      TCCR1A |= (1 << WGM11);
      TCCR1B |= (1 << WGM13);
     
      // Set prescaler to 1 and starts PWM
      TCCR1B |= (1 << CS10);
      TCCR1B |= (0 << CS11);
     
      // Set PWM frequency = 100kHz, duty-cycle = 20%
      ICR1 = (F_CPU / (1*200000)) - 1;
      OCR1A = ICR1 / (100 / 20);
    }
     
    void loop()
    {
      // main loop code
    }
    Last edited by BradtheRad; 14th March 2019 at 08:07. Reason: Put code in formatted window



  9. #9
    Super Moderator
    Points: 29,994, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,947
    Helped
    1134 / 1134
    Points
    29,994
    Level
    42
    Blog Entries
    9

    Re: 100 kHz interleaved signal generation?

    How I can get the other phase-shifted signal?
    One thing is modulation by pulse width varying in time, where the signal period is constant, but another thing is phase modulation where the pulse width can be constant. Should we assume that you mean by "phase-shifted", another PWM signal? Keep in mind that many uC's PWM peripherals have the same source for the period, therefore you likely would not be able to generate another PWM output shifted in time. Anyway, how about doing some calculation on you own? There are numerous tools available on the web that could help you use another timer to provide such a delay; this is a straightforward task.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  10. #10
    Advanced Member level 4
    Points: 6,247, Level: 18
    kripacharya's Avatar
    Join Date
    Dec 2012
    Location
    New Delhi
    Posts
    1,209
    Helped
    182 / 182
    Points
    6,247
    Level
    18

    Re: 100 kHz interleaved signal generation?

    Using assembly instructions and bit bang, it is easy to achieve your waveforms. A single cycle of 100Khz translates to 160 processor cycles. More than enough.

    - - - Updated - - -

    Using even fast PWM you will not get more than 62.5Khz frequency



  11. #11
    Advanced Member level 4
    Points: 8,293, Level: 21
    Achievements:
    7 years registered
    paulfjujo's Avatar
    Join Date
    Jun 2008
    Location
    France 01120
    Posts
    1,328
    Helped
    270 / 270
    Points
    8,293
    Level
    21

    Re: 100 kHz interleaved signal generation?

    hello,

    i confirm .. with asm or asm in C langage !
    but not with an Arduino...nor PWM

    1Mhz output with Fosc=16Mhz
    so easier to output 100Khz

    you also can use internal FOSC, and do a litle adjustement by OSCTUNE register in the range of +-3%



    •   AltAdvertisement

        
       

  12. #12
    Advanced Member level 4
    Points: 6,247, Level: 18
    kripacharya's Avatar
    Join Date
    Dec 2012
    Location
    New Delhi
    Posts
    1,209
    Helped
    182 / 182
    Points
    6,247
    Level
    18

    Re: 100 kHz interleaved signal generation?

    Quote Originally Posted by paulfjujo View Post
    hello,

    i confirm .. with asm or asm in C langage !
    but not with an Arduino...nor PWM

    1Mhz output with Fosc=16Mhz
    so easier to output 100Khz

    you also can use internal FOSC, and do a litle adjustement by OSCTUNE register in the range of +-3%
    What does all this mean ? Kindly explain. without explanation it does not mean anything at all. It's just a bunch of words written together which somehow might sound very smart.... but it doesn't make any sense. So please clarify.



  13. #13
    Advanced Member level 4
    Points: 8,293, Level: 21
    Achievements:
    7 years registered
    paulfjujo's Avatar
    Join Date
    Jun 2008
    Location
    France 01120
    Posts
    1,328
    Helped
    270 / 270
    Points
    8,293
    Level
    21

    Re: 100 kHz interleaved signal generation?

    hello,

    Quote Originally Posted by kripacharya View Post
    . but it doesn't make any sense. So please clarify.
    not more explanation than in post #10 ...

    did you click on the link ?

    even explanation are in french, C code or ASM code is universal ..

    just one loop to toogle a bit (output of MCU) ON then OFF .. in asm ..
    Frequency depend of the FOSC of the used MCU ..and number of MCU cycles to do an operation.
    NOP instruction is used to adjust the period, so the frequency .
    in this case a PIC18Fxxxx is used , not an Arduino.



  14. #14
    Advanced Member level 4
    Points: 6,247, Level: 18
    kripacharya's Avatar
    Join Date
    Dec 2012
    Location
    New Delhi
    Posts
    1,209
    Helped
    182 / 182
    Points
    6,247
    Level
    18

    Re: 100 kHz interleaved signal generation?

    Quote Originally Posted by paulfjujo View Post
    ...
    just one loop to toogle a bit (output of MCU) ON then OFF .. in asm ..
    Frequency depend of the FOSC of the used MCU ..and number of MCU cycles to do an operation.
    NOP instruction is used to adjust the period, so the frequency ....
    Yes, the name given to such a method is 'bit banging'. Arduino boards are typically run from 16Mhz crystal/ resonators, hence 1 processor cycle is 62.5nS. Most asm opcodes are single cycle (like NOP), while some are 2-cycle (like SBI, CBI), and some 3-cycle (like JMP).

    Of course the downside is that cpu gets totally tied.

    - - - Updated - - -

    Precision of frequency depends solely on accuracy of crystal, and in case of 100Khz the duty cycle can be varied with precision of 1/160.



--[[ ]]--