//**************************************************************************
// JI30-35 Vent Door Controller                        S215  10 0930
// *************************************************************************
#include "s215-header16-f45.h" 
#pragma code
//||||||||||||||||||||||||| BEGINNING OF MAIN PROGRAM ||||||||||||||||||||||
void main (void)
{
char rightswitch, leftswitch, mtrcw, mtrccw, powertomotor, boxhot;

InitPorts();
//----------------------------------
while (1==1)
{
//|||||||||||||||||||||||||||||||| MAIN LOOP |||||||||||||||||||||||||||||||
rightswitch = SW0 ^ 0b00000001;	//Door-closed microswitch
/*
 The symbol ^ means "Exclusive OR." The above statement exclusive ORs all
eight bits of SW0 with the eight bits of 0b00000001. The result is that
bits 1 to 7 of SW0 are unchanged, and bit 0 is inverted. It's convenient to
do this so that when the right microswitch is pressed, rightswitch will be
a 1. (When the microswitch is pressed, it sends back 0 volts to the PIC.
When it is not pressed, it sends back 5 volts. This all seems a bit
backwards. The above statement eliminates this "backwardness.")
*/
leftswitch =  SW1 ^ 0b00000001;	//Door-open microswitch
boxhot = SW2;		//Thermistor (1 = battery box is too hot)

//When the left microswitch is pressed, the door is open. When the right
//  microswitch is pressed, the door is closed.

if (boxhot == 1)
{
	// Open the door (run the door to the left)
	if (leftswitch == 1)	// Door already opened. Don't burn out the
							//   motor by continuing to run it.
		powertomotor = 0;
	else
	{
		mtrccw = 1;	// Open the door (because it isn't all the
					//   open yet)
		mtrcw = 0;
		powertomotor = 1;
	}
}
else
{
	// Close the door (run the door to the right)
	if (rightswitch == 1)	// Door already closed. Don't burn out the
							//   motor by continuing to run it.
		powertomotor = 0;
	else
	{
		mtrcw = 1;	// Close the door (because it isn't all the way
					//   closed yet).
		mtrccw = 0;
	 	powertomotor = 1;
	}
}

//Send the signals to the output pins.
LED0 = mtrcw;
LED1 = mtrccw;
LED2 = powertomotor;

//||||||||||||||||||||||||||||| END OF MAIN LOOP |||||||||||||||||||||||||||		
}
}
//||||||||||||||||||||||||||| END OF MAIN PROGRAM ||||||||||||||||||||||||||

