//************************************************************************************
//**  
//**  File name:     C:\Documents and Settings\Dave\Desktop\DMUFILES\DMUCountdown.c
//**  Generated by:  Flowcode v4.1.1.55
//**  Date:          Thursday, October 08, 2009 11:30:54
//**  Licence:       Professional
//**  Registered to: Dave Benning
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Defines for microcontroller
#define P16F628A
#define MX_EE
#define MX_EE_TYPE1
#define MX_EE_SIZE 128
#define MX_UART
#define MX_UART_B
#define MX_UART_TX 2
#define MX_UART_RX 1
#define MX_PWM
#define MX_PWM_CNT 1
#define MX_PWM_TRIS1 trisb
#define MX_PWM_1 3

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

//Configuration data
#pragma DATA 0x2007, 0x3f50

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

//Macro function declarations
void FCM_INTERRUPT_TMR0();
void FCM_INTERRUPT_RB0INT();
void FCM_INTERRUPT_PORTB();


//Variable declarations
char FCV_LOCKOUT;
char FCV_TRIGGER;
char FCV_COUNT;

char FCLV_LOOP1;
char FCLV_LOOP2;

//Defines:

/**** Macro Substitutions ****
1 = Segment Port
0 = Common Port
192 = Pattern 0
249 = Pattern 1
164 = Pattern 2
176 = Pattern 3
153 = Pattern 4
146 = Pattern 5
130 = Pattern 6
248 = Pattern 7
128 = Pattern 8
152 = Pattern 9
127 = Pattern DP
8 = Common Mask
247 = Common Mask Inverted
0 = Segment Mask Inverted
1 = Common Anode Display Type
******************************/




//LED7Seg10: //Macro function declarations

void FCD_LED7Seg10_ShowDigit(char Value, char DecimalPoint);
void FCD_LED7Seg10_ClearDigit();
//Defines:

/**** Macro Substitutions ****
porta = LED Port Register
trisa = LED Data Direction Register
4 = LED Pin Mask
1 = LED Active Polarity
******************************/




//LED0: //Macro function declarations

void FCD_LED0_LEDOn();
void FCD_LED0_LEDOff();
//Defines:

/**** Macro Substitutions ****
porta = LED Port Register
trisa = LED Data Direction Register
2 = LED Pin Mask
1 = LED Active Polarity
******************************/




//LED1: //Macro function declarations

void FCD_LED1_LEDOn();
void FCD_LED1_LEDOff();



//LED7Seg10: //Macro implementations


void FCD_LED7Seg10_ShowDigit(char Value, char DecimalPoint)
{
	
	    #if (1 == 0)								//Segment Port Definitions
	      #define MX_7SEG1_SEGMENT_PORT  porta
	      #define MX_7SEG1_SEGMENT_TRIS  trisa
	    #endif

	    #if (1 == 1)
	      #define MX_7SEG1_SEGMENT_PORT  portb
	      #define MX_7SEG1_SEGMENT_TRIS  trisb
	    #endif

	    #if (1 == 2)
	      #define MX_7SEG1_SEGMENT_PORT  portc
	      #define MX_7SEG1_SEGMENT_TRIS  trisc
	    #endif

	    #if (1 == 3)
	      #define MX_7SEG1_SEGMENT_PORT  portd
	      #define MX_7SEG1_SEGMENT_TRIS  trisd
	    #endif

	    #if (1 == 4)
	      #define MX_7SEG1_SEGMENT_PORT  porte
	      #define MX_7SEG1_SEGMENT_TRIS  trise
	    #endif

	    #if (0 == 0)								//Common Port Definitions
	      #define MX_7SEG1_COMMON_PORT   porta
	      #define MX_7SEG1_COMMON_TRIS   trisa
	    #endif

	    #if (0 == 1)
	      #define MX_7SEG1_COMMON_PORT   portb
	      #define MX_7SEG1_COMMON_TRIS   trisb
	    #endif

	    #if (0 == 2)
	      #define MX_7SEG1_COMMON_PORT   portc
	      #define MX_7SEG1_COMMON_TRIS   trisc
	    #endif

	    #if (0 == 3)
	      #define MX_7SEG1_COMMON_PORT   portd
	      #define MX_7SEG1_COMMON_TRIS   trisd
	    #endif

	    #if (0 == 4)
	      #define MX_7SEG1_COMMON_PORT   porte
	      #define MX_7SEG1_COMMON_TRIS   trise
	    #endif

	    //set pattern array for the display
		rom char* cSegmentArray = {192, 249, 164, 176, 153, 146, 130, 248, 128, 152};

		//get pattern for digit
		char cSegmentValue = cSegmentArray[Value % 10];
		if (DecimalPoint)
			cSegmentValue = cSegmentValue & 127;

		//set up ports
		MX_7SEG1_COMMON_TRIS = MX_7SEG1_COMMON_TRIS & 247;
		MX_7SEG1_SEGMENT_TRIS = MX_7SEG1_SEGMENT_TRIS & 0;

		//display the digit
	    #if (1 == 1)

			//common anode
			MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & 0) | cSegmentValue;
			MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | 8);

		#else

			//common cathode
			MX_7SEG1_SEGMENT_PORT = (MX_7SEG1_SEGMENT_PORT & 0) | ~cSegmentValue;
			MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & 247);

		#endif

		//undefine symbols
	    #undef MX_7SEG1_SEGMENT_PORT
	    #undef MX_7SEG1_SEGMENT_TRIS
	    #undef MX_7SEG1_COMMON_PORT
	    #undef MX_7SEG1_COMMON_TRIS

}

