/*
 * File:   main.c
 * Author: boos
 *
 * Created on July 12, 2019, 7:46 PM
 */

// CONFIG1
#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (low voltage programming disabled)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

#include <xc.h>

// define names

#define ONOFF RB0
#define SWH RB1
#define SWM RB2
#define H24 RB3

#define S0  RB4
#define S1  RB5
#define S2  RB6
#define S3  RB7
#define S4  RC0
#define S5  RC1

#define M0  RC2
#define M1  RC3
#define M2  RC4
#define M3  RC5
#define M4  RC6
#define M5  RC7

#define H0  RA1
#define H1  RA2
#define H2  RA3
#define H3  RA4
#define PM  RA5

// these variables store the time
unsigned char seconds = 0, minutes = 0, hours = 0, hours12 = 0;

// time buffer
int TIME_BUFFER = 0;

// switch debounce buffers
int SWH_BUFFER = 0;
int SWM_BUFFER = 0;

// the main function
void main (void) {

    // enable weak pull-up resistors on PORTB
    nRBPU = 0;
    
    // switch OFF the analog to digital converter (ADC)
    // because we do not need it here
    ADON = 0;
    ANS0 = 0;
    ANS1 = 0;
    ANS2 = 0;
    ANS3 = 0;
    ANS4 = 0;
    ANS8 = 0;
    ANS9 = 0;
    ANS10 = 0;
    ANS11 = 0;
    ANS12 = 0;
    ANS13 = 0;
    
    // these pins are inputs
    TRISB0 = 1;
    TRISB1 = 1;
    TRISB2 = 1;
    TRISB3 = 1;
    
    // and all of these pins are outputs
    TRISB4 = 0;
    TRISB5 = 0;
    TRISB6 = 0;
    TRISB7 = 0;
    TRISC0 = 0;
    TRISC1 = 0;
    TRISC2 = 0;
    TRISC3 = 0;
    TRISC4 = 0;
    TRISC5 = 0;
    TRISC6 = 0;
    TRISC7 = 0;
    TRISA1 = 0;
    TRISA2 = 0;
    TRISA3 = 0;
    TRISA4 = 0;
    TRISA5 = 0;
    
    // initialize TIMER0
    
    // TIMER0 works with 1/4 of the internal clock
    // (that amounts to 1.048576 MHz)
	T0CS = 0;

	// set the TIMER0 prescaler to 1:1
	PSA = 1;
	PS0 = 1;
	PS1 = 1;
	PS2 = 1;

	// create an interrupt when TIMER0 overflows
	T0IE = 1;

    // enable interrupts
	GIE = 1;
    
    // the main loop
    while (1) {
        
        // only show the time if the toggle
        // switch is turned to the ON position
        if (!ONOFF) {
        
            // show seconds
            S0 = (seconds & 0b000001) >> 0;
            S1 = (seconds & 0b000010) >> 1;
            S2 = (seconds & 0b000100) >> 2;
            S3 = (seconds & 0b001000) >> 3;
            S4 = (seconds & 0b010000) >> 4;
            S5 = (seconds & 0b100000) >> 5;

            // show minutes
            M0 = (minutes & 0b000001) >> 0;
            M1 = (minutes & 0b000010) >> 1;
            M2 = (minutes & 0b000100) >> 2;
            M3 = (minutes & 0b001000) >> 3;
            M4 = (minutes & 0b010000) >> 4;
            M5 = (minutes & 0b100000) >> 5;

            // show hours in AM/PM format
            if (H24) {

                // convert to 12 hour display
                hours12 = hours;
                if (hours > 12) {
                    hours12 -= 12;
                } else if (hours == 0) {
                    hours12 = 12;
                }
                H0 = (hours12 & 0b000001) >> 0;
                H1 = (hours12 & 0b000010) >> 1;
                H2 = (hours12 & 0b000100) >> 2;
                H3 = (hours12 & 0b001000) >> 3;

                // PM indicator
                if (hours > 11) {
                    PM = 1;
                } else {
                    PM = 0;
                }

            // show hours in 24 hour format
            } else {

                H0 = (hours & 0b000001) >> 0;
                H1 = (hours & 0b000010) >> 1;
                H2 = (hours & 0b000100) >> 2;
                H3 = (hours & 0b001000) >> 3;
                PM = (hours & 0b010000) >> 4;

            }
            
            // adjust hours?
            if ((!SWH) && (SWH_BUFFER == 0)) {
                SWH_BUFFER = 1000;
                hours++;
                if (hours >= 24) {
                    hours = 0;
                }
                seconds = 0;
            }

            // adjust minutes?
            if ((!SWM) && (SWM_BUFFER == 0)) {
                SWM_BUFFER = 1000;
                minutes++;
                if (minutes >= 60) {
                    minutes = 0;
                }
                seconds = 0;
            }
         
            
        // switch all LEDs off if the toggle switch
        // is set to the OFF position
        } else {
        
            S0 = 0;
            S1 = 0;
            S2 = 0;
            S3 = 0;
            S4 = 0;
            S5 = 0;
            M0 = 0;
            M1 = 0;
            M2 = 0;
            M3 = 0;
            M4 = 0;
            M5 = 0;
            H0 = 0;
            H1 = 0;
            H2 = 0;
            H3 = 0;
            PM = 0;
        
        }
        
    }
    
    return;
    
}

// the interrupt service routine
// (this routine gets called 4096 times a second)
void __interrupt () isr (void) {    

    // has TIMER0 reached its maximum?
	if (T0IF) {
        
        // increase the buffer variable
        // whenever TIMER0 overflows
        TIME_BUFFER++;
        
        // has it overflown 4096 times?
        // then one second has passed!
        if (TIME_BUFFER >= 4095) {
            
            // reset buffer
            TIME_BUFFER = 0;

            // update the time
            seconds++;
            if (seconds >= 60) {
                seconds = 0;
                minutes++;
                if (minutes >= 60) {
                    minutes = 0;
                    hours++;
                    if (hours >= 24) {
                        hours = 0;
                    }
                }
            }
            
        }
        
        // debounce switches
        if (SWH_BUFFER > 0) {
            SWH_BUFFER--;
        }
        if (SWM_BUFFER > 0) {
            SWM_BUFFER--;
        }
        
		// IMPORTANT: reset the interrupt flag!
		T0IF = 0;

	}

}