TiOLUWA
TiOLUWA

Reputation: 225

How do you create an Array of pointers to constant arrays in Microchip C30 compiler

I'm trying to create an array which contains pointers to constant arrays. I'm using C30 V3.31 in MPLABX, I'm trying to create a segment map for my LCD with the PIC24FJ128GA310. the idea is to have all the LCD digits in constant arrays, and then have an array that contains pointers to all the digits of the LCD for easy access. the code segment is shows below:

enum SEGS 
 {    
   SEG_1A = COM0 |0 , SEG_1B = COM0 |53, SEG_2A = COM0 |33, SEG_K1 = COM0 |44,
   SEG_K2 = COM0 |22, SEG_K8 = COM0 |21, SEG_4A = COM0 |62, SEG_4B = COM0 |61,      
   SEG_5A = COM0 |60, SEG_5B = COM0 |59, SEG_6A = COM0 |58, SEG_6B = COM0 |50,     
   SEG_7A = COM0 |61, SEG_7B = COM0 |48, SEG_8A = COM0 |59, SEG_8B = COM0 |26,    
   SEG_9A = COM0 |25, SEG_9B = COM0 |24, SEG_10A= COM0 |23, SEG_10B= COM0 |45,    
   SEG_2B = COM0 |52, SEG_3A = COM0 |32, SEG_3B = COM0 |51, SEG_S3 = COM0 |24 
 } 

 const BYTE DIGIT1[] = {SEG_1A,SEG_1B,SEG_1C,SEG_1D,SEG_1E,SEG_1F,SEG_1G}; 
 const BYTE DIGIT2[] = {SEG_2A,SEG_2B,SEG_2C,SEG_2D,SEG_2E,SEG_2F,SEG_2G}; 
 const BYTE DIGIT3[] = {SEG_3A,SEG_3B,SEG_3C,SEG_3D,SEG_3E,SEG_3F,SEG_3G}; 
 const BYTE DIGIT4[] = {SEG_4A,SEG_4B,SEG_4C,SEG_4D,SEG_4E,SEG_4F,SEG_4G}; 
 const BYTE DIGIT5[] = {SEG_5A,SEG_5B,SEG_5C,SEG_5D,SEG_5E,SEG_5F,SEG_5G}; 
 const BYTE DIGIT6[] = {SEG_6A,SEG_6B,SEG_6C,SEG_6D,SEG_6E,SEG_6F,SEG_6G}; 
 const BYTE DIGIT7[] = {SEG_7A,SEG_7B,SEG_7C,SEG_7D,SEG_7E,SEG_7F,SEG_7G}; 
 const BYTE DIGIT8[] = {SEG_8A,SEG_8B,SEG_8C,SEG_8D,SEG_8E,SEG_8F,SEG_8G}; 
 const BYTE DIGIT9[] = {SEG_9A,SEG_9B,SEG_9C,SEG_9D,SEG_9E,SEG_9F,SEG_9G}; 
 const BYTE DIGIT10[] = {SEG_10A,SEG_10B,SEG_10C,SEG_10D,SEG_10E,SEG_10F,SEG_10G}; 

 const BYTE * const Digits[] ={ 
 DIGIT1,DIGIT2,DIGIT3,DIGIT4,DIGIT5,DIGIT6,DIGIT7,DIGIT8,DIGIT9,DIGIT10}; 

 enum DIGITS {DIG1,DIG2,DIG3,DIG4,DIG5,DIG6,DIG7,DIG8,DIG9,DIG10}; 

 //... FUNCTIONS
 void DisplayDigit2(BYTE idx,BYTE Mask) 
 {    
      BYTE digit = *Digits[idx];    
      for(i=0; i<8;i++){        
           DisplaySeg(digit,(Mask&1));        
           Mask >>=1;    
      } 
 } 


 void DisplaySeg(BYTE segcode,BOOL state) 
 {    
      BYTE mapbyte,com,bitmap,index;    
      com = (segcode&0xC0)>>6;    
      segcode = (segcode & 0x3F);    
      index = segcode/16;    
      mapbyte = (com * 4) + index;    
      bitmap = (segcode%16);    
      if (state)        
           lcdvals[mapbyte] |= (1<<bitmap);    
      else        
           lcdvals[mapbyte] &= ~(1<<bitmap); 
 } 
 ... 
 //Calling the function 
 DisplayDigit2(DIG1,ONE); 

Can anyone please help me confirm if I did it right? The code compiles without any errors or warning, and I can see all the variables and pointers in the watch window, but the function DisplayDigit2() does not execute completely.

When I step through it, the debugger stops on the first line,

 BYTE digit = *Digits[idx];    

and simple displays this message in the status bar "user program stopped".

Upvotes: 1

Views: 715

Answers (1)

Aditya Tantry
Aditya Tantry

Reputation: 65

You mention

BYTE digit = *Digits[idx];

I am thinking you are trying to access Digits[0] which would be DIGIT1[] array. Inside which you want access all 7 values using your for-loop. If so, assigning BYTE digit = *Digits[idx] may not work. try

BYTE * digit = Digits[idx]

so you are assigning the pointer inside Digits[0] to a local pointer. You will need to tweak your DisplaySeg code as well. Also, I am not sure what happens when you declare

const BYTE * const Digits[]

Just,

const BYTE * Digits[]

should be enough. :)

Hope it helps Aditya

Upvotes: 1

Related Questions