Replace PBP with Assembly concerning Timer0


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102

    Default Replace PBP with Assembly concerning Timer0

    Hello,

    I was talking to some representatives of microchip personally and they recommended using the timer0, 1 or 2 to count instead of using a loop like

    Code:
    while x= y
      N = N +1
    WEND
    due to the timer's resolution.

    Using the above example how does the assembly replacement with timer0 or timer2 look like ?

    Is this statement of MC true ?

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by selbstdual View Post
    Hello,

    I was talking to some representatives of microchip personally and they recommended using the timer0, 1 or 2 to count instead of using a loop like

    Code:
    while x= y
      N = N +1
    WEND
    due to the timer's resolution.

    Using the above example how does the assembly replacement with timer0 or timer2 look like ?

    Is this statement of MC true ?
    Hey, remember me? I'm still sitting on the table (upside down of course) eating off a folded up chair...
    At the risk of sounding like a jerk, I'm going to 'teach you how to fish' rather than serve you a seafood dinner...so here goes...
    Select a PIC, any PIC will do, preferably one of the more popular ones these days, read the chapter on a Timer, any timer will do. Let me know when you've got that done.
    On another note, I had friday off and I'm in Tucson, Az. for a couple of months, so I took a trip up to Microchip's office's in Chandler yesterday. Who did you talk to? I talked to a bunch of people.

  3. #3
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default What is your application ?

    Hi,

    It depends on your application. Using any of the timers you can do a counting at the background then depending on the elapsed period you can do other jobs. Normally all the timer registers are accessible through PBP itself. If you need a tight timebase then asm gives you a better control on the actual instruction cycle used. Timer0 may not have a start/stop control. It is clocked from a prescaler or Fosc (external also possible). When timer0 rollsover it sets a hardware interrupt flag TMR0IF. Even if you are not using interrupts you can poll this flag.

    Mister_E has done a great utility to make all your timer/pwm/usart calculations a breeze. Find it here http://www.picbasic.co.uk/forum/atta...4&d=1162909841

    For your example code something like this can be done in PBP:
    Code:
    ' THIS IS MEANT FOR A 18f452 , 16F PICS HAVE DIFFERENT TIMER0 SETTINGS
            X   VAR BYTE       ' SOME VARIABLE
            Y   VAR BYTE       ' SOME VARIABLE
    
    TIMER_ZERO  VAR WORD       ' VARIABLE TO STORE TIMER0
    
    T0CON = %00000111  ' OFF,16BIT,PRESCALE=256
    
    INIT:
    TMR0H = 0          ' CLEAR THE HIGH BYTE OF TIMER ZERO (UPDATED WHEN TMR0L IS WRITTEN)
    TMR0L = 0          ' CLEAR THE LOW BYTE OF TIMER ZERO
    
    T0CON.0 = 1        ' START TIMER0
    MAIN_LOOP:
    
    ' DO WHATEVER YOU WANT
    
    
    
    IF X != Y THEN MEASURE   ' IF X AND Y DIFFERS THEN GET THE READING
    
    GOTO MAIN_LOOP
    
    MEASURE:
    T0CON.0 = 0                       ' STOP TIMER0    
    TIMER_ZERO.BYTE0 = TMR0L  ' READ LOW BYTE FIRST (TMR0H IS UPDATED)
    TIMER_ZERO.BYTE1 = TMR0H  ' READ HIGH BYTE FIRST 
    
    GOTO INIT                ' DO IT OVER AGAIN
    Please note that since you are checking the condition at the end of the loop, anything consuming time inside would give you erronious results. As for the resolution part it is best to keep the loop running and letting the timer count. The lower the prescale value the greater the resolution. But it seems that you would be doing other tasks as well if x and y are not SFRs or PORT registers. Cause your program need to do something with x and y otherwise they will never end the initially matched condition.

    It is best that you post your requirements clearly so that friends here can comeup with something useful for you.
    Regards

    Sougata

  4. #4
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default

    Dear sougata,

    the current code looks like this:

    Code:
        WHILE CMCON.6 = 0
            Value = Value + 1
        WEND
    This is all, there is no interruption. I am using the PIC 16F628A as a minion.

    I changed the code to fit my minion, but it can count only to 255 as it is an 8-Bit timer here.

    How do I deal with an overflow, means INTCON.T0IF = 1 or INTCON.2 = 1. It should be something like ValueNew = ValueOld + 256.

    Current code, not tested

    Code:
    TIMER_ZERO  VAR WORD        ' VARIABLE TO STORE TIMER0
    
    OPTION_REG = %00000111      
    
    INIT:
    TMR0 = 0                     'Full register contains t0-value
    
    INTCON.2 = 0                ' START TIMER0/Overflow-Bit = 0
    MAIN_LOOP:
    
        Value = Value + 1
    
    
    
    IF CMCON.6 != 0 THEN M   ' IF Komp.output DIFFERS THEN GET THE READING
    
    GOTO MAIN_LOOP
    
    M:
    INTCON.2 = 1                      ' STOP TIMER0/Simulate overflow  
    TIMER_ZERO = TMR0            'As it is 8-Bit
    
    GOTO INIT                ' DO IT OVER AGAIN
    Finally Value should contain TIMER_ZERO's value.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Wink

    Hi selbstdual,

    what do you need to count exactly? Events, internal clock tick, amount of hair lost in the process?

    Well.. what's your current project?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Unhappy

    Hair ? Is this a joke ? I kept on laughing the whole day. Very funny.

    It is about counting the time until CMCON.6 gets a high level.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Unhappy Rtfds ???

    Just connect pin 2 (C1OUT) to pin 9 or 12 ... and read TMR1H and TMRIL just when needed ...

    Details are in that ****ING DATA SHEET... fig 7.1

    Alain

    PS: No hair lost ... was a pleasure !!!
    Last edited by Acetronics2; - 18th February 2007 at 19:31.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    OR, stop the timer on a comparator interrupt...

    In meantime, is there a specific time range?

    I'll be back...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default Let's progress

    There is no TMR1H and L as TMR0 is 8 Bit. I cannot use TMR1 as I am using the compare-module. Additionally, Pin2 and Pin12 are used.

    I would need to check for the timer to overflow in the loop instead of reading its value. As basic is not machinecode I don't know how many tics I am missing by that method, you know ...

    MisterE: Interrupts are disabled(INTCON = 0), I am just looking at the 'time' it takes for the comparator's output to change.

    What do you mean by time range ?

    Before we go too much into detail here: Using my method compared to the built in timer, how many more ticks does the built in timer notice ?
    Last edited by selbstdual; - 18th February 2007 at 19:16. Reason: Clarify

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by selbstdual View Post
    What do you mean by time range ?
    Min and Max expected delay between the time you Start the Timer and 'till the comparator's output will change.

    Before we go too much into detail here: Using my method compared to the built in timer, how many more ticks does the built in timer notice ?
    Hard to say, but it can be measured with a scope + extra I/O OR by using something like Darrel suggested bellow...
    http://www.picbasic.co.uk/forum/showthread.php?t=365

    i'll do something simple here to help you to start. i think you're not as this far anyway.

    stay tunned
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default

    Currently I get values between 1800 and 400 for 'Value'. Whatever, don't hurry to answer this question because my current implementation works already. This is just for finetuning...
    Last edited by selbstdual; - 18th February 2007 at 21:34.

  12. #12
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Why don't you use interrupts?

    Hi,

    I would prefer using interrupts.

    Your TIMER_ZERO is already a word variable. So on a timer zero interrupt you can just increment the HighByte of the variable and normally dump the TMR0 to the lowbyte. This would make a pseudo 16bit timer. On a comparator interrupt, get the readings. It should not be very difficult in ASM . By the way what is your clock frequency and your timer0 prescaler value ? If you are using a high prescale then Darrel's Instant Interrupt may work.
    Regards

    Sougata

  13. #13
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default

    Difference of time(MyMethod <-> Timer0) is the linchpin of this thread. I can't see a connection between your post and the main question.

    This should not be measured. To calculate it, the timer0's speed is needed(How many cycles mean how many countings) and I want to know how fast my current implementation is(there is a number somewhere in the greatly written manual that states how much time these instructions take).

    Then I get a comparison.
    Last edited by selbstdual; - 21st February 2007 at 03:20.

  14. #14
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by selbstdual View Post
    Difference of time(MyMethod <-> Timer0) is the linchpin of this thread. .
    As far as I understood you need to get the time elapsed between two states of your comparator. You can always get two intermediate readings. Get the difference between the readings. Even if prescale is 1:1 and you are using ASM interrupts then you know how may cycles you consumed to take a reading and asjust your results thus. This should give you far more accurate results than looping around. Cause you can just stop the timer at the comparator interrupt. Get the results. I suggested a pseudo 16bit because I don't know your possible timings between comparator changes. Thus timer zero may overflow. You did ask how to handle overflow!! With the additional Highbyte incrementing on overflow you do get a 16bit timer.
    Regards

    Sougata

Similar Threads

  1. Assembly Language inside PBP
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th October 2009, 21:52
  2. Replies: 2
    Last Post: - 8th February 2009, 05:10
  3. 4 Chanel Dmx512 ready assembly code to PBP ?
    By syscoder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 21st March 2007, 23:55
  4. Passing arrays PBP <-> Assembly
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th April 2006, 01:01
  5. Assembly Language + PBP
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th January 2006, 13:54

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts