Menu

Need help with timer

Help
2010-08-26
2013-05-30
  • Nobody/Anonymous

    here is my code, but my timer overfills the 16 bit capacity too quickly and i need to have another variable to store the count of overflows, but how?

    #chip 12F683, 8
    #config INTRC_OSC_NOCLKOUT, MCLRE=off, WDT=off
    #define Switch gpio.0
    Ser_Init
    OPTION_REG.7 = 0 'sets pullup on
    set WPU.0 on
    dir gpio.1 out
    dir gpio.5 out
    Dir Switch In
    DataCount = 0
    dim lcdvaluetemp as word
    dim lcdvalue as word
    InitTimer1 Osc, PS1_8
    set gpio.5 on
    Dim TimerValue As Word
    wait 2 s

    main:
    XMIT_PRINT ("Chronograph v1.0")
    Do
        ClearTimer 1
        Wait Until Switch = Off
        StartTimer 1
        Wait Until Switch = On
        StopTimer 1
       
        'Read the timer
         TimerValue = Timer1
    Xmit_rs232 (h'FE') 'ok clear screen
    Xmit_RS232(h'01')
    wait 10 ms
         bin2ascii2(timervalue_H)
         bin2ascii2(timervalue)

        'Log the timer value
        'EPWrite(DataCount, TimerValue_H)
        'EPWrite(DataCount + 1, TimerValue)
        'DataCount = datacount + 2

    Loop
    goto main

     
  • Nobody/Anonymous

    here is my code now:
    #chip 12F683, 8
    #config INTRC_OSC_NOCLKOUT, MCLRE=off, WDT=off
    #define Switch gpio.0
    Ser_Init
    OPTION_REG.7 = 0 'sets pullup on
    on interrupt Timer1Overflow call IncCounter
    set WPU.0 on
    dir gpio.1 out
    dir gpio.5 out
    Dir Switch In
    DataCount = 0
    dim lcdvaluetemp as word
    dim lcdvalue as word
    InitTimer1 Osc, PS1_8
    set gpio.5 on
    Dim TimerValue As Word
    dim full as word
    full = 0
    wait 2 s

    main:
    'XMIT_PRINT ("  Chronograph v1.0  ")
    'XMIT_PRINT (" Copyright RCM 2010 ")
    'wait 1 s
    'Xmit_rs232 (h'7C')
    'Xmit_rs232 (h'0A')
    Do
        ClearTimer 1
        Wait Until Switch = Off
        StartTimer 1
        Wait Until Switch = On
        StopTimer 1
       
        'Read the timer
         TimerValue = Timer1
    Xmit_rs232 (h'FE') 'ok clear screen
    Xmit_RS232(h'01')
    wait 10 ms

    full=full*65535+timervalue
    XMIT_PRINT ("  Chronograph v1.0  ")
    XMIT_PRINT (" Copyright RCM 2010 ")
         bin2ascii2(full_H)
         bin2ascii2(full)
    full=10000000/full
    XMIT_PRINT ("uS          ")
         bin2ascii2(full_H)
         bin2ascii2(full)
    XMIT_PRINT ("FPS")
        ' bin2ascii2(timervalue_H)
        ' bin2ascii2(timervalue)
    full = 0
        'Log the timer value
        'EPWrite(DataCount, TimerValue_H)
        'EPWrite(DataCount + 1, TimerValue)
        'DataCount = datacount + 2

    Loop
    goto main

    Sub IncCounter
    full=full+1
    end sub

    sub XMIT_PRINT (PrintData$)
    PrintLen = PrintData(0)
    if PrintLen = 0 then exit sub
    'Write Data
    for SysPrintTemp = 1 to PrintLen
    XMIT_RS232(PrintData(SysPrintTemp))
    next
    end sub

    Sub XMIT_RS232(Xmit_Byte)#NR
    SerTxLow
    wait baud us
    For cntr = 1 to 8
    Rotate Xmit_Byte Right
    If Status.C ON Then SerTxHigh
    If Status.C Off Then SerTxLow
    wait baud us
    Next
    SerTxHigh
    wait baud us
    end sub

    Sub Ser_Init
    ;slight adjustment was required for 9600bps delay value
    #define baud 104 ;expressed as frequency in us
    #define halfbaud 52 ;place Read of SerRx in middle of bit
    #define SerTxHigh Set GPIO.1 On
    #define SerTxLow Set GPIO.1 Off
    dir GPIO.1 out ;Tx
    SerTxHigh ;Initial RS232 idle state
    end sub

    sub Bin2ascii2 (LCDValue)#NR
    SERCEN = 0 
    SERDEC = 0 
    SERUN = 0 
    LCDValueTemp = 0

    'lcdvaluetemp = lcdvalue / 10000
    'SERtho = LCDValueTemp + 48
    'if sertho<>48 then
    'XMIT_RS232 Sertho
    'end if
    'LCDValue = lcdvalue - LCDValueTemp * 10000

    lcdvaluetemp = lcdvalue / 1000
    SERCEN = LCDValueTemp + 48
    'if sercen<>48 then
    XMIT_RS232 SerCen
    'end if
    LCDValue = lcdvalue - LCDValueTemp * 1000                

    lcdvaluetemp = lcdvalue / 100
    SERCEN = LCDValueTemp + 48
    'if sercen<>48 then
    XMIT_RS232 SerCen
    'end if
    LCDValue = LCDValue - LCDValueTemp * 100 
     
    LCDValueTemp = LCDValue / 10 
    SERDEC = LCDValueTemp + 48 
    XMIT_RS232 Serdec
    LCDValue = LCDValue - LCDValueTemp * 10

    SERUN = LCDValue + 48
    'if serun<>48 then
    'XMIT_RS232 46 '"."
    XMIT_RS232 Serun
    'end if
    end sub

    i got the overflow figured out but my uS time does not seem to be accurate? and i get a letter in my number sometimes, i think my bin2ascii if off or the adding of the varibles?

     
  • kent_twt4

    kent_twt4 - 2010-09-01

    Use the new Bin2ascii sub, due to change in compiler.  See here https://sourceforge.net/projects/gcbasic/forums/forum/579126/topic/3509908

    I don't beleive GCBasic is going to do the maths involved with statements like:

    full=10000000/full
    

    Using a 20 Mhz crystal will greatly increase the accuracy on the measurements, and minimize the interrupt routine overhead.  A math operation would be required to take the interrupt routine overhead into account on Timer1 rollovers.

    Seeing a letter in the output may indicate a baud rate problem.  Experiment with different baud definitions like 103..102 etc. till stable values are obtained.

     
  • Mauried

    Mauried - 2010-09-01

    The odd letter in serial output data is caused by using software usart whilst having interrupts enabled
    If an interrupt occurs whilst the software usart is sending data, then the bit times get altered, and the data is corrupted.
    Gcbasic can only do 16 bit math.
    There are some 32 bit math routines around, but they need a lot of memory to run in.
    No hope in a small 12F series chip.

     
  • Mauried

    Mauried - 2010-09-01

    GCbasic does have an equivalent routine to Bin2ascii although its not obvious.
    The string function str(numeric value) returns the ascii value of the numeric value as a string.
    So you can use
    Dim Temp as String
    Number=123
    Temp=str(number)
    Printdata Temp

     

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.