Menu

Where comes the delay from

Help
bed
2017-11-26
2017-11-27
  • bed

    bed - 2017-11-26

    Hi, a simple loop should generate 11 Pulses of 22µs with pause of 4µs.
    But even I omitt the wait 4µs I get 15µs low pulses.
    Why? I stripped the Code to the minimum
    I used in pricip this from Help:
    http://gcbasic.sourceforge.net/help/_generate_accurate_pulses.html
    The Hi Pulse is 22µs ,not 23
    the LOW is 15µs

        #chip 18f25k22, 8
        #option Explicit
    
        #define LEDD1 PORTB.1
    
        Dir     LEDD1         Out
    ; ---- Setup Pulse parameters
    #define PulsePin PORTB.5
        Dim Time_us As WORD
        Dir PulsePin Out     'Pulsout pin
        Set PulsePin off
        InitTimer1 Osc, PS1_2  'For 8Mhz Chip
    
       'Set the LED states
        LEDD1 = 1
    
    ' Frequency 36.7 KHz
    ' Period 27.248 µs
        dim BURSTPULSE,period as byte
        PERIOD = 14
        do
            Wait 200 ms
            for BURSTPULSE = 0 to 10
                Pulse_Out_US (23)  
            '   wait 4 us
            next BURSTPULSE 
        loop
    
    
        SUB PULSE_OUT_US (IN Variable as WORD)
        TMR1H = 65535 - Variable_H      'Timer 1 Preset High
        TMR1L = (65535 - Variable) + 4  'Timer 1 Preset Low
        Set TMR1ON ON                'Start timer1
        Set PulsePin ON               'Set Pin high
        Do While TMR1IF = 0      'Wait  for Timer1 overflow
            Loop
        Set PulsePin off          ' Pin Low
        Set TMR1ON OFF            ' Stop timer 1
        TMR1IF = 0             'Clear the Int flag
        END SUB
    

    Scope

    I must misst something, but can't think of it.

     

    Last edit: bed 2017-11-26
  • William Roth

    William Roth - 2017-11-26

    The delay is a result of overhead related to the instructions required by the loop and jumping to a sub and returning, These all take time. With your chip 8MHz, 1 us resolution is not likely, Increase the chip MHz to 16 or 32 and it will improve.

    That being said there are much better ways to generate accurate pulses.

    Let me make you something that is a bit more accurate. About 2 hrs

     

    Last edit: William Roth 2017-11-26
  • bed

    bed - 2017-11-26

    William, thanx in advance. The 8 MHz is set, if I must go higher then I have to use external OSC. The Target PIC is an PIC16F818. Unfotunatley the PCB is already produced. :-(
    This 8 MHz 25k22 is just a test if it is possible to manage the 36.7KHz Pulse Bursts

     
  • William Roth

    William Roth - 2017-11-26

    @Bed

    For your specific application the included code should work better as there is much less instruction time on the "off time" side.

    You can tweak the period by adjusting the value of "PR2" the timer2 Match register. The value of 158 is a corrected value that considers the instructions required to handle the interrupt routine. I have no no magic formula. I just got it close and then tweaked it until it was correct. The chip needs to to operate at 32Mhz to keep the instruction cycle time as low as possible.

    I get a period of 27.2us and a frequency of 36.77KHz. Pretty close. Please try this and see what your scope tells you. Let me know.

    #chip 18f25K22, 32
    #option Explicit
    #define LEDD1 PORTB.1
    Dir LEDD1  Out
    
    '; ---- Setup Pulse parameters
    #define PulsePin PORTB.5
    Dir PulsePin Out
    Set PulsePin off
    Dim Count as byte
    
    PR2 = 158  '// This is the timer2 Period or Match register
               '// When TMR2 increments and "matches" PR2
               '// the TMR2 interrupt flag is set.
               '// This register sets the pulse on time.
    
    InitTimer2 (2,PS2_4,0)
    Starttimer 2
    On interrupt Timer2Match Call PulseCounter
    
    Count = 0
    
    Do
      'Waiting for Interrupt
    
    Loop
    
    Sub PulseCounter
       Set PulsePin OFF
       Count++
       If count > 10 then
          Wait 200 ms
          Count = 0
       End if
       Wait 3 us     '// actually 4us due to some overhead
       Set PulsePin ON
       TMR2 = 0     '// Clear timer2 to zero
    End Sub
    
     
  • William Roth

    William Roth - 2017-11-27

    I just read your message. Let me see what I can do with 8 Mhz on a 16F818 or something close.

    Sometimes simple is better. This works acceptably well on 16F1829 and should be the same on 16F818. Period is 27.2 us. Frequency is ~36.76KHz

    NOP adds a short delay to fine tune delays

    Use Osctune register to nail it dow to exactly 36.7Khz

    #chip 16f1829, 8
    #option Explicit
    #define LEDD1 PORTB.1
    Dir LEDD1  Out
    
    '; ---- Setup Pulse parameters
    #define PulsePin PORTB.5
    Dir PulsePin Out
    Set PulsePin off
    Dim Count as byte
    
    '// Trim with OSCTUNE to get a period of exactly 27.2 us  
    OSCTUNE = b'00111001'
    
    Do
        Repeat 11
            Set portB.5 On
            Wait 22 us
            NOP
            set portB.5 off
            NOP : NOP
        End Repeat
    
        Wait 200 ms
    Loop
    
     
  • bed

    bed - 2017-11-27

    Hello William, thank you for taking the time to analyze my problem and to finish it right on time.
    I did the finetuning with assembler 30 years ago. (that was for a heating controller with 68HC11, at that time it was about the duty cycle of a SMT-160 temperature sensor).
    Wonderful how easy it is to mix in Great Cow BASIC!
    What I did not know was the Basic Loop REPEAT. I read the same in the Help System .. wonderful.
    Thanks again for your support!

     
  • bed

    bed - 2017-11-27

    This evening I do not have enough time to test, but a short Test of Repeat -- instead of for ..next
    Result: from 15µs to 13.8µs.
    Not very much, but for the last percent it is worth to use it as a standard way, at least if the counter value isn't needed anyway.

     
    • Chris Roper

      Chris Roper - 2017-11-27

      You can also try my favorite:

      Do
      ...
      ...
      Loop
      

      or the old fashioned

      Start:
      ...
      ...
      Goto Start
      

      Both may shave time, but I am not sure how much if any.

      Cheers
      Chris

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.