/***************************************************************************/
//Information
/***************************************************************************/

//Project: Clock [24 hour digital display- hh:mm]+ chimes ext.(from 16F628A)
//Inputs: 4- pushbutton h+, h-, m+, m- to set
//Outputs: 4- multiplexed 7-seg leds [hh:mm], and
// 4 data lines,one pulse line, to drive 16f628A soundPIC 'Westminster Chimes'
//Files: 1-   main_21.11.14.c (this file)
//Author: pjh                                    
//Date: 14.10.14-21.10.14
//uC: = PIC 16F887 (and peripheral 16F628A for sounds)
//Compiler: XC8, MPLABIDE
//Clock: = HS XTAL@4Mhz = 1Mz instruction cycle
//Function: Ergh- a clock! (based on BuVillain on Microchip forum- Aug 2013)
//Notes: see very end of doc

/***************************************************************************/
//Includes
/***************************************************************************/

#include <xc.h> //supercedes legacy 'bydevice' declarations

/***************************************************************************/
//Configuration Settings
/***************************************************************************/

//CONFIG1 (Word1)
#pragma config FOSC = INTRC_NOCLKOUT//Sets internal RC osc- no clkout on RA7
#pragma config MCLRE = OFF      //Master Clear Enable Off
#pragma config CP = OFF         //Code Protect off
#pragma config CPD = OFF        //Code Data Protect off
#pragma config BOREN = ON       //Brown Out Enable on
#pragma config IESO = OFF       //Internal/ext. Oscillator Switchover off
#pragma config WDTE = OFF       //Watchdog Timer Enable off
#pragma config PWRTE = ON       //Power Up Timer Enable on
#pragma config LVP = OFF        //Low Voltage Programming off
#pragma config FCMEN = ON       //Fail safe Clock Monitor on
//CONFIG2 (Word2)
#pragma config BOR4V = BOR40V   //Brown Out Reset = ??TODO 4V but like this??
#pragma config WRT = OFF        //Flash program memory self write enable off

/***************************************************************************/
//Global Variable Declarations
/***************************************************************************/

#define _XTAL_FREQ 4000000 //delay macros(__delay_m/us())depend on this value)
#define FOSC 4000000  //target device clock frequency
#define FCYC (FOSC/4)  //target device instruction (clock) frequency
#define XTAL_FREQ FOSC  //crystal frequency- 4MHz TODO!- NO XTAL!!

volatile unsigned char secs;  //full seconds count

volatile unsigned char mins; //full minutes count
volatile unsigned char mins1; //xx:xX (on display)
volatile unsigned char mins2; //xx:Xx (on display)

volatile unsigned char hrs; //full hours count
volatile unsigned char hrs1; //xX:xx (on display)
volatile unsigned char hrs2; //Xx:xx (on display)

volatile short int days; // full days count (0-364) for clock adjustments
volatile unsigned char active_display; //Current active display (digit) TODO
volatile unsigned char select_digit; //which digit to display (1-4) TODO
volatile unsigned char select_digit_count; //?? TODO
volatile unsigned char db_counter = 0;//debnce counter (4-PBs- RBx- actve LOW)
volatile unsigned char chimes_suppress_flag; //Suppresses chimes
//												during clock adjusts

volatile long int tmr0_counter; //TMR0 ovrflw cntr(254 rolover=3937.007874=1S)
volatile long int tmr1_counter; //TMR1 overflw cntr(???TODO)

/***************************************************************************/
//Declare Functions
/***************************************************************************/

//General clock stuff
void interrupt tmr0_int (void);
void scan_buttons (void); //checks for h+, h-, m+, m_ PBs
void cycle_display (void); //drives the multiplexer TODO
void check_rollover (void); //converts secs to mins to hours to days
void switch_minutes (void); //splits 'teens' into two displays- minutes
void switch_hours (void); //splits 'teens' into two displays- hours
void strobe_display (void); //drives the multiplexer TODO why not in top one?
void output_7segdata (void); //puts the correct data on the lines
void adjust_clock (void); //provides 4- periodic adjustment per annum
void tick (void); //ergh- generates tick (directly from 887)
void beep (void); //triggers soundPIC to beep
void flash (void); //provides short pulse to display colon each second

