# basic binary math issues

#### harry99932

Joined Dec 30, 2010
38
Hi guys, wonder if you can help me,
Im currently stuck into my first proper ish pic project (7 seg thermometer) and while ive learnt a lot along the way im struggling with converting adc output to temperature the reason being ive not had to deal with dividing in binary before. ive read a few basic guides but most of it launches into multiplying 16 bit numbers by 8 bits etc. The only other way i can see of doing it is to subtract the constant and inc file for every subtraction without a carry bit. However this potentially as i can see it could leave me 1degree out if i divide the voltage value buy my sensors 6.4mv/deg and i end up with a remainder of 6.3 mv etc

Can someone explain a way around this or pointme in the direction of a good beginners guide? Ive found the little niche of adding a negative as opposed to subtracting (after much head scratching!  ) if that helps.

Harry

#### Clay

Joined Feb 12, 2010
21
If you are running Microsoft Windows, call up 'calculator', select
'scientific', then select 'binary', and away you go.

Best regards,

/Clay

#### Georacer

Joined Nov 25, 2009
5,182
I don't think that counts as a serious answer.

#### Clay

Joined Feb 12, 2010
21
Sorry, I meant it to be an serious answer for:

'Can someone explain a way around this'

I can dig out a rigorous text-book solution for binary division if

required.

Best regards,

/Clay

#### Georacer

Joined Nov 25, 2009
5,182
The OP needs a binary division algorithm in order to program a PIC microcontroller to measure temperature.
Clearly MS calculator isn't an option.

#### ian_gregg

Joined Jan 19, 2011
21
Basic concept with micros is to only use integers as that is what they are good at. I am not much use with the really low level as I do all my micro work in C, currently CVAVR but I have used HiTech with PICs. If you want to display decimals just multiply all your numbers internally by 10 or 100, being aware of overflow issues and you can display 2100 for 21.00 degC with a decimal point stuck on the display in the appropriate position.

For the scaling, if you have 12 bits, 0-4095 stored in a 16 bit integer then you can multiply by up to 16 without overflow. You need to find a multiplier, divisor and offset to get from your ADC number to your temperature number.
Output = ADC * multiplier / divisor - offset.
The division will be integer division with no remainder so there is a rounding error there but if you are going for an output number representing degC * 100 then the rounding error is only of the order of 0.01 degC
You get the numbers from theory and/or experiment. You would need the ADC reference, sensor sensitivity and zero readings. A great trick for finding the multiplier and divisor is to measure/calculate the ratio that you want and put it in Excel and format the cell as a fraction with 2 or 3 digits which gives you the nearest ratio of whole numbers to your ratio.

Last edited:

#### JDT

Joined Feb 12, 2009
657
If you only want a readout over a limited temperature range it might be easier to use a look-up table in progam memory. In the mid-range PIC's the program memory is 14 bits wide. So each "word" can store 3 BCD digits with 2 spare bits. The 2 spare bits can be used for negative indication and for the "one hundred" digit. Therefore easy to produce a 3 1/2 digit signed display.

I use an excel spreadsheet to do the calculation and produce the MPLAB code that can then be pasted in.

If you are interested I can attach some examples.