[SOLVED] problem in MikroC code for LCD

Status
Not open for further replies.

Qaisar Azeemi

Full Member level 5
Joined
Feb 11, 2011
Messages
315
Helped
16
Reputation
32
Reaction score
15
Trophy points
1,298
Location
Peshawar, Pakistan, Pakistan
Activity points
3,829
Hi!

i am using mikoC PRO for PIC v5.6.1 and programming a 16x2 LCD display using PIC16F877A. wana display data on lcd but getting errors.

since i am in learning process so using engineers garage code.

Code:
#define RS PORTD.F4    //RD4
#define EN PORTD.F5    //RD5
#define Lcdport PORTC  //lcd data pins

void lcd_ini();
void dis_cmd(unsigned char);
void dis_data(unsigned char);
void lcdcmd(unsigned char);
void lcddata(unsigned char);

void main()
{
  TRISD.f4 = 0;
  TRISD.f5 = 0;
  TRISB.f2 = 0x00;
  TRISB.f3 = 0x00;
 // TRISC.f0 =0xff;
 // TRISC.f1 =0xff;
  TRISC=0x00; //portC output

 unsigned char data0[]="Shenztech PvtLtd";
 unsigned int i=0;
 lcd_ini();

  while(1)
  {
  
          for (i=0; i!='\0'; i++)
           {
              dis_data(data0[i]);
              Delay_ms(200);
           
           }
   }
   
}
//..............................;;;;;;;.........................................


void lcd_ini()
{
     dis_cmd(0x02);      // 4 bit mode select
      dis_cmd(0x28);     //2 line; 5x7 display; 4bit mode
       dis_cmd(0x0c);
        dis_cmd(0x06);
         dis_cmd(0x80);    //start from line 1 character 0
}


 //..............................;;;;;;;........................................
 
 void dis_cmd(unsigned char cmd_value)
{
     unsigned char cmd_value1;
     cmd_value1=(cmd_value & 0xf0);   // mask the lower nibble
     lcdcmd(cmd_value1); //send to lcd
      cmd_value1=((cmd_value1<<4) & 0xf0);
      lcdcmd(cmd_value1);
}

//................................;;;;;.........................................

void lcdcmd(unsigned char cmdout)
{
     Lcdport=cmdout;
     
     RS=0;
     EN=1;
     Delay_ms(10);
     EN=0;
}

 //.............................;;;;;;.........................................

void lcddata(unsigned char dataout)
{
     Lcdport=dataout;
     RS=1;
     EN=1;
     Delay_ms(10);
     EN=0;
}



i received the following errors:


array deceleration syntax is seem to be ok. but still there is error. any help will be much appritiated.

regards
 

Re: problem in MikroC coad for LCD

in C one has to declare variables before executable statements, try moving the declaractions, e.g.
Code:
void main()
{
 unsigned char data0[]="Shenztech PvtLtd";
 unsigned int i=0;
  TRISD.f4 = 0;
  TRISD.f5 = 0;
  TRISB.f2 = 0x00;
  TRISB.f3 = 0x00;
 // TRISC.f0 =0xff;
 // TRISC.f1 =0xff;
  TRISC=0x00; //portC output

 lcd_ini();

  while(1)
a few compilers don't enforce the rule
 
Re: problem in MikroC coad for LCD

you have declared a function prototype
Code:
void dis_data(unsigned char);
and called it
Code:
              dis_data(data0[i]);
but not defined it, i.e. a function header with a body of code
Code:
void dis_data(unsigned char)    // function header
{
       ....          //  function code
}
 

Re: problem in MikroC coad for LCD

code is now ok but i am getting problem in my hardware design.
i've connected upper nibbles of lcd i.e; form pin 11 to 14 to upper nibble of port C of PIC16f877A while rd4 is connected to pin4 (rs) of lcd and rd5 to pin 6 (E) of LCD power pins of lcd are connected to power supply and unused data pins (lower nibble) and R/W pins are directly grounded.



but still lcd is not displaying anything; the first line of lcd is full of black squares.
 
Last edited:

Re: problem in MikroC coad for LCD

LCDs are very slow devices compared to modern microcontrollers
make sure you have delays togive the device time to respond, e.g.
Code:
// Write a nibble to the LCD
// may have to adjust delays to suit the processor and clock
void lcdNibble(int n)
{
    int lcd=LCDdata;
	mSecDelay(1);
  	LCDdata = (lcd&0xfff0) | ((n & 0x0f));			// send out lower Nibble
	mSecDelay(1);
    LCDstatus.E=1;					// take clock E high 
	mSecDelay(1);
    LCDstatus.E=0;
	mSecDelay(1);
 }
I find a milliSecond works well
 

Re: problem in MikroC coad for LCD

Check this

mikro C Code

Code:
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections

void main(){
  TRISB = 0x00;                           // Configure PORTB pins as digital
  PORTB = 0x00;
  
  Lcd_Init();                             // Initialize Lcd
  Lcd_Cmd(_LCD_CLEAR);                    // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);               // Cursor off


  while(1) {                              // Endless loop
       Lcd_Out(1,1,"Shenztech PvtLtd");   // Write text in first row
  }
}
 

Attachments

  • ss17.jpg
    135.6 KB · Views: 276
  • lcd.rar
    13.2 KB · Views: 96
Re: problem in MikroC coad for LCD

@internetuser2k12:

yes it worked; thank you very much. how can i program for different kind of animations on LCD via mikroC. that is to make the text moving from left to right etc. and how to use timers and interrupts.
 

Re: problem in MikroC coad for LCD

now as you have a working code you can do anything on LCD by few modification in your code
refer to the data sheet of LCD you are using
it will give you complete command set which you will have to execute to get desired out put on LCD
suppose you want to blink a text on lcd then you just need to send appropriate data to the function lcdcmd (i think its there in data sheet)
refer it
 

Re: problem in MikroC coad for LCD

Refer to mikroC documentation. You will get answers for how to move text and also how to use interrupts and timers.

Try this code

Code:
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections

void main(){
  TRISB = 0x00;                           // Configure PORTB pins as digital
  PORTB = 0x00;
  
  Lcd_Init();                             // Initialize Lcd
  Lcd_Cmd(_LCD_CLEAR);                    // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);               // Cursor off


  while(1) {                              // Endless loop
       Lcd_Out(1,1,"Shenztech PvtLtd");   // Write text in first row
       //Lcd_Cmd(_LCD_SHIFT_LEFT);
       Lcd_Cmd(_LCD_SHIFT_RIGHT);
       Delay_ms(250);
  }
}
 
Last edited:

Re: problem in MikroC coad for LCD

hey Qaisar Azeemi you can look under the tools menu of mikro C pro for lcd custom character edit. its very versatile tool. the rest of it is in documentation.
an advice: do not use these kind of heavily library dependent software, it will make us very lazy and we will be ignorant about all the internal workings of the controller.
 

Re: problem in MikroC coad for LCD


i am quite agree with you Magnatron. acctually i am new to pic programming and started with MPLAB IDE but find it complex i made programs but they have hex file loading problems as you see my last thread www.edaboard.com/threads/260949/ the problem was the hex file format. if you know the solution kindly post it there. i will be very thankful to you.

- - - Updated - - -

how can i use portA and port E of PICs as an input ports and how to configure it as ADC ports?
 

Re: problem in MikroC coad for LCD

To configure PORTA and PORTE as input ports

Code:
TRISA = 0xFF;
TRISE = 0b111;
 

Re: problem in MikroC coad for LCD

To configure PORTA and PORTE as input ports

Code:
TRISA = 0xFF;
TRISE = 0b111;

thank you very much... i have a few questions regarding pic programming.
1) how to program timers and interrupts using mikroC.

2) how to clear the LCD display data RAM? because i want to shift a text on lcd screen under normal conditions, but when the controller detect some signal at its any input it should change the data in display data RAM permanently with other given data.

3)how to change command to lcd. i.e; deactivate one command and activate other. i used
Lcd_Cmd(_LCD_SHIFT_RIGHT); to display a data on screen but when i change the data and want it to b still on lcd rather moving but the right shift command reactivated again and starting shifting it.
 
Last edited:

Re: problem in MikroC coad for LCD

C:\Program Files\Mikroelektronika\mikroC\Examples
refer to this folder on your computer
great number of well documented codes are given
from here you can learn all the basic programing in micro c
 

Re: problem in MikroC coad for LCD

thank you very much... i have a few questions regarding pic programming.
1) how to program timers and interrupts using mikroC.
hey qaisar its all in the documentation section of the mikro c . run the pgm and click on the help button. you can search for timers and interrupts there. Documentation is hmm.. brief. you can learn from there.
you can do that by either overwriting the text or clearing the lcd by using the command
Code:
 Lcd_Cmd(_LCD_CLEAR);
you can clear the lcd for that.
or use a simple if command when you not want to scroll the data
if(the_data_is_changed)
{
scroll_and_display();
}
else
{
display_data();
}
 

Re: problem in MikroC coad for LCD

Thank you very much Magnatron. now there is another problem.
1) i want to monitor 4 inputs of my micro continuously just like interrupt; and perform appropriate action on specific key press. Please see the sanerio in the figure below:


here i have four inputs Enter, Reset, Up and Down. during normal operation of controller if i press enter it enters a specific routine that should monitor all the four inputs and perform the specific function specified to it. enter is to enter a specific routine, reset is to get out of the routine, up and down are for scroll the options and/or to increment/decrement the numbers displayed on lcd screen. kindly help me to write down the routine/program for that in MikroC.

2) and how to tackle with "IRP bit must be set manually" problem. how can i select register banks manually???
thank you
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…