Lookup table help

Discussion in 'Embedded Systems and Microcontrollers' started by Yoopercamp23, Feb 15, 2014.

  1. Yoopercamp23

    Thread Starter New Member

    Jan 30, 2014
    7
    0
    I am creating code and using a PIC16F1823 that will read an analog input, send it through the ADC, then display the result of just ADRESH in hex on a dual seven segment display.
    I would like to set up a lookup table that I can give the most significant and least significant halves of ADRESH to and have it give me the according segments to turn on on the seven segment display.

    I was wondering if anyone can point me in the right direction with this, thanks!
    P.s. the dual seven segment display will have to be multiplexed and I am trying to figure that out also.

    Here is what I have so far:

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. unsigned char ad_convert(void);//function prototype
    4. unsigned char hex(unsigned char);//converter function
    5.  
    6. __Config=0x09A4; //set up config 1
    7. __Config=0x1CFF; //set up config 2
    8.  
    9.  
    10. void main(void)
    11. {
    12.     PORTA=0x00; //clear Port A
    13.     LATA=0x00; //Clear Lat A
    14.     PORTC=0x00; //Clear Port C
    15.     LATC=0x00; //Clear LAT C
    16.     ANSELA=0x01; //set RA0 as analog In, all other I/O)
    17.     ANSELC=0x00; //all port C I/)
    18.     TRISA=0x09; //RA0,RA3 inputs all others outputs
    19.     TRISC=0x00; //All port C output
    20.     OSCON=0x68; //set FOSC to 4MHz
    21.     TMR0=0;
    22.     T0IF=0;
    23.  
    24. while(1)// begin infinite while loop
    25.     {
    26.         unsigned char value; //variable to store 0-255 A-D conversion
    27.         unsigned char hex1, hex2; //hex values after conversion
    28.         value=ad_convert(); //call to function to read analog in and convert
    29.         hex1=value>>4;
    30.        
    31. THIS IS WHERE I WANT TO SENT THE BITS OF HEX1 TO A LOOKUP TABLE TO GET THE CORRECT SEGMENTS TO TURN ON.
    32.        
    33.  
    34.     }
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41.  
    42. }
    43.  
    44.  
    45. unsigned char ad_convert(void) //unsigned char to return a value between 0-255
    46. {
    47.     //unsigned char dig_value; //variable to store 0-255 value
    48.     PIE1=0x40; //enable ADIF
    49.     ANSELA=0x01; //set RA0 as analog In, all other I/O)
    50.     ADCON1=0x40; //set ADCS to FOSC/4, and Vref is VDD
    51.     ADCON0=0x01; //Turn on ADC, set analog in on RA0
    52.     __delay_us(25);
    53.     GO_nDONE=1; //start acquisition
    54.     __delay_us(25);
    55.         while(ADIF==0) //wait for AD converter flag to trip
    56.         {
    57.             ad_convert=ADRESH;
    58.             ADIF=0; //reset interrupt flag
    59.             ADCON=0x00; //turn off ADC
    60.  
    61.         }
    62.  
    63.     return(ad_convert)
    64.  
    65. }
    66.  
     
    Last edited by a moderator: Feb 16, 2014
  2. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,357
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    As the array is truly a constant you'll save some memory by defining it as such:

    Code ( (Unknown Language)):
    1.  
    2. const unsigned char SEGMENTS[] = {0x3F, 0x06, 0x5B, 0x4F,
    3.                                   0x66, 0x6D, 0x7D, 0x07,
    4.                                   0x7F, 0x67, 0x77, 0x7C,
    5.                                   0x39, 0x5E, 0x79, 0x71};
    6.  
    With a properly configured A2D (ie, left shifted for an 8 bit result) you may access the segnent data like so:

    Code ( (Unknown Language)):
    1.  
    2. Latch_Lo_Segment = SEGMENTS[ADRESH & 0x0F];  // mask for lowest 4 bits
    3. Latch_Hi_Segment = SEGMENTS[ADRESH >> 4];    // shift highest 4 bits to lowest 4
    4.  
    Do note I did not check the array data from MrChips' link.
     
  4. Yoopercamp23

    Thread Starter New Member

    Jan 30, 2014
    7
    0
    ok great responses. Thanks! I am still in the process of learning the basics but im starting to get the hang of it now.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    BTW, I never mentioned WHY using a "const" (constant) array was "better."

    That is due to the PIC being a Harvard architecture where code and storage are separate. If the array is in RAM (not const) then, well, it takes RAM up, and that is usually precious. Making it a const leaves it in ROM, and just in ROM since when you assign RAM array elements you necessarily need to pull the data from ROM anyway.

    So just leave the constant data in the constant (program) ROM and be done with it!
     
Loading...