//************************************************************************************
//**  
//**  File name:     C:\Documents and Settings\user1\Desktop\flowcode\jbuggy sound meter.c
//**  Generated by:  Flowcode v4.3.7.63
//**  Date:          Wednesday, May 12, 2010 21:29:31
//**  Licence:       Professional
//**  Registered to: Julian Silver
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Defines for microcontroller
#define P18F4455
#define MX_EE
#define MX_EE_TYPE3
#define MX_EE_SIZE 256
#define MX_SPI
#define MX_SPI_BCB
#define MX_SPI_SDI 0
#define MX_SPI_SDO 7
#define MX_SPI_SCK 1
#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_B
#define MX_I2C_SDA 0
#define MX_I2C_SCL 1
#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
#define MX_USB_TYPE1

//Functions
#define MX_CLK_SPEED 48000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic18.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x300000, 0x20
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300001, 0xe
#endif
#ifdef _HI_TECH_C
__CONFIG(1, 0xe20);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300002, 0x3f
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300003, 0x1e
#endif
#ifdef _HI_TECH_C
__CONFIG(2, 0x1e3f);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300004, 0x0
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300005, 0x81
#endif
#ifdef _HI_TECH_C
__CONFIG(3, 0x8100);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300006, 0x81
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300007, 0x0
#endif
#ifdef _HI_TECH_C
__CONFIG(4, 0x81);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300008, 0xf
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300009, 0x80
#endif
#ifdef _HI_TECH_C
__CONFIG(5, 0x800f);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000a, 0xf
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000b, 0xa0
#endif
#ifdef _HI_TECH_C
__CONFIG(6, 0xa00f);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000c, 0xf
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000d, 0x0
#endif
#ifdef _HI_TECH_C
__CONFIG(7, 0xf);
#endif

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

//Macro function declarations


//Variable declarations
char FCV_MIC;


//Defines:

/***** Macro Substitutions ****
0 = ADC Conversion 8-bit or 10-bit
200 = Left IR Threshold
200 = Front IR Threshold
200 = Right IR Threshold
128 = Unused
128 = Unused
128 = Unused
1 = Wait for button press at start
*******************************/

//common defines
#define MX_RETTYPE       0
#define MX_L_THRESH      200
#define MX_F_THRESH      200
#define MX_R_THRESH      200
#define MX_WAITFORBUTTON 1
#define MX_MOTORBIAS_L	 0
#define MX_MOTORBIAS_R	 0

//ADC Channel Defines
#define MX_ADC_LDR    9
#define MX_ADC_IRL    1
#define MX_ADC_IRF    3
#define MX_ADC_IRR    4
#define MX_ADC_MIC    8




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

void FCD_FormulaFlowcode0_FF_Enable(char nIdx);
void FCD_FormulaFlowcode0_FF_SetDutyCycle(char nIdx, char nDuty);
void FCD_FormulaFlowcode0_FF_SampleADC(char channel);
short FCD_FormulaFlowcode0_FF_ReadADC();
void FCD_FormulaFlowcode0_Forward(char power);
void FCD_FormulaFlowcode0_Reverse(char power);
void FCD_FormulaFlowcode0_Stop();
void FCD_FormulaFlowcode0_SpinLeft(char power);
void FCD_FormulaFlowcode0_SpinRight(char power);
void FCD_FormulaFlowcode0_SetMotors(short left_power, short right_power);
void FCD_FormulaFlowcode0_WriteLEDs(char byte_out);
void FCD_FormulaFlowcode0_LEDOn(char led);
void FCD_FormulaFlowcode0_LEDOff(char led);
char FCD_FormulaFlowcode0_ReadSwitch(char sensor);
void FCD_FormulaFlowcode0_WaitForSwitch(char sensor);
void FCD_FormulaFlowcode0_PlayNote(char note, short delay_ms);
short FCD_FormulaFlowcode0_ReadMic();
short FCD_FormulaFlowcode0_ReadLDR();
char FCD_FormulaFlowcode0_CheckIR(char sensor);
short FCD_FormulaFlowcode0_ReadIRSensor(char sensor);
char FCD_FormulaFlowcode0_ReadLineSensor(char sensor);



//FormulaFlowcode(0): //Macro implementations


void FCD_FormulaFlowcode0_FF_Enable(char nIdx)
{
	    pr2 = 255;
	    t2con = 0x04;
	    if (nIdx == 1)
	    {
	        ccp1con = 0x0C;
	        clear_bit(trisc, 2);
	    }
	    if (nIdx == 2)
	      {
	        ccp2con = 0x0C;
	        clear_bit(trisc, 1);
	    }
}

