Basic P18f252 wrighting to an LCD screen HD44780 16x4

Status
Not open for further replies.

richie244

Newbie level 2
Joined
Apr 27, 2009
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,295
#include <p18f252.h>

I am using MPLAB v8.10 and am trying to innitialise an LCD display and write something to it just to prove I have control pf it via the pic in 8 bit mode. It will bviously be part of a larger program eventualy and I have also written the program for A/D as shown.
At the moment nothing appears on the LCD and I can see where in the program I hae gone wrong. Please help


Code:
#include <p18f252.h>
#include <stdlib.h>
#include <string.h>
#include <delays.h>


void Initial()
{
	TRISA = 0xFF; // port A, all bits, inputs
	TRISB = 0x00;

	ADCON1 = 0b10000000; //AD port configuration
					     //AAAAAAAA VDD VSS
	ADCON0bits.ADCS1=1;
	ADCON0bits.ADCS0=0;  // ADC clock = Fsoc/32
	ADCON0bits.CHS2 =0;
	ADCON0bits.CHS1 =0;
}

unsigned char readADC(int ch){
	unsigned char adcValue;

	if(ch == 0) ADCON0bits.CHS0 =0;  // select channel 0
	else        ADCON0bits.CHS0 =1;

	ADCON0bits.ADON =1;  // turn on ADC
	ADCON0bits.GO_DONE = 1; 	// start conversion
 	while(ADCON0bits.GO_DONE) 	// wait for conversion end
 		;
    adcValue = ADRESH; 
	return adcValue;
}
void LcdEnable();


/*****************************************
* Display
/*****************************************/
   #define   lcd_rs   PORTCbits.RC2
   #define   lcd_rw   PORTCbits.RC3
   #define   lcd_e   PORTCbits.RC4
   #define   lcd_cmd_wri   0x00
   #define   lcd_data_wri   0x01
   #define   lcd_busy_rd   0x02
   #define   lcd_set_function   0x3C   //00111100 8 bit interface, 4 line mode, 5x11 dot format
   #define   lcd_set_visible   0x0F    //Display on, cursor underline on, cursor blink on
   #define   lcd_set_shift   0x16      //Cursor move, Right shift
   #define   lcd_set_mode   0x06       //Increment, display shift off
   #define   lcd_set_cgaddr   0x40
   #define   lcd_set_ddaddr   0x80
   #define   lcd_clr   0x01
   #define   lcd_init   0x30
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   int i,j;	
   const int Twentyms = 640000;	
   const int Tenms = 320000;
   const int Onetwofivems = 4000000;
   const int Twohundredus = 6400;
   const int Onehundredus = 3200;
   const int Sixtyfourms = 2048000;	   
	

void WrCmd2Lcd(char cmd);
void WrDat2Lcd(char data);
void InitLcd();
void Speech(char words,int position);
void LcdWait();
void LcdPutPar(int par); 

void LcdEnable()
{
   lcd_e=1;   
   j=Tenms;
   for (i = 0; i < j; i++); //delay for 10ms
   lcd_e=0;
}
void WrCmd2Lcd(char cmd)
{
   TRISB = 0x00;
   lcd_rw=0;
   lcd_rs=1;
   PORTB=cmd;
   LcdEnable();
}
void WrDat2Lcd(char data)
{
   TRISB =0x00;
   lcd_rw=0;
   lcd_rs=0;
   PORTB = data;
   LcdEnable();
   
}

void InitLcd()
{
   lcd_e=0;
   j=Onetwofivems;
   for (i = 0; i < j; i++); //delay for 125 ms
   WrCmd2Lcd(lcd_init);
   j=Twentyms;
   for (i = 0; i < j; i++); //delay for 20ms		
   LcdEnable();
   j=Twohundredus;
   for (i = 0; i < j; i++); //delay for 200us
   LcdEnable();
   j=Onehundredus;
   for (i = 0; i < j; i++); //delay for 100us
   WrCmd2Lcd(lcd_set_function);
   j=Tenms;
   for (i = 0; i < j; i++); //delay for 10ms
   WrCmd2Lcd(lcd_set_visible);
   j=Tenms;
   for (i = 0; i < j; i++); //delay for 10ms
   WrCmd2Lcd(lcd_clr);
   j=Onehundredus;
   for (i = 0; i < j; i++); //delay for 100us
   WrCmd2Lcd(lcd_set_mode);
   j=Tenms;
   for (i = 0; i < j; i++); //delay for 10ms
   WrCmd2Lcd(lcd_set_ddaddr);
   j=Onehundredus;
   for (i = 0; i < j; i++); //delay for 100us
}

void LcdWait()
{
   int status;
   TRISB=0xFF;
   TRISB = lcd_busy_rd;
   do
   {
      lcd_e=1;
      status = ADCON1;
      lcd_e=0;
   }
   while(status & 0x80);   // test busy flag.
}

void Speech(char words,int position)
{
   



char *textptr;
   //textptr = words;
   WrCmd2Lcd(position);
   LcdWait();
   j=Sixtyfourms;
   for (i = 0; i < j; i++); //delay for 64ms
   do
   {
   WrDat2Lcd(*textptr);
   *textptr++;
   }
   while(*textptr != '\n');
}

void LcdPutPar(int par)
{
    TRISBbits.TRISB0 = PORTBbits.RB0,par & 0X01;
    TRISBbits.TRISB1 = PORTBbits.RB1,par & 0X02;
    TRISBbits.TRISB2 = PORTBbits.RB2,par & 0X04;
    TRISBbits.TRISB3 = PORTBbits.RB3,par & 0X08;
 	TRISBbits.TRISB0 = PORTBbits.RB4,par & 0X10;
    TRISBbits.TRISB1 = PORTBbits.RB5,par & 0X20;
    TRISBbits.TRISB2 = PORTBbits.RB6,par & 0X40;
    TRISBbits.TRISB3 = PORTBbits.RB7,par & 0X80;
} 

void main(void) {
	unsigned char voltage1, voltage2;
	while(1){
//	 	voltage1 = readADC(0);
//	 	voltage2 = readADC(1);
		InitLcd();
//		WrCmd2Lcd(0X06);
//		LcdEnable();
  		WrDat2Lcd("A"); 
//		LcdWait();
		while(1);
	}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…