//************************************************************************************
//**  
//**  File name:     E:\EEPROM STORING\Storing and retrieving INT variables.c
//**  Generated by:  Flowcode v4.2.3.58
//**  Date:          Friday, April 23, 2010 17:40:37
//**  Licence:       Professional
//**  Registered to: SAHYADRI ELECTRO CONTROLS 
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Defines for microcontroller
#define P16F946
#define MX_EE
#define MX_EE_SIZE 256
#define MX_SPI
#define MX_SPI_C
#define MX_SPI_SDI 7
#define MX_SPI_SDO 4
#define MX_SPI_SCK 6
#define MX_UART
#define MX_UART_C
#define MX_UART_TX 6
#define MX_UART_RX 7
#define MX_I2C
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 7
#define MX_I2C_SCL 6
#define MX_PWM
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 trisc
#define MX_PWM_1 5
#define MX_PWM_TRIS2 trisd
#define MX_PWM_2 2

//Functions
#define MX_CLK_SPEED 8000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x2007, 0x30f5
#endif
#ifdef HI_TECH_C
__CONFIG(0x30f5);
#endif

//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Macro function declarations


//Variable declarations
char FCV_DATA_OUT2;
short FCV_DIN;
short FCV_DOUT;
char FCV_DATA_OUT;
char FCV_DATA_IN2;
char FCV_DATA_IN;


//Defines:

/**** Macro Substitutions ****
******************************/




//EEPROM0: //Macro function declarations

char FCD_EEPROM0_EEPROMRead(short addr);
void FCD_EEPROM0_WriteEEPROM(short addr, char data);
//Defines:

/**** Macro Substitutions ****
portb = D1 Port
trisb = D1 Data Direction
portb = D2 Port
trisb = D2 Data Direction
portb = D3 Port
trisb = D3 Data Direction
portb = D4 Port
trisb = D4 Data Direction
portb = RS Port
trisb = RS Data Direction
portb = E Port
trisb = E Data Direction
0 = Data 1_Pin
1 = Data 2 Pin
2 = Data 3 Pin
3 = Data 4 Pin
4 = RS Pin
5 = Enable Pin
LCD_4523324 = Unique Component Reference Number
2 = Row Count
16 = Column Count
******************************/

	//component connections
	#define LCD_4523324_PORT0    portb
	#define LCD_4523324_TRIS0    trisb
	#define LCD_4523324_PORT1    portb
	#define LCD_4523324_TRIS1    trisb
	#define LCD_4523324_PORT2    portb
	#define LCD_4523324_TRIS2    trisb
	#define LCD_4523324_PORT3    portb
	#define LCD_4523324_TRIS3    trisb
	#define LCD_4523324_PORT4    portb
	#define LCD_4523324_TRIS4    trisb
	#define LCD_4523324_PORT5    portb
	#define LCD_4523324_TRIS5    trisb
	#define LCD_4523324_BIT0    	0
	#define LCD_4523324_BIT1    	1
	#define LCD_4523324_BIT2    	2
	#define LCD_4523324_BIT3    	3
	#define LCD_4523324_RS      	4
	#define LCD_4523324_E       	5
	#define LCD_4523324_ROWCNT	2
	#define LCD_4523324_COLCNT	16

	#ifdef _BOOSTC
	  #define LCD_4523324_DELAY   delay_10us(10)
	#endif
	#ifdef _C2C_
	  #define LCD_4523324_DELAY   delay_us(100)
	#endif
	#ifdef HI_TECH_C
	  #define LCD_4523324_DELAY   __delay_us(120)
	#endif
	#ifndef LCD_4523324_DELAY
	  #define LCD_4523324_DELAY   delay_us(100)
	#endif




//LCDDisplay0: //Macro function declarations

void FCD_LCDDisplay0_RawSend(char in, char mask);
void FCD_LCDDisplay0_Start();
void FCD_LCDDisplay0_Clear();
void FCD_LCDDisplay0_PrintASCII(char Character);
void FCD_LCDDisplay0_Command(char in);
void FCD_LCDDisplay0_Cursor(char x, char y);
void FCD_LCDDisplay0_PrintNumber(short Number);
void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String);
void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions);
void FCD_LCDDisplay0_ClearLine(char Line);
void FCD_LCDDisplay0_RAM_Write(char nIdx, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7);



//EEPROM0: //Macro implementations


