//************************************************************************************
//**  
//**  File name:     D:\sachs_work\ECIO\pic v4 test\pwm_test.c
//**  Generated by:  Flowcode v4.3.7.63
//**  Date:          Thursday, July 08, 2010 17:59:27
//**  Licence:       Professional
//**  Registered to: WOLFGANG SACHS
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Definitionen für Microcontroller
#define P18F4685
#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 trisd
#define MX_PWM_2 4
#define MX_ECAN

//Funktionen
#define MX_CLK_SPEED 40000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic18.h>
#endif

//Konfigurations-Daten

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

//Makro-Funktions-Deklarationen


//Variablen-Deklarationen


//Defines:

/**** Macro Substitutions ****
255 = Timer 2 Rollover Value
0x04 = Timer 2 Prescaler Value
%c = Unused
0 = Alternate Pin FCD_PWM0_Enable
******************************/




//PWM(0): //Makro-Funktions-Deklarationen

void FCD_PWM0_Enable(char nIdx);
void FCD_PWM0_Disable(char nIdx);
void FCD_PWM0_SetDutyCycle(char nIdx, char nDuty);
void FCD_PWM0_ChangePeriod(char nPeriodVal, char nPrescalerVal);
void FCD_PWM0_SetDutyCycle10bit(char nIdx, short nDuty);



//PWM(0): //Makro-Implementierungen


void FCD_PWM0_Enable(char nIdx)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #else
	    #ifndef MX_PWM_CNT
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_CNT)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_CNT)
			#endif
	    #endif
	    #if (MX_PWM_CNT < 1)
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (MX_PWM_CNT < 1)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (MX_PWM_CNT < 1)
			#endif
	    #endif
	    #if (MX_PWM_CNT > 2)
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (MX_PWM_CNT > 2)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (MX_PWM_CNT > 2)
			#endif
	    #endif
	    #ifndef MX_PWM_TRIS1
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_TRIS1)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_TRIS1)
			#endif
	    #endif
	    #ifndef MX_PWM_1
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_1)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_1)
			#endif
	    #endif
	    #if (MX_PWM_CNT == 2)
	      #ifndef MX_PWM_TRIS2
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_TRIS2)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_TRIS2)
			#endif
	      #endif
	      #ifndef MX_PWM_2
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_2)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_2)
			#endif
	      #endif
	    #endif
	    #if (0 == 1)
	      #ifndef MX_PWM_TRIS1a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_TRIS1a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_TRIS1a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	      #ifndef MX_PWM_1a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_1a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_1a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	    #endif
	    #if (0 == 2)
	      #ifndef MX_PWM_TRIS2a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_TRIS2a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_TRIS2a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	      #ifndef MX_PWM_2a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_2a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_2a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	    #endif
	  #endif

	 #ifdef MX_PWM
	    pr2 = 255;
	    t2con = 0x04;

	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccp1con = 0x0C;

	      #if (0 == 1)
	       #ifndef MX_PWM_ALT_ERROR
	        clear_bit(MX_PWM_TRIS1a, MX_PWM_1a);
	       #endif
	      #else
	        clear_bit(MX_PWM_TRIS1, MX_PWM_1);
	      #endif
		}
	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccp2con = 0x0C;
	      #if (0 == 2)
	       #ifndef MX_PWM_ALT_ERROR
	        clear_bit(MX_PWM_TRIS2a, MX_PWM_2a);
	       #endif
	      #else
	        clear_bit(MX_PWM_TRIS2, MX_PWM_2);
	      #endif
	   }
	  #endif
	 #endif

	 #ifdef MX_PWM_ALT_ERROR
	   #undef MX_PWM_ALT_ERROR
	 #endif

}

void FCD_PWM0_Disable(char nIdx)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	 #ifdef MX_PWM
	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccp1con = 0x00;
	    }
	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccp2con = 0x00;
	    }
	  #endif
	 #endif

}

void FCD_PWM0_SetDutyCycle(char nIdx, char nDuty)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	 #ifdef MX_PWM
	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccpr1l = nDuty;
	    }
	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccpr2l = nDuty;
	    }
	  #endif
	 #endif

}

void FCD_PWM0_ChangePeriod(char nPeriodVal, char nPrescalerVal)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	  #ifdef MX_PWM
	    pr2 = nPeriodVal;

	    switch (nPrescalerVal)
	    {
	        case 1:
	            t2con = 0x04;
	            break;

	        case 4:
	            t2con = 0x05;
	            break;

	        case 16:
	            t2con = 0x06;
	            break;
	    }
	  #endif

}

void FCD_PWM0_SetDutyCycle10bit(char nIdx, short nDuty)
{
	
		char nCCPxCONtemp;

	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	 #ifdef MX_PWM
	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccpr1l = (nDuty & 0x3FC) >> 2;

	    	nDuty = (nDuty & 0x03) << 4;
	    	nCCPxCONtemp = (ccp1con & 0xCF) | nDuty;
	    	ccp1con = nCCPxCONtemp;
	    }

	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccpr2l = (nDuty & 0x3FC) >> 2;

	    	nDuty = (nDuty & 0x03) << 4;
	    	nCCPxCONtemp = (ccp2con & 0xCF) | nDuty;
	    	ccp2con = nCCPxCONtemp;
	    }

	  #endif
	 #endif

}

//Makro-Implementierungen

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


	//Interrupt-Initialisierungs-Kode
	


	//Aufruf Komponenten-Makro
	//Aufruf Komponenten-Makro: PWM(0)::Enable(1)
	FCD_PWM0_Enable(1);


	//Aufruf Komponenten-Makro
	//Aufruf Komponenten-Makro: PWM(0)::SetDutyCycle(1, 120)
	FCD_PWM0_SetDutyCycle(1, 120);


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}



