
<font color=green>/*
| "LCDtest4.c"
| LCD command/data transmittal
| program for ATMEL (Temic)T80C51 
| microcontroller
======================================
| Tests operation of a connected 2x16
| LCD 44780-based display module, 8 bit 
| dat/cont. sent, 2 control bits:
| RS = control/data = 0/1 = P3.2
| E = enable = P3.4
| R/W* = P3.3
| P1 = output
| (P3 = output)
| IE = interrupts disabled
|--------------------------------------
| 8/1/04 Marty McLeod
| IDE: Keil uVision2 Eval V2.40
--------------------------------------*/</font>

<font color=blue>#include</font> <reg5101.h>


unsigned int i;
unsigned int j;

//Function prototypes here
void SEND_DATA_BYTE(void);
void SEND_CONT_BYTE(void);
void BUSYF_CHK_WAIT(void);

//Begin main of program

void <font color=blue>main</font>(void)
{
	// Initialization of 8051:
	IE = 0;         //Interrupts disabled
	P1 = 0x00;	//Set port 1 to OUTPUT
	P3 = 0x00;	//Set port 3, all bits, to OUTPUT
//
	SP = 0x40;	//Stack pointer at 40h
	PSW = 0;	//Clear program status word PSW
//
	P3_2 = 0;       //RS,
	P3_3 = 0;       //R/W,
	P3_4 = 0;       //& E cleared first
//
/*	Initialize LCD.....	*/
	//1: Function set
	//(8 bit data, 2 lines, font=5x8 pixels)
	P1 = 0x38;
	SEND_CONT_BYTE();
	//Now clear output
	P1 = 0x00;
	//Wait for not busy
	BUSYF_CHK_WAIT();

	//2: Clear LCD
	P1 = 0x01;
	SEND_CONT_BYTE();
	//Now clear output
	P1 = 0x00;
	//Wait for not busy
	BUSYF_CHK_WAIT();
	
	//3: Disable cursor
	P1 = 0x0C;
	SEND_CONT_BYTE();
	//Now clear output
	P1 = 0x00;
	//Wait for not busy
	BUSYF_CHK_WAIT();

	//4: Entry mode set
	//Set mode to 'right'
	P1 = 0x06;
	SEND_CONT_BYTE();
	//Now clear output
	P1 = 0x00;
	//Wait for not busy
	BUSYF_CHK_WAIT();

/* Data output section */
/* Characters sent are 'U' and 'A' */

	P1 = 0x55;	//Send 'U' ASCII to LCD
	SEND_DATA_BYTE();
	//Now clear output
	P1 = 0x00;
	//Wait for not busy
	BUSYF_CHK_WAIT();

	P1 = 0x41;	//Send 'A' ASCII to LCD
	SEND_DATA_BYTE();
	//Now clear output
	P1 = 0x00;
	//Wait for not busy
	BUSYF_CHK_WAIT();


	//Enter continous loop for test purpose
	/*-------END------*/

	while(1)
	{
	}
	
}<font color=green>//End of MAIN</font>

<font color=green>/** Define functions below **/</font>
void <font color=blue>SEND_CONT_BYTE(void)</font>
 {
   P3_2 = 0;
   P3_4 = 1;
   P3_4 = 0;
  }

void <font color=blue>SEND_DATA_BYTE(void)</font>
 {
   P3_2 = 1;
   P3_4 = 1;
   P3_4 = 0;
   //
   //Now reset to Control format on RS bit..
   P3_2 = 0;
 }
	
void <font color=blue>BUSYF_CHK_WAIT(void)</font>
 {
   //Set P1 to input
   P1 = 0xFF;
   //Set R/W = 1 (read mode)
   P3_3 = 1;	   
   //Wait for not busy
   while(P1_7)
	{
	}
   //Set P1 back to output
   P1 = 0x00;
   //Clear R/W
   P3_3 = 0;	
  }
/** END OF FILE **/
