Friday, 25 September 2009

Declaring Variables in MikroC

I've been getting emails lately asking if there is any special sequence for declaring variables or functions using mikroc. The answer is no! You declare them just as you would in normal / embedded C. For example, I will use a snippet from a sample MikroC project. The following program prints text to a 2x16 lcd display.


char *text = "mikroElektronika";


void main() {
  Lcd_Init(&PORTD);             // Lcd_Init_EP4, see Autocomplete


  LCD_Cmd(LCD_CLEAR);           // Clear display
  LCD_Cmd(LCD_CURSOR_OFF);      // Turn cursor off
  Delay_ms(1000);
  LCD_Out(1,1, text);           // Print text to LCD, 1st row, 1st column
  Delay_ms(1000);
  LCD_Out(2,6,"mikroE");        // Print text to LCD, 2nd row, 6th column
}


If you were to change the name of the main() function, you could use it in another project to write text to a 2x16 lcd. Have a look at this:


char *text = "mikroElektronika";


void print_LCDtxt() {
  Lcd_Init(&PORTD);             // Lcd_Init_EP4, see Autocomplete


  LCD_Cmd(LCD_CLEAR);           // Clear display
  LCD_Cmd(LCD_CURSOR_OFF);      // Turn cursor off
  Delay_ms(1000);
  LCD_Out(1,1, text);           // Print text to LCD, 1st row, 1st column
  Delay_ms(1000);
  //LCD_Out(2,6,"mikroE");        // Print text to LCD, 2nd row, 6th column
}


If you were to copy and paste this in an existing mikroC project (provided this code doesn't conflict with yoou existing ports), you would be able to call this function at any time, printing whatever text you have placed in the *text char.

Hope this answers your question! Feel free to comment :)

No comments: