PDA

View Full Version : Writing to high/low variable pairs?



kevj
- 31st August 2007, 18:59
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:



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:



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. :)

Bruce
- 31st August 2007, 19:12
Have a look here: http://www.picbasic.co.uk/forum/showthread.php?t=3891

kevj
- 1st September 2007, 06:00
Thanks Bruce!


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

I see these code snips...



@Timer1 = TMR1L
Timer1 VAR WORD EXT


and "re-loading the timer is just as simple as..."



T1CON.0 = 0 ; stop timer
Timer1 = Timer1 + TimerConst
T1CON.0 = 1 ; turn timer back on


and "another example"



@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.

Bruce
- 1st September 2007, 13:42
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.

GrandPa
- 1st September 2007, 18:00
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