/************************************************************************
*	Program:		valve.c												*
*	Description:	This program was written to test the pneumatic		*
*					valves and cylinders.  It will prompt the user		*
*					to select the desired function (manual or 			*
*					automatic positioning) and to select the desired	*
*					characteristics of the PWM signal for the			*
*					valves.												*
*	Programmer:		Scott Nortman										*
*	Date:			February 3, 2000									*
*************************************************************************/

/**************************** Constants *********************************/
//#define valve TOC
#define tolerance 10 
#define valve_sensor analog(1)
/**************************** End of Constants **************************/

/*************************** Interrupts *********************************/
#pragma interrupt_handler TOC2_isr
/*************************** End of Interrupts **************************/

/************************** Includes ************************************/
#include <stdio.h>
#include <tjpbase.h>
#include <analog.h>
#include <vectors.h>
#include <math.h>
#include <mil.h>
#include <hc11.h>
/************************ End of includes *******************************/

/************************** Global Variables*****************************/
unsigned int duty_cycle, period;

/************************ End of Global Variables ***********************/


/************************** Function Declarations  **********************/
void valve_position (unsigned int , unsigned int , unsigned int);
void PWM (unsigned int , unsigned int);
void done();
/******************** End of Function Declarations  *********************/

/************************** Function Defintions *************************/
void valve_position (unsigned int position, unsigned int duty, unsigned int pd)
// This function sets the desired position of the pneumatic cylinder
// shaft +/- the tolerance.  The cylinder moves to its new position
// at a speed determined by the PWM function.
{
	
	printf("You're  at the beginning of valve_position.\n");
	while (position > (valve_sensor + tolerance))	//If the cylinder is too far out, move it in
	{
		printf("The analog value is:\t%d\n", valve_sensor);
		SET_BIT (PORTA, 0x80);		//This sets PORTA bit7 to move cylinder in
		PWM (duty, pd);	//After the bit is set, PWM to move the cylinder
		/*if (valve_sensor - tolerance < position && position > valve_sensor + tolerance){
			done();
			return;
			}*/
	} 
	while (position < (valve_sensor - tolerance))
	{
		printf("The analog value is:\t%d\n", valve_sensor);
		CLEAR_BIT (PORTA, 0x80);
		PWM (duty, pd);
		/*if (valve_sensor - tolerance < position && position > valve_sensor + tolerance){
			done();
			return;
		}*/
	}	
}

void PWM ( unsigned int duty_cycle2, unsigned int period2)//This is the function to generate PWM
{
	printf("You're at PWM.\n");
	SET_BIT (TMSK1, 0x40); //Enable interrupt on OC2
	SET_BIT (TCTL1, 0x40); //Enable toggle
	duty_cycle = duty_cycle2;
	period = period2;
	
}

void done()
{
	CLEAR_BIT (TMSK1, 0x40); //Disable interrupt on OC2
	SET_BIT (TCTL1, 0x80); //set OC2 line to 0 on compare
	SET_BIT (CFORC, 0x40); //force successful compare
}

void TOC2_isr()
{
//	if (TCTL1 & 0x40)
//	{
//		CLEAR_BIT (TCTL1, 0x40); //Clear bit to set for falling edge next interrupt

		TOC2 += duty_cycle;		//Add time to TOC2 for duty cycle
//	}
//	if (!(TCTL1 & 0x40))
//	{
//		SET_BIT (TCTL1, 0x40);
//		TOC2 += (period - duty_cycle);
//	}
	
	CLEAR_FLAG(TFLG1, 0x40); //Clear interrupt flag
}
/************************** End of Function Defintions ******************/

/**************************** Main **************************************/

unsigned int x, y, z;
void main(void)
{
	/* VT100 clear screen */ 
	char clear[]= "\x1b\x5B\x32\x4A\x04"; 
	
	/* VT100 position cursor at (x,y) = (3,12) command is "\x1b[3;12H"*/
  	char place[]= "\x1b[1;1H";	/*Home*/
	
	SET_BIT(PACTL, 0x80);  	
  	init_analog();
  	init_clocktjp();
  	init_serial();

  	INTR_ON(); /*turn on HC11 interrupt system*/

  	printf("%s", clear);  
  	printf("%s", place);
  	
  	printf("\tTitle:\t\tvalve.c\n"
  "\tProgrammer:\tScott Nortman\n"
  "\tDate:\t\tJanuary, 2000\n"
  "\tVersion\t\t1\n\n");
  
	printf("PNEUMATIC CYLINDER DIAGNOSTIC PROGRAM\n\n");
	
	while(1)
	{
		printf("Enter number of e clocks for the period\n");
		z = read_int();
		printf("Enter e clocks for duty cycle\n");
		y = read_int();
		printf("Enter the desired position\n");
		x = read_int();
		
		valve_position(x, y, z);
		
//		printf("%s", clear);  
 //		printf("%s", place);
		
	}/*End While*/
  	
}/*End Main*/


/**************************** End of Main *******************************/