Hi all,
I have been stuck lately with classes, course projects and assignments so I couldn't make much progress on the personal projects front. I recently bought a PICKit 2 clone USB ICSP programmer and have been trying to get an LCD working on my breadboard. Here's the code
I have single-stepped through the code in hardware with no results. I have been programming 8051 chips for my microcontroller course recently and seem to have forgotten some PIC basics so kindly excuse any apparent stupid mistake. I did consider that there might be something wrong with the hardware setup as well as the LCD but I just wanted to be sure of the firmware first.
Thanks!
I have been stuck lately with classes, course projects and assignments so I couldn't make much progress on the personal projects front. I recently bought a PICKit 2 clone USB ICSP programmer and have been trying to get an LCD working on my breadboard. Here's the code
Rich (BB code):
//main.c
//Driver software for LCD
#include<htc.h>
#define _XTAL_FREQ 20000000
__CONFIG(FOSC_HS & LVP_OFF & CP_OFF & CP_OFF & BOREN_OFF & DEBUG_OFF & WDTE_OFF & PWRTE_OFF);
#define DATA PORTC //data bus
#define BUSY RC7 //busy flag
#define ENABLE RD0 //enable signal
#define RW RD1 //read/write signal (R=1/W=0)
#define RS RD2 //register select (Text=1, Instructions=0)
#define SETBIT(x) x=x||1
#define CLRBIT(x) x=x&&0
void Push() //push data into the LCD
{
SETBIT(ENABLE);
CLRBIT(ENABLE);
}
void LCD_Busy()
{
TRISC7=1; //RC7 as input
CLRBIT(ENABLE);
SETBIT(RW); //read mode
CLRBIT(RS); //instructions
while(1) //code execution halted until LCD is busy
{
SETBIT(ENABLE);
if(!BUSY)
break;
CLRBIT(ENABLE);
}
CLRBIT(ENABLE);
CLRBIT(RW); //write mode
TRISC7=0;
}
void Init()
{
TRISC=0x00; //PORTC as output
TRISD0=0;
TRISD1=0;
TRISD2=0; //control signals
}
void LCD_Init()
{
CLRBIT(ENABLE); //deactivate LCD
SETBIT(RW); //write mode
CLRBIT(RS); //write instructions
//start sending instructions to the LCD
DATA=0x38; //initialize two row display
Push(); //push data onto LCD
LCD_Busy(); //wait until LCD processes it
DATA=0x1; //clear LCD
Push();
LCD_Busy();
DATA=0xE; //turn cursor ON
Push();
LCD_Busy();
DATA=0x6; //move cursor right automatically
Push();
LCD_Busy();
}
void LCD_Write(unsigned char ch)
{
CLRBIT(ENABLE);
SETBIT(RW); //write mode
SETBIT(RS); //write text
DATA=ch;
Push();
LCD_Busy();
}
void LCD_Clear()
{
CLRBIT(ENABLE); //deactivate LCD
SETBIT(RW); //write mode
CLRBIT(RS); //write instructions
DATA=0x1; //clear LCD
Push();
LCD_Busy();
}
void main()
{
Init();
LCD_Init();
LCD_Write('H');
LCD_Write('E');
LCD_Write('L');
LCD_Write('L');
LCD_Write('O');
LCD_Write('!');
}
Thanks!