Lookup table help

Thread Starter

Yoopercamp23

Joined Jan 30, 2014
7
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:

Rich (BB code):
#include <htc.h>
unsigned char ad_convert(void);//function prototype
unsigned char hex(unsigned char);//converter function

__Config=0x09A4; //set up config 1
__Config=0x1CFF; //set up config 2


void main(void)
{
    PORTA=0x00; //clear Port A
    LATA=0x00; //Clear Lat A
    PORTC=0x00; //Clear Port C
    LATC=0x00; //Clear LAT C
    ANSELA=0x01; //set RA0 as analog In, all other I/O)
    ANSELC=0x00; //all port C I/)
    TRISA=0x09; //RA0,RA3 inputs all others outputs
    TRISC=0x00; //All port C output
    OSCON=0x68; //set FOSC to 4MHz
    TMR0=0;
    T0IF=0;

while(1)// begin infinite while loop
    {
        unsigned char value; //variable to store 0-255 A-D conversion
        unsigned char hex1, hex2; //hex values after conversion
        value=ad_convert(); //call to function to read analog in and convert
        hex1=value>>4;
        
THIS IS WHERE I WANT TO SENT THE BITS OF HEX1 TO A LOOKUP TABLE TO GET THE CORRECT SEGMENTS TO TURN ON. 
        

    }







}


unsigned char ad_convert(void) //unsigned char to return a value between 0-255
{
    //unsigned char dig_value; //variable to store 0-255 value
    PIE1=0x40; //enable ADIF
    ANSELA=0x01; //set RA0 as analog In, all other I/O)
    ADCON1=0x40; //set ADCS to FOSC/4, and Vref is VDD
    ADCON0=0x01; //Turn on ADC, set analog in on RA0
    __delay_us(25);
    GO_nDONE=1; //start acquisition
    __delay_us(25);
        while(ADIF==0) //wait for AD converter flag to trip
        {
            ad_convert=ADRESH;
            ADIF=0; //reset interrupt flag
            ADCON=0x00; //turn off ADC

        }

    return(ad_convert)

}
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,415
As the array is truly a constant you'll save some memory by defining it as such:

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

Rich (BB code):
Latch_Lo_Segment = SEGMENTS[ADRESH & 0x0F];  // mask for lowest 4 bits
Latch_Hi_Segment = SEGMENTS[ADRESH >> 4];    // shift highest 4 bits to lowest 4
Do note I did not check the array data from MrChips' link.
 

ErnieM

Joined Apr 24, 2011
8,415
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!
 
Top