LCD

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
TrevorDawes
Posts: 159
Joined: 06 Feb 2010 15:01
Location: Durban South Africa

LCD

#1 Post by TrevorDawes » 21 Jun 2012 04:15

Hi all, is it possible to connect 2 x LCD modules to a single PIC?
I would like to have independent displays for a static transfer switch I am building.

Thanking all the PIC Guru's

Trevor

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: LCD

#2 Post by jtemples » 21 Jun 2012 11:16

All that matters is the LCD_EN signal.

So you can hang everything else off a bus, and then gate the LCD_EN signal to individual displays, you can then hang as many LCDs off the same bus as you want.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: LCD

#3 Post by Mince-n-Tatties » 21 Jun 2012 17:32

here is a basic example of "A" method to handle more than one 2x16 LCD. I have kept it simple to make it easy to follow.

The code shows an option with all 3 LCDs being communicated with at the same time and it shows each LCD being send individual data.

Code: Select all

// LCD module connections

sbit LCD_RS at RB3_bit;
sbit LCD_EN at RB2_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_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;
sbit LCD_RS_Direction at TRISB3_bit;
sbit LCD_EN_Direction at TRISB2_bit;


// End LCD module connections

void LCD_none_select (){

  TRISA1_bit = 0;                    // Enable LCD 1
  TRISA2_bit = 0;                    // Disable LCD 2
  TRISA3_bit = 0;                    // Disable LCD 3
  Delay_ms(1);
}

void LCD_all_select (){

  TRISA1_bit = 1;                    // Enable LCD 1
  TRISA2_bit = 1;                    // Disable LCD 2
  TRISA3_bit = 1;                    // Disable LCD 3
  Delay_ms(1);
}
void LCD_1_select (){
  
  TRISA1_bit = 1;                    // Enable LCD 1
  TRISA2_bit = 0;                    // Disable LCD 2
  TRISA3_bit = 0;                    // Disable LCD 3
  Delay_ms(1);
}

void LCD_2_select (){

  TRISA1_bit = 0;                    // Disable LCD 1
  TRISA2_bit = 1;                    // Enable LCD 2
  TRISA3_bit = 0;                    // Disable LCD 3
  Delay_ms(1);
}

void LCD_3_select (){

  TRISA1_bit = 0;                    // Disable LCD 1
  TRISA2_bit = 0;                    // Disable LCD 2
  TRISA3_bit = 1;                    // Enable LCD 3
  Delay_ms(1);
}

void main() {

  ANSEL  = 0;                        // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                      // Disable comparators
  C2ON_bit = 0;

  PORTA = 0xFF; // set all to logic high
  TRISA = 0xFF; //set all to input

  // make all LCDs run at the same time as if working with a single LCD
  LCD_all_select ();
  LCD_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,"LCD ALL Init at");        // Write text in first row
  Lcd_Out(2,1,"the same time");      // Write text in Second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  LCD_none_select ();
  Delay_ms(1000);
  
  // talk to each LCD and display text on each
  LCD_1_select();
  LCD_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,"LCD 1 Row 1");        // Write text in first row
  Lcd_Out(2,1,"Init Complete");      // Write text in Second row
  
  LCD_2_select();
  LCD_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,"LCD 2 Row 1");        // Write text in first row
  Lcd_Out(2,1,"Init Complete");      // Write text in Second row
  
  LCD_3_select ();
  LCD_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,"LCD 3 Row 1");        // Write text in first row
  Lcd_Out(2,1,"Init Complete");      // Write text in Second row

  LCD_none_select ();                // Force all LCD enable pins safe


}
screen_capture_all_init.jpg
screen_capture_all_init.jpg (240.79 KiB) Viewed 7378 times
screen_capture_individ.jpg
screen_capture_individ.jpg (246 KiB) Viewed 7378 times
Best Regards

Mince

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: LCD

#4 Post by jtemples » 21 Jun 2012 17:40

void LCD_1_select (){
if you give those functions / macros an argument to indicate the pin, you can compact the routines further. For example, you will have a lcd_select(lcdx) that selects a lcd and lcd_deselect(lcdx) to deselect a lcd.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: LCD

#5 Post by Mince-n-Tatties » 21 Jun 2012 19:20

jtemples wrote:
void LCD_1_select (){
if you give those functions / macros an argument to indicate the pin, you can compact the routines further. For example, you will have a lcd_select(lcdx) that selects a lcd and lcd_deselect(lcdx) to deselect a lcd.
from my previous post... " I have kept it simple to make it easy to follow."
Best Regards

Mince

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: LCD

#6 Post by jtemples » 21 Jun 2012 20:43

Because of simplicity and ease of use, you want to break it down to just two routines to activate / deactivate over any number of lcds, not 5 routines to cover 3 lcds.

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: LCD

#7 Post by jtemples » 21 Jun 2012 23:05

You can actually put a wrapper around the code and make a series of macros to make this entirely transparent to the users.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: LCD

#8 Post by Mince-n-Tatties » 21 Jun 2012 23:55

you go right ahead and post whatever code you like
Best Regards

Mince

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: LCD

#9 Post by jtemples » 22 Jun 2012 02:05

I will give you two.

Code: Select all

#define LCDx_Select(lcdx) lcdx = 1  //select lcdx
#define LCDx_Deselect(lcdx) lcdx = 0 //deselect lcdx

// talk to each LCD and display text on each
  //LCD_1_select();
  LCDx_Select(LCDx_1); //select lcd1
  LCD_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,"LCD 1 Row 1");        // Write text in first row
  Lcd_Out(2,1,"Init Complete");      // Write text in Second row
  LCDx_Deselect(LCDx_1); //deselect lcd1
...
Essentially, you treat the lcds as a spi device and LCDx_n as the CS line.

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: LCD

#10 Post by jtemples » 22 Jun 2012 02:14

The next stage is to wrap the LCDx_() functions.

Code: Select all

...

#define Lcdx_Init(lcdx) do {LCDx_Select(lcdx); Lcd_Init(); LCDx_Deselect(lcdx);} while (0)
#define Lcdx_Cmd(lcdx, cmd) do {LCDx_Select(lcdx); Lcd_Cmd(cmd); LCDx_Deselect(lcdx);} while (0)
#define Lcdx_Out(lcdx, x, y, str) do {LCDx_Select(lcdx); Lcd_Out(x, y, str); LCDx_Deselect(lcdx); } while (0)

...

 //LCD_2_select();
  Lcdx_Init(LCDx_2);
  Lcdx_Cmd(LCDx_2, _LCD_CLEAR);               // Clear display
  Lcdx_Cmd(LCDx_2, _LCD_CURSOR_OFF);          // Cursor off
  Lcdx_Out(LCDx_2, 1,1,"LCD 2 Row 1");        // Write text in first row
  Lcdx_Out(LCDx_2, 2,1,"Init Complete");      // Write text in Second row
For the user code, notice the similarity between it and the single lcd version - they are almost identical.

So with those macros, a user can easily transport his/her single LCD code to a multi-LCD platform, with minimum changes.

Again, those two approaches have their own pros and cons but with the use of lcdx_select() or lcdx_deselect(), you only have two macros to remember.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: LCD

#11 Post by Mince-n-Tatties » 22 Jun 2012 22:06

jtemples wrote:I will give you two.
you can give them to the OP, i dont need it. :D

like i said already i am happy that my code post is easier for the newbie target to read and follow. KISS!

"ease of use" does not mean easy to read or easy to follow for the noobs.
Best Regards

Mince

Post Reply

Return to “mikroC PRO for PIC General”