//************************************************************************************
//**  
//**  File name:     C:\Users\PKSINGH\Desktop\FLOWCODE FILES UNDER TEST\pulse measurement\PulseMeasure.c
//**  Generated by:  Flowcode v4.3.6.61
//**  Date:          Friday, May 27, 2011 16:11:08
//**  Licence:       Professional
//**  Registered to: PRAVAT SINGH
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_AVR

//Defines for microcontroller
#define MX_AVR
#define MX_EE
#define MX_EE_SIZE 1024
#define MX_SPI
#define MX_SPI_B
#define MX_SPI_SDI 6
#define MX_SPI_SDO 5
#define MX_SPI_SCK 7
#define MX_UART
#define MX_UART_D
#define MX_UART_TX 1
#define MX_UART_RX 0
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 1
#define MX_I2C_SCL 0
#define MX_PWM
#define MX_PWM_PORT PORTD
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 DDRD
#define MX_PWM_1 5
#define MX_PWM_TRIS2 DDRD
#define MX_PWM_2 4
#define MX_PWMTYPE 1

//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>
#include <MX_util\delay.h>
#include <MX_util\bit_cmds.h>

//Configuration data
#pragma DATA 0x0, 0xdf
#pragma DATA 0x1, 0xff

//ICD functionality
#define USE_FLOWCODE_ICD
#define ICD_CLK_PORT 3
#define ICD_DATA_PORT 3
#define ICD_CLK_PIN 0
#define ICD_DATA_PIN 1
#define ICD_NUM_BPS 8
signed int brk_pnt[ICD_NUM_BPS] = {-1, -1, -1, -1, -1, -1, -1, -1};
#define ICD_STACK_SIZE 8
unsigned char icd_stack[ICD_STACK_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
#define ICD_ID0 0x6a
#define ICD_ID1 0x53
#define ICD_ID2 0x40
#define ICD_ID3 0x86
#define ICD_ID4 0x0d
#define ICD_ID5 0x53
#define ICD_ID6 0x6b
#define ICD_ID7 0x3a

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

//Macro function declarations
void FCM_int();


//Variable declarations
#define FCSZ_TIME_STR 20
volatile double FCV_FLO_COUNT;
volatile short FCV_INT_COUNT;
volatile char FCV_TIME_STR[FCSZ_TIME_STR];
volatile double FCV_TIME;


//Defines:

/**** Macro Substitutions ****
PORTA = D1 Port
DDRA = D1 Data Direction
PORTA = D2 Port
DDRA = D2 Data Direction
PORTA = D3 Port
DDRA = D3 Data Direction
PORTA = D4 Port
DDRA = D4 Data Direction
PORTA = RS Port
DDRA = RS Data Direction
PORTA = E Port
DDRA = E Data Direction
2 = Data 1_Pin
3 = Data 2 Pin
4 = Data 3 Pin
5 = Data 4 Pin
0 = RS Pin
1 = Enable Pin
LCD_1573592 = Unique Component Reference Number
2 = Row Count
16 = Column Count
******************************/

	//component connections
	#define LCD_1573592_PORT0    PORTA
	#define LCD_1573592_DDR0    DDRA
	#define LCD_1573592_PORT1    PORTA
	#define LCD_1573592_DDR1    DDRA
	#define LCD_1573592_PORT2    PORTA
	#define LCD_1573592_DDR2    DDRA
	#define LCD_1573592_PORT3    PORTA
	#define LCD_1573592_DDR3    DDRA
	#define LCD_1573592_PORT4    PORTA
	#define LCD_1573592_DDR4    DDRA
	#define LCD_1573592_PORT5    PORTA
	#define LCD_1573592_DDR5    DDRA
	#define LCD_1573592_BIT0    	2
	#define LCD_1573592_BIT1    	3
	#define LCD_1573592_BIT2    	4
	#define LCD_1573592_BIT3    	5
	#define LCD_1573592_RS      	0
	#define LCD_1573592_E       	1
	#define LCD_1573592_ROWCNT	2
	#define LCD_1573592_COLCNT	16

    #define LCD_1573592_DELAY   delay_us(100)




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

static void FCD_LCDDisplay0_RawSend(char in, char mask);
static void FCD_LCDDisplay0_Start();
static void FCD_LCDDisplay0_Clear();
static void FCD_LCDDisplay0_PrintASCII(char Character);
static void FCD_LCDDisplay0_Command(char in);
static void FCD_LCDDisplay0_Cursor(char x, char y);
static void FCD_LCDDisplay0_PrintNumber(short Number);
static void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String);
static void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions);
static void FCD_LCDDisplay0_ClearLine(char Line);
static void FCD_LCDDisplay0_RAM_Write(char nIdx, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7);
//Defines:

/**** Macro Substitutions ****
PINA = Switch Pin Register
DDRA = Switch Data Direction Register
0 = Switch Pin
0 = Switch Polarity Control
0 = Switch Debounce Delay Value
******************************/




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

static char FCD_SWITCH0_ReadState();
static void FCD_SWITCH0_WaitUntilHigh();
static void FCD_SWITCH0_WaitUntilLow();

//Supplementary defines
char ICD_Ignore = 0; 



//LCDDisplay(0): //Macro implementations


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

		clear_bit(LCD_1573592_PORT0, LCD_1573592_BIT0);
		clear_bit(LCD_1573592_PORT1, LCD_1573592_BIT1);
		clear_bit(LCD_1573592_PORT2, LCD_1573592_BIT2);
		clear_bit(LCD_1573592_PORT3, LCD_1573592_BIT3);
		clear_bit(LCD_1573592_PORT4, LCD_1573592_RS);
		clear_bit(LCD_1573592_PORT5, LCD_1573592_E);
		pt = ((in >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(LCD_1573592_PORT0, LCD_1573592_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_1573592_PORT1, LCD_1573592_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_1573592_PORT2, LCD_1573592_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_1573592_PORT3, LCD_1573592_BIT3);
		if (mask)
		    set_bit(LCD_1573592_PORT4, LCD_1573592_RS);
		LCD_1573592_DELAY;
		set_bit (LCD_1573592_PORT5, LCD_1573592_E);
		LCD_1573592_DELAY;
		clear_bit (LCD_1573592_PORT5, LCD_1573592_E);
		pt = (in & 0x0f);
		LCD_1573592_DELAY;
		clear_bit(LCD_1573592_PORT0, LCD_1573592_BIT0);
		clear_bit(LCD_1573592_PORT1, LCD_1573592_BIT1);
		clear_bit(LCD_1573592_PORT2, LCD_1573592_BIT2);
		clear_bit(LCD_1573592_PORT3, LCD_1573592_BIT3);
		clear_bit(LCD_1573592_PORT4, LCD_1573592_RS);
		clear_bit(LCD_1573592_PORT5, LCD_1573592_E);
		if (pt & 0x01)
		    set_bit(LCD_1573592_PORT0, LCD_1573592_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_1573592_PORT1, LCD_1573592_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_1573592_PORT2, LCD_1573592_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_1573592_PORT3, LCD_1573592_BIT3);
		if (mask)
		    set_bit(LCD_1573592_PORT4, LCD_1573592_RS);
		LCD_1573592_DELAY;
		set_bit (LCD_1573592_PORT5, LCD_1573592_E);
		LCD_1573592_DELAY;
		clear_bit (LCD_1573592_PORT5, LCD_1573592_E);
		LCD_1573592_DELAY;
}

static void FCD_LCDDisplay0_Start()
{
	
		set_bit(LCD_1573592_DDR0, LCD_1573592_BIT0);
		set_bit(LCD_1573592_DDR1, LCD_1573592_BIT1);
		set_bit(LCD_1573592_DDR2, LCD_1573592_BIT2);
		set_bit(LCD_1573592_DDR3, LCD_1573592_BIT3);
		set_bit(LCD_1573592_DDR4, LCD_1573592_RS);
		set_bit(LCD_1573592_DDR5, LCD_1573592_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);

}

static void FCD_LCDDisplay0_Clear()
{
	
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);

}

static void FCD_LCDDisplay0_PrintASCII(char Character)
{
	
		FCD_LCDDisplay0_RawSend(Character, 0x10);

}

static void FCD_LCDDisplay0_Command(char in)
{
	
		FCD_LCDDisplay0_RawSend(in, 0);
		Wdt_Delay_Ms(2);

}

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

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

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

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

		#if (LCD_1573592_COLCNT == 20)
			else if (y==2)
				y=0x94;
			else
				y=0xd4;
		#endif
	  #endif

		FCD_LCDDisplay0_RawSend(y+x, 0);
		Wdt_Delay_Ms(2);

}

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

}

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

}

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

}

static void FCD_LCDDisplay0_ClearLine(char Line)
{
	
		char count;
		char rowcount;

		//Define number of columns per line
		#if (LCD_1573592_ROWCNT == 1)
			rowcount=80;
		#endif

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

		#if (LCD_1573592_ROWCNT == 4)
			#if (LCD_1573592_COLCNT == 16)
				rowcount=16;
			#endif
			#if (LCD_1573592_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);

}

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



//SWITCH(0): //Macro implementations


static char FCD_SWITCH0_ReadState()
{
	
		char switchval;

		clear_bit ( DDRA, 0 );				//Convert pin to input
		asm("nop");							//Allow input voltage to settle

		if (test_bit( PINA, 0 ))				//Read state on input
			switchval = 1;
		else
			switchval = 0;

		if ( 0 == 1 )						//Active low polarity
		{
			if (switchval)					//Invert logic
				switchval = 0;
			else
				switchval = 1;
		}
		return (switchval);			//Return state of switch as least significant bit

}

static void FCD_SWITCH0_WaitUntilHigh()
{
	
		clear_bit ( DDRA, 0 );				//Convert pin to input
		asm("nop");							//Allow input voltage to settle

		if ( 0  == 0 )							//Active high polarity
		{
			while (test_bit( PINA, 0 ) == 0);	//Wait for pin to go high
		}
		else									//Active low polarity
		{
			while (test_bit( PINA, 0 ));			//Wait for pin to go low
		}
		if ( 0 )								//Timeout specified?
			Wdt_Delay_Ms(0);

}

static void FCD_SWITCH0_WaitUntilLow()
{
	
		clear_bit ( DDRA, 0 );					//Convert pin to input
		asm("nop");								//Allow input voltage to settle

		if ( 0 == 0 )							//Active high polarity
		{
			while (test_bit( PINA, 0 ));			//Wait for pin to go low
		}
		else									//Active low polarity
		{
			while (test_bit( PINA, 0 ) == 0);	//Wait for pin to go high
		}
		if ( 0 )								//Timeout specified?
			Wdt_Delay_Ms(0);

}

//Macro implementations

void FCM_int()
{
	
	ICD_PUSH(1);

	FC_MARKER(16);
	FC_MARKER(17);
	//Calculation
	//Calculation:
	//  int_count = int_count + 1
	FCV_INT_COUNT = FCV_INT_COUNT + 1;
	

	FC_MARKER(18);
	ICD_POP();

}

//Supplementary implementations


int main()
{
	
	//Initialisation
	MCUCSR=0x00;
	wdt_disable();


	//Interrupt initialisation code
	
	//Enable initial stop condition
	dbg_mode = 0x08;

	ICD_PUSH(0);

	FC_MARKER(0);
	FC_MARKER(1);
	//Call Component Macro
	//Call Component Macro: LCDDisplay(0)::Start
	FCD_LCDDisplay0_Start();


	FC_MARKER(2);
	//Loop
	//Loop: While 1
	while (1)
	{
		FC_MARKER(2);
		if ((1) == 0) break;
		FC_MARKER(3);
		//Calculation
		//Calculation:
		//  int_count = 0
		FCV_INT_COUNT = 0;
		

		FC_MARKER(4);
		//Call Component Macro
		//Call Component Macro: SWITCH(0)::WaitUntilHigh
		FCD_SWITCH0_WaitUntilHigh();


		FC_MARKER(5);
		//Interrupt
		//Interrupt: Enable TMR0
		TCCR0 &= 0xf8;
		TCCR0 |= 0x01;
		sei();
		TIMSK |= (1 << TOIE0);


		FC_MARKER(6);
		//Call Component Macro
		//Call Component Macro: SWITCH(0)::WaitUntilLow
		FCD_SWITCH0_WaitUntilLow();


		FC_MARKER(7);
		//Interrupt
		//Interrupt: Disable TMR0
		TIMSK &= ~(1 << TOIE0);


		FC_MARKER(8);
		//Calculation
		//Calculation:
		//  flo_count = int2float(int_count)
		FCV_FLO_COUNT = float32_from_int32(FCV_INT_COUNT);
		

		FC_MARKER(9);
		//Calculation
		//Calculation:
		//  time = fdiv(flo_count, 9600)
		FCV_TIME = float32_div(FCV_FLO_COUNT, 9600);
		

		FC_MARKER(10);
		//String Manipulation
		//String Manipulation:
		//  time_str = FloatToString$(time)
		FCI_FLOAT_TO_STRING(FCV_TIME,6,FCV_TIME_STR,FCSZ_TIME_STR);
		

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


		FC_MARKER(12);
		//Call Component Macro
		//Call Component Macro: LCDDisplay(0)::PrintString(time_str)
		FCD_LCDDisplay0_PrintString(FCV_TIME_STR,FCSZ_TIME_STR);


		FC_MARKER(13);
		//Call Component Macro
		//Call Component Macro: LCDDisplay(0)::PrintString(" secs")
		FCD_LCDDisplay0_PrintString(" secs",5);


		FC_MARKER(14);
	}


	FC_MARKER(15);
	ICD_POP();

	//Code to signal end of program to ICD
	dbg_mode = 0x08;

	FC_MARKER(-1);

	mainendloop: goto mainendloop;
}

	//Handler code for [TMR0]
	#ifndef MX_TIMER0OVF_HANDLER
	#define MX_TIMER0OVF_HANDLER
	ISR(TIMER0_OVF_vect)
	{
		FCM_int();
	}
	#else
	#warning The <TIMER0 OVF> interrupt has previously been enabled, so the macro <int> may never get called.
	#endif