void FCD_FormulaFlowcode0_FF_SetDutyCycle(char nIdx, char nDuty)
{
	
	//Left motor
	    if (nIdx == 1)
	    {
	        ccpr1l = nDuty;
	    }

	//Right motor
	    else if (nIdx == 2)
	    {
	        ccpr2l = nDuty;
	    }
}

void FCD_FormulaFlowcode0_FF_SampleADC(char channel)
{
	    char cnt;
	    adcon1 = 0x00;
	    adcon2 = 0x03;
	    adcon0 = 0x01 | (channel << 2);
	    cnt = 0;
	    while (cnt < 100) cnt++;
	    adcon0 = adcon0 | 0x02;
	    while (adcon0 & 0x02) ;
	    adcon1 = 0x0f;
	    adcon0 = 0x00;
}

short FCD_FormulaFlowcode0_FF_ReadADC()
{
		#if (MX_RETTYPE == 0)
			return adresh & 0xFF;
		#else
			short iRetVal;
			iRetVal = (adresh << 2);
			iRetVal += (adresl >> 6);
			return (iRetVal);
		#endif
}

void FCD_FormulaFlowcode0_Forward(char power)
{
		char power_L, power_R;
	    char motor_power_L, motor_power_R;
	    trise = trise & 0xFC;
	    porte = porte | 0x03;

		char scaleFactor;
		unsigned int scaledVal;

		if(MX_MOTORBIAS_L)				//Left bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_L) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			power_L = scaledVal;
		}else{
			power_L = power;
		}

		if(MX_MOTORBIAS_R)				//Right bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_R) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			power_R = scaledVal;
		}else{
			power_R = power;
		}

	    motor_power_L = 255 - power_L;
	    motor_power_R = 255 - power_R;

	    FCD_FormulaFlowcode0_FF_SetDutyCycle(1, motor_power_R);
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(2, motor_power_L);
}

void FCD_FormulaFlowcode0_Reverse(char power)
{
		char power_L, power_R;
	    char motor_power_L, motor_power_R;
	    trise = trise & 0xFC;
	    porte = porte & 0xFC;

		char scaleFactor;
		unsigned int scaledVal;

		if(MX_MOTORBIAS_L)				//Left bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_L) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			power_L = scaledVal;
		}else{
			power_L = power;
		}

		if(MX_MOTORBIAS_R)				//Right bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_R) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			power_R = scaledVal;
		}else{
			power_R = power;
		}

	    motor_power_L = power_L;
	    motor_power_R = power_R;

	    FCD_FormulaFlowcode0_FF_SetDutyCycle(1, motor_power_R);
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(2, motor_power_L);
}

void FCD_FormulaFlowcode0_Stop()
{
	    trise = trise & 0xFC;
	    porte = porte & 0xFC;

	    FCD_FormulaFlowcode0_FF_SetDutyCycle(1, 0);
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(2, 0);
}

void FCD_FormulaFlowcode0_SpinLeft(char power)
{
	    char motor_left, motor_right;
	    char motor_power;
	    trise = trise & 0xFC;
	    porte = porte & 0xFC;

		char scaleFactor;
		unsigned int scaledVal;

		if(MX_MOTORBIAS_L)				//Left bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_L) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			motor_left = scaledVal;
		}else{
			motor_left = power;
		}

		if(MX_MOTORBIAS_R)				//Right bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_R) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			motor_right = scaledVal;
		}else{
			motor_right = power;
		}

	    motor_right = 255 - power;
	    motor_left = power;

	    porte = porte | 0x02;
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(1, motor_right);
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(2, motor_left);
}

void FCD_FormulaFlowcode0_SpinRight(char power)
{
	    char motor_left, motor_right;
	    trise = trise & 0xFC;
	    porte = porte & 0xFC;

		char scaleFactor;
		unsigned int scaledVal;

		if(MX_MOTORBIAS_L)				//Left bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_L) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			motor_left = scaledVal;
		}else{
			motor_left = power;
		}

		if(MX_MOTORBIAS_R)				//Right bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_R) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			motor_right = scaledVal;
		}else{
			motor_right = power;
		}

	    motor_right = power;
	    motor_left = 255 - power;

	    porte = porte | 0x01;
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(1, motor_right);
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(2, motor_left);
}

