Menu

help with timer

Help
2010-01-14
2013-05-30
  • Nobody/Anonymous

    here is the basic idea, i want to be able to get the time in "ms" that the port is on and save it
    #chip 16F688, 8 'mhz
    #config OSC=INTOSC, MCLRE=off, WDT=off
    InitTimer1 Osc, PS1_1
    Dir portc.1 in
    main:
    if portc.1 on  then startTimer 1
    if portc.1 off  then
    stopTimer 1
    epwrite 1, timer1
    cleartimer 1
    end if
    goto main

     
  • Nobody/Anonymous

    Here are a few of my observations, based on a quick scan of the code:
    1) With an 8MHz clock and a 1:1 prescale value, the timer will roll over after just 30msec. Is that long enough?
    2) The nesting of your IF statements isn't obvious, as there's a missing END IF
    3) Beware of how often you write to EEPROM, as it wears out after some number of writes (read the spec)
    4) There will be a time delay for every EEPROM write (a couple of msec??) that will make the timing inaccurate and variable.
    5) If your program has to do anything else except poll the PORTC.1 input to determine its state, the whole timing scheme will be blown out of the water.

    A better way to do this might to be to use a PORTB input, set it up so that an interrupt is generated whenever the input changes,  read a continuously running TIMER1 on each interrupt, and calculate the difference in timer values between interrupts. This will ensure that you always get an accurate, reproduceable timing no matter what else your program has to do.

     
  • Nobody/Anonymous

    Can you give an example of code for the "better way" you are talking about if poss

     

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.