//Chimes stuff
void enable (void); //provides pulse to 16F628A to initiate sound routines
void chimes_quarter (void); //quarter-hour chime
void chimes_half (void); //half-hour chime
void chimes_threequarter (void); //three-quarter-hour chime
void chimes_hours (void); //full hour chime
void chime_1_oclock (void); //one o'clock sequence (628 chime and bong)
void chime_2_oclock (void); //two o'clock sequence (628 chime and bongs)
void chime_3_oclock (void); //ditto etc
void chime_4_oclock (void);
void chime_5_oclock (void);
void chime_6_oclock (void);
void chime_7_oclock (void);
void chime_8_oclock (void);
void chime_9_oclock (void);
void chime_10_oclock (void);
void chime_11_oclock (void);
void chime_12_oclock (void);

/***************************************************************************/
//Main Program ()
/***************************************************************************/

void main(void)
{
    ANSEL = 0x00; // Disable analog
    ANSELH = 0x00; //Disable analog
	SCS = 1; //System Clock Select = Internal clock
	PSA = 1; //Prescaler assign to WDT [i.e. DON'T want it affecting tmr0]
	T0CS = 0; //Timer0 Clock Source Select bit = int instr cycle = FOSC/4)
	T0SE = 0; //Timer0 Source Edge Select = low to high	
	T0IE = 1; //Timer0 Interrupt Enable = Enabled
	T0IF = 0; //Timer0 Interrupt flag set to zero (NOT set)	
	TMR0 = 0; //Timer0 Reset initialise
	
	PEIE = 1; //Peripheral Interrupts enabled	
	PIE1 = 1; //Enables TMR1 overflow interrupt
	T1CON = 0b100000101; //	Timer1 control register as:-
	//	(active-high; always; prescale 00; LP off; no synch; internal; enable)
		
	nRBPU = 0; //PortB pull-ups enable by individual port latch values
	WPUB0 = 1; //Pull-up resistor connected to PortB.0 pin (m-)
	WPUB1 = 1; //Pull-up resistor connected to PortB.1 pin (m+)
	WPUB2 = 1; //Pull-up resistor connected to PortB.2 pin (h-)
	WPUB3 = 1; //Pull-up resistor connected to PortB.3 pin (h+)

	//Initialise I/O- Configure and initialise ALL ports:-
	//----------------------------------------------------

	PORTA = 0x00; //Initialise port- all off (active low)
	PORTB = 0x00; //Initialise port- all HIGH
	PORTC = 0x00; //Initialise port- all off (active low)
	PORTD = 0x00; //Initialise port- all LOW
	PORTE = 0x00; //Initialise port (X)
	TRISA = 0b00000000; //Configure RA7-RA6 input(xtal)!,RA5,4(X)3-RA0 o'put; 
	TRISB = 0b00001111; //Configure RB7-RB4 output(X); RB3-RB0 input
	TRISC = 0b00000000; //Configure whole port as output (7segdata)
	TRISD = 0b00000000;	//Configure RD7, RD6 output(X), RD5-RD0 output; 
	TRISE = 0b00000000; //(X)- output
	
	//Initialise variables and set clock for 18:13
	//---------------------------------------------
	days = 0; hrs2 = 1; hrs1 = 8; hrs = 18; mins2 = 1; mins1 = 3; mins = 13;
	secs=0;	active_display = 1; select_digit = 1; select_digit_count = 0;
	tmr0_counter=0; tmr1_counter=0;
	

	//Release GIE
	//-----------
	GIE = 1;	//Global Interrupt Enable = Enabled
//___________________________________________________________________________

	//Main while loop
	while (1) { // cosmos exists && I am breathing
		scan_buttons (); //Check for user adjustment
		cycle_display (); //
		check_rollover (); //cycle through digits-TODO!!!
		switch_minutes (); //split minutes digits
		switch_hours (); //split hours digits
		strobe_display (); //refresh display
		output_7segdata (); //and output the data
		adjust_clock (); //correct for timer error
		chimes_quarter(); //chimes
		chimes_half(); //chimes
		chimes_threequarter(); //chimes
		chimes_hours(); //chimes!!
	} //end main while loop()
} //end main()
	
