//-----------------------------------------------------------------------------------------------------//
// PROJECT NAME    - UART
// AUTHOR MAIL     - bhau2727@gmail.com
// DATE OF ALTERED - 27/7/2011
// HARDWARE        - PCF 8591 WITH P89V51RD2 BASED 
// SOFTWARE        - INPUTS   HIGH LEVEL - 1 // LOW LEVEL - 0 //CONNECT PC TO MC AND OPEN HYPER TERMINAL,U CAN RECIVE
//                   HELLO WORLD! FROM MC
//-----------------------------------------------------------------------------------------------------//

//----------------------------------HEADER Declarations--------------------------------------//
#include <reg51.h>
#include <string.h>

//----------------------------------lcd Declarations--------------------------------------//

void initlcd(void);  //intiate lcd
void cmd_lcd(unsigned char command);
void data_lcd(unsigned char data2lcd);
void lcd_shortdelay(void);
void delay_ms(unsigned char milliseconds);
void printlcd(unsigned char *message, unsigned char row, unsigned char col);
#define LCD_PORT P1
sbit enable=P3^4;
sbit regsel=P3^5;

//----------------------------------uart Declarations--------------------------------------//

void inituart(void);
void Tx_Data(unsigned char);
void SCI_String_Tx(unsigned char *string);

//----------------------------------main function--------------------------------------//

void main(void)
{
	initlcd();
	inituart();
	while(1)
	{
		cmd_lcd(0x0C);
		printlcd("SENDING DATA..",0,0);//print in frist line
		delay_ms(1000);
		SCI_String_Tx("HELLO WORLD!"); //send data to terminal
		printlcd("HELLO WORLD!",1,0);  //print in second line
		delay_ms(1000);
		cmd_lcd(0x01);
		delay_ms(10);
		
	}
}

//----------------------------------uart functions--------------------------------------//

void inituart()
{
	TMOD=0X21;
	SCON=0X50;
	TH1=0XFD;
	TR1=1;
	
}

void Tx_Data(unsigned char v)
{
  	 SBUF=v;
	 while(TI==0);
	 TI=0;
}


void SCI_String_Tx(unsigned char *string) 
{
	while(*string)
	{
		Tx_Data(*(string++));
	}
}

//----------------------------------lcd functions--------------------------------------//

void initlcd()
{
	enable=0;
	delay_ms(20);
	cmd_lcd(0x30);
	delay_ms(5);
	cmd_lcd(0x30);
	cmd_lcd(0x30);
	cmd_lcd(0x38);
	cmd_lcd(0x08);
//	cmd_lcd(0x01);
	cmd_lcd(0x06);
	cmd_lcd(0x0e);
}

void cmd_lcd(unsigned char command)
{
	LCD_PORT=command;
	regsel=0;
	enable=1;
	enable=0;
	lcd_shortdelay();
}

void data_lcd(unsigned char databyte)
{	
	LCD_PORT=databyte;
	regsel=1;
	enable=1;
	enable=0;
	lcd_shortdelay();
}

void printlcd(unsigned char *message, unsigned char row, unsigned char col)
{
	cmd_lcd(0x80 + (0x40*row) + col);
	
	for(; *message; message++)
		data_lcd(*message);
	
}

void lcd_shortdelay(void)
{
	unsigned char t=23;
	while(t--);
}
	
void delay_ms(unsigned char ms)
{
	unsigned char t1;
	unsigned int t2;

	for(t1=0; t1<ms; t1++){
		for(t2=0; t2<711; t2++);
	}
}