void FCD_FormulaFlowcode0_SetMotors(short left_power, short right_power)
{
	    char motor_left, motor_right, power;
	    trise = trise & 0xFC;
	    porte = porte & 0xFC;

		char scaleFactor;
		unsigned int scaledVal;

		//absolute power
		if (left_power < 0)
			power = 0 - left_power;
		else
			power = left_power;

	    //max power is +/-255
		if (power > 255) power = 255;

		if(MX_MOTORBIAS_L)				//Left bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_L) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			power = scaledVal;
		}

	    if (left_power >= 0)
	    {
	        porte = porte | 0x01;
	        motor_left = 255 - power;
	    }
	    else
	    {
	        motor_left = power;
	    }


		//absolute power
		if (right_power < 0)
			power = 0 - right_power;
		else
			power = right_power;

		if(MX_MOTORBIAS_R)				//Left bias - reduce left motor speed
		{
			scaleFactor = (255 - MX_MOTORBIAS_R) + 1;
			scaledVal = (unsigned int)(power * scaleFactor); 	//New value in high byte of result
			scaledVal >>= 8;									//Shift high byte to low byte
			power = scaledVal;
		}

	    if (right_power >= 0)
	    {
	        porte = porte | 0x02;
	        motor_right = 255 - power;
	    }
	    else
	    {
	        motor_right = power;
	    }

	    FCD_FormulaFlowcode0_FF_SetDutyCycle(1, motor_right);
	    FCD_FormulaFlowcode0_FF_SetDutyCycle(2, motor_left);
}

void FCD_FormulaFlowcode0_WriteLEDs(char byte_out)
{
	    trisd = 0x00;
	    portd = byte_out;
}

void FCD_FormulaFlowcode0_LEDOn(char led)
{
	    trisd = 0x00;
	    portd |= (1 << led);
}

void FCD_FormulaFlowcode0_LEDOff(char led)
{
	    trisd = 0x00;
	    portd &= ~(1 << led);
}

char FCD_FormulaFlowcode0_ReadSwitch(char sensor)
{
	    char out = 0;
	    char retVal = 0;
	    trisb = trisb | 0x30;
	    sensor = sensor & 0xDF;

	    if (sensor == 'L' || sensor == 0)
	    {
	        if(test_bit(portb, 5))
	        	retVal = 1;
		}
	    else
	    {
	        if(test_bit(portb, 4))
	        	retVal = 1;
		}

	    return retVal;
}

void FCD_FormulaFlowcode0_WaitForSwitch(char sensor)
{
	    char iMask = 0x10;
	    char iButtonPress = 0;

	    trisb = trisb | 0x30;
	    sensor = sensor & 0xDF;

	    if (sensor == 'L' || sensor == 0)
	        iMask = 0x20;

	    //wait for either button down
	    while (!iButtonPress)
	        iButtonPress = portb & iMask;

	    //delay for basic debouncing of the switch
	    delay_ms(200);

	    //wait for either button up
	    while (iButtonPress)
	        iButtonPress = portb & iMask;

}

void FCD_FormulaFlowcode0_PlayNote(char note, short delay_ms)
{
	    char inv_note;
	    short delayus, delayms, loop;
	    inv_note = 255 - note;
	    delayus = (20 * inv_note);
	    delayms = delayus / 1000;
	    if (delayms == 0)
	        delayms = 1;
	    trisc = trisc & 0xFE;
	    clear_bit(portc, 0);
	    for(loop=0; (loop * delayms) < delay_ms; loop++)
	    {
	        delay_10us(inv_note);
	        set_bit(portc, 0);
	        delay_10us(inv_note);
	        clear_bit(portc, 0);
	    }
}

short FCD_FormulaFlowcode0_ReadMic()
{
	    int temp = 0;
	    trisb = trisb | 0x04;

	    FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_MIC);
	    temp = FCD_FormulaFlowcode0_FF_ReadADC();
	    return (temp);
}

short FCD_FormulaFlowcode0_ReadLDR()
{
	    int temp = 0;
	    trisb = trisb | 0x08;

	    FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_LDR);
	    temp = FCD_FormulaFlowcode0_FF_ReadADC();
	    return (temp);
}

char FCD_FormulaFlowcode0_CheckIR(char sensor)
{
	    int temp = 0;
	    int thresh;
	    porta = 0x00;
	    trisa = 0x2A;
	    sensor = sensor & 0xDF;
	    if (sensor == 'L' || sensor == 0)
	    {
	        porta = 0x01;
	        delay_10us(1);
	        FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_IRL);
	        porta = 0x00;
	        thresh = MX_L_THRESH;
	    }
	    else if (sensor == 'F' || sensor == 1)
	    {
	        porta = 0x04;
	        delay_10us(1);
	        FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_IRF);
	        porta = 0x00;
	        thresh = MX_F_THRESH;
	    }
	    else
	    {
	        porta = 0x10;
	        delay_10us(1);
	        FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_IRR);
	        porta = 0x00;
	        thresh = MX_R_THRESH;
	    }

	    temp = FCD_FormulaFlowcode0_FF_ReadADC();

	    if (thresh > temp)
	        return 1;
	    else
	        return 0;
}

