16f690 500C temperature measure

Discussion in 'Programmer's Corner' started by Dodgydave, Jul 3, 2013.

  1. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,984
    745
    ok so i have a thermocouple to give out 500c with an amp it gives out 10mV/C so 500C is 5Volts. I can alter the gain to give 1mV per degC so 500C is 500mV. I will be using Mpasm .

    So i input the voltage into the pic and convert it using the AD, to a value in the upper and lower registers. Should i set the Vref to 1024mV and this gives me 1 degree per bit, so 500C is 500 bits, that would be 256bits in upper reg and 244 in lower reg, how do i add these bits together to give a total so i can a divide by 100,10,1 to put them in 3 files huns,tens units,the division is not a problem its how to add the two files together as the maximum in the registers is 255? Also is there abetter way to do this with the V ref?
     
    Last edited: Jul 3, 2013
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    First I do not think a Vref=1.024 volt is within the Vref spec for your PIC. You should try 2.048 or 4.096 volt as reference. But see the datasheet for the Vref limit. Second doing this in C is much more simple. Just as you know it ;)
     
  3. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,984
    745
    I dont understand C i use mpasm
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    To avoid confusion, refer to a value of 500 as 500 counts instead of 500 bits.
    A value or count of 255 will require 8 bits.
    A value or count of 500 will require 9 bits. Hence in order to store any value that is greater than 255 and less than or equal to 65535 you will need two 8-bit registers to represent the 16-bit integer.
    For a value of 500, the upper order register will contain 1 and the lower register will contain 244.

    There are ASM code examples readily available for converting 8-bit and 16-bit values to BCD (binary coded decimal). There are two basic programming techniques to do this.

    One algorithm is to divide by 10 repeatedly until the dividend is zero, each time saving the remainder.

    The second algorithm is to do repeated subtraction by 100 until the remainder is less than 100. This will require subtraction from double byte (16-bit) values. This is then followed by repeated subtraction by 10 in order to find the tens value. The remainder is the units value.

    If your values are limited to the range of 0 to 511, there is another simplistic algorithm. Start with the three BCD registers cleared to zero.

    D2 = 0
    D1 = 0
    D0 = 0

    If the upper ADC register is 1, preset the BCD registers to 256.

    D2 = 2
    D1 = 5
    D0 = 6

    Using the lower ADC register count, decrement this number of counts by one until the value is zero, each time incrementing the BCD registers D2, D1, D0. That is, add 1 to D0 and carry over 1 to D1 each time D0 increments from 9 to 10, at which point you reset D0 to zero. Similarly, carry over the overflows of D1 into D2.

    Programmatically this is easy to envision and code on a PIC.
     
    Last edited: Jul 3, 2013
    Dodgydave likes this.
  5. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,984
    745
    So i thought of that say its 300C,the upper bit is 1 then its 256C , then i put 2,5, 6 into three files labeled Huns, Tens, Units, and now i need to do a divide by 100,10,1 on the lower reg to get the extra temperature 44 bits (256+44=300C) to put the results into

    another three files called huns-temp, tens-temp, units-temp,( with zero in huns-temp, 4 in tens-temp, and 4 in units-temp,) this is where i get confused as how to add both units , tens and huns together and make sure they rollover and increment the higher files, just like adding up normally? Or do you have an easier way?


    PS if i have 4 in units-temp and add it to 6units, then i should get zero in units, and the DC will be set in the Status reg indicating a rollover, so i can then increment the tens file by 1 ,and then add the tens and tens temp together the same way?
     
    Last edited: Jul 3, 2013
  6. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,984
    745
    Vref minimum is 0.6V max is 5,1V
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Converting the low order eight bits to BCD and then adding to 256 will also work.
    I am not an experienced PIC ASM programmer but I believe looking at the DC bit of the STATUS register will not work. Instead, add the two digits and test if the result is greater than 9. If true, set the result to zero, add one to the next digit (tens) and add the other tens digit and test for overflow, etc.

    I believe now is a good time for you to learn C.
     
  8. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    I used to say that, till about a month ago. MPLABX + XC8 are excellent for learning. The code formatter was essential for me as it took wonky looking code examples and beautified them.
     
  9. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,984
    745
    Not interested in C. Thanks
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Dodgydave likes this.
  11. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Well if you must stick with assembler at least use the 18F series if possible.
     
    Dodgydave likes this.
  12. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
  13. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,984
    745
    Sorted it out chaps, thanks now my oven has a 3 digit led temp reading!
     
Loading...