PDA

View Full Version : Can someone please explain the operator */



triton99
- 29th November 2011, 13:46
Could someone explain the */ operator to me. Im using the MAX6675 for a determining the temperature. The temperature is stored as a 12bit value. The example found here (http://www.emesystems.com/OL2therm.htm#MAX667x_program) uses:



shiftin thdata,thclk,0,[result\16] ' get data from MAX6675

TempC=result>>3*/640 ' for MAX6675 +/- 0.1 degC, up to 1023.7


TempC is a WORD variable.

Then when the program displays the temperature it first divides by 10. ie.


debug dec THC/10,".",dec1 TempC


What effect does the */ have on the result variable? How does "Middle 16bit Multiplication" work?

Thanks

HenrikOlsson
- 29th November 2011, 14:16
Hi,
It's like multiplying by 1/256 units. In other words the result = x */ 640 is the same as saying result = x * 640 / 256.

What happens internally is a 16*16bit multiplication which results in a 32bit result but the the middle 16bits are returned as the result and the 8 lowest significant bits are discarded.

Let's try this:
result = 100 */ 640
Internally this results in a value of 64000, which, when looked at as 32bits, are 00000000 00000000 11111010 00000000
Then the middle 16bits are returned and assigned to the variable result, ie 00000000 11111010 which in decimal is 250.
100*640/256=250

Put yet another way, let's say you need to multiply a value by 3.64 (100*3.64=364). The value to use with the */ operator is first calculated as 3.64*256=932 (rounded).
result = 100*/932
Internally you'll get 100*932=93200 which is then "divided" by 256 (simply by returning the middle 16 bits of the 32bit intermediate result) before it's returned to you. 93200/256=364 which is what we'd expect.

/Henrik.