//************************************************************************************
//**  
//**  File name:     C:\Documents and Settings\Sean\My Documents\My Flowcode\PIC\Encoder\Encoder2.c
//**  Generated by:  Flowcode v3.4.7.48
//**  Date:          Monday, May 12, 2008 09:22:53
//**  Licence:       Professional
//**  Registered to: Michael Sean King
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

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

//Functions
#include <system.h>
#pragma CLOCK_FREQ 19660800

//Configuration data

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

//Macro function declarations


//Variable declarations
char FCV_DFLTCON;
short FCV_POSCNT;
short FCV_MAXCNT;
char FCV_MAXCNTH;
char FCV_POSCNTH;
char FCV_MAXCNTL;
char FCV_POSCNTL;




//LCDDisplay0: //Macro function declarations

void FCD_LCDDisplay0_GetDefines();
void FCD_LCDDisplay0_Start();
void FCD_LCDDisplay0_Clear();
void FCD_LCDDisplay0_PrintASCII(char Character);
void FCD_LCDDisplay0_Command(char in);
void FCD_LCDDisplay0_RawSend(char in, char mask);
void FCD_LCDDisplay0_Cursor(char x, char y);
void FCD_LCDDisplay0_PrintNumber(short Number);
void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String);

//Supplementary defines




//LCDDisplay0: //Macro implementations


void FCD_LCDDisplay0_GetDefines()
{
	
	}  //Dummy end of function to allow defines to be added correctly

	//component connections
	#define LCD_197626_PORT    portb
	#define LCD_197626_TRIS    trisb
	#define LCD_197626_BIT0    0
	#define LCD_197626_BIT1    1
	#define LCD_197626_BIT2    2
	#define LCD_197626_BIT3    3
	#define LCD_197626_RS      4
	#define LCD_197626_E       5

	#ifdef _BOOSTC
	  #define LCD_197626_DELAY   delay_10us(10)
	#endif
	#ifdef _C2C_
	  #define LCD_197626_DELAY   delay_us(100)
	#endif
	#ifndef LCD_197626_DELAY
	  #define LCD_197626_DELAY   delay_us(100)
	#endif



	//internal function prototypes
	void LCD_197626_RawSend(char nIn, char nMask);

	//internal function implementations
	void LCD_197626_RawSend(char nIn, char nMask)
	{
		unsigned char pt;
		unsigned char outVal;
		outVal = LCD_197626_PORT;
		clear_bit(outVal, LCD_197626_BIT0);
		clear_bit(outVal, LCD_197626_BIT1);
		clear_bit(outVal, LCD_197626_BIT2);
		clear_bit(outVal, LCD_197626_BIT3);
		clear_bit(outVal, LCD_197626_RS);
		clear_bit(outVal, LCD_197626_E);
		pt = ((nIn >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(outVal, LCD_197626_BIT0);
		if (pt & 0x02)
		    set_bit(outVal, LCD_197626_BIT1);
		if (pt & 0x04)
		    set_bit(outVal, LCD_197626_BIT2);
		if (pt & 0x08)
		    set_bit(outVal, LCD_197626_BIT3);
		if (nMask)
		    set_bit(outVal, LCD_197626_RS);
		LCD_197626_PORT = outVal;
		LCD_197626_DELAY;
		set_bit (LCD_197626_PORT, LCD_197626_E);
		LCD_197626_DELAY;
		clear_bit (LCD_197626_PORT, LCD_197626_E);
		pt = (nIn & 0x0f);
		LCD_197626_DELAY;
		outVal = LCD_197626_PORT;
		clear_bit(outVal, LCD_197626_BIT0);
		clear_bit(outVal, LCD_197626_BIT1);
		clear_bit(outVal, LCD_197626_BIT2);
		clear_bit(outVal, LCD_197626_BIT3);
		clear_bit(outVal, LCD_197626_RS);
		clear_bit(outVal, LCD_197626_E);
		if (pt & 0x01)
		    set_bit(outVal, LCD_197626_BIT0);
		if (pt & 0x02)
		    set_bit(outVal, LCD_197626_BIT1);
		if (pt & 0x04)
		    set_bit(outVal, LCD_197626_BIT2);
		if (pt & 0x08)
		    set_bit(outVal, LCD_197626_BIT3);
		if (nMask)
		    set_bit(outVal, LCD_197626_RS);
		LCD_197626_PORT = outVal;
		LCD_197626_DELAY;
		set_bit (LCD_197626_PORT, LCD_197626_E);
		LCD_197626_DELAY;
		clear_bit (LCD_197626_PORT, LCD_197626_E);
		LCD_197626_DELAY;
	}

	// Dummy function to close the defines section off
	void LCD_197626_Dummy_Function();
	void LCD_197626_Dummy_Function()
	{

}

void FCD_LCDDisplay0_Start()
{
	
		clear_bit(LCD_197626_TRIS, LCD_197626_BIT0);
		clear_bit(LCD_197626_TRIS, LCD_197626_BIT1);
		clear_bit(LCD_197626_TRIS, LCD_197626_BIT2);
		clear_bit(LCD_197626_TRIS, LCD_197626_BIT3);
		clear_bit(LCD_197626_TRIS, LCD_197626_RS);
		clear_bit(LCD_197626_TRIS, LCD_197626_E);

		delay_ms(12);

		LCD_197626_RawSend(0x33, 0);
		delay_ms(2);
		LCD_197626_RawSend(0x32, 0);
		delay_ms(2);
		LCD_197626_RawSend(0x2c, 0);
		delay_ms(2);
		LCD_197626_RawSend(0x06, 0);
		delay_ms(2);
		LCD_197626_RawSend(0x0c, 0);
		delay_ms(2);

		//clear the display
		LCD_197626_RawSend(0x01, 0);
		delay_ms(2);
		LCD_197626_RawSend(0x02, 0);
		delay_ms(2);

}

void FCD_LCDDisplay0_Clear()
{
	
		LCD_197626_RawSend(0x01, 0);
		delay_ms(2);
		LCD_197626_RawSend(0x02, 0);
		delay_ms(2);

}

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

}

void FCD_LCDDisplay0_Command(char in)
{
	
		LCD_197626_RawSend(in, 0);
		delay_ms(2);

}

void FCD_LCDDisplay0_RawSend(char in, char mask)
{
	//Error Reading Code For LCD Display::Macro_RawSend
}

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

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

	  #if (2 == 4)
		if (y==0)
			y=0x80;
		else if (y==1)
			y=0xc0;
		else if (y==2)
			y=0x94;
		else
			y=0xd4;
	  #endif

		LCD_197626_RawSend(y+x, 0);
		delay_ms(2);

}

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

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

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

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

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

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

}

void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String)
{
	
		char idx;
		for (idx=0; idx<MSZ_String; idx++)
		{
			if (String[idx]==0)
			{
				break;
			}
			LCD_197626_RawSend(String[idx], 0x10);
		}

}

//Macro implementations

//Supplementary implementations


void main()
{
	
	//Initialisation
	ansel0 = 0x00;


	//Interrupt initialisation code
	


	//Set MAXCNT and DFLTCON values
	//Calculation:
	//  MAXCNT = 4999
	//  DFLTCON = 0x38
	FCV_MAXCNT = 4999;
	FCV_DFLTCON = 0x38;
	

	//Calculate MAXCNTH and MAXCNTL values
	//Calculation:
	//  MAXCNTL = MAXCNT & 0x00FF
	//  MAXCNTH = ( MAXCNT & 0xFF00 ) >> 8
	FCV_MAXCNTL = FCV_MAXCNT & 0x00FF;
	FCV_MAXCNTH = ( FCV_MAXCNT & 0xFF00 ) >> 8;
	

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


	//Clear POSCNT registers
	//C Code:
	cap2bufh = 0;  // cap2bufh == poscnth
	cap2bufl = 0;  // cap2bufl == poscntl


	//Program MAXCNT registers
	//C Code:
	cap3bufh = FCV_MAXCNTH;  // cap3bufh = maxcnth
	cap3bufl = FCV_MAXCNTL;  // cap3bufl = maxcntl
	dfltcon = FCV_DFLTCON;


	//Configure QEI
	//C Code:
	qeicon = 0x98;  // Position mode (4x); reset on period match


	//Loop
	//Loop: While 1
	while (1)
	{
		//Read POSCNT registers
		//C Code:
		FCV_POSCNTH = cap2bufh;
		FCV_POSCNTL = cap2bufl;


		//Calculate POSCNT value
		//Calculation:
		//  POSCNT = ( POSCNTH << 8 ) + POSCNTL
		FCV_POSCNT = ( FCV_POSCNTH << 8 ) + FCV_POSCNTL;
		

		//Clear LCD
		//Call Component Macro: LCDDisplay(0)::Clear
		FCD_LCDDisplay0_Clear();


		//Display POSCNT
		//Call Component Macro: LCDDisplay(0)::PrintNumber(POSCNT)
		FCD_LCDDisplay0_PrintNumber(FCV_POSCNT);


		//Delay
		//Delay: 100 ms
		delay_ms(100);


	}


	mainendloop: goto mainendloop;
}

void interrupt(void)
{
}