/***************************************************************************/
//Reading Switches
/***************************************************************************/

	void scan_buttons (void) {
		//Check for button presses
		//TODO!- this interferes badly with the display???
//___________________________________________________________________________
		
		if (RB0==0) { //only if m- PB pressed
			// Button pressed, now debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB0 == 1) //if button UP (RD1 = HIGH)
				db_counter = 0; //fail- restart counting
			} //until button down for 10 successive counts
			
			//now decrement the register
			chimes_suppress_flag = 1; //set chimes suppress flag
			if (mins > 0) { //any value of 1 or greater
			mins--;
			}
			else { //mins == 0
				mins = 59;
				hrs--; //subtract one hour also!
			}
			beep(); //and give the user some feedback!
			chimes_suppress_flag = 0; //clear chimes suppress flag				
			
			//wait for button release, debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB0 == 0) // if button DOWN (RD1 LOW)
				db_counter = 0; //fail- restart counting
			} //until button up for 10 successive counts		
		} //end m- if
//___________________________________________________________________________
		
		if (RB1==0) { //only if m+ PB pressed
			// Button pressed, now debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB1 == 1) //if button UP (RD1 HIGH)
				db_counter = 0; //fail- restart counting
			} //until button down for 10 successive counts
			
			//now increment the register
			chimes_suppress_flag = 1; //set chimes suppress flag
			mins++;
			beep(); //and give the user some feedback!
			chimes_suppress_flag = 0; //clear chimes suppress flag
			
						
			//wait for button release, debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB1 == 0) // if button DOWN (RD1 LOW)
				db_counter = 0; //fail- restart counting
			} //until button up for 10 successive counts
			
		} //end m+ if	
//___________________________________________________________________________
		if (RB2==0) { //only if h- PB pressed
			
			// Button pressed, now debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB2 == 1) //if button UP (RD1 HIGH)
				db_counter = 0; //fail- restart counting
			} //until button down for 10 successive counts
			
			// now decrement the register
			chimes_suppress_flag = 1; //set chimes suppress flag
			if (hrs > 0) { //any value 1 or greater
			hrs--;
			}
			else { //hrs == 0
				hrs = 23;
			}			
			beep(); //and give the user some feedback!
			chimes_suppress_flag = 0; //clear chimes suppress flag	
			
			//wait for button release, debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB2 == 0) // if button DOWN (RD1 LOW)
				db_counter = 0; //fail- restart counting
			} //until button up for 10 successive counts
			
		} //end h- if		
//___________________________________________________________________________

		if (RB3==0) { //only if h+ PB pressed
			// Button pressed, now debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB3 == 1) //if button UP (RD1 HIGH)
				db_counter = 0; //fail- restart counting
			} //until button down for 10 successive counts
			
			//now increment the register
			chimes_suppress_flag = 1; //set chimes suppress flag
			hrs++;
			beep(); //and give the user some feedback!
			chimes_suppress_flag = 0; //clear chimes suppress flag
			
			//wait for button release, debounce by counting
			for (db_counter=0; db_counter<=10; db_counter++) {
				__delay_ms(1); //sample every 1mS
				if (RB3 == 0) // if button DOWN (RD1 LOW)
				db_counter = 0; //fail- restart counting
			} //until button up for 10 successive counts	
		} //end h+ if	
	} //end scan_buttons() 	
	
/***************************************************************************/
//Digits and Display Control
/***************************************************************************/

	void switch_minutes (void) { //split minutes digits- thanks '1and0'
		mins1 = mins;
		mins2 = 0;
		while (mins1 >= 10) {
			mins1 -= 10;
			mins2++;
		}
	} //end switch_minutes
//___________________________________________________________________________
	
	void switch_hours (void) { //split hours digits
		hrs1 = hrs;
		hrs2 = 0;
		while (hrs1 >= 10) {
			hrs1 -= 10;
			hrs2++;
		}
	} //end switch_hrs
