/*
 * File:   main.c
 * Author: boos
 *
 * Created on July 7, 2020, 8:03 PM
 */

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection Bit (NO CPU system divide)
#pragma config USBLSCLK = 48MHz // USB Low SPeed Clock Selection bit (System clock expects 48 MHz, FS/LS USB CLKENs divide-by is set to 8.)
#pragma config PLLMULT = 3x     // PLL Multiplier Selection Bit (3x Output Frequency Selected)
#pragma config PLLEN = ENABLED  // PLL Enable Bit (3x or 4x PLL Enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (Low-voltage programming disabled)

#include <xc.h>

// how are your segments connected to the TLC5916 driver?
// key:                       a  b  c  d  e  f  g  .
unsigned char seg_to_bit[] = {6, 5, 3, 2, 1, 7, 8, 4};

// declare functions
void sendValue (unsigned char value);
unsigned char convertNumberToPattern (unsigned char number);

// define locations of shift register controls
#define LE RC2
#define SDI RC3
#define CLK RC4
#define nOE RC5

// variables
unsigned char number=0, brightness=0;
int idle=0;

// main function
void main (void) {
    
    // set internal oscillator to 4MHz
    IRCF0 = 1;
    IRCF1 = 0;
    IRCF2 = 1;
    IRCF3 = 1;
    
    // ADC settings
    
	// ADC sampling frequency per bit is F_osc/16
	ADCS0 = 1;
	ADCS1 = 0;
    ADCS2 = 1;
    
    // result alignment
    ADFM = 0;
    
    // RA4 as analog input
	TRISA4 = 1;
    ANSA4 = 1;
    
    // select channel AN3 (which corresponds to RA4)
    CHS0 = 1;
    CHS1 = 1;
    CHS2 = 0;
    CHS3 = 0;
    CHS4 = 0;
    
    // turn the ADC on
	ADON = 1;
    
    // set outputs and disable analog features on pins RC2 and RC3
    TRISC2 = 0;
    TRISC3 = 0;
    TRISC4 = 0;
    TRISC5 = 0;
    ANSC2 = 0;
    ANSC3 = 0;

    
    // main loop
    while (1) {
        
        // get potentiometer position
        GO = 1; while (GO);
        brightness = ADRESH >> 1;
        
        // switch to special mode
        nOE = 1;
        CLK=1;CLK=0;
        nOE = 0;
        CLK=1;CLK=0;
        nOE = 1;
        CLK=1;CLK=0;
        LE=1;
        CLK=1;CLK=0;
        LE=0;
        CLK=1;CLK=0;
        
        // send current value
        for (int j=0; j<4; j++) {
            SDI = (brightness >> 0) & 1; CLK = 1; CLK = 0;
            SDI = (brightness >> 1) & 1; CLK = 1; CLK = 0;
            SDI = (brightness >> 2) & 1; CLK = 1; CLK = 0;
            SDI = (brightness >> 3) & 1; CLK = 1; CLK = 0;
            SDI = (brightness >> 4) & 1; CLK = 1; CLK = 0;
            SDI = (brightness >> 5) & 1; CLK = 1; CLK = 0;
            SDI = (brightness >> 6) & 1; CLK = 1; CLK = 0; // HC
            SDI = 1; CLK = 1; CLK = 0; // always 1
            SDI = 0;
        }
        LE=1;
        LE=0;
        
        
        // go back to normal mode
        nOE = 1;
        CLK=1;CLK=0;
        nOE = 0;
        CLK=1;CLK=0;
        nOE = 1;
        CLK=1;CLK=0;
        CLK=1;CLK=0;
        CLK=1;CLK=0;
        nOE = 0;
        
        // send current numbers (anything from 0 to 9)
        sendValue( convertNumberToPattern( (number + 11)%10 ));
        sendValue( convertNumberToPattern( (number + 6)%10 ));
        sendValue( convertNumberToPattern( (number + 4)%10 ));
        sendValue( convertNumberToPattern(number) );
        LE = 1;
        LE = 0;
        
        // increment number and reset at 10
        idle++;
        if (idle >= 100) {
            idle=0;
            number++;
            if (number >= 10) {
                number = 0;
            }
        }
        
    }
    
    return;
    
}

// This function converts a number into the 7-segment pattern.
unsigned char convertNumberToPattern (unsigned char number) {
    
    // what is the number?
    switch (number) {
        
        // numbers       abcdefg.
        case 0: return 0b11111100;
        case 1: return 0b01100000;
        case 2: return 0b11011010;
        case 3: return 0b11110010;
        case 4: return 0b01100110;
        case 5: return 0b10110110;
        case 6: return 0b10111110;
        case 7: return 0b11100000;
        case 8: return 0b11111110;
        case 9: return 0b11110110;
        
        // default symbol is an empty space
        default: return 0b00000000;
        
    }
    
}

// This function sends an eight-bit value
// to the TLC5916 driver ICs.
void sendValue (unsigned char seg_pattern) {

    // store decoded segment pattern here
    unsigned char bit_pattern = 0;
    
    // decode segment pattern into bit pattern
    for (int n = 0; n < 8; n++) {
        if ((seg_pattern >> (7-n)) & 1) {
            bit_pattern |= 1 << (seg_to_bit[n] - 1);
        }
    }
    
    // send bit pattern bit by bit,
    // starting with the most significant bit
	for (int n = 7; n >= 0; n--) {
        
        SDI = (bit_pattern >> n) & 1;
        CLK = 1;
		CLK = 0;
        
	}
    
    // set data pin back to zero
    SDI = 0;

}
