Conversion of 10-bit word into ASCII


Closed Thread
Results 1 to 3 of 3
  1. #1
    Barry Johnson's Avatar
    Barry Johnson Guest

    Default Conversion of 10-bit word into ASCII

    Hi,

    I am having a bit of difficulty understanding the results of the program below. The WORD adval is intended to be the 10-bit output of the ADC (1st line after loop). I then want to display it on HyperTerm as a digital word (next line). It is my understanding that placing # before adval, i.e., #adval, automatically converts adval into digital ASCII characters in the SEROUT command. For example, if adval = 827, SEROUT with #adval would (should) send "8","2","7".

    Consequently, I expected to observe values on the screen ranging from 0 to 1023 since adval is a 10-bit word, but instead I see values ranging from 0 to over 12300.

    I believe it is correct to right justify the upper 2 bits of adval by setting bit 7 of ADCON1 to 1.

    Any ideas what I am doing wrong please?

    Thanks,
    Barry

    ' PIC16F819
    ' ---- ---------
    ' RB1 Max232(RX)
    ' RB2 Max232(TX)

    ' -----[ Includes/Defines ]---------------------------------------------------------
    '
    include "modedefs.bas" 'include serout defines
    ' Define ADCIN parameters
    Define ADC_BITS 10 ' Set number of bits in result
    Define ADC_CLOCK 0 ' Set clock source to high speed
    Define ADC_SAMPLEUS 50 ' Set sampling time in uS
    define OSC 16

    ' -----[ Variables ]-------------------------------------------------------
    '
    RX var byte ' Receive byte
    adval var word ' vakue read of reverse power
    adcnt var byte 'counter of number of samples taken
    period var word ' sets the time between samples
    ' -----[ Initialization ]--------------------------------------------------
    '
    Init:
    TRISB = %00000010 'All port b output except pin 1 (RX) is input
    PORTB = %00000000 'Initialize PortB to all zeros and LED to off
    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %100000010 ' Set PORTA analog; MSB = 1 --> right justify adval
    period = 100 ' initial period = 100 ms

    ' -----[ Main Code ]-------------------------------------------------------
    '
    main:
    ' ****** [Menu setup on PC screen] *************************
    serout 2, T2400, ["Press 1 to Start", 10, 13] 'Display action on PC screen

    serin 1, T2400, RX 'Receive action request
    RX = RX - $30 'Convert ASCII number to decimal
    If RX <> 1 then Error 'Test for good value
    adcnt = 0
    serout 2, T2400, ["Sample period is ",#period," ms", 10, 13]
    serout 2, T2400, ["Press 1 to Accept or 2 to Change", 10, 13]
    serin 1, T2400, RX 'Receive period question response
    RX = RX - $30 'Convert ASCII number to decimal
    If RX > 2 then Error 'Test for good value
    if rx = 1 then goto loop
    serout 2, T2400, ["Enter new sample period in tenths of a second (2 = 200 ms)", 10, 13]
    serin 1, T2400, RX 'Receive new period value
    RX = RX - $30 'Convert ASCII number to decimal
    period = RX*100 ' new period in milliseconds
    If RX > 9 then Error 'Test for good value
    loop:
    ADCIN 1, adval ' Read channel 1 to adval
    serout 2, T2400, [#adval, 10, 13] 'Display adval on PC screen
    adcnt = adcnt +1
    if adcnt = 100 then end

    Pause period 'wait "period" milliseconds
    goto loop

    Error:
    serout 2, T2400, ["error", 10, 13, "Try again", 10, 13]
    goto main

    end

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    The ADCON1 register isn't 9 bits wide...

    ADCON1 = %100000010

    which probably means you're not right justifying.

  3. #3
    Barry Johnson's Avatar
    Barry Johnson Guest


    Did you find this post helpful? Yes | No

    Default

    Thank you, I bet that is the problem. Will try it out tomorrow.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 07:25
  3. DS2760 Thermocouple Kit from Parallax in PicBasicPro
    By seanharmon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th July 2008, 23:19
  4. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 15:23
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts