Menu

timer1 accuracy

Help
2017-06-10
2017-06-11
  • stan cartwright

    stan cartwright - 2017-06-10

    Hi,I posted this in wrong section,sorry. Using timer1 and displaying time between pin high and low the pin fed from signal gen 1 to 100 hz and changing clock and prescaler the display values vary a lot from 4 to 5 digits.Any idea why. The signal gen seems ok on scope. The line #define I2C_DISABLE_INTERRUPTS ON shouldn't affect timer1? Would it affect an interrupt on timer1 overflow?
    converted from timer1 microchip example in timer help. Cheers

    #chip 18F25K22, 16
    #option explicit
    #include <glcd.h>
    #config osc=int,WDT=OFF
    ;
    ; Define I2C settings
    #define I2C_MODE Master
    #define I2C_DATA PORTC.4
    #define I2C_CLOCK PORTC.3
    #define I2C_DISABLE_INTERRUPTS ON
    #define LCD_SPEED SLOW
    ; ----- Define GLCD Hardware settings
    #define GLCD_TYPE GLCD_TYPE_SSD1306
    #define GLCD_I2C_Address 0x78
    ;this is the main program
    #define Switch PORTA.0
    Dir Switch In
    Dim TimerValue As Word
    ;
    InitTimer1 Osc, PS1_1
    GLCDfntDefaultSize = 1
    GLCDCLS
    GLCDPrint (0, 0 , "TIME")
    GLCDfntDefaultSize=2
    ; This code to time high to low time
    do
      ClearTimer 1
      do until switch=1
      Loop
      StartTimer 1
      do until switch=0
      Loop
      StopTimer 1
      TimerValue = Timer1
      GLCDPrint (0, 32 , str(timervalue)+"   ")
      wait 1 s
    Loop
    
     

    Last edit: stan cartwright 2017-06-10
  • William Roth

    William Roth - 2017-06-11

    The code is measuring partial pulses because it starts measuring abritrarily upon any high level, even if it half way through a pulse.

    Add a test at the beginning of the loop so that if the pulse is already high, wait for it to go low. Then start measuring on the next positive edge.

    So add this right after "cleartimer"
    .
    ~~~~
    Do until Switch = 0
    Loop
    ~~~~

     
  • stan cartwright

    stan cartwright - 2017-06-11

    Of course! 5v is not just on and 0v is not just off. Doh.
    Hmm thinking again
    ~~~
    ClearTimer 1
    Do until Switch = 0:Loop
    Do until Switch = 1:Loop
    ~~~
    It's still not defined logic level. What's the difference?
    Playing with idea is interesting seeing repeated samples on lcd.

     

    Last edit: stan cartwright 2017-06-11
  • stan cartwright

    stan cartwright - 2017-06-12

    By changing the input level I can see the trigger (schmit?) point. I'll be aware of that in future if it causes me probs. Ta for reply.

     

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.