//___________________________________________________________________________

	void check_rollover (void) {

		if (secs>=60) {
			mins++;
			secs=0;
		}
		if (mins>=60) {
			hrs++;
			mins=0;
		}
		if (hrs>=24) {
			days++; //days gets 'rollover to zero' in adjust_clock
			hrs=0;
		}
	} //end check_rollover()			
//___________________________________________________________________________
	
	void cycle_display (void) { //TODO- THIS IS AWFUL!!-IT'S LIKE SPAGHETTI!!!

		//cycle digit to display
		//----------------------
		select_digit_count++;
		
		if (select_digit_count == 2) {
			select_digit++;
			select_digit_count = 0;
		}
		if (select_digit == 5) {
			select_digit = 1;
		}
	} //end cycle_display()
//___________________________________________________________________________
		
	void strobe_display (void) {
	//Strobe the display
	//-----------------------------------------
	PORTA = 0b11111111; //blank all segments (to stop ghosting)
	switch (select_digit)
			{
			case 1: active_display = mins1; PORTA = 0b11111110; break;
			case 2: active_display = mins2; PORTA = 0b11111101; break;
			case 3: active_display = hrs1; PORTA = 0b11111011; break;
			case 4: active_display = hrs2; PORTA = 0b11110111; break;
			}
	} //end strobe_display()	
//___________________________________________________________________________
	
	void output_7segdata (void) { //Output 7-seg display data
		switch (active_display) {
			case 0: PORTC = 0b00000011; break; //'0'
			case 1: PORTC = 0b10011111; break; //'1'
			case 2: PORTC = 0b00100101; break; //'2'
			case 3: PORTC = 0b00001101; break; //'3'
			case 4: PORTC = 0b10011001; break; //'4'
			case 5: PORTC = 0b01001001; break; //'5'
			case 6: PORTC = 0b01000001; break; //'6'
			case 7: PORTC = 0b00011111; break; //'7'
			case 8: PORTC = 0b00000001; break; //'8'
			case 9: PORTC = 0b00001001; break; //'9'
			}
	} //end output_7segdata()
		
/***************************************************************************/
//Clock Adjustment  (based on loose Gregorian year of 31 556 952S/annum)
/***************************************************************************/

// ISR initialised to 2, drives clock on rollover 0xFF to 0x00 = 254 count
//4000000/4 = 1000000 Fcyc/254 = 3937.007874 rollovers/S
// giving accumulating error of +0.007874 rollovers/S (i.e. fast!)
//clock drifts by:-
//+0.007874 rollovers/S = 248484.5424 rollovers/Yr = 63.11507377S/Yr FAST
//
//Adjustments are made at intervals called 'adjust_period'(s) thus:-
//  (1 day x 7 = adjust_period)
//	-each 7 days (adjust_period x 1)  -1S (gives -52.17857143 S/Yr)
//	-each 28 days (adjust_period x 4)  -1S (gives -10.93650234 S/Yr
//	-each 364 days (adjust_period x 13) +2S (gives = +2.006868132 S/Yr)
// all adjustments occur at 03:07 time
// TOTAL ERROR CORRECTION YIELDS CLOCK RUNNING SLOW BY 0.1012723881 S/Yr

/***************************************************************************/	
	
	void adjust_clock (void) {
		if (hrs ==3 && mins == 7) { //only at 'adjust time' 03:07
			if (days % 7 == 0) { //if adjust period is multiple of 7 days
				secs--; //remove 1 second each 7 days
				if (days % 28 == 0) { // if adjust period is 28
					secs--; // remove 1 second each 28 days
					if (days % 364 == 0) { //if adjust period is 364
						secs=secs+2; //add 2 seconds each 364 days
						days=0; // reset days counter
					} //end 364 days if
				} //end 28 days if			
			} //end 7 days if		
		} //end time if		
	} //end adjust_clock
	//TODO- a manual adjust here for local conditions/temperature??
	//TODO perhaps eventually, an external switch ??? +/-2/5/10S??
		