char FCD_EEPROM0_EEPROMRead(short addr)
{
		char data = 0;

		#ifdef _BOOSTC
			#ifdef MX_EE
				#ifndef MX_EEADR
					#ifdef EEADR
						#define MX_EEADR eeadr
					#endif
					#ifdef EEADRL
						#define MX_EEADR eeadrl
					#endif
				#endif
				#ifndef MX_EEDATA
					#ifdef EEDATA
						#define MX_EEDATA eedata
					#endif
					#ifdef EEDATAL
						#define MX_EEDATA eedatal
					#endif
					#ifdef EEDATL
						#define MX_EEDATA eedatl
					#endif
				#endif

				#ifndef MX_EEADR	//Catch Error
					  #pragma error EEPROM error - MX_EEADR not set
				#endif

				#ifndef MX_EEDATA
					  #pragma error EEPROM error - MX_EEDATA not set
				#endif

				MX_EEADR = addr & 0xff;
				#if (MX_EE_SIZE > 256)
					eeadrh = (addr & 0xff00) >> 8;
				#endif
				#ifdef MX_EE_TYPE2
					clear_bit(eecon1, EEPGD);
				#endif
				#ifdef MX_EE_TYPE3
					clear_bit(eecon1, EEPGD);
					clear_bit(eecon1, CFGS);
				#endif
				set_bit(eecon1, RD);
				data = MX_EEDATA;
			#else
				#ifdef MX_EE_TYPE67X
					#pragma error "FlowCode does not support EEPROM for this device"
				#else
					#ifdef MX_EE_TYPE62X
						#pragma error "FlowCode does not support EEPROM for this device"
					#else
						#pragma error "Chip does not have EEPROM memory"
					#endif
				#endif
			#endif
		#endif
		#ifdef HI_TECH_C
			data = eeprom_read(addr);
		#endif

		return (data);

}

void FCD_EEPROM0_WriteEEPROM(short addr, char data)
{
		#ifdef _BOOSTC
			#ifdef MX_EE
				#ifndef MX_EEADR
					#ifdef EEADR
						#define MX_EEADR eeadr
					#endif
					#ifdef EEADRL
						#define MX_EEADR eeadrl
					#endif
				#endif
				#ifndef MX_EEDATA
					#ifdef EEDATA
						#define MX_EEDATA eedata
					#endif
					#ifdef EEDATAL
						#define MX_EEDATA eedatal
					#endif
				#endif

				#ifndef MX_EEADR	//Catch Error
					  #pragma error EEPROM error - MX_EEADR not set
				#endif

				#ifndef MX_EEDATA
					  #pragma error EEPROM error - MX_EEDATA not set
				#endif

				char bInterruptsEnabled;

				//wait for previous EE writes to complete...
				while (test_bit(eecon1, WR));

				MX_EEADR = addr & 0xff;
				#if (MX_EE_SIZE > 256)
					eeadrh = (addr & 0xff00) >> 8;
				#endif
				MX_EEDATA = data;
				#ifdef MX_EE_TYPE2
					clear_bit(eecon1, EEPGD);
				#endif
				#ifdef MX_EE_TYPE3
					clear_bit(eecon1, EEPGD);
					clear_bit(eecon1, CFGS);
				#endif
				set_bit(eecon1, WREN);
				bInterruptsEnabled = test_bit(intcon, GIE);
				clear_bit(intcon, GIE);			//Disable Interrupts

				eecon2 = 0x55;
				eecon2 = 0xAA;
				set_bit(eecon1, WR);
				while (test_bit(eecon1, WR));	//wait for EE write to complete...

				if (bInterruptsEnabled)
					set_bit(intcon, GIE);		//Re-enable Interrupts
				clear_bit(eecon1, WREN);
			#else
				#ifdef MX_EE_TYPE67X
					#pragma error FlowCode does not support EEPROM for this device
				#else
					#ifdef MX_EE_TYPE62X
						#pragma error FlowCode does not support EEPROM for this device
					#else
						#pragma error Chip does not have EEPROM memory
					#endif
				#endif
			#endif
		#endif
		#ifdef HI_TECH_C
			eeprom_write(addr, data);
		#endif

}



//LCDDisplay0: //Macro implementations


