PORT D7 - LCD Data pin 7
PORT D6 - LCD Data pin 6
PORT D5 - LCD Data pin 5
PORT D4 - LCD Data pin 4
PORT RC6 - EN
PORT RC5 - RW
PORT RC4 - RS
What´s the difference? Your headline says "4 bit initialisation"Should I send only 4 bit data at time ? or should I send in upper and lower nibble ?
Hi,
What´s the difference? Your headline says "4 bit initialisation"
I assume I don´t understand what the question/problem is. Please explain.
btw: There are probably millions of informations (documents, code, schematics, videos...) in the internet how to set up a display for/in 4 bit mode..
Klaus
generally: PortD = PD7 | PD6 | PD5 | PD4 | PD3 | PD2 | PD1 | PD0PORT D7 - LCD Data pin 7
PORT D6 - LCD Data pin 6
PORT D5 - LCD Data pin 5
PORT D4 - LCD Data pin 4
is not according the datasheet.Power ON
RS = 0; RW = 0;
Wait 40ms
PORTD =
What I said before, these instructions are sent as two nibbles. That's the normal operation in 4-bit mode. Once the interface has been conclusively set to 4-bit mode, you are starting to send regular commands, which consist always of two nibbles.I have noticed that at starting there is one instruction in one box and after some sequence there is two instruction in one box
I don't know what that indicates ?
What I said before, these instructions are sent as two nibbles. That's the normal operation in 4-bit mode. Once the interface has been conclusively set to 4-bit mode, you are starting to send regular commands, which consist always of two nibbles.
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576 // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7 // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF // Code Protect Boot (Disabled)
#pragma config CPD = OFF // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF // Config. Write Protect (Disabled)
#pragma config WRTB = OFF // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF // Table Read Protect Boot (Disabled)
#define RS LATCbits.LATC4
#define RW LATCbits.LATC5
#define EN LATCbits.LATC6
#define LCD_Port LATD
void Port_Initialized (void)
{
LATA = LATB = LATC = LATD = LATE = 0;
TRISA = 0b0000000;// all are output, Unused
TRISB = 0b0000000;// all are output, Unused
TRISC = 0b0000000;// RS RW EN
TRISD = 0b0000000;// LCD
TRISE = 0b0000000;// All are output, Unused
ANCON0 = 0; // digital port
ANCON1 = 0; // digital port
CM1CON = 0; // Comparator off
CM2CON = 0; // Comparator off
ADCON0 = 0; // A/D conversion Disabled
}
void LCD_E_Pulse(void)
{
EN = 1;
__delay_us(4);
EN = 0;
__delay_us(4);
}
void LCD_WriteCmd(unsigned char command)
{
RS = 0; RW = 0;
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
}
void LCD_WriteData(unsigned char data)
{
}
void LCD_Init(void)
{
__delay_ms(40);
LCD_WriteCmd(0x30); //Function set
__delay_ms(5);
LCD_WriteCmd(0x30); //Function set
__delay_us(100);
LCD_WriteCmd(0x30); ////Function set
LCD_WriteCmd(0x20); // 4 bit long
}
void main ()
{
Port_Initialized();
while (1)
{
}
}
I am struggling with two function in my codeHi,
doesn´t look bad ... although I didn´t check thoroughly.
so now you need to follow datasheet sections
* Interfacing to the MPU
* Interfacing to a 4-bit MPU
read through the documents that already exist in the internet. Watch videos.
Then show us your idea, so we can discuss about it.
Klaus
void LCD_WriteCmd(unsigned char command)
{
RS = 0; RW = 0;
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
}
void LCD_Init(void)
{
__delay_ms(40);
LCD_WriteCmd(0x30); //Function set
__delay_ms(5);
LCD_WriteCmd(0x30); //Function set
__delay_us(100);
LCD_WriteCmd(0x30); ////Function set
LCD_WriteCmd(0x20); // 4 bit long
}
so far so good. you mask your data with 0xF= and ouput it as HIGH nibble.LCD_Port = data & 0xF0 ; // send upper nibble
data: D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0
mask: 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0
makes: D7 | D6 | D5 | D4 | 0 | 0 | 0 | 0 = higher nibble
data: D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0
shifted four bits left. (zeros in)
data: D3 | D2 | D1 | D0 | 0 | 0 | 0 | 0 = lower nibble
Here is my complete code. LCD is not showing data as expected . What's wrong with program?Hi,
so far so good. you mask your data with 0xF= and ouput it as HIGH nibble.
You see: no need to mask with "0xF0". you may omit it.
Klaus
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576 // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7 // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF // Code Protect Boot (Disabled)
#pragma config CPD = OFF // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF // Config. Write Protect (Disabled)
#pragma config WRTB = OFF // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF // Table Read Protect Boot (Disabled)
#define RS LATCbits.LATC4
#define RW LATCbits.LATC5
#define EN LATCbits.LATC6
#define LCD_Port LATD
void Port_Initialized (void)
{
LATA = LATB = LATC = LATD = LATE = 0;
TRISA = 0b0000000;// all are output, Unused
TRISB = 0b0000000;// all are output, Unused
TRISC = 0b0000000;// RS RW EN
TRISD = 0b0000000;// LCD
TRISE = 0b0000000;// All are output, Unused
ANCON0 = 0; // digital port
ANCON1 = 0; // digital port
CM1CON = 0; // Comparator off
CM2CON = 0; // Comparator off
ADCON0 = 0; // A/D conversion Disabled
}
void LCD_E_Pulse(void)
{
EN = 1;
__delay_us(4);
EN = 0;
__delay_us(4);
}
void LCD_WriteCmd(unsigned char data)
{
RS = 0; RW = 0;
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
LCD_Port = (data << 4 );
LCD_E_Pulse();
}
void LCD_Init(void)
{
__delay_ms(40);
LCD_WriteCmd(0x30); //Function set 00110000
__delay_ms(5);
LCD_WriteCmd(0x30); //Function set 00110000
__delay_us(100);
LCD_WriteCmd(0x30); ////Function set 00110000
LCD_WriteCmd(0x20); // 4 bit 00100000
LCD_WriteCmd(0x0C); // Display ON
LCD_WriteCmd(0x2C); // 4 bit, 1-line display 5 × 8 dot character font 00101100
LCD_WriteCmd(0x08); // clear display 00001000
}
void LCD_WriteData(unsigned char data)
{
RS = 1; // select Data register
RW = 0 ; // write mode
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
LCD_Port = (data << 4 ); // send lower nibble
LCD_E_Pulse();
}
void buffer( unsigned char *p)
{
while (*p != '\0')
{
LCD_WriteData(*p);
p++;
}
}
void main ()
{
unsigned int i = 0;
unsigned char Data1 []={"EDA Board"};
Port_Initialized();
LCD_Init();
buffer(Data1);
while (1)
{
}
}
Hi Brian I have added delay in function but still I am not getting data as expectedAlmost there....
Your problem is you do not have a delay between each character being written to the LCD. Understand that inside the LCD module there is another MCU and it runs at quite low speed. Your MCU runs MUCH faster so you have to be careful not to 'overfeed it'. There are two ways to do that, the fastest is to read the status bit back from the LCD (RW = 1 then read the data lines) or you can use the easier but possibly slower way of just adding a short delay after sending each character.
Brian.
void LCD_WriteCmd(unsigned char data)
{
RS = 0; RW = 0;
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
__delay_ms(40);
LCD_Port = (data << 4 ); // send lower nibble
LCD_E_Pulse();
}
sbit LCD_D7 at LATB3_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D4 at LATB0_bit
void LCD_E_Pulse(void)
{
EN = 1;
Delay_us(5);
EN = 0;
Delay_us(5);
}
void LCD_WriteCmd(unsigned char dat2)
{
RS = 0;
Delay_us(5);
RW = 0;
Delay_us(5);
// LCD_Port = data & 0xF0 ; // send upper nibble
LCD_Port = dat2 >> 4 ;
LCD_E_Pulse();
// LCD_Port = (data << 4 );
LCD_Port = dat2 & 0x0F ;
LCD_E_Pulse();
}
void LCD_WriteData(unsigned char dat)
{
RS = 1; // select Data register
Delay_us(10);
RW = 0 ; // write mode
LCD_Port = dat >> 4; // send upper nibble
LCD_E_Pulse();
LCD_Port = (dat & 0x0F ); // send lower nibble
LCD_E_Pulse();
//LCD_Port = dat & 0xF0 ; // send upper nibble
//LCD_E_Pulse();
//LCD_Port = (dat << 4 ); // send lower nibble
//LCD_E_Pulse();
}
void bufferx( unsigned char *px)
{
while (*(px)>0)
{
LCD_WriteData(*(px));
px++;
Delay_ms(1);
}
}
void LCD_Init2(void)
{
Delay_ms(40);
LCD_WriteCmd(0x30); //Function set 00110000
Delay_ms(25);
LCD_WriteCmd(0x30); //Function set 00110000
Delay_ms(25);
LCD_WriteCmd(0x30); ////Function set 00110000
Delay_ms(25);
LCD_WriteCmd(0x20); // 4 bit 00100000
LCD_WriteCmd(0x28); // 4 bit, 2-line display 5 × 8 dot character font 00101100
LCD_WriteCmd(0x0C); // Display ON
LCD_WriteCmd(0x08); // clear display 00001000
LCD_WriteCmd(0x0F); // <--- added after completly removing mikroE LCD library
}
void main()
{
init Hardware and UART ......etc ......
CPrint1(" test2 with independant LCD software\r\n");
LCD_Init2();
Delay_ms(10);
do
{
LCD_WriteCmd(_LCD_CLEAR);// =1
Delay_ms(10);
LCD_WriteCmd(128); //_LCD_FIRST_ROW;
Delay_ms(10);
strcpy(CRam1,"Test2 New LCD ");
bufferx(CRam1);
Delay_ms(3000);
LCD_WriteCmd (192); // _LCD_SECOND_ROW);
Delay_ms(10);
strcpy(CRam1,"EDA Board 2 ");
bufferx(CRam1);
Delay_ms(3000);
}
while(1);
void buffer( unsigned char *p)
{
while (*p != '\0')
{
LCD_WriteData(*p);
Delay_ms(1); // you can probably make this shorter
p++;
}
}
You should have a LCD_Write_CmdNibble() function for the first initialisation steps.
A question not yet addressed, what's connected to the lower 4 bits of Port D (LCD_Port)? People use 4-Bit mode to save GPIO lines for other purposes, if this is the case in your application, the lower bits must be masked out in writes to Port D. Otherwise, if the bits are unused, why don't you use it t odrive the LCD module in 8-bit mode?
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576 // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7 // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF // Code Protect Boot (Disabled)
#pragma config CPD = OFF // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF // Config. Write Protect (Disabled)
#pragma config WRTB = OFF // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF // Table Read Protect Boot (Disabled)
#define RS LATCbits.LATC4
#define RW LATCbits.LATC5
#define EN LATCbits.LATC6
#define LCD_Port LATD
void Port_Initialized (void)
{
LATA = LATB = LATC = LATD = LATE = 0;
TRISA = 0b0000000;// all are output, Unused
TRISB = 0b0000000;// all are output, Unused
TRISC = 0b0000000;// RS RW EN
TRISD = 0b0000000;// LCD
TRISE = 0b0000000;// All are output, Unused
ANCON0 = 0; // digital port
ANCON1 = 0; // digital port
CM1CON = 0; // Comparator off
CM2CON = 0; // Comparator off
ADCON0 = 0; // A/D conversion Disabled
}
void LCD_E_Pulse(void)
{
EN = 1;
__delay_us(4);
EN = 0;
__delay_us(4);
}
LCD_Write_CmdNibble(unsigned char data)
{
RS = 0; RW = 0;
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
}
void LCD_WriteCmd(unsigned char data)
{
RS = 0; RW = 0;
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
__delay_ms(5);
LCD_Port = (data << 4 );
LCD_E_Pulse();
}
void LCD_Init(void)
{
__delay_ms(40);
LCD_Write_CmdNibble(0x30); //Function set 00110000
__delay_ms(5);
LCD_Write_CmdNibble(0x30); //Function set 00110000
__delay_us(100);
LCD_Write_CmdNibble(0x30); ////Function set 00110000
LCD_WriteCmd(0x20); // 4 bit 00100000
LCD_WriteCmd(0x0C); // Display ON
LCD_WriteCmd(0x2C); // 4 bit, 1-line display 5 × 8 dot character font 00101100
LCD_WriteCmd(0x08); // clear display 00001000
}
void LCD_WriteData(unsigned char data)
{
RS = 1; // select Data register
RW = 0 ; // write mode
LCD_Port = data & 0xF0 ; // send upper nibble
LCD_E_Pulse();
__delay_ms(5);
LCD_Port = (data << 4 ); // send lower nibble
LCD_E_Pulse();
}
void buffer( unsigned char *p)
{
while (*p != '\0')
{
LCD_WriteData(*p);
__delay_ms(1) ; // you can probably make this shorter
p++;
}
}
void main ()
{
unsigned int i = 0;
unsigned char Data1 []={"EDA Board"};
Port_Initialized();
LCD_Init();
buffer(Data1);
while (1)
{
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?