/***************************************************************************/
//Miscellaneous Functions
/***************************************************************************/
	
	void tick (void) { //ticks on each second [independent of chimes]
		RD5=1; //active HIGH
		__delay_ms(10);
		RD5=0;
	} //end tick()
	
	void enable (void) { //pulses pin 27 to tell soundpic that data is ready
		RD4=1; //active HIGH
		__delay_us(100);
		RD4=0;
	} //end enable()
	
	void beep (void) { //user feedback for switch press [generated by 628A]
			PORTD = 0b00000000; //set up data for beep
			enable (); //send pulse to initiate
			PORTD = 0b00000000; //'reset' to- ergh !! default state	
	}
	
	void flash (void) { //flashes the central display colon [dp]on seconds
		RC0=0; //active LOW pulse
		__delay_ms(100); //wait
		RC0=1; //return to default (off) state (HIGH)	
	}				

/***************************************************************************/
//Chimes Functions
/***************************************************************************/

//delays for ending chimes 'on time' [@90bpm] (!) are:-
//quarter:      minutes = 14; seconds = 56 (-4S)
//half:         minutes = 29; seconds = 52 (-8S)
//threequarter: minutes = 44; seconds = 48 (-12S)
//hour:         minutes = 59; seconds = 44 (-16S)
	
//***************************************************************************

	void chimes_quarter(void){ // gfeb [on 628A [TYP]]
		if (chimes_suppress_flag == 0 && mins == 14) { //only if minute is 14
			if (secs == 56) { //only if seconds is 56
				PORTD = 0b00000001; //set up data for quarter_hour chime
				enable (); //send pulse to initiate	
				PORTD = 0b00000000; //reset
			}	
		}	
	} //end chimes_quarter()
//___________________________________________________________________________

	void chimes_half(void){ // egfb, efge	
		if (chimes_suppress_flag == 0 && mins == 29) { //only if minute is 29
			if (secs == 52) { //only if seconds is 52
				PORTD = 0b00000010; //set up data for half_hour chime
				enable (); //send pulse to initiate
				PORTD = 0b00000000; //reset			
			}	
		}	
	} //end chimes_half()
//___________________________________________________________________________

	void chimes_threequarter(void){ // gefb, bfge, gfeb	
		if (chimes_suppress_flag == 0 && mins == 44) { //only if minute is 44
			if (secs == 48) { //only if seconds is 48
				PORTD = 0b00000011; //set up data for quarter chime
				enable (); //send pulse to initiate
				PORTD = 0b00000000; //reset			
			} //end if	
		} //end other if	
	} //end chimes_threequarter()
//___________________________________________________________________________

	void chimes_hours (void) {// egfb, efgb, gefb, bfge,+ bongs!
		char chime_time;
		chime_time = hrs;
		if (chimes_suppress_flag == 0 && hrs>=13) { //correct 24h to 12h for correct chimes	
			chime_time = hrs-12;
		}
		if(chimes_suppress_flag == 0 && mins == 59) { //only if minutes is 59
			if (secs == 44) { //only if seconds is 44
				switch (chime_time) { //switch for bong numbers
					case 1: chime_1_oclock();
					case 2: chime_2_oclock();
					case 3: chime_3_oclock();
					case 4: chime_4_oclock();
					case 5: chime_5_oclock();
					case 6: chime_6_oclock();
					case 7: chime_7_oclock();
					case 8: chime_8_oclock();
					case 9: chime_9_oclock();
					case 10: chime_10_oclock();
					case 11: chime_11_oclock();
					case 12: chime_12_oclock();
				
				} //end switch	
			} //end if seconds
		} //end if minutes
	} //end chime_hours()		
