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
LCD
-
- Posts: 2780
- Joined: 25 Dec 2008 15:22
- Location: Scotland
Re: LCD
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.
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
}
Best Regards
Mince
Mince
-
- Posts: 2780
- Joined: 25 Dec 2008 15:22
- Location: Scotland
Re: LCD
from my previous post... " I have kept it simple to make it easy to follow."jtemples wrote: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.void LCD_1_select (){
Best Regards
Mince
Mince
-
- Posts: 2780
- Joined: 25 Dec 2008 15:22
- Location: Scotland
Re: LCD
I will give you two.
Essentially, you treat the lcds as a spi device and LCDx_n as the CS line.
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
...
Re: LCD
The next stage is to wrap the LCDx_() functions.
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.
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
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.
-
- Posts: 2780
- Joined: 25 Dec 2008 15:22
- Location: Scotland
Re: LCD
you can give them to the OP, i dont need it.jtemples wrote:I will give you two.
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
Mince