In the MikrC Pro compiler using the const code variables sometimes seems headache as a beginner. Specially if you are trying to write that string letterals to LCD and getting the error of illegal pointer errors. In this post, I am going to show a simple hack to display the const code variables to lcd which I get from this forum and it worked for me.

What is const code Variable?

If you are not familiar with the concept of const code variables, you may need to look into the official help file of the MikrC pro compiler. It is a variable which is constant which means you cannot change it later. Also, this is written into the code memory instead of the RAM. While using the PIC microcontrollers, the biggest challenge is to optimize the usage of RAM specially in case of some microcontrollers which have very little amount of RAM.

Just like in my case, I was using PIC16F72 microcontroller for a project and quickly ran out of RAM when I start displaying strings on the LCD. This left me no options but to write the strings character by character like this.

          Lcd_Chr(2,12,'F');
          Lcd_Chr_CP('U');
          Lcd_Chr_CP('L');
          Lcd_Chr_CP('L');
Code language: C++ (cpp)

With the help of character-by-character printing I was able to shift the strings to the code memory, but the code length was going to increase like hell and readability and bad code smells were too profuse. So, I have to think of some alternatives. If you remember our assembly codes, you should be aware that we put some constant string literals into the code memory using DB directive. So why can’t we do same in C language? Well, we can do that, and that is why the const code keyword is introduced.

Declaring a constat in Code Memory

As described in previous paragraph, the const code declarative let us declare the constant variable into the Code memory, which is ROM, instead of writing the global variables in the RAM. Obviously, we need the global variables for a good reason and the usage of RAM should be done wisely as it is a limited resource in Embedded Systems. So, we have to declare the constants as const and if they are long, consider them shifting to the code memory with the const code keyword. Here is how you can declare them.

const code char txt1[] = " FYP Solutions\0";
const code char txt2[] = " Ph:03214932078\0";

Code language: C++ (cpp)

Make sure to put the null character at the end of the string which is \0. This way you won’t be able to confuse compiler about the ending of your strings.

Display the Strings from Code Memory to Lcd

Now it is time to display the strings which we saved in code memory in our previous lines, to be shown on the LCD. We are using the 16×2 LCD and using the official MikroC pro library for the Liquide Crystal display. If you are thinking that this could be as simple as following.

     Lcd_Init();                        // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
     Lcd_Out(1,1,txt1);                 // Write text in first row
     Lcd_Out(2,1,txt2);                 // Write text in second row

Code language: C++ (cpp)

You are wrong, as this will give you error as shown in image below.

mikroc illegal pointer error
MikroC Illegal Pointer Error

To solve this issue we have to rewrite the display function as follows.

void LcdWriteFromROM( char row, char col, const char *text )
{
 Lcd_Out( row, col, "" );
 while( *text )
 Lcd_Chr_CP( *text++ );
}

Code language: C++ (cpp)

In this function we are displaying character by character untill the string is finished by null character. So now you can simply use this function as you were trying to do before but now using this function.

     Lcd_Init();                        // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);               // Clear display
     LcdWriteFromROM(1,1,txt1);                 // Write text in first row
     LcdWriteFromROM(2,1,txt2);                 // Write text in second rowCode language: JavaScript (javascript)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.