/********************************************************************
* FileName:        Sample LCD file.c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* This file controls an external LCD using the LCD Module
*
*	Wiring (this is important for you project LCD):
*		LCD side	- PIC side
*
*		1: Vss		- Ground
*		3: Vo		- 1.2ish volts (middle leg of pot)
*		5: RW pin	- PORTD pin RD5 (could just ground it if writing only)
*		7: DB0		- not used in 4 bit mode
*		9: DB2		- not used in 4 bit mode
*		11: DB4		- PORTD pin RD0
*		13: DB6		- PORTD pin RD2
*
*		2: Vdd		- Power (5 volts) - could've also used PORTD pin RD7
*		4: RS pin	- PORTD pin RD4
*		6: EN pin	- PORTD pin RD6
*		8: DB1		- not used in 4 bit mode
*		10: DB3		- not used in 4 bit mode
*		12: DB5		- PORTD pin RD1
*		14: DB7		- PORTD pin RD3
*
*   Explaining the PIC side lines with a few more words:
*
*	  PORTD RD7:RD4 are control/power lines to LCD module
*		PORTD.6 = LCD EN    : LCD enable signal
*		PORTD.5 = RD/WR		: LCD Read/Write operation, (Read = 1; Write = 0)
*		PORTD.4 = RS        : LCD Register Select, (Instruction Reg = 0; Data Reg = 1)
*
*	  PORTD RD3:RD0 are data lines for LCD Module -- LCD Module used in 4-bit mode
*		PORTD.3 = LCD D7
*		PORTD.2 = LCD D6
*		PORTD.1 = LCD D5
*		PORTD.0 = LCD D4
*
* Author               Date        Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// Naveen Raj			6/9/03	Original MPAM (Microchip Application Maestro) code
// David Fisher			2/1/08	Modified the Application Maestro XLCD code


/** Header Files ***************************************************/     
#include <p18f4520.h> 
#include <stdio.h>
#include "LCD_Module.h"

/** Define Constants Here ******************************************/
#define sample 100

/** Local Function Prototypes **************************************/
void low_isr(void);
void high_isr(void);
void sampleFunction(void);

// ============================================================
// Configuration Bits 
// ============================================================
#pragma config OSC = INTIO67  // Must setup OSCCON for 4MHz!
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF
#pragma config XINST = OFF

/** Declare Interrupt Vector Sections ****************************/
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
   _asm goto high_isr _endasm
}

#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
   _asm goto low_isr _endasm
}

/** Global Variables *********************************************/
	int sampleVariable;
	char line1[16];
	char line2[16];
	char variableString[] = "x=";
	
/*****************************************************************
* Function:        void main(void)
******************************************************************/
#pragma code
void main (void)
{
	// Everything in this XLCD module program must run at 4 MHz
	OSCCONbits.IRCF2 = 1;
	OSCCONbits.IRCF1 = 1;
	OSCCONbits.IRCF0 = 0;

	ADCON1= 0x0F;
	TRISD = 0x00;
	TRISC = 0x00;
	
	XLCDInit();			//initialize the LCD module
	XLCDClear();		// Clear LCD

	XLCDL1home();
	
		// example to display strings directly           
		XLCDPutRomString("    Direct text ");

	XLCDL1home();
		// example to display string variables		
		XLCDPutRamString(variableString);
		sprintf(line1,"%d",5);		// Fancy way to make a string variable
		XLCDPutRamString(line1);
	
	XLCDL2home();
	
		XLCDPut(4+48);
		XLCDPut('-');
		XLCDPut('P');
		XLCDPut('i');
		XLCDPut('n');
		XLCDPut(' ');
	    XLCDPut('M');
	    XLCDPut('o');
	    XLCDPut('d');
	    XLCDPut('e');
	
	// Feel free to play with the different LCD commands
	// These are additional commands that we 
	// didn't give you access to in the PICDEM 2 LCD module
	
	// Cursor mode
		//XLCDCursorOnBlinkOn();   	
		//XLCDCursorOnBlinkOff();   	
		XLCDDisplayOnCursorOff(); 			 	
		
	// Moving the cursor without printing
		XLCDCursorMoveRight();    	
		XLCDCursorMoveRight();    	
		XLCDPut('2');
		XLCDCursorMoveLeft();
		XLCDCursorMoveLeft();
		XLCDPut('1');
		XLCDCursorMoveRight();    	
		XLCDPut('3');
		
	// Moving the display (probably don't need, neat though)
		//XLCDDisplayMoveLeft();     	
		//XLCDDisplayMoveRight() ;
	
	while (1)
    {
		// This area loops forever
    }
}

/*****************************************************************
* Local functions go after main but before the interrupts if needed
******************************************************************/

/*****************************************************************
* Function:			void sample(void)
* Input Variables:	
* Output return:	
* Overview:			You change this template of a function if needed
******************************************************************/
void sampleFunction(){
	// Some function that does a specific task
}	



/*****************************************************************
* Function:        void high_isr(void)
* Purpose:			
******************************************************************/
#pragma interrupt high_isr
void high_isr(void)
{
	// High Priority Interrupt Service Routine (High ISR)
}

/******************************************************************
* Function:        void low_isr(void)
* Purpose:			
********************************************************************/
#pragma interruptlow low_isr
void low_isr(void)
{
	// Low Priority Interrupt Service Routine (Low ISR)
}