/*
    6/27/03
    Copyright Spark Fun Electronics 2003
    
    Tests the LCD on the PIC-MTA

    For the 28 Pin PIC16F873A-20MHz
    
    Thanks to Olimex for providing the original C code!
    
*/
#define HS_Osc //20MHz operations

#include "d:\Pics\c\16F873.h"  // device dependent interrupt definitions
#include "d:\Pics\code\Delay.c"   // Delays. What else?

#pragma config|= 0x3F32 //HS Ocs


//Hardware port definitions
//-------------------------------------------------------------------

#define		E		RA5
#define		R_W		RA3
#define		RS		RA2

//Constant definitions
//---------------------------------------------------------------------
#define 	LED_ON		0
#define		LED_OFF		1
#define		OFF			0
#define		ON			1

#define		DISP_ON			0b00001100	//LCD control constants
#define		DISP_OFF		0b00001000	//
#define		CLR_DISP		0b00000001	//
#define		CUR_HOME		0b00000010	//
#define		ENTRY_INC		0b00000110	//
#define		DD_RAM_ADDR		0b10000000	//
#define		DD_RAM_ADDR2	0b11000000	//
#define		CG_RAM_ADDR		0b01000000	//

//-----------------------------------------------------
void SEND_CHAR (unsigned char c)
{
    uns8 data_char;
    
    delay_ms(20);

	data_char = c & 0b11110000;		//get upper nibble	
	data_char = data_char >> 4;		//set D4-D7

	PORTC = data_char;			//send data to LCD
	R_W=0;				//set LCD to write
	RS=1;				//set LCD to data mode
	E=1;				//toggle E for LCD
	E=0;

	data_char = c & 0b00001111;
	PORTC = data_char;
	R_W=0;				//set LCD to write
	RS=1;				//set LCD to data mode
	E=1;				//toggle E for LCD
	E=0;
}

void SEND_CMD (unsigned char d)
{
    uns8 data_char;

    delay_ms(10);

	data_char = d & 0b11110000;		//get upper nibble	
	data_char = data_char >> 4;		//set D4-D7

	PORTC = data_char;			//send data to LCD
	R_W=0;				//set LCD to write
	RS=0;				//set LCD to data mode
	E=1;				//toggle E for LCD
	E=0;

	data_char = d & 0b00001111;
	PORTC = data_char;
	R_W=0;				//set LCD to write
	RS=0;				//set LCD to data mode
	E=1;				//toggle E for LCD
	E=0;
}
//-----------------------------------------------------


void main( void )

{
    //PORT DIRECTION INITIALIZATION
    //-------------------------------
	//RBPU=1;				//PORTB pull-ups are disabled
	T0CS=1;				//Transition on RA4/T0CKI pin
	T0SE=1;				//Increment on high-to-low transition on RA4/T0CKI pin
	PSA=1;				//Prescaler is assigned to the WDT	
	TMR0=0;   

	PORTA=0;

	ADCON1=0b.0000.1110;		//only RA0 is ADC input
	TRISA= 0b.1101.0001;

	CHS0=0;				//select first chanel
	CHS1=0;
	CHS2=0;	

	ADON=1;				//A/D converter module is operating
	ADFM=1;				//Right Justified. 6 most significant bits of ADRESH are read as ‘0’
	
	PORTB=0b.0011.1111;	
	TRISB=0b.1110.1111;   //0 = Output, 1 = Input

	PORTC=0b.0000.0000;
	TRISC=0b.1100.0000;   //0 = Output, 1 = Input
	
//--------------------init LCD after reset-------------

	RS=0;				
	R_W=0;

	delay_ms(1100);
	PORTC=0b.0000.0011;		
	E=1;
	E=0;

    delay_ms(10);
	PORTC=0b.0000.0011;
    E=1;
	E=0;

    delay_ms(10);
	PORTC=0b.0000.0011;
    E=1;
	E=0;

    delay_ms(10);
	PORTC=0b.0000.0010;
    E=1;
	E=0;

	SEND_CMD(DISP_ON);
	SEND_CMD(CLR_DISP);

//Send Text
//--------------- 

	SEND_CMD(CLR_DISP);
    SEND_CHAR('G');
    SEND_CHAR('o');
    SEND_CHAR(' ');
    SEND_CHAR('O');
    SEND_CHAR('l');
    SEND_CHAR('i');
    SEND_CHAR('m');
    SEND_CHAR('e');
    SEND_CHAR('x');
    SEND_CHAR('!');
    SEND_CHAR('L');
    SEND_CHAR('C');
    SEND_CHAR('D');
	SEND_CHAR('-');
	SEND_CHAR('O');
	SEND_CHAR('K');
    delay_ms(10000);
	SEND_CMD(CLR_DISP);
}