void FCD_LED7Seg10_ClearDigit()
{
	
	    #if (1 == 0)								//Segment Port Definitions
	      #define MX_7SEG1_SEGMENT_PORT  porta
	      #define MX_7SEG1_SEGMENT_TRIS  trisa
	    #endif

	    #if (1 == 1)
	      #define MX_7SEG1_SEGMENT_PORT  portb
	      #define MX_7SEG1_SEGMENT_TRIS  trisb
	    #endif

	    #if (1 == 2)
	      #define MX_7SEG1_SEGMENT_PORT  portc
	      #define MX_7SEG1_SEGMENT_TRIS  trisc
	    #endif

	    #if (1 == 3)
	      #define MX_7SEG1_SEGMENT_PORT  portd
	      #define MX_7SEG1_SEGMENT_TRIS  trisd
	    #endif

	    #if (1 == 4)
	      #define MX_7SEG1_SEGMENT_PORT  porte
	      #define MX_7SEG1_SEGMENT_TRIS  trise
	    #endif

	    #if (0 == 0)								//Common Port Definitions
	      #define MX_7SEG1_COMMON_PORT   porta
	      #define MX_7SEG1_COMMON_TRIS   trisa
	    #endif

	    #if (0 == 1)
	      #define MX_7SEG1_COMMON_PORT   portb
	      #define MX_7SEG1_COMMON_TRIS   trisb
	    #endif

	    #if (0 == 2)
	      #define MX_7SEG1_COMMON_PORT   portc
	      #define MX_7SEG1_COMMON_TRIS   trisc
	    #endif

	    #if (0 == 3)
	      #define MX_7SEG1_COMMON_PORT   portd
	      #define MX_7SEG1_COMMON_TRIS   trisd
	    #endif

	    #if (0 == 4)
	      #define MX_7SEG1_COMMON_PORT   porte
	      #define MX_7SEG1_COMMON_TRIS   trise
	    #endif

	    //clear the digit
		#if (1 == 1)

			//common anode
			MX_7SEG1_SEGMENT_PORT = 0xFF;
			MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT & 247);

		#else

			//common cathode
			MX_7SEG1_SEGMENT_PORT = 0x00;
			MX_7SEG1_COMMON_PORT = (MX_7SEG1_COMMON_PORT | 8);

		#endif

}



//LED0: //Macro implementations


void FCD_LED0_LEDOn()
{
	
		trisa = trisa & ~4;			//Convert pin to output

		if( 1 )					//Active high polarity
			porta = porta | 4;
		else					//Active low polarity
			porta = porta & ~4;

}

void FCD_LED0_LEDOff()
{
	
		trisa = trisa & ~4;			//Convert pin to output

		if( 1 )					//Active high polarity
			porta = porta & ~4;
		else					//Active low polarity
			porta = porta | 4;

}



//LED1: //Macro implementations


void FCD_LED1_LEDOn()
{
	
		trisa = trisa & ~2;			//Convert pin to output

		if( 1 )					//Active high polarity
			porta = porta | 2;
		else					//Active low polarity
			porta = porta & ~2;

}

void FCD_LED1_LEDOff()
{
	
		trisa = trisa & ~2;			//Convert pin to output

		if( 1 )					//Active high polarity
			porta = porta & ~2;
		else					//Active low polarity
			porta = porta | 2;

}

//Macro implementations

void FCM_INTERRUPT_TMR0()
{
	
}

void FCM_INTERRUPT_RB0INT()
{
	
}

void FCM_INTERRUPT_PORTB()
{
	
}

void main()
{
	//Counting on a seven segment display
	
	//Initialisation
	cmcon = 0x07;


	//Interrupt initialisation code
	option_reg = 0xC0;


	//Output
	//Output: 0 -> A4
	trisa = trisa & 0xef;
	if (0)
		porta = (porta & 0xef) | 0x10;
	else
		porta = porta & 0xef;


	//7 SEG CLEAR
	//Call Component Macro: LED7Seg1(0)::ClearDigit
	FCD_LED7Seg10_ClearDigit();


	//Call Component Macro
	//Call Component Macro: LED(0)::LEDOff
	FCD_LED0_LEDOff();


	//Call Component Macro
	//Call Component Macro: LED(1)::LEDOff
	FCD_LED1_LEDOff();


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


	//Loop
	//Loop: Loop 2 times
	for (FCLV_LOOP1=0; FCLV_LOOP1<2; FCLV_LOOP1++)
	{
		//Delay
		//Delay: 500 ms
		delay_ms(255);
		delay_ms(245);


		//Output
		//Output: 1 -> A4
		trisa = trisa & 0xef;
		if (1)
			porta = (porta & 0xef) | 0x10;
		else
			porta = porta & 0xef;


		//Call Component Macro
		//Call Component Macro: LED7Seg1(0)::ShowDigit(8, 1)
		FCD_LED7Seg10_ShowDigit(8, 1);


		//Delay
		//Delay: 500 ms
		delay_ms(255);
		delay_ms(245);


		//Output
		//Output: 0 -> A4
		trisa = trisa & 0xef;
		if (0)
			porta = (porta & 0xef) | 0x10;
		else
			porta = porta & 0xef;


		//Call Component Macro
		//Call Component Macro: LED7Seg1(0)::ClearDigit
		FCD_LED7Seg10_ClearDigit();


		//Delay
		//Delay: 500 ms
		delay_ms(255);
		delay_ms(245);


		//Call Component Macro
		//Call Component Macro: LED(0)::LEDOn
		FCD_LED0_LEDOn();


		//Delay
		//Delay: 500 ms
		delay_ms(255);
		delay_ms(245);


		//Call Component Macro
		//Call Component Macro: LED(0)::LEDOff
		FCD_LED0_LEDOff();


		//Delay
		//Delay: 500 ms
		delay_ms(255);
		delay_ms(245);


		//Call Component Macro
		//Call Component Macro: LED(1)::LEDOn
		FCD_LED1_LEDOn();


		//Delay
		//Delay: 500 ms
		delay_ms(255);
		delay_ms(245);


		//Call Component Macro
		//Call Component Macro: LED(1)::LEDOff
		FCD_LED1_LEDOff();


	}


	//Loop
	//Loop: Loop 3 times
	for (FCLV_LOOP2=0; FCLV_LOOP2<3; FCLV_LOOP2++)
	{
		//Delay
		//Delay: 200 ms
		delay_ms(200);


		//Call Component Macro
		//Call Component Macro: LED(1)::LEDOn
		FCD_LED1_LEDOn();


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


		//Call Component Macro
		//Call Component Macro: LED(1)::LEDOff
		FCD_LED1_LEDOff();


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


		//Call Component Macro
		//Call Component Macro: LED(1)::LEDOn
		FCD_LED1_LEDOn();


	}


	//Delay
	//Delay: 1 s
	delay_s(1);


	//Connection Point
	//Connection Point: [A]: A
FCC_Main_A:


	//Input
	//Input: A0 -> Trigger
	trisa = trisa | 0x01;
	FCV_TRIGGER = ((porta & 0x01) == 0x01);


	//Decision
	//Decision: Trigger=1?
	if (FCV_TRIGGER==1)
	{
		//Output
		//Output: 1 -> A4
		trisa = trisa & 0xef;
		if (1)
			porta = (porta & 0xef) | 0x10;
		else
			porta = porta & 0xef;


	} else {
		//Output
		//Output: 0 -> A4
		trisa = trisa & 0xef;
		if (0)
			porta = (porta & 0xef) | 0x10;
		else
			porta = porta & 0xef;


		//Goto Connection Point
		//Goto Connection Point: [A]: A
		goto FCC_Main_A;


	}


	//RED ON
	//Call Component Macro: LED(0)::LEDOn
	FCD_LED0_LEDOn();


	//GREEN OFF
	//Call Component Macro: LED(1)::LEDOff
	FCD_LED1_LEDOff();


	//Initialise COUNT
	//Calculation:
	//  COUNT = 10
	FCV_COUNT = 10;
	

	//Inner Loop: 0 > 9
	//Loop: While COUNT > 0
	while (FCV_COUNT > 0)
	{
		//decrement COUNT
		//Calculation:
		//  COUNT = COUNT - 1
		FCV_COUNT = FCV_COUNT - 1;
		

		//Display COUNT on the 7Seg
		//Call Component Macro: LED7Seg1(0)::ShowDigit(COUNT, 0)
		FCD_LED7Seg10_ShowDigit(FCV_COUNT, 0);


		//Delay
		//Delay: 1 s
		delay_s(1);


	}


	//Call Component Macro
	//Call Component Macro: LED7Seg1(0)::ClearDigit
	FCD_LED7Seg10_ClearDigit();


	//Call Component Macro
	//Call Component Macro: LED(1)::LEDOn
	FCD_LED1_LEDOn();


	//Call Component Macro
	//Call Component Macro: LED(0)::LEDOff
	FCD_LED0_LEDOff();


	//Goto Connection Point
	//Goto Connection Point: [A]: A
	goto FCC_Main_A;


	mainendloop: goto mainendloop;
}

void interrupt(void)
{
}



