//************************************************************************************
//**  
//**  File name:     C:\Users\Bill Swann\Desktop\FlowCode\DATE\Interrupts\Interrupt for1 second when a PB is Pushed.c
//**  Title:         
//**  Description:   
//**  
//**  Generated by:  Flowcode v5.5.2.1
//**  Date:          Monday, February 11, 2013 20:07:21
//**  Licence:       Professional
//**  Registered to: billduck1302
//**  Licence key:   BDJX8E
//**  
//**  
//**  http://www.matrixmultimedia.com
//**  
//************************************************************************************


#define MX_AVR

#define MX_USES_UINT8 1
#define MX_USES_SINT16 0
#define MX_USES_CHAR 0
#define MX_USES_FLOAT 0
#define MX_USES_SINT32 0
#define MX_USES_BOOL 1
#define MX_USES_UINT16 0
#define MX_USES_UINT32 0
//Defines for microcontroller
#define FC_CAL_AVR
#define MX_ADC
#define MX_ADC_TYPE_10
#define MX_ADC_BITS_10
#define MX_EE
#define MX_EE_SIZE 4096
#define MX_SPI_1
#define MX_SPI_1_MISO_PORT PORTB
#define MX_SPI_1_MISO_TRIS DDRB
#define MX_SPI_1_MISO_PIN 3
#define MX_SPI_1_MOSI_PORT PORTB
#define MX_SPI_1_MOSI_TRIS DDRB
#define MX_SPI_1_MOSI_PIN 2
#define MX_SPI_1_CLK_PORT PORTB
#define MX_SPI_1_CLK_TRIS DDRB
#define MX_SPI_1_CLK_PIN 1
#define MX_SPI_1_SS_PORT PORTB
#define MX_SPI_1_SS_TRIS DDRB
#define MX_SPI_1_SS_PIN 0
#define MX_UART_1
#define MX_UART_1_TX_PORT PORTE
#define MX_UART_1_TX_TRIS DDRE
#define MX_UART_1_TX_PIN 1
#define MX_UART_1_RX_PORT PORTE
#define MX_UART_1_RX_TRIS DDRE
#define MX_UART_1_RX_PIN 0
#define MX_UART_2
#define MX_UART_2_TX_PORT PORTH
#define MX_UART_2_TX_TRIS DDRH
#define MX_UART_2_TX_PIN 1
#define MX_UART_2_RX_PORT PORTH
#define MX_UART_2_RX_TRIS DDRH
#define MX_UART_2_RX_PIN 0
#define MX_UART_ID
#define MX_UART_UCSRC
#define MX_MI2C
#define MX_I2C_1
#define MX_I2C_1_SDA_PORT PORTD
#define MX_I2C_1_SDA_TRIS DDRD
#define MX_I2C_1_SDA_PIN 1
#define MX_I2C_1_SCL_PORT PORTD
#define MX_I2C_1_SCL_TRIS DDRD
#define MX_I2C_1_SCL_PIN 0
#define MX_PWM
#define MX_PWMTYPE 1
#define MX_PWM_CNT 16
#define MX_PWM_TMR0_CNT 2
#define MX_PWM_TMR1_CNT 3
#define MX_PWM_TMR2_CNT 2
#define MX_PWM_TMR3_CNT 3
#define MX_PWM_TMR4_CNT 3
#define MX_PWM_TMR5_CNT 3
#define MX_PWM_PSCA1
#define MX_PWM_PSCA8
#define MX_PWM_PSCA64
#define MX_PWM_PSCA256
#define MX_PWM_PSCA1024
#define MX_PWM_1_PORT PORTB
#define MX_PWM_1_TRIS DDRB
#define MX_PWM_1_PIN 7
#define MX_PWM_2_PORT PORTG
#define MX_PWM_2_TRIS DDRG
#define MX_PWM_2_PIN 5
#define MX_PWM_3_PORT PORTB
#define MX_PWM_3_TRIS DDRB
#define MX_PWM_3_PIN 5
#define MX_PWM_4_PORT PORTB
#define MX_PWM_4_TRIS DDRB
#define MX_PWM_4_PIN 6
#define MX_PWM_5_PORT PORTB
#define MX_PWM_5_TRIS DDRB
#define MX_PWM_5_PIN 7
#define MX_PWM_6_PORT PORTB
#define MX_PWM_6_TRIS DDRB
#define MX_PWM_6_PIN 4
#define MX_PWM_7_PORT PORTH
#define MX_PWM_7_TRIS DDRH
#define MX_PWM_7_PIN 6
#define MX_PWM_8_PORT PORTE
#define MX_PWM_8_TRIS DDRE
#define MX_PWM_8_PIN 3
#define MX_PWM_9_PORT PORTE
#define MX_PWM_9_TRIS DDRE
#define MX_PWM_9_PIN 4
#define MX_PWM_10_PORT PORTE
#define MX_PWM_10_TRIS DDRE
#define MX_PWM_10_PIN 5
#define MX_PWM_11_PORT PORTH
#define MX_PWM_11_TRIS DDRH
#define MX_PWM_11_PIN 3
#define MX_PWM_12_PORT PORTH
#define MX_PWM_12_TRIS DDRH
#define MX_PWM_12_PIN 4
#define MX_PWM_13_PORT PORTH
#define MX_PWM_13_TRIS DDRH
#define MX_PWM_13_PIN 5
#define MX_PWM_14_PORT PORTL
#define MX_PWM_14_TRIS DDRL
#define MX_PWM_14_PIN 3
#define MX_PWM_15_PORT PORTL
#define MX_PWM_15_TRIS DDRL
#define MX_PWM_15_PIN 4
#define MX_PWM_16_PORT PORTL
#define MX_PWM_16_TRIS DDRL
#define MX_PWM_16_PIN 5

