Math operations & Syntax?


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2007
    Posts
    67

    Default Math operations & Syntax?

    I'm a bit confused on the actual use and syntax when using WORD sized variables.

    I'm going to measure a timed event, then use that time as a basis for a pass/fail trial of another timed event. I need to measure the time (16-bit), then manipulate it (subtract a few microseconds), then use that manipulated result to continually pre-load the timer and see if the event happens again within the similar allotted time frame.


    Here is what I plan to do and how I will syntax it - wanted to see if I'm doing this correctly?

    Using TMR1 ...

    Code:
    'Variables
    '-------------
    tMeasure VAR WORD   'Holder for the time
    tResult VAR WORD      'Holder for modified time
    tSet VAR WORD	    'Holder for timer set value
    
    
    'Measure event, manipulate Data
    '-------------
    T1CON.0=0           'Stop timer
       TMR1H = %00000000   'Reset the timer
       TMR1L = %00000000    '......
       PIR1.0=0            'Clear timer overflow bit
       T1CON.0=1           'Start timer
    
            ''' Wait for event to measure
    
       T1CON.0=0     'Stop Timer
    
    tMeasure = TMR1   		'Should put timer stop value into tMeasure
    tResult = tMeasure - 100 	'Subtract 100 cycles
    tSet = 65025 - tResult          'Setpoint for timer
    
            '' If I pre-load the timer with this tSet number, then wait
            '' for it to time out, it should run for a time period in cycles
            '' equal to tResult - right?
    
    
    'Perform trial timeout
    '--------------------
       T1CON.0=0           'Stop timer
       TMR1 = tSet
       PIR1.0=0            'Clear timer overflow bit
       T1CON.0=1           'Start timer
       WHILE PIR1.0=0 : WEND
       TOGGLE probe     'Should happen about tResult cycles after 
                              'starting the timer, yes?
    
    '

    My real questions are with the manipulation of the 16-bit variables. When I write "TMR1 = tSet", the TMR1H and TMR1L registers are filled correctly with the high byte and low byte of tSet - correct?

    Normally I would set TMR1 by manually setting the TMR1H and TMR1L with binary code like "TMR1H = %11101100".

    Also, do I need to worry about which direction the data is justified? I know it's important, but if I do it as written, is PBP smart enough to get the justification correct for me?


    Thank you!
    Last edited by kevj; - 22nd February 2008 at 08:13.

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    If I'm reading your question correctly...
    %00010001
    is the same as
    %10001

    %00000001
    is the same as
    %1
    is the same as
    1
    is the same as
    $1

    Is this what you're thinking?

  3. #3
    Join Date
    Aug 2007
    Posts
    67


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Is this what you're thinking?
    No, sorry. Not what I was asking. lol.

    I'm asking if I create a 16 bit variable, and populate it by saying "myVar = TRM1", will it automatically plug the TMR1H and TMR1L values together and stick them into myVar?

    And if I add 100 to that value, then populate the timer registers again by plugging this variable back in like this "TMR1 = myVar", will the binary values of the 2 8-bit registers get split correctly and put back into TMR1H and TMR1L as appropriate?

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by kevj View Post
    I'm asking if I create a 16 bit variable, and populate it by saying "myVar = TRM1", will it automatically plug the TMR1H and TMR1L values together and stick them into myVar?
    And if I add 100 to that value, then populate the timer registers again by plugging this variable back in like this "TMR1 = myVar", will the binary values of the 2 8-bit registers get split correctly and put back into TMR1H and TMR1L as appropriate?
    Ok, I got you...
    It may depend on the PIC, not sure, the datasheets will tell.
    But...
    I'm looking at the 18F4620 datasheet, there's a section in there under Timer 1 that talks about 8 bit vs 16 bit Timer 1 writes.
    Basically, you can't go TMR1 = var, you do actually have to use 2 8bit writes. You can't directly access TMR1H, it gets buffered in the 16 bit R/W mode. (section 12.2 of the 18F4620 datasheet)

  5. #5
    Join Date
    Aug 2007
    Posts
    67


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Ok, I got you...
    It may depend on the PIC, not sure, the datasheets will tell.
    But...
    I'm looking at the 18F4620 datasheet, there's a section in there under Timer 1 that talks about 8 bit vs 16 bit Timer 1 writes.
    Basically, you can't go TMR1 = var, you do actually have to use 2 8bit writes. You can't directly access TMR1H, it gets buffered in the 16 bit R/W mode. (section 12.2 of the 18F4620 datasheet)
    Okay perfect - that is exactly what I'm asking, and now the follow up...

    What is the exact syntax to split off the high and low bits? That really is my confusion at this point. I want to do something like this, but I'm not sure the syntax...

    Code:
    myHolder VAR WORD
    
    ''' stop timer, which is now holding the result I want to measure
    
    myHolder (the low byte) = TMR1L
    myHolder (the high byte) = TMR1H
    
    myHolder = myHolder - 200
    
    ''' now I'll goto another sub where I'm using the timeout as a trial
    
    TMR1L = (the low byte of myHolder)
    TMR1H = (the high byte of myHolder)
    
    '

    So I guess I'm just asking for the syntax of how to reference a high bit or a low bit in or out of a 16-bit variable, and also to clairify that once I have the value in "myHolder", I can do all the simple add/subtract math I want to it just like any normal variable as long as the values remain between 0 and 64,000andwhatever.

    Right? :-)

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by kevj View Post
    What is the exact syntax to split off the high and low bits? That really is my confusion at this point. I want to do something like this, but I'm not sure the syntax...

    Code:
    myHolder VAR WORD
    ''' stop timer, which is now holding the result I want to measure
    myHolder (the low byte) = TMR1L
    myHolder (the high byte) = TMR1H
    myHolder = myHolder - 200
    ''' now I'll goto another sub where I'm using the timeout as a trial
    TMR1L = (the low byte of myHolder)
    TMR1H = (the high byte of myHolder)
    So I guess I'm just asking for the syntax of how to reference a high bit or a low bit in or out of a 16-bit variable, and also to clairify that once I have the value in "myHolder", I can do all the simple add/subtract math I want to it just like any normal variable as long as the values remain between 0 and 64,000andwhatever.
    Right? :-)
    It's all in the manual under the "Variables" section, highbyte, lowbyte, bitxxx, aliasing, whatever...

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    These two threads should answer your question & then some. The 2nd using the EXT
    modifier is really nifty.

    http://www.picbasic.co.uk/forum/showthread.php?t=544

    http://www.picbasic.co.uk/forum/showthread.php?t=3891
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    Join Date
    Aug 2007
    Posts
    67


    Did you find this post helpful? Yes | No

    Default

    Thanks guys! Much appreciated!

Similar Threads

  1. Resolution integer math assistance?
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th January 2010, 03:01
  2. PBPL Math...new math takes more cycles...Always?
    By skimask in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2008, 10:22
  3. instruction cycles for math operations
    By Michael Wakileh in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th July 2007, 11:03
  4. Math Operations on pbp
    By Jannia04 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th May 2007, 15:33
  5. PIC18Fxx math 24 & 32 bit
    By ronsimpson in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 2nd December 2006, 12:52

Members who have read this thread : 0

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