+ Reply to Thread
Results 1 to 4 of 4

Thread: Data Convertion

  1. #1
    Spideys Newbie
    Join Date
    Nov 2009
    Location
    Johannesburg, South-Africa
    Posts
    4

    Data Convertion

    Hi there ppl.

    working with a P18F4331 and doing A/D convertion. I want to display the result on PORTB but as you know P18f4331 is a 10bit A/D converter and PortB only has 8Bits

    If A/D is justified right

    char x,y;
    int z;

    x = ADRESL; //low byte
    y = ADRESH; // high byte

    and i have the following "nested if " ( deviding 10bit value into the 8bits of portB)

    if(z <= 128)
    PORTB = 0x01;
    else if(z <= 256)
    PORTB = 0X03;
    else if(z <= 384)
    PORTB = 0x07;
    else if(z <= 512)
    PORTB = 0x0f;
    else if(z <= 640)
    PORTB = 0x1f;
    else if(z <= 768)
    PORTB = 0x3f;
    else if(z <= 896)
    PORTB = 0x7f;
    else if(z < 896)
    PORTB = 0xff;

    how do i define z where z = Highbyte,lowbyte combind. Basically how do i make a 10 bit value form 2 8bit values x & y


  2. #2
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,696

    Justify left and just copy ADRESH to PORTB.

    Edit, if you still want to combine into an int then do Result=ADRESH*256+ADRESL;.

    Mike.
    Last edited by Pommie; 1st December 2009 at 02:20 PM.

  3. #3
    Spideys Newbie
    Join Date
    Nov 2009
    Location
    Johannesburg, South-Africa
    Posts
    4

    Thanks Again for responding Mike...

    That was what i did but saw strange results (just testing with a POT conected to AN0) as i turn the POT light connected to Portb do light up as resistance decrease, but sometimes lights turn off. i thought that there might be an C-instruction, that could combine the the two byte values into one int or long int.

    z = x + (256 * y); // this was my initial idea.

    otherwise it is just the POT that is screwy

    X + Y = Z
    11110000 + 00000011 = 1111110000

  4. #4
    birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent
    Join Date
    Feb 2009
    Location
    Montreal, Quebec
    Posts
    1,061

    Quote Originally Posted by Spideys View Post
    Thanks Again for responding Mike...

    That was what i did but saw strange results (just testing with a POT conected to AN0) as i turn the POT light connected to Portb do light up as resistance decrease, but sometimes lights turn off. i thought that there might be an C-instruction, that could combine the the two byte values into one int or long int.

    z = x + (256 * y); // this was my initial idea.

    otherwise it is just the POT that is screwy

    X + Y = Z
    11110000 + 00000011 = 1111110000
    If its sometimes showing blank perhaps you are polling the ADC before it is ready again (the internal capacitor is discharged).
    Mike
    My website: www.ElectroBird.net

+ Reply to Thread

Similar Threads

  1. signal convertion
    By chandu13 in forum Micro Controllers
    Replies: 3
    Latest: 14th April 2007, 11:19 AM
  2. Analog to Digital Convertion
    By demestav in forum Micro Controllers
    Replies: 11
    Latest: 12th April 2006, 02:22 PM
  3. USB to serial convertion
    By BBB in forum Electronic Projects Design/Ideas/Reviews
    Replies: 1
    Latest: 22nd September 2005, 10:05 AM
  4. video convertion
    By waterrat in forum Electronic Projects Design/Ideas/Reviews
    Replies: 1
    Latest: 3rd April 2005, 11:35 AM
  5. Microphone convertion help
    By bd13 in forum Electronic Projects Design/Ideas/Reviews
    Replies: 2
    Latest: 25th July 2004, 03:46 AM

Tags for this Thread