/*******************************************************************************
 *	TestSwitches.c		Greenpower CarComputer Test Program
 *			T.Barnaby,	BEAM Ltd,	2008-02-28
 *
 *	Copyright (c) Beam Ltd and Chipping Sodbury School
 *******************************************************************************
 *
 * Function:	Test sthe Display's 4 switches
 * Version:	1.0
 */
#include <string.h>
#include <pic18f2520.h>
#include <picLib3.c>

#pragma stack 0x200 0x100		// Setup stack

#define	CLOCK		4000000		// Clock frequency

void isr(void) interrupt 1 {
	// Check timer 1 interrupt
	if(PIR1bits.CCP1IF){
		timerTick();

//		PORTBbits.RB7 = !PORTBbits.RB7;
		PIR1bits.CCP1IF = 0;		// Clear timer interrupt flag
	}
}

void main(void) {
	UInt16	v;
	char	str[16];
	
	// Set up Oscillator
	OSCCON = 0x62;				// Internal 4MHZ

	// Setup Ports
	TRISA = 0xFF;				// PORTA Input
	TRISB = 0x0F;           		// PORTB top 4 bits Output
	TRISC = 0xFF;				// PORTC Input

	PORTB = 0;				// LED's off

	// Initialise system
	i2cInit();
	lcdInit();
	usartInit();
	timerInit();
	adcInit();

	PORTB = 0x00;
	
	// Enable interrupts	
	INTCON = 0;				// Clear interrupt flag bits
	INTCONbits.PEIE = 1;			// Enable peripheral interrupts
	PIE1bits.CCP1IE = 1;			// TMR1 overflow interrupt enable
	INTCON2bits.INTEDG0 = 1;		// INT0 Interrupt on +ve edge 
	INTCONbits.INT0IE = 1;			// Enable INT0 Speed interrupt
	INTCONbits.GIE = 1;			// Global interrupt enable

        while(1){
		v = adcRead(9);
		strWordDec(str, 0, v);
		str[6] = 0;
		lcdWriteStr(0, 0, str);
		
		v = (v + 32) / 64;
		strWordDec(str, 0, v);
		str[6] = 0;
		lcdWriteStr(0, 1, str);
		
		PORTB = v << 4;
        }
}