short FCD_FormulaFlowcode0_ReadIRSensor(char sensor)
{
	    int temp = 0;
	    porta = 0x00;
	    trisa = 0x2A;
	    sensor = sensor & 0xDF;

	    if (sensor == 'L' || sensor == 0)
	    {
	        porta = 0x01;
	        delay_10us(1);
	        FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_IRL);
	        porta = 0x00;
	    }
	    else if (sensor == 'F' || sensor == 1)
	    {
	        porta = 0x04;
	        delay_10us(1);
	        FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_IRF);
	        porta = 0x00;
	    }
	    else
	    {
	        porta = 0x10;
	        delay_10us(1);
	        FCD_FormulaFlowcode0_FF_SampleADC(MX_ADC_IRR);
	        porta = 0x00;
	    }

	    temp = FCD_FormulaFlowcode0_FF_ReadADC();
	    return (temp);
}

char FCD_FormulaFlowcode0_ReadLineSensor(char sensor)
{
		char retVal = 0;
	    trisb = trisb | 0x03;
	    sensor = sensor & 0xDF;

	    if (sensor == 'L' || sensor == 0)
	    {
	        if(test_bit(portb, 1))
	        	retVal = 1;
		}
	    else
	    {
	        if(test_bit(portb, 0))
	        	retVal = 1;
		}
	    return retVal;
}

//Macro implementations

void main()
{
	
	//Initialisation
	adcon1 = 0x0F;
	ucfg = 0x08;
cr_bit(pie2, USBIE);

	#if (MX_WAITFORBUTTON != 0)
	    char iButtonPress = 0;

	    //wait for either button down
	    while (!iButtonPress)
	        iButtonPress = portb & 0x30;

	    //delay for basic debouncing of the switch
	    delay_ms(200);

	    //wait for either button up
	    while (iButtonPress)
	        iButtonPress = portb & 0x30;
	#endif

	    //enable PWMs and reset them
	    clear_bit(trisc, 2);
	    clear_bit(trisc, 1);
	    pr2 = 255;
	    t2con = 0x04;
	    ccpr1l = 0;
	    ccpr2l = 0;
	    ccp1con = 0x0C;
	    ccp2con = 0x0C;


	//Interrupt initialisation code
	


	//Loop
	//Loop: While 1
	while (1)
	{
		//Call Component Macro
		//Call Component Macro: mic=FormulaFlowcode(0)::ReadMic
		FCV_MIC = FCD_FormulaFlowcode0_ReadMic();


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


		//Output
		//Output: 0 -> PORT D
		trisd = 0x00;
		portd = 0;


		//Decision
		//Decision: mic>0?
		if (FCV_MIC>0)
		{
			//Output
			//Output: 1 -> D0
			trisd = trisd & 0xfe;
			if (1)
				portd = (portd & 0xfe) | 0x01;
			else
				portd = portd & 0xfe;


		}


		//Decision
		//Decision: mic>31?
		if (FCV_MIC>31)
		{
			//Output
			//Output: 1 -> D1
			trisd = trisd & 0xfd;
			if (1)
				portd = (portd & 0xfd) | 0x02;
			else
				portd = portd & 0xfd;


		}


		//Decision
		//Decision: mic>62?
		if (FCV_MIC>62)
		{
			//Output
			//Output: 1 -> D2
			trisd = trisd & 0xfb;
			if (1)
				portd = (portd & 0xfb) | 0x04;
			else
				portd = portd & 0xfb;


		}


		//Decision
		//Decision: mic>93?
		if (FCV_MIC>93)
		{
			//Output
			//Output: 1 -> D3
			trisd = trisd & 0xf7;
			if (1)
				portd = (portd & 0xf7) | 0x08;
			else
				portd = portd & 0xf7;


		}


		//Decision
		//Decision: mic>124?
		if (FCV_MIC>124)
		{
			//Output
			//Output: 1 -> D4
			trisd = trisd & 0xef;
			if (1)
				portd = (portd & 0xef) | 0x10;
			else
				portd = portd & 0xef;


		}


		//Decision
		//Decision: mic>155?
		if (FCV_MIC>155)
		{
			//Output
			//Output: 1 -> D5
			trisd = trisd & 0xdf;
			if (1)
				portd = (portd & 0xdf) | 0x20;
			else
				portd = portd & 0xdf;


		}


		//Decision
		//Decision: mic>186?
		if (FCV_MIC>186)
		{
			//Output
			//Output: 1 -> D6
			trisd = trisd & 0xbf;
			if (1)
				portd = (portd & 0xbf) | 0x40;
			else
				portd = portd & 0xbf;


		}


		//Decision
		//Decision: mic>217?
		if (FCV_MIC>217)
		{
			//Output
			//Output: 1 -> D7
			trisd = trisd & 0x7f;
			if (1)
				portd = (portd & 0x7f) | 0x80;
			else
				portd = portd & 0x7f;


		}


	}


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}



