PDA

View Full Version : Math operations & Syntax?



kevj
- 22nd February 2008, 08:09
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 ...




'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!

skimask
- 22nd February 2008, 13:49
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?

kevj
- 22nd February 2008, 17:56
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?

skimask
- 22nd February 2008, 18:03
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)

kevj
- 22nd February 2008, 18:11
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...




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

skimask
- 22nd February 2008, 18:40
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...



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

Bruce
- 22nd February 2008, 20:29
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

kevj
- 23rd February 2008, 01:40
Thanks guys! Much appreciated!