//Functions
#define F_CPU 16000000UL
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <avr\io.h>
#include <avr\interrupt.h>
#include <avr\eeprom.h>
#include <avr\wdt.h>

//Configuration data

//Internal functions
#include "C:\Program Files (x86)\Flowcode(AVR)\v5\FCD\internals.c"

//Macro function declarations
void FCM_DisplayPB_Pushed();
void FCM_Init();
void FCM_SensePortB_High();
void FCM_CLS();


//Variable declarations
#define FCV_FALSE (0)
#define FCV_TRUE (1)
volatile MX_BOOL FCV_RIGHT_JOGPB;
volatile MX_BOOL FCV_DOWN_STOPPB;
volatile MX_BOOL FCV_LEFT_SETUPPB;
volatile MX_BOOL FCV_UP_RUNPB;




//RS232(0): //Defines:

/**** Macro Substitutions ****
a = Unique Reference
b = Uart Channel		(0-software / 1-4 hardware)
c = tx pin
d = tx port
e = rx pin
f = rx port
g = flow control		(0-Off / 1-On)
h = cts pin
i = cts port
j = rts pin
k = rts port
l = baud rate
m = data bits			(7/8/9) (7 only supported in software modes)
n = return type		(0-byte / 1-MX_UINT16)
o = echo enable		(0-Off / 1-On)
******************************/

//Definitions for RS232 UART slot allocation
#ifndef	MX_UART_REF1
	#define MX_UART_REF1
	#define RS232_11_MX_UART_UREF			1
	#define MX_UART_CHANNEL_1		1
	#define MX_UART_TX_PIN_1		0
	#define MX_UART_TX_PORT_1		porta
	#define MX_UART_TX_TRIS_1		trisa
	#define MX_UART_RX_PIN_1		0
	#define MX_UART_RX_PORT_1		porta
	#define MX_UART_RX_TRIS_1		trisa
	#define MX_UART_FLOWEN_1		0
	#define MX_UART_CTS_PIN_1		4
	#define MX_UART_CTS_PORT_1		portc
	#define MX_UART_CTS_TRIS_1		trisc
	#define MX_UART_RTS_PIN_1		0
	#define MX_UART_RTS_PORT_1		portc
	#define MX_UART_RTS_TRIS_1		trisc
	#define MX_UART_BAUD_1			9600
	#define MX_UART_DBITS_1			8
	#define MX_UART_RETURN_1		0
	#define MX_UART_ECHO_1			0
	#define MX_UART_INT_1			0
