Menu

Synchronized delay using timer0

Help
2014-04-26
2014-04-27
  • Jacques Nilo

    Jacques Nilo - 2014-04-26

    Hi all
    I am trying to program synchronized delay using timer0 (timer1 being already used). I need usec precision. Something of the following structure:

    Startwatch0 ' Initialization
    some code here ' Execution time of this code can vary
    do while (stopwatch0 < 1400 ) loop ' 1400 usec after startwatch0 the program continues

    Any idea about the best way to proceed or any code example I could follow ?
    Thank for your help !
    Jacques

     
  • kent_twt4

    kent_twt4 - 2014-04-26

    I've never tried to get us precision with TMR0. This is a PIC device? If so, then I did try to get close by using On Interrupt Timer0Overflow, check for how many times, then wait for the remainder to pass in a Do While Loop. How the conditional statements are handled really depends on how much time the other code requires to run, if it is repetitive, and so forth.

    To get us precision then would have to run it in a simulator like MPLAB or something else to account for all the overhead.

    Untested pseudo code:

    'Assuming 4 Mhz Clock then 1400us / 256 = 5 overflows, with 120us remainder
    On Interrupt Timer0Overflow call CountOverflows '  
    ...
    ...
    Main:
    count = 0
    clrf TMR0    'initialize TMR0 register to zero
    ...          'do code while waiting for timeout
    ...
    Do Until count = 5    'waste whole number of overflows   
    Loop
    Do until TMR0.6 = 1   'waste remainder time till 120us is used up
    Loop
    Do until TMR0.5 = 1
    Loop
    'etc...
    ...    'do code after timeout
    ...
    goto Main    'start over again
    
    sub CountOverflows
        count += 1
        If count = 6 then count = 1
    end sub
    

    Edit: changed If statement count = 5, to Do Until count = 5 Loop

     

    Last edit: kent_twt4 2014-04-26
  • Anonymous

    Anonymous - 2014-04-26

    This is something I've been interested in, but haven't gotten around to yet. However I did notice that that free program I mentioned a while back has a utility in it for dealing with the overhead involved. It might prove useful.

    http://sourceforge.net/p/gcbasic/discussion/629990/thread/4f502852/#08a7

     
  • Jacques Nilo

    Jacques Nilo - 2014-04-26

    Hi tried kent_twt4 proposal. But apparently count does not increment itself. Here is my code. Any suggestion? Many thanks for your support !

    ;Chip Settings
    #chip 16F690, 8
    
    'Config UART
    #define USART_BLOCKING true
    #define USART_BAUD_RATE 19200
    ' Define serial port addresses
    #define SerInPort  PORTB.5    ' RX
    #define SerOutPort PORTB.7    ' TX
    'Set pin directions
    Dir SerInPort In
    Dir SerOutPort Out
    InitTimer1 osc,PS1_8
    
    On Interrupt Timer0Overflow Call IncCounter
    
    Sub IncCounter
        count += 1
        if count = 11 then count = 1 end if
    End Sub
    
    Function detect175Hz() as bit
    ' 140 sampling in 200ms
    Repeat 140
      count = 0
      clrf TMR0
    '  *** My code here ***
      Do Until count = 10 Loop    ' this should lead to 1400us after start ?
    end repeat
    end function
    
    ' Main program to test the lenght of detect175Hz 
    clearTimer 1
    StartTimer 1
    detect175Hz
    StopTimer 1
    HSerPrint Timer1  ' should give back 50000 for 200ms sampling
    hserprintCRLF
    
     

    Last edit: Jacques Nilo 2014-04-27
  • kent_twt4

    kent_twt4 - 2014-04-26

    Lots going on here, never hurts to simply code while checking basic functionality. Take a look at this line, it does not seem to make it thru to assembly code:

     Do Until count = 10 Loop
    
    Should be:
    
    Do Until count = 10
    Loop
    
     
  • Jacques Nilo

    Jacques Nilo - 2014-04-27

    I edited my previous post to keep it as short as possible... The line:
    Do Until count = 10 Loop
    did not generated any compiler error but I will try with what you sugest, do my homework, study MPLAB SIM and come back later on the forum with a solution or more questions :-)
    Many thanks for your help. I appreciate.

     
  • kent_twt4

    kent_twt4 - 2014-04-27

    Can't possibly post up errors for every incorrect syntax? Here is the assembly for "Do Until count = 10 Loop. Condition is never met.

    ;Do Until count = 10 Loop    ' this should lead to 1400us after start ?
    SysDoLoop_S1
    ;end repeat
        decfsz  SysRepeatTemp1,F
        goto    SysRepeatLoop1
    

    Here is assembly for correct Do/Loop syntax.

    ;Do Until count = 10
    SysDoLoop_S1
        movlw   10
        subwf   COUNT,W
        btfsc   STATUS, Z
        goto    SysDoLoop_E1
    ;Loop    ' this should lead to 1400us after start ?
        goto    SysDoLoop_S1
    SysDoLoop_E1
    ;end repeat
        decfsz  SysRepeatTemp1,F
        goto    SysRepeatLoop1
    
     

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.