LCD connecting

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Hi.
Ran in to some problems with pins on my 16f1509
need to move the datastream for my LCD from port B ( RB4-7)
and over to Port C. ( RC4-7 ) and still need the RS & E on RC1-2
is that possible ?
Can i just change the line in LCD.C "#define LCD_DATA PORTB" to
"#define LCD_DATA PORTC"
and still use "#define LCD_RS RC2" "#define LCD_EN RC1"
Scematic attached.
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
You can in hardware but you'll have to look at the code that writes the 4 bit data to the port. Since PORTB is only 4 bits, you can just write the data to the port, ignoring the 4 LSbits. Moving the bus to PORTC would require you to change only the 4 data bits without affecting the other bits on the port. This can be done by AND and OR like so:

Rich (BB code):
char temp;
 temp = LATC & 0x0f;  read current port, clear bits 4-7, preserve 0-3
 temp |= New_LCD_Data ; assumes new LCD data is justified in temp as dddd0000
 LATC = temp;
After writing to the bus, you can use bsf,bcf to strobe the other bits.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
think i know what you mean, but i tink the code is a bir wrong.
1. read the port. and clear bit 4-7, ok, dont see how.
2.
Rich (BB code):
 temp |= New_LCD_Data ; assumes new LCD data is justified in temp as dddd0000
meaning ??
3. wat NeXT, u are use ASM code together with c code.



You can in hardware but you'll have to look at the code that writes the 4 bit data to the port. Since PORTB is only 4 bits, you can just write the data to the port, ignoring the 4 LSbits. Moving the bus to PORTC would require you to change only the 4 data bits without affecting the other bits on the port. This can be done by AND and OR like so:

Rich (BB code):
char temp;
 temp = LATC & 0x0f;  read current port, clear bits 4-7, preserve 0-3
 temp |= New_LCD_Data ; assumes new LCD data is justified in temp as dddd0000
 LATC = temp;
After writing to the bus, you can use bsf,bcf to strobe the other bits.
 

JohnInTX

Joined Jun 26, 2012
4,787
I didn't intend to write complete code, just give you the basic idea... We'll call it pseudocode.

think i know what you mean, but i tink the code is a bir wrong.
1. read the port. and clear bit 4-7, ok, dont see how.
2.
Rich (BB code):
 temp |= New_LCD_Data ; assumes new LCD data is justified in temp as dddd0000
meaning ??
3. wat NeXT, u are use ASM code together with c code.
1). You are reading the current value of LATC to preserve bits 0-3 since when done, you can only write the full 8 bits. By reading into a temp variable then doing the bitwise AND on temp, you clear bits 4-7 (the old LCD data) and leave the other bits alone. This paves the way for the ORing of the new LCD data.

2) You combine the new LCD data 4-7 with the old bits in temp 0-3 by ORing:
0000bbbb (temp, bbbb is bits 0-3 that you want to preserve, 0000 is from the AND)
dddd0000 (new LCD data dddd0000, bits 0-3 here are 0 to preserve bbbb in temp. OR'ed together you get:

ddddbbbb the new data combined with the unchanged old bits. That's what gets written to the port.

3)What's next is that you get the next 4 bit nibble of the LCD data, justify it to dddd0000 again (by shifting) then you have the 2ed half of the LCD data byte and you repeat the process. That part of the code should be the same as if you used PORTB for the databus.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Here is my "new" code, and it Works ;)

Rich (BB code):
 */
#ifndef _XTAL_FREQ
 // Unless specified elsewhere, 4MHz system frequency is assumed
 #define _XTAL_FREQ 4000000
#endif

#include <htc.h>
#include "lcd.h"
#define LCD_RS RB6
#define LCD_EN RB7
#define LCD_D4 RC4 // Data bits
#define LCD_D5 RC5 // Data bits
#define LCD_D6 RC6 // Data bits
#define LCD_D7 RC7 // Data bits
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{   static unsigned char temp;
     temp=c;
 __delay_us(40);
 
 if(c & 0x80) LCD_D7=1; else LCD_D7=0;
 if(c & 0x40) LCD_D6=1; else LCD_D6=0;
 if(c & 0x20) LCD_D5=1; else LCD_D5=0;
 if(c & 0x10) LCD_D4=1; else LCD_D4=0;
 LCD_STROBE();
 if(c & 0x08) LCD_D7=1; else LCD_D7=0;
 if(c & 0x04) LCD_D6=1; else LCD_D6=0;
 if(c & 0x02) LCD_D5=1; else LCD_D5=0;
 if(c & 0x01) LCD_D4=1; else LCD_D4=0;
 LCD_STROBE();
}
/*
 *  Clear and home the LCD
 */
void
lcd_clear(void)
{
 LCD_RS = 0;
 lcd_write(0x1);
 __delay_ms(2);
}
/* write a string of chars to the LCD */
void
lcd_puts(const char * s)
{
 LCD_RS = 1; // write characters
 while(*s)
  lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
 LCD_RS = 1; // write characters
 lcd_write( c );
}

/*
 * Go to the specified position
 */
void
lcd_goto(unsigned char pos)
{
 LCD_RS = 0;
 lcd_write(0x80+pos);
}
 
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
  
//NY RUTINE  
  LCD_RS = 0; // write control bytes
__delay_ms(15);// power on delay
 LCD_D4 = 1; // init! 
 LCD_D5 = 1; //
 LCD_STROBE(); 
 __delay_ms(5);
 LCD_STROBE(); // init! 
 __delay_us(100);
 LCD_STROBE(); // init! 
 __delay_us(5);
 LCD_D4 = 0; // set 4 bit mode
 LCD_STROBE(); 
 __delay_us(40);
 
 lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
 lcd_write(0x0C);// display on
 lcd_write(0x06);// entry mode advance cursor
 lcd_write(0x01);// clear display and reset cursor
}
I didn't intend to write complete code, just give you the basic idea... We'll call it pseudocode.



1). You are reading the current value of LATC to preserve bits 0-3 since when done, you can only write the full 8 bits. By reading into a temp variable then doing the bitwise AND on temp, you clear bits 4-7 (the old LCD data) and leave the other bits alone. This paves the way for the ORing of the new LCD data.

2) You combine the new LCD data 4-7 with the old bits in temp 0-3 by ORing:
0000bbbb (temp, bbbb is bits 0-3 that you want to preserve, 0000 is from the AND)
dddd0000 (new LCD data dddd0000, bits 0-3 here are 0 to preserve bbbb in temp. OR'ed together you get:

ddddbbbb the new data combined with the unchanged old bits. That's what gets written to the port.

3)What's next is that you get the next 4 bit nibble of the LCD data, justify it to dddd0000 again (by shifting) then you have the 2ed half of the LCD data byte and you repeat the process. That part of the code should be the same as if you used PORTB for the databus.
 
Top