#else
 #ifndef	MX_UART_REF2
	#define MX_UART_REF2
	#define RS232_11_MX_UART_UREF			2
	#define MX_UART_CHANNEL_2		1
	#define MX_UART_TX_PIN_2		0
	#define MX_UART_TX_PORT_2		porta
	#define MX_UART_TX_TRIS_2		trisa
	#define MX_UART_RX_PIN_2		0
	#define MX_UART_RX_PORT_2		porta
	#define MX_UART_RX_TRIS_2		trisa
	#define MX_UART_FLOWEN_2		0
	#define MX_UART_CTS_PIN_2		4
	#define MX_UART_CTS_PORT_2		portc
	#define MX_UART_CTS_TRIS_2		trisc
	#define MX_UART_RTS_PIN_2		0
	#define MX_UART_RTS_PORT_2		portc
	#define MX_UART_RTS_TRIS_2		trisc
	#define MX_UART_BAUD_2			9600
	#define MX_UART_DBITS_2			8
	#define MX_UART_RETURN_2		0
	#define MX_UART_ECHO_2			0
	#define MX_UART_INT_2			0
 #else
  #ifndef	MX_UART_REF3
	#define MX_UART_REF3
	#define RS232_11_MX_UART_UREF			3
	#define MX_UART_CHANNEL_3		1
	#define MX_UART_TX_PIN_3		0
	#define MX_UART_TX_PORT_3		porta
	#define MX_UART_TX_TRIS_3		trisa
	#define MX_UART_RX_PIN_3		0
	#define MX_UART_RX_PORT_3		porta
	#define MX_UART_RX_TRIS_3		trisa
	#define MX_UART_FLOWEN_3		0
	#define MX_UART_CTS_PIN_3		4
	#define MX_UART_CTS_PORT_3		portc
	#define MX_UART_CTS_TRIS_3		trisc
	#define MX_UART_RTS_PIN_3		0
	#define MX_UART_RTS_PORT_3		portc
	#define MX_UART_RTS_TRIS_3		trisc
	#define MX_UART_BAUD_3			9600
	#define MX_UART_DBITS_3			8
	#define MX_UART_RETURN_3		0
	#define MX_UART_ECHO_3			0
	#define MX_UART_INT_3			0
  #else
   #ifndef	MX_UART_REF4
	#define MX_UART_REF4
	#define RS232_11_MX_UART_UREF			4
	#define MX_UART_CHANNEL_4		1
	#define MX_UART_TX_PIN_4		0
	#define MX_UART_TX_PORT_4		porta
	#define MX_UART_TX_TRIS_4		trisa
	#define MX_UART_RX_PIN_4		0
	#define MX_UART_RX_PORT_4		porta
	#define MX_UART_RX_TRIS_4		trisa
	#define MX_UART_FLOWEN_4		0
	#define MX_UART_CTS_PIN_4		4
	#define MX_UART_CTS_PORT_4		portc
	#define MX_UART_CTS_TRIS_4		trisc
	#define MX_UART_RTS_PIN_4		0
	#define MX_UART_RTS_PORT_4		portc
	#define MX_UART_RTS_TRIS_4		trisc
	#define MX_UART_BAUD_4			9600
	#define MX_UART_DBITS_4			8
	#define MX_UART_RETURN_4		0
	#define MX_UART_ECHO_4			0
	#define MX_UART_INT_4			0
   #endif
  #endif
 #endif
#endif

#define RS232_11_UART_Init		CAL_APPEND(FC_CAL_UART_Init_, RS232_11_MX_UART_UREF)
#define RS232_11_UART_Send		CAL_APPEND(FC_CAL_UART_Send_, RS232_11_MX_UART_UREF)
#define RS232_11_UART_Receive		CAL_APPEND(FC_CAL_UART_Receive_, RS232_11_MX_UART_UREF)
#define RS232_11_UART_Update_Baud CAL_APPEND(FC_CAL_UART_Update_Baud_, RS232_11_MX_UART_UREF)

extern void RS232_11_UART_Init();
extern void RS232_11_UART_Send(MX_UINT16 nChar);
extern MX_SINT16 RS232_11_UART_Receive(MX_UINT8 nTimeout);
extern void RS232_11_UART_Update_Baud(MX_UINT8 newbaud);




//RS232(0): //Macro function declarations

static void FCD_RS2320_SendRS232Char(MX_SINT16 nChar);
static void FCD_RS2320_SendRS232String(MX_STRING String, MX_UINT8 MSZ_String);
static MX_SINT16 FCD_RS2320_ReceiveRS232Char(MX_UINT8 nTimeout);
static void FCD_RS2320_ReceiveRS232String(MX_CHAR* FCR_RETVAL, MX_UINT8 FCR_RETVAL_SIZE, MX_UINT8 nTimeout, MX_UINT8 NumBytes);
static void FCD_RS2320_ChangeHWBaud(MX_UINT8 newbaud);



//RS232(0): //Macro implementations


static void FCD_RS2320_SendRS232Char(MX_SINT16 nChar)
{
	
		RS232_11_UART_Send ( nChar);

}

static void FCD_RS2320_SendRS232String(MX_STRING String, MX_UINT8 MSZ_String)
{
	
		MX_UINT8 idx;
		for(idx = 0; idx < MSZ_String; idx++)
		{
			if (String[idx] == 0)
				break;
			else RS232_11_UART_Send ( String[idx] );
		}

}

static MX_SINT16 FCD_RS2320_ReceiveRS232Char(MX_UINT8 nTimeout)
{
	
		return ( RS232_11_UART_Receive ( nTimeout) );

}

static void FCD_RS2320_ReceiveRS232String(MX_CHAR* FCR_RETVAL, MX_UINT8 FCR_RETVAL_SIZE, MX_UINT8 nTimeout, MX_UINT8 NumBytes)
{
	
		MX_UINT8 idx;
		MX_SINT16 RS232_TO = 255;
		MX_SINT16 in;

		if ( 0 != 0 )
			RS232_TO = 256;

		if (NumBytes > FCR_RETVAL_SIZE)
			NumBytes = FCR_RETVAL_SIZE;

		for (idx = 0; idx < NumBytes; idx++)
		{
			in = RS232_11_UART_Receive ( nTimeout);
			if(in < RS232_TO)
				FCR_RETVAL[idx] = in & 0xFF;
			else
				break;
		}

		if (idx < FCR_RETVAL_SIZE)
			FCR_RETVAL[idx] = 0;

}

static void FCD_RS2320_ChangeHWBaud(MX_UINT8 newbaud)
{
	
		RS232_11_UART_Update_Baud (newbaud);

}

#include "C:\Program Files (x86)\Flowcode(AVR)\v5\CAL\includes.c"

//Macro implementations

//The goal is to recognise which PB is pushed, and excute code accordingly. Most likely, it will change a variable - Menu Position- which goes back to a switch starement in Main.

void FCM_DisplayPB_Pushed()
{

	//Call Macro
	//Call Macro: CLS()
	FCM_CLS();

	//Decision
	//Decision: Up_RunPB = 0?
	if (FCV_UP_RUNPB == 0)
	{

		//Print "Got It"
		//Call Component Macro: SendRS232String("Got It")
		FCD_RS2320_SendRS232String("Got It", 6);

	} else {

		//"In ISR_Not_UpRunPB"
		//Call Component Macro: SendRS232String("In ISR_Not_UpRunPB")
		FCD_RS2320_SendRS232String("In ISR_Not_UpRunPB", 18);

	}

}

//I was told that switches need this to pull them up.

void FCM_Init()
{

	//CLS
	//Call Macro: CLS()
	FCM_CLS();

	//Comment:
	//Contrast on LCD

	//Contrast-Prefix
	//Call Component Macro: SendRS232Char(0xfe)
	FCD_RS2320_SendRS232Char(0xfe);

	//Contrast
	//Call Component Macro: SendRS232Char(0x52)
	FCD_RS2320_SendRS232Char(0x52);

	//Contrast Value
	//Call Component Macro: SendRS232Char(30)
	FCD_RS2320_SendRS232Char(30);

	//Key In Version
	//Call Component Macro: SendRS232String("INT_1Sec_4PBV1")
	FCD_RS2320_SendRS232String("INT_1Sec_4PBV1", 14);

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

	//CLS
	//Call Macro: CLS()
	FCM_CLS();

	//Comment:
	//Pull up on switches

	//Pull Up
	//Output: 1 -> B4
	DDRB = DDRB | 0x10;
	if ((1))
		PORTB = (PORTB & 0xEF) | 0x10;
	else
		PORTB = PORTB & 0xEF;

	//Pull Up
	//Output: 1 -> B5
	DDRB = DDRB | 0x20;
	if ((1))
		PORTB = (PORTB & 0xDF) | 0x20;
	else
		PORTB = PORTB & 0xDF;

	//Pull Up
	//Output: 1 -> B6
	DDRB = DDRB | 0x40;
	if ((1))
		PORTB = (PORTB & 0xBF) | 0x40;
	else
		PORTB = PORTB & 0xBF;

	//Pull Up
	//Output: 1 -> B7
	DDRB = DDRB | 0x80;
	if ((1))
		PORTB = (PORTB & 0x7F) | 0x80;
	else
		PORTB = PORTB & 0x7F;

}


void FCM_SensePortB_High()
{

	//Input
	//Input: B4 -> Up_RunPB
	DDRB = DDRB & 0xEF;
	FCV_UP_RUNPB = ((PINB & 0x10) == 0x10);

	//Input
	//Input: B5 -> Right_JogPB
	DDRB = DDRB & 0xDF;
	FCV_RIGHT_JOGPB = ((PINB & 0x20) == 0x20);

	//Input
	//Input: B6 -> Down_StopPB
	DDRB = DDRB & 0xBF;
	FCV_DOWN_STOPPB = ((PINB & 0x40) == 0x40);

	//Input
	//Input: B7 -> Left_SetupPB
	DDRB = DDRB & 0x7F;
	FCV_LEFT_SETUPPB = ((PINB & 0x80) == 0x80);

}


void FCM_CLS()
{

	//Code to clear LCD
	//Call Component Macro: SendRS232Char(0xfe)
	FCD_RS2320_SendRS232Char(0xfe);

	//Code to clear LCD
	//Call Component Macro: SendRS232Char(0x51)
	FCD_RS2320_SendRS232Char(0x51);

}

//The program Calls Init( Sets LCD Contrast and Outputs 1 to switches). Then a macro to sense the switch positions and assign the inputs to variables. Then an ISR to print "Got It" if a PB is pushed. Whe while(1) loop prints something, and cycles forever. 

int main()
{
	//Initialization
	MCUCR=0x00;
	MCUSR=0x00;
	wdt_disable();

	
		RS232_11_UART_Init( );		//Call initialise function


	//Interrupt initialization code
	EIMSK=0x00;


	//Init
	//Call Macro: Init()
	FCM_Init();

	//SensePortB
	//Call Macro: SensePortB_High()
	FCM_SensePortB_High();

	//Interrupt
	//Interrupt: Enable PORTB
	sei();
	PCMSK1 = 0xF0;
	PCICR |= (1 << PCIE1);

	//Loop
	//Loop: While 1
	while (1)
	{

		//Print "Waiting"
		//Call Component Macro: SendRS232String("Waiting")
		FCD_RS2320_SendRS232String("Waiting", 7);

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

		//CLS
		//Call Macro: CLS()
		FCM_CLS();


	}

	mainendloop: goto mainendloop;
}

	//Handler code for [PORTB]
	#ifndef MX_PCINT0_HANDLER
	#define MX_PCINT0_HANDLER
	ISR(PCINT0_vect)
	{
		FCM_DisplayPB_Pushed();
	}
	#else
	#warning The <PORTB Change> interrupt has previously been enabled, so the macro <DisplayPB_Pushed> may never get called.
	#endif




