Problem with pic Usart

Discussion in 'Embedded Systems and Microcontrollers' started by Christian86, Mar 4, 2010.

  1. Christian86

    Thread Starter New Member

    Mar 22, 2009
    I am doing a project where a temperature sensor signal is A/D converted by a pic, which then sends the result via the USART to a PC.
    The A/D converter on the pic is 10 bit and the result is configured as Left Justified.
    5V is equivalent to the digital value 1000. The USART is set to 8 bit asynchronous mode.
    After the conversion I send the contents of the register ADRESH to the USART.

    So my problem:
    Hyper terminal shows the character 'm' (decimal 109), while the measurement of the analog signal gives 2.168 V.

    The digital value of 2.168 V should be:
    (1000 / 5) * 2.168 = 433.6
    I know this is not possible to represent with a byte, but my question then is what does the pic do with the number? It sends 109, is this somehow a truncated value of 433?

    regards / Christian
  2. CVMichael

    AAC Fanatic!

    Aug 3, 2007
    Right now you are sending only the High byte of the ADC. What about the low byte (ADRESL) ?

    You want to send 10 bits, so this means it is divided in 2 bytes, ADRESL for the first 8 bits and ADRESH for the last 2 bits.

    Then on the computer you have to join the 8 + 2 bits so you get the 10 bits, and convert that into an Integer.

    Then do the conversion on the computer to display the voltage.

    So if you measured 2.168 V, then the integer should be: (5/1024)/2.168 = 444

    Doing the reverse (when you will get 444 from pic): 444 * (5/1024) = 2.16796875
  3. Markd77

    Senior Member

    Sep 7, 2009
    Looks perfect - 109 is about 433/4 so you are sending the most significant 8 bits of the A/D result.
    I use a freeware program called Realterm which works pretty well. More advanced than Hyperterminal.
  4. Markd77

    Senior Member

    Sep 7, 2009
    That is correct for a right justified ADC result. For left justified the formula is:

    ADC = (high byte * 4) + (low byte/64)
  5. Markd77

    Senior Member

    Sep 7, 2009
    True, I'd only use left justified if I didn't care about the 2 LSBs of the result and then I'd just ignore ADRESL.
    It would be better to use right justified if full precision is required.