Dumb question: How to "concatenate" two bytes

Discussion in 'Embedded Systems and Microcontrollers' started by blackmamba, Dec 4, 2009.

  1. blackmamba

    Thread Starter New Member

    Dec 4, 2009
    16
    0
    I'm sorry to disturb with these easy questions, but I cant quite figure it out.
    I am using the an ADC which outputs a 10bit result in two separate 8 bit locations(ADRESH, ADRESL). I need to combine both into one single 10bit word.
    Also I need to convert this binary value to decimal. What is an optimal way to do that?

    THanks for your time
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    If the two odd bits are the high ones, place them in a register and shift left 8 places. Add the lower 8 bits to make your composite value.

    Optimal? That is up to the programmer. You can write a hex -> decimal conversion algorithm or place a table of conversions into ROM. Just use the hex value to address the ROM and read out the conversion.
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    For the BCD conversion take a look at Microchip document AN526. Should tell you all you need to know.
     
  4. blackmamba

    Thread Starter New Member

    Dec 4, 2009
    16
    0
    Thanks for the help, but I still need a little more :)
    are lookup tables generally put in program memory instead of data? I am working with an 8bit MCU. would it be possible to make 1024 long lookup table with each entry being 10bits? Do I have access to the program memory? I am working with the 12f683.

    Thanks
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It's not going to be easy. You can store a lookup table in program memory using the RETLW instruction, but its only going to be 8 bit. There is also a problem that you will have to split the table to avoid page boundaries. RETLW only takes an 8 bit input.
     
  6. blackmamba

    Thread Starter New Member

    Dec 4, 2009
    16
    0
    Thank you sir. I appreciate it. I'm a newbie hence all the questions.
    The spec sheet says program memory is 14bits wide though. why is it that it'll only take 8bit values.
    Also for programming in C, can you give some leads on getting the look-up table started.
    Also, do you know whre I can learn to do this myself online?

    Thank you much.
     
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Unfortunately I don't understand C, it looks like funny squiggles to me. Hopefully someone can point you in the right direction.
     
  8. blackmamba

    Thread Starter New Member

    Dec 4, 2009
    16
    0
    I have got some code to implement the look-up table in C, but I'm still having some issues.
    The code should create a 256 byte array, populate it with delay times in us and then stick it in program memory for me to refrence based on ADC byte outputs. Each ADC byte output would correspond to a delay time in the lookup table.
    Can someone tell me what I'm doing wrong as the code wont compile.

    Thanks


    const int delay_table[256]; //lookup table
    int i;
    for (i=0; i<256; i++);
    delay_table=((1/120000000)-(1/120000000*i/255)); //lookup table values
    ///////////////////////////////////////////////////////////////////////////////
    void main (void)
    {
    ADCON0=0x02; // start A/D conversion
    do{} // crude polling of GO/DONE bit to see when it gets
    while (ADIF!=1); //cleared which indicates ADC is complete
    ADIF=0;
    int dig_controlvol;
    //long dig_controlvol_l;
    dig_controlvol=ADRESH;
    //dig_controlvol<<=8;

    //dig_controlvol_l=ADRESL;
    //dig_controlvol+=dig_controlvol_l;

    dim_delay=delay_table[dig_controlvol]; //ADC result corresponds to lookup table time in us


    //dim_delay =
    }
     
Loading...