Hello!
I'm working on a major project, and I'm having trouble integrating an LCD module with the pic18f452. I'm using MPLAB IDE v8.87, c18 compiler. My LCD just shows a black line! So any help would be much appreciated!
Here's some info about the hardware:
I'm using a PICDEM2+ board (green) but the LCD on it is broken, hence using an external one. The LCD uses a KS0066U controller, but I know it's basically the same as a HD44780 controller.
The connections are as follows:
Pins D4-D7 - D0-D3
E - RA1
RW - RA2
RS - RA3
Vee - I'm using a voltage divider which gives a voltage of approx. 1V (someone stole my trimpot)
The code is as follows:
I'm working on a major project, and I'm having trouble integrating an LCD module with the pic18f452. I'm using MPLAB IDE v8.87, c18 compiler. My LCD just shows a black line! So any help would be much appreciated!
Here's some info about the hardware:
I'm using a PICDEM2+ board (green) but the LCD on it is broken, hence using an external one. The LCD uses a KS0066U controller, but I know it's basically the same as a HD44780 controller.
The connections are as follows:
Pins D4-D7 - D0-D3
E - RA1
RW - RA2
RS - RA3
Vee - I'm using a voltage divider which gives a voltage of approx. 1V (someone stole my trimpot)
The code is as follows:
Code:
#include <p18f452.h>
#include <delays.h>
#define EN PORTAbits.RA1 // Assigning RA1 for Enable on LCD controller
#define RW PORTAbits.RA2 // Assigning RA2 for Write on LCD controller
#define RS PORTAbits.RA3 // Assigning RA3 for Register Select on LCD
//#define PORTD LATD
#define line1_pos 0x80
#define line2_pos 0xC0
// Writes command in 4-bit mode to LCD
void lcd_cmd(unsigned char c){
unsigned char temp;
EN = 0;
RS = 0;
RW = 0;
temp = c;
temp = temp >> 4;
temp = temp & 0x0F; //clear top bits of dat
PORTD = PORTD & 0xF0; //clear bottom bits of port (interested only in DB7-DB4)
PORTD = PORTD | temp;
EN = 1;
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
//Total is approximately 450ns
EN = 0;
Delay1KTCYx(20); //Delay for 5ms = 20,000 Tcy for command writes
temp = c;
temp = temp & 0x0F; //clear top bits of dat
PORTD = PORTD & 0xF0; //clear bottom bits of port (interested only in DB7-DB4)
PORTD = PORTD | temp;
EN = 1;
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
//Total is approximately 450ns
EN = 0;
Delay1KTCYx(20); //Delay for 5ms = 20,000 Tcy for command writes
}
// Write character data in 4-bit data to LCD
void lcd_data(unsigned char c){
unsigned char temp;
EN = 0;
RS = 1;
RW = 0;
temp = c;
temp = temp >> 4;
temp = temp & 0x0F; //clear top bits of dat
PORTD = PORTD & 0xF0; //clear bottom bits of port (interested only in DB7-DB4)
PORTD = PORTD | temp;
EN = 1;
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
EN = 0;
Delay100TCYx(8); //Delay for 200us = 800 Tcy for data writes
temp = c;
temp = temp & 0x0F; //clear top bits of dat
PORTD = PORTD & 0xF0; //clear bottom bits of port (interested only in DB7-DB4)
PORTD = PORTD | temp;
EN = 1;
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
Delay1TCY() //Delay of exactly 1 Tcy = 0.25us
EN = 0;
Delay100TCYx(8); //Delay for 200us = 800 Tcy for data writes
}
// Clears LCD display
void lcd_clear(void){
lcd_cmd(0x01);
Delay1KTCYx(5); //5ms
}
//Writes nibble to PORTD
void lcd_nibble(unsigned char nib){
unsigned char temp;
temp = nib;
temp = temp & 0x0F; //clear top bits of dat
PORTD = PORTD & 0xF0; //clear bottom bits of port (interested only in DB7-DB4)
PORTD = PORTD | temp;
}
//Initialise the LCD
void lcd_init(void){
Delay1KTCYx(120); //Wait for 30ms = 120,000 Tcy for LCD to power up
// Set up LCD interface to 4-bit mode, 2 lines, 5*10 dots
lcd_cmd(0x28);
Delay100TCYx(2); //Delay for ~40us = 160 Tcy
// Enable display/cursor - cursor moves right, display is not shifted
lcd_cmd(0x08);
Delay100TCYx(2); //Delay for ~40us = 160 Tcy
// Clear display
lcd_cmd(0x01);
Delay1KTCYx(7); //Delay for ~1.53ms = 6120 Tcy
// Set cursor move direction
lcd_cmd(0x06);
Delay100TCYx(2); //Delay for ~40us = 160 Tcy
// Turn on display
lcd_cmd(0x0C);
Delay100TCYx(2); //Delay for ~40us = 160 Tcy
}
// Write string to LCD
void lcd_write(const char *message){
while(*message){
lcd_data(*message++);
}
}
// Select line
void lcd_line(unsigned char position){
//Line 1
if (position = 1){
lcd_cmd(0x80);
}
//Line 2
if (position = 2){
lcd_cmd(0xC0);
}
}
// Main code
void main(){
TRISA = 0x00;
TRISD = 0x00;
EN = 0;
RS = 0;
RW = 0;
PORTA = 0x00;
PORTD = 0x00;
Delay1KTCYx(1); // 1 ms delay
lcd_init();
while(1){
lcd_line(1);
lcd_write("Hello");
}
}
Last edited: