//************************************************************************************
//**  
//**  File name:     C:\Users\sachs-wolf\Desktop\vergleich\v4_print_HI_PA.c
//**  Generated by:  Flowcode v4.1.1.55
//**  Date:          Friday, September 25, 2009 10:19:18
//**  Licence:       Professional
//**  Registered to: WOLFGANG SACHS
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Definitionen für Microcontroller
#define P18F4620
#define MX_EE
#define MX_EE_TYPE3
#define MX_EE_SIZE 1024
#define MX_SPI
#define MX_SPI_C
#define MX_SPI_SDI 4
#define MX_SPI_SDO 5
#define MX_SPI_SCK 3
#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 4
#define MX_I2C_SCL 3
#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
#define MX_PWM_TRIS2a trisb
#define MX_PWM_2a 3

//Funktionen
#include <system.h>
#pragma CLOCK_FREQ 39321600

//Konfigurations-Daten
#pragma DATA 0x300000, 0xff
#pragma DATA 0x300001, 0x6
#pragma DATA 0x300002, 0x19
#pragma DATA 0x300003, 0x1e
#pragma DATA 0x300004, 0xff
#pragma DATA 0x300005, 0x81
#pragma DATA 0x300006, 0x80
#pragma DATA 0x300007, 0xff
#pragma DATA 0x300008, 0xf
#pragma DATA 0x300009, 0xc0
#pragma DATA 0x30000a, 0xf
#pragma DATA 0x30000b, 0xe0
#pragma DATA 0x30000c, 0xf
#pragma DATA 0x30000d, 0x40

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

//Makro-Funktions-Deklarationen


//Variablen-Deklarationen


//Defines:

/**** Macro Substitutions ****
porta = D1 Port
trisa = D1 Data Direction
porta = D2 Port
trisa = D2 Data Direction
porta = D3 Port
trisa = D3 Data Direction
porta = D4 Port
trisa = D4 Data Direction
porta = RS Port
trisa = RS Data Direction
porta = E Port
trisa = 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_2033438 = Unique Component Reference Number
2 = Row Count
16 = Column Count
******************************/

	//component connections
	#define LCD_2033438_PORT0    porta
	#define LCD_2033438_TRIS0    trisa
	#define LCD_2033438_PORT1    porta
	#define LCD_2033438_TRIS1    trisa
	#define LCD_2033438_PORT2    porta
	#define LCD_2033438_TRIS2    trisa
	#define LCD_2033438_PORT3    porta
	#define LCD_2033438_TRIS3    trisa
	#define LCD_2033438_PORT4    porta
	#define LCD_2033438_TRIS4    trisa
	#define LCD_2033438_PORT5    porta
	#define LCD_2033438_TRIS5    trisa
	#define LCD_2033438_BIT0    	0
	#define LCD_2033438_BIT1    	1
	#define LCD_2033438_BIT2    	2
	#define LCD_2033438_BIT3    	3
	#define LCD_2033438_RS      	4
	#define LCD_2033438_E       	5
	#define LCD_2033438_ROWCNT	2
	#define LCD_2033438_COLCNT	16

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




//LCDDisplay0: //Makro-Funktions-Deklarationen

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);



//LCDDisplay0: //Makro-Implementierungen


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

		clear_bit(LCD_2033438_PORT0, LCD_2033438_BIT0);
		clear_bit(LCD_2033438_PORT1, LCD_2033438_BIT1);
		clear_bit(LCD_2033438_PORT2, LCD_2033438_BIT2);
		clear_bit(LCD_2033438_PORT3, LCD_2033438_BIT3);
		clear_bit(LCD_2033438_PORT4, LCD_2033438_RS);
		clear_bit(LCD_2033438_PORT5, LCD_2033438_E);
		pt = ((in >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(LCD_2033438_PORT0, LCD_2033438_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_2033438_PORT1, LCD_2033438_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_2033438_PORT2, LCD_2033438_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_2033438_PORT3, LCD_2033438_BIT3);
		if (mask)
		    set_bit(LCD_2033438_PORT4, LCD_2033438_RS);
		LCD_2033438_DELAY;
		set_bit (LCD_2033438_PORT5, LCD_2033438_E);
		LCD_2033438_DELAY;
		clear_bit (LCD_2033438_PORT5, LCD_2033438_E);
		pt = (in & 0x0f);
		LCD_2033438_DELAY;
		clear_bit(LCD_2033438_PORT0, LCD_2033438_BIT0);
		clear_bit(LCD_2033438_PORT1, LCD_2033438_BIT1);
		clear_bit(LCD_2033438_PORT2, LCD_2033438_BIT2);
		clear_bit(LCD_2033438_PORT3, LCD_2033438_BIT3);
		clear_bit(LCD_2033438_PORT4, LCD_2033438_RS);
		clear_bit(LCD_2033438_PORT5, LCD_2033438_E);
		if (pt & 0x01)
		    set_bit(LCD_2033438_PORT0, LCD_2033438_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_2033438_PORT1, LCD_2033438_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_2033438_PORT2, LCD_2033438_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_2033438_PORT3, LCD_2033438_BIT3);
		if (mask)
		    set_bit(LCD_2033438_PORT4, LCD_2033438_RS);
		LCD_2033438_DELAY;
		set_bit (LCD_2033438_PORT5, LCD_2033438_E);
		LCD_2033438_DELAY;
		clear_bit (LCD_2033438_PORT5, LCD_2033438_E);
		LCD_2033438_DELAY;
}

void FCD_LCDDisplay0_Start()
{
	
		clear_bit(LCD_2033438_TRIS0, LCD_2033438_BIT0);
		clear_bit(LCD_2033438_TRIS1, LCD_2033438_BIT1);
		clear_bit(LCD_2033438_TRIS2, LCD_2033438_BIT2);
		clear_bit(LCD_2033438_TRIS3, LCD_2033438_BIT3);
		clear_bit(LCD_2033438_TRIS4, LCD_2033438_RS);
		clear_bit(LCD_2033438_TRIS5, LCD_2033438_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_2033438_ROWCNT == 1)
	    y=0x80;
	  #endif

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

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

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

		#if (LCD_2033438_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;
		for (idx=0; idx<MSZ_String; idx++)
		{
			if (String[idx]==0)
			{
				break;
			}
			FCD_LCDDisplay0_RawSend(String[idx], 0x10);
		}

}

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_2033438_ROWCNT == 1)
			rowcount=80;
		#endif

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

		#if (LCD_2033438_ROWCNT == 4)
			#if (LCD_2033438_COLCNT == 16)
				rowcount=16;
			#endif
			#if (LCD_2033438_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);
}

//Makro-Implementierungen

void main()
{
	
	//Initialisierung
	adcon1 = 0x0F;


	//Interrupt-Initialisierungs-Kode
	


	//Aufruf Komponenten-Makro
	//Aufruf Komponenten-Makro: LCDDisplay(0)::Start
	FCD_LCDDisplay0_Start();


	//Aufruf Komponenten-Makro
	//Aufruf Komponenten-Makro: LCDDisplay(0)::PrintASCII("HI")
	FCD_LCDDisplay0_PrintASCII('H');
	FCD_LCDDisplay0_PrintASCII('I');


	mainendloop: goto mainendloop;
}

void interrupt(void)
{
}



