/*************************************************************************
Sw_to_led_18C						Uses PIC 18F242
Runs on Derbot. Moves state of front microswitches to leds
Files c018i.o and p18f242.lib are included by the Linker Script. 
TJW 22.10.05					     					Tested 24.10.05 
**************************************************************************
Clock is 4MHz 
Configuration Word default, except: crystal oscillator (HS), power-up timer on,
 brown-out detect off, WDT off, LV Program disabled, all others default*/

	#include <p18F242.h>
	#include <delays.h>		//header file for delays

//Function Prototypes (Library prototypes are in Header files)
	void initialise (void);
	void diagnostic (void);

void main (void)
{	
	initialise ();	//call initialise function
	diagnostic();	//call diagnostic function
	
//move microswitch states to diag leds
loop:
 	if (PORTBbits.RB4 == 0)
	PORTCbits.RC6 = 0;
	else PORTCbits.RC6 = 1;

 	if (PORTBbits.RB5 == 0)
	PORTCbits.RC5 = 0;
	else PORTCbits.RC5 = 1;

	goto 	loop;
}

//Initialises SFRs, and sets initial outputs. Assumes hardware is "Build Stage 1". 
//All unused port bits set to output.
void initialise (void)
{	TRISA = 0b00000000;		//All bits output (none used in this program)
	TRISB = 0b00110000;		//Bits 5 and 4 (microswitches) only are input
	TRISC = 0b10000000;		//All bits output, except bit 7 (mode switch)

//Switch all outputs off
	PORTA = 0;
	PORTB = 0;
	PORTC = 0;
}

//Diagnostic: switches leds on for 1s (Tcy = 1us)
void diagnostic (void)
{	PORTCbits.RC6 = 1;
	PORTCbits.RC5 = 1;
	Delay10KTCYx (100);
	PORTCbits.RC6 = 0;
	PORTCbits.RC5 = 0;
	Delay10KTCYx (100);
}