//___________________________________________________________________________

	void chime_1_oclock (void) {
		PORTD = 0b00000100;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_2_oclock (void) {
		PORTD = 0b00000101;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_3_oclock (void) {
		PORTD = 0b00000110;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_4_oclock (void) {
		PORTD = 0b00000111;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_5_oclock (void) {
		PORTD = 0b00001000;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_6_oclock (void) {
		PORTD = 0b00001001;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_7_oclock (void) {
		PORTD = 0b00001010;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_8_oclock (void) {
		PORTD = 0b00001011;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_9_oclock (void) {
		PORTD = 0b00001100;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_10_oclock (void) {
		PORTD = 0b00001101;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_11_oclock (void) {
		PORTD = 0b00001110;
		enable();
		PORTD = 0b00000000; //reset
	}
//___________________________________________________________________________	
	void chime_12_oclock (void) {
		PORTD = 0b00001111;
		enable();
		PORTD = 0b00000000; //reset
	}	

/***************************************************************************/
//Timer0 Interrupt [T0IF] (on overflow from 0xFF to 0x00)- The engine!
/***************************************************************************/

	void interrupt tmr0_int (void)
	{
			if (T0IE && T0IF) { //timer0 has overflowed and flags been set
				T0IF = 0; //so, reset the flag
				tmr0_counter++; //and increment the counter
					//now check counter for a one second increase
					if (tmr0_counter == 3937) {
						// (initialised to 2)ISR rollover on count of 254
						//(3937.007874=1S) see adjustment function
						secs++;  //if yes, increment seconds
						//tick(); //and tick!
						//flash(); //pulses the central display colon
						tmr0_counter = 2; //and reset this counter
					} //otherwise count some more!
			} //end if
	} //end interrupt()
	
//***************************************************************************
// Timer1 Interrupt [TMR1IF] (on overflow from 0xFFFF to 0x0000) Multiplexer
//***************************************************************************

void interrupt tmr1_int (void) {
	if (TMR1IE && TMR1IF) { //timer1 has overflowed and flags been set
		TMR1IF = 0; //so, reset the flag
		tmr1_counter ++; //and increment the counter
		// now check for required strobe period (200Hz over 4 digits
		// equals 1250uS per digit [800 digits/S])
		if (tmr1_counter == 1250) { //
			
			//[INCREMENT DIGIT TO DISPLAY- TODO!!!!!!????]
			
			tmr1_counter = 64286; //65536 (0xFFFF) - 1250! i.e. reset
		} //otherwise, count some more
	} //end if
} //end interrupt


/***************************************************************************/
//Other development notes and stuff
/***************************************************************************/

//see pjh schematic
//see 16f628soundPIC code
// Obviously, the final design will have external 4Mhz XTAL on 13, 14

// TODO The clock 'engine' needs to be on TMR1 (50,000 rollover x 20)
// TODO the multiplexer should be rewritten as a TMR0 isr running at 200Hz
//				
// TODO Can I write in a tock ?? even and odds??
// TODO Can I add leading zeros to switch statements?? ergh general question!
// TODO improve colon-  flash() ??PWM pulsating (be nice..!)
// TODO Do the function calls within the ISR alter its timing at all? !!!!

// TODO a separate clock adjust facility for local conditions??
// TODO VERY bad ghosting on display- TO SORT OUT! ?ISR driver sort it!!
// TODO tick electronics on amp to sort- too quiet- ? separate amp anyway??
// TODO flash() interferes with display?? why????
// TODO initiation of chimes 'jiggers' the display for a while- why?
// TODO Introduced TMRI ISR and now won't build due to Interrupt Vector conflict!




/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
//Old notes and rubbish and scribbles   IGNORE ME
/***************************************************************************/

//	Timer1 needs:-
//	T1CON = 0b100000101
//	(a-high; always; prescale 00; LP off; no synch; internal; enable)
//	INTCON:-  GIE = 1, PEIE = 1; enable; enable
//	CM2CON1:- T1GSS, C2SYNC // neither required here?


////***************************************************************************
//// Timer1 Interrupt [TMR1IF] (on overflow from 0xFFFF to 0x0000) Multiplexer
////***************************************************************************
//
//void interrupt tmr1_int (void) {
//	if (TMR1IE && TMR1IF) { //timer1 has overflowed and flags been set
//		TMR1IF = 0; //so, reset the flag
//		tmr1_counter ++; //and increment the counter
//		// now check for required strobe period (200Hz over 4 digits
//		// equals 1250uS per digit [800 digits/S])
//		if (tmr1_counter == 1250) { //
//			
//			[INCREMENT DIGIT TO DISPLAY- TODO!!!!!!????]
//			
//			tmr1_counter = 64286; //65536 (0xFFFF) - 1250! i.e. reset
//		} //otherwise, count some more
//	} //end if
//} //end interrupt

