PICkit™ 3 Starter Kit User’s Guide
DS41628B-page 92 2012 Microchip Technology Inc.
EXAMPLE 3-53:
For PIC18 devices, the compiler will recognize the “char” type as a byte and assign two
byte addressable data points per program memory word. Notice the data space differ-
ence if this ‘const’ keyword is forgone.
FIGURE 3-20: DECLARED AS A CONSTANT
FIGURE 3-21: NOT DECLARED AS A CONSTANT
The compiler placed 16 more bytes into data space (as it should) when the const key-
word was omitted. With the keyword in place, notice how only one byte of RAM is used.
This one byte is used in main, adc_value, which holds the top eight Most Significant
bits.
Notice how much more space there is in program memory than in data memory.
Sixteen bytes of RAM accounts for 4% of the total available space while adding the
array into Flash only adds an additional 2% of used space.
The main loop then uses the ADC value as the offset into the array which will return the
correct gray code equivalent.
EXAMPLE 3-54:
const unsigned char gray_code[] = { //lookup table for binary->gray code
0b0000,0b0001,0b0011,0b0010,0b0110,
0b0111,0b0101,0b0100,0b1100,0b1101,
0b1111,0b1110,0b1010,0b1011,0b1001,
0b10000
};
Memory Summary:
Program space used 7Eh ( 126) of 4000h bytes ( 0.8%)
Data space used 1h ( 1) of 1A0h bytes ( 0.2%)
Memory Summary:
Program space used 9Ch ( 156) of 4000h bytes ( 1.0%)
Data space used 11h ( 17) of 1A0h bytes ( 4.1%)
while(1){
adc_value = adc(); //get the ADC value from the POT
adc_value >> = 4; //save only the top 4 MSbs
LATC = gray_code[adc_value]; //convert to Gray Code and display on the LEDs
}