Writing to high/low variable pairs?


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

    Default Writing to high/low variable pairs?

    Thanks for the help so far everyone - I had a break through last night and got an LED to blink. Small victory but it's progress. lol.


    I have a Q about syntax of writing data to paired bytes for example TMR1H and TMR1L - high/low pair.

    Let's say I want to write a value of 48060. Right now I put that into my calc and convert it to binary - in this case 1011101110111100. I split that and write it into the variable pair like this:

    Code:
       TMR1H = %10111011   'Set timer to 48060
       TMR1L = %10111100
    This isn't easy though - time consuming, requires to enter and re-type a binary number, etc. I'd really like to write code that looks something like this:

    Code:
    TMR1H:L = 48060
    ... and have PBP figure it out for me.


    The major complication is, what happens when I want to perform logic on this number such as {perform some calculation which may equal 100 to 30,000 and subtract that result from the value I'm going to place into TMR1} - I'm not sure how to do that, split out the binary components and assign those components to the H/L pair.

    I wondered if I assigned the binary represnetation of "048" for the H, and "060" for the low byte - if that equals the same result.

    Guidance?


    And on a similar subject, what's the deal with HEX? I see a lot of code examples where people entere hex values - I understand it's the same thing, but is there some advantage to entering hex? Can I convert that 48060 value to hex them somehow assign it to the H/L pair in one shot and PBP will assign the right bits to the right bytes?

    I'd like to perform the above assignment of TMR1 value as quickly as possible - as it's written it seems like 2 instructions (and probably a few more in ASM) but it seems there should be a way to assign both H/L in one shot and cut the instruction cycles in half.


    Thanks everyone!! I would have thrown this stuff in the trash by now if it weren't for this forum.

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


    Did you find this post helpful? Yes | No

    Default

    Regards,

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

  3. #3
    Join Date
    Aug 2007
    Posts
    67


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce!


    .... I found the relevant part, but I don't understand it.

    I see these code snips...

    Code:
    @Timer1 = TMR1L
    Timer1  VAR  WORD EXT
    and "re-loading the timer is just as simple as..."

    Code:
    T1CON.0 = 0  ; stop timer
    Timer1 = Timer1 + TimerConst
    T1CON.0 = 1  ; turn timer back on
    and "another example"

    Code:
    @Capture = CCPR1L
    Capture   VAR  WORD EXT


    In the first example...

    @Timer1 = TMR1L
    Timer1 VAR WORD EXT

    "@" is a straight ASM line, apparently defining "Timer1" to equal TMR1L. Though this says nothing for what TMR1H should be called. The second line Timer1 VAR WORD obviously defines Timer1 as a variable for PBP - and the EXT has something to do with not doing something with it until it compiles - I didn't understand what that post was getting at.


    I'm also again seeing in the above post the inter-changing of various forms of hex numbering. I see "1234h" as well as "$1234" and again, I don't understand why you would use the hex in the first place as it's difficult to read - and again my question, is there some way to just write the value you want "12,385" in your code and have PBP turn it into binary or hex as required when compiling? The need to have to "hex" all your integers seems like a complicated additional step and I don't understand what it needs to be done.

    Thanks.
    Last edited by kevj; - 1st September 2007 at 06:13.

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


    Did you find this post helpful? Yes | No

    Default

    If you don't understand Darrels' EXT explanation, you might want to come back to it at some
    point, and re-read it. It can be confusing if you don't get it.

    However, what you're trying to do doesn't require you to work in binary. You can just create
    a word sized variable, perform whatever math you need to on it, and then load it into Timer1
    high & low bytes.

    TimerConst VAR WORD

    This gives you a 16-bit variable. Now you can do things like TimerConst = 9612. TimerConst
    = TimerConst * 5. Now TimerConst = 48060. PBP automatically stores the result for you as
    two 8-bit binary values.

    Everything is stored as binary since the PIC internal structure is 8-bit registers.

    To load TimerConst into Timer1 low & high bytes you use TMR1L = TimerConst.LowByte, and
    TMR1H = TimerConst.HighByte.

    You can't do something like TMR1H:L=48060 since you're dealing with two 8-bit file registers
    for TMR1L and TMR1H. And you're dealing with two 8-bit values for TimerConst.

    It takes two separate operations. Load the low byte of TimerConst into TMR1L. Then load
    the high byte of TimerConst into TMR1H.
    Regards,

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

  5. #5
    Join Date
    Jul 2007
    Posts
    53


    Did you find this post helpful? Yes | No

    Default TMR0 read/write

    I'd like to perform the above assignment of TMR1 value as quickly as possible - as it's written it seems like 2 instructions (and probably a few more in ASM) but it seems there should be a way to assign both H/L in one shot and cut the instruction cycles in half.
    No! If you read the datasheet carefully you will find;

    A write to the high byte of Timer0 must also take place
    through the TMR0H Buffer register. The Timer0 high
    byte is updated with the contents of TMR0H when a
    write occurs to TMR0L. This allows a user to write all
    16 bits to both the high and low bytes of Timer0 at once.
    Meaning you first write TMR0H, then TMR0L. Both register will updates at the same time you will write TMR0L.

    J-P

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. Changing declared variables names on the fly
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th December 2006, 06:34
  5. WORD vs BYTE variable lengths
    By bartman in forum General
    Replies: 0
    Last Post: - 28th November 2005, 21:16

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