Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

mikroc lcd display

Status
Not open for further replies.

tkw

New Member
im using microc pro. n i face problem in the lcd display part. PIC that im using is 18f452. i try out the example code given by the library but there is no display in my lcd. Can anyone show me some simple example for lcd display code??
 
The sample code that comes with the compiler works fine. You don't need more code!

Check the display is plugged in properly, and that the LED switch for PORTB is turned off on the development board. Also you will need to adjust the contrast pot.
 
Mr Rb, can u help me n see whether this coding can work for my PIC18f452 lcd display.??

// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

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

char txt1[] = "mikroElektronika";
char txt2[] = "EasyPIC6";
char txt3[] = "Lcd4bit";
char txt4[] = "example";

char i; // Loop variable

void Move_Delay() { // Function used for text moving
Delay_ms(500); // You can change the moving speed here
}

void main(){
//ANSEL = 0; // Configure AN pins as digital I/O
//ANSELH = 0;
//C1ON_bit = 0; // Disable comparators
//C2ON_bit = 0;

Lcd_Init(); // Initialize LCD

Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,6,txt3); // Write text in first row

Lcd_Out(2,6,txt4); // Write text in second row
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display

Lcd_Out(1,1,txt1); // Write text in first row
Lcd_Out(2,5,txt2); // Write text in second row

Delay_ms(2000);

// Moving text
for(i=0; i<4; i++) { // Move text to the right 4 times
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}

while(1) { // Endless loop
for(i=0; i<8; i++) { // Move text to the left 7 times
Lcd_Cmd(_LCD_SHIFT_LEFT);
Move_Delay();
}

for(i=0; i<8; i++) { // Move text to the right 7 times
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}
}
}
 
Sorry I can't test it as you seem to have used non-standard pin connections for the LCD so it won't work on my EasyPIC6 board. I assume you made your own PCB?

What I can suggest is you strip the code down to the very simplest example to make the LCD just write text, and then check everything to make sure your hardware is working. I have posted your code below, but stripped down to the very minimum code;

(Also, please use the CODE TAGS in future so your code displays with proper formatting!)
Code:
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

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

void main()
{
  ANSEL = 0; // Configure AN pins as all digital I/O (safer this way to test)
  ANSELH = 0;
  C1ON_bit = 0; // Disable comparators
  C2ON_bit = 0;

  TRISB = 0b00000000;  // set PORTB all outputs!

  Lcd_Init(); // Initialize LCD (this also clears it!)

  Lcd_Out(1,1,"Test line 1"); // Write text in first row
  Lcd_Out(2,1,"Test line 2"); // Write text in second row

  while(1)  // Endless loop
  {
  }
}

Also you should check all your CONFIG flags in the project setup as the 18F452 is a complex chip and all the config flags need to be right! :)
 
Last edited:
mr RB, thanks for your advise. The LCD part i already solved, now im going into the next process. Which is the selection process. Means by using push button to do selection. For example by pushing the first button the LCD will display A, if push button 2 is press then the LCD will show B. do u have any reference coding dat i can take as reference??
 
Good to hear you got it working. :)

As for the pushbutton that selects the mode, you should read up on "button debouncing in firmware" and choose an option for that.

Also, for reading the MikroC help file is pretty good, I just checked there is a function for reading buttons and it debounces the buttons for you;
Code:
Prototype unsigned short Button(unsigned short *port, unsigned short pin, unsigned short time, unsigned short active_state);
 
Returns Returns 0 or 255.
 
Description Function eliminates the influence of contact flickering upon pressing a button (debouncing).

Parameter port specifies the location of the button; parameter pin is the pin number on designated port and goes from 0..7; parameter time is a debounce period in milliseconds; parameter active_state can be either 0 or 1, and it determines if the button is active upon logical zero or logical one.
 
Requires Nothing.
 
Example Example reads RB0, to which the button is connected; on transition from 1 to 0 (release of button), PORTD is inverted:

do {
  if (Button(&PORTB, 0, 1, 1)) oldstate = 1;
  if (oldstate && Button(&PORTB, 0, 1, 0)) {
    PORTD = ~PORTD;
    oldstate = 0;
  }
} while(1);

That is in the help file under "Button". :)
 
what is the oldstate use for?? if i wan to display something during logic 1 is the coding written like dis:
do {
if (Button(&PORTA, 1, 1, 1)){
oldstate = 1;
Lcd_Out(1,1,txt1); // Write text in first row
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
}

if (oldstate && Button(&PORTB, 1, 1, 0)) {
Lcd_Out(1,1,txt2); // Write text in first row
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
oldstate = 0;
}
} while(1);
another thing is what is this stand for
PORTD = ~PORTD; // invert portD
what does it invert?? how if i din put that in??
lastly, i don understand this part too :
if (oldstate && Button(&PORTB, 0, 1, 0))
 
Ok I don't want to sound rude but it seems you don't have much experience with C code programming, and making an LCD project with a 18F452 is a pretty complicated project!

Maybe you should try some simpler C projects, or work very slowly with this project reading a lot and learning as you go.

Please read the help file in MikroC for the button() function, and reading the help file is a good idea for learning C and that compiler. It's a good help file.

I fixed your example above to use the button() function to display a text string on the LCD;
Code:
// unsigned short Button(unsigned short *port, unsigned short pin, unsigned short time, unsigned short active_state)
// if PORTA.F3 pressed for 20 mS to state 1 (HI), then display text 
if (Button(&PORTA, 3, 20, 1))
{
  Lcd_Out(1,1,txt1); // Write text in first row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR); // Clear display
}

Like I said you will need to work slowly and do a LOT of reading from the help file and from the other project examples. You might also want to join the MikroC support forum at the MikroE.com web site, to get help there and learn from other people's MikroC projects there. :)
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top