void FCD_LCDDisplay0_RawSend(char in, char mask)
{
		unsigned char pt;

		clear_bit(LCD_4523324_PORT0, LCD_4523324_BIT0);
		clear_bit(LCD_4523324_PORT1, LCD_4523324_BIT1);
		clear_bit(LCD_4523324_PORT2, LCD_4523324_BIT2);
		clear_bit(LCD_4523324_PORT3, LCD_4523324_BIT3);
		clear_bit(LCD_4523324_PORT4, LCD_4523324_RS);
		clear_bit(LCD_4523324_PORT5, LCD_4523324_E);
		pt = ((in >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(LCD_4523324_PORT0, LCD_4523324_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_4523324_PORT1, LCD_4523324_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_4523324_PORT2, LCD_4523324_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_4523324_PORT3, LCD_4523324_BIT3);
		if (mask)
		    set_bit(LCD_4523324_PORT4, LCD_4523324_RS);
		LCD_4523324_DELAY;
		set_bit (LCD_4523324_PORT5, LCD_4523324_E);
		LCD_4523324_DELAY;
		clear_bit (LCD_4523324_PORT5, LCD_4523324_E);
		pt = (in & 0x0f);
		LCD_4523324_DELAY;
		clear_bit(LCD_4523324_PORT0, LCD_4523324_BIT0);
		clear_bit(LCD_4523324_PORT1, LCD_4523324_BIT1);
		clear_bit(LCD_4523324_PORT2, LCD_4523324_BIT2);
		clear_bit(LCD_4523324_PORT3, LCD_4523324_BIT3);
		clear_bit(LCD_4523324_PORT4, LCD_4523324_RS);
		clear_bit(LCD_4523324_PORT5, LCD_4523324_E);
		if (pt & 0x01)
		    set_bit(LCD_4523324_PORT0, LCD_4523324_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_4523324_PORT1, LCD_4523324_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_4523324_PORT2, LCD_4523324_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_4523324_PORT3, LCD_4523324_BIT3);
		if (mask)
		    set_bit(LCD_4523324_PORT4, LCD_4523324_RS);
		LCD_4523324_DELAY;
		set_bit (LCD_4523324_PORT5, LCD_4523324_E);
		LCD_4523324_DELAY;
		clear_bit (LCD_4523324_PORT5, LCD_4523324_E);
		LCD_4523324_DELAY;
}

void FCD_LCDDisplay0_Start()
{
	
		clear_bit(LCD_4523324_TRIS0, LCD_4523324_BIT0);
		clear_bit(LCD_4523324_TRIS1, LCD_4523324_BIT1);
		clear_bit(LCD_4523324_TRIS2, LCD_4523324_BIT2);
		clear_bit(LCD_4523324_TRIS3, LCD_4523324_BIT3);
		clear_bit(LCD_4523324_TRIS4, LCD_4523324_RS);
		clear_bit(LCD_4523324_TRIS5, LCD_4523324_E);

		Wdt_Delay_Ms(12);

		FCD_LCDDisplay0_RawSend(0x33, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x33, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x32, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x2c, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x06, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x0c, 0);
		Wdt_Delay_Ms(2);

		//clear the display
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_Clear()
{
	
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_PrintASCII(char Character)
{
	
		FCD_LCDDisplay0_RawSend(Character, 0x10);

}

void FCD_LCDDisplay0_Command(char in)
{
	
		FCD_LCDDisplay0_RawSend(in, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_Cursor(char x, char y)
{
	
	  #if (LCD_4523324_ROWCNT == 1)
	    y=0x80;
	  #endif

	  #if (LCD_4523324_ROWCNT == 2)
		if (y==0)
			y=0x80;
		else
			y=0xc0;
	  #endif

	  #if (LCD_4523324_ROWCNT == 4)
		if (y==0)
			y=0x80;
		else if (y==1)
			y=0xc0;

		#if (LCD_4523324_COLCNT == 16)
			else if (y==2)
				y=0x90;
			else
				y=0xd0;
		#endif

		#if (LCD_4523324_COLCNT == 20)
			else if (y==2)
				y=0x94;
			else
				y=0xd4;
		#endif
	  #endif

		FCD_LCDDisplay0_RawSend(y+x, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_PrintNumber(short Number)
{
	
		short tmp_int;
		char tmp_byte;
		if (Number < 0)
		{
			FCD_LCDDisplay0_RawSend('-', 0x10);
			Number = 0 - Number;
		}

		tmp_int = Number;
		if (Number >= 10000)
		{
			tmp_byte = tmp_int / 10000;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10000;
				tmp_byte--;
			}
		}
		if (Number >= 1000)
		{
			tmp_byte = tmp_int / 1000;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 1000;
				tmp_byte--;
			}
		}
		if (Number >= 100)
		{
			tmp_byte = tmp_int / 100;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 100;
				tmp_byte--;
			}
		}
		if (Number >= 10)
		{
			tmp_byte = tmp_int / 10;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10;
				tmp_byte--;
			}
		}
		FCD_LCDDisplay0_RawSend('0' + tmp_int, 0x10);

}

void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String)
{
	
		char idx = 0;

		for (idx=0; idx<MSZ_String; idx++)
		{
			#ifdef _BOOSTC
				if (String[idx] == 0)
				{
					break;
				}
				FCD_LCDDisplay0_RawSend(String[idx], 0x10);
			#endif

			#ifdef HI_TECH_C
				if (*String == 0)
				{
					break;
				}
				FCD_LCDDisplay0_RawSend(*String, 0x10);
				String++;
			#endif
		}

}

void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions)
{
	
		char cmd = 0;
		char count;

		//Choose the direction
		switch (Direction)
		{
			case 0:
			case 'l':
			case 'L':

				cmd = 0x18;
				break;

			case 1:
			case 'r':
			case 'R':

				cmd = 0x1C;
				break;

			default:
				break;
		}

		//If direction accepted then scroll the specified amount
		if (cmd)
		{
			for (count = 0; count < Num_Positions; count++)
				FCD_LCDDisplay0_Command(cmd);
		}

}

void FCD_LCDDisplay0_ClearLine(char Line)
{
	
		char count;
		char rowcount;

		//Define number of columns per line
		#if (LCD_4523324_ROWCNT == 1)
			rowcount=80;
		#endif

		#if (LCD_4523324_ROWCNT == 2)
			rowcount=40;
		#endif

		#if (LCD_4523324_ROWCNT == 4)
			#if (LCD_4523324_COLCNT == 16)
				rowcount=16;
			#endif
			#if (LCD_4523324_COLCNT == 20)
				rowcount=20;
			#endif
		#endif

		//Start at beginning of the line
		FCD_LCDDisplay0_Cursor (0, Line);

		//Send out spaces to clear line
		for (count = 0; count < rowcount; count++)
			FCD_LCDDisplay0_RawSend(' ', 0x10);

		//Move back to the beginning of the line.
		FCD_LCDDisplay0_Cursor (0, Line);

}

void FCD_LCDDisplay0_RAM_Write(char nIdx, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7)
{
	   //set CGRAM address
	   FCD_LCDDisplay0_RawSend(64 + (nIdx << 3), 0);
	   delay_ms(2);

	   //write CGRAM data
	   FCD_LCDDisplay0_RawSend(d0, 0x10);
	   FCD_LCDDisplay0_RawSend(d1, 0x10);
	   FCD_LCDDisplay0_RawSend(d2, 0x10);
	   FCD_LCDDisplay0_RawSend(d3, 0x10);
	   FCD_LCDDisplay0_RawSend(d4, 0x10);
	   FCD_LCDDisplay0_RawSend(d5, 0x10);
	   FCD_LCDDisplay0_RawSend(d6, 0x10);
	   FCD_LCDDisplay0_RawSend(d7, 0x10);

	   //Clear the display
	   FCD_LCDDisplay0_RawSend(0x01, 0);
	   delay_ms(2);
	   FCD_LCDDisplay0_RawSend(0x02, 0);
	   delay_ms(2);
}

//Macro implementations

void main()
{
	
	//Initialisation
	ansel = 0x00;
cmcon0 = 0x07;


	//Interrupt initialisation code
	option_reg = 0xC0;


	//Startup LCD
	//Call Component Macro: LCDDisplay(0)::Start
	FCD_LCDDisplay0_Start();


	//Initialise the data to be stored
	//Calculation:
	//  DIN = 1024
	FCV_DIN = 1024;
	

	//Call Component Macro
	//Call Component Macro: EEPROM(0)::Write(0, DIN)
	FCD_EEPROM0_WriteEEPROM(0, FCV_DIN);


	//Print out data in
	//Call Component Macro: LCDDisplay(0)::PrintNumber(DIN)
	FCD_LCDDisplay0_PrintNumber(FCV_DIN);


	//Split the INT variable into Bytes
	//Calculation:
	//  Data_In = DIN & 0xFF
	//  Data_In2 = DIN >> 8
	FCV_DATA_IN = FCV_DIN & 0xFF;
	FCV_DATA_IN2 = FCV_DIN >> 8;
	

	//Move LCD Cursor to 2nd Line
	//Call Component Macro: LCDDisplay(0)::Cursor(0, 1)
	FCD_LCDDisplay0_Cursor(0, 1);


	//Recombine the bytes into an INT
	//Calculation:
	//  DOUT = Data_Out2 << 8
	//  DOUT = DOUT | Data_Out
	FCV_DOUT = FCV_DATA_OUT2 << 8;
	FCV_DOUT = FCV_DOUT | FCV_DATA_OUT;
	

	//Print out data out
	//Call Component Macro: LCDDisplay(0)::PrintNumber(DOUT)
	FCD_LCDDisplay0_PrintNumber(FCV_DOUT);


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}



