/*
 * File:   main.c
 * Author: boos
 *
 * Created on January 24, 2020, 05:02 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>

// define clock frequency to be 4MHz
#define _XTAL_FREQ 4000000

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;

	// RC0 as analog input
	TRISC0 = 1;
    ANSC0 = 1;
    
    // select channel AN4 (which corresponds to RC0)
    CHS0 = 0;
    CHS1 = 0;
    CHS2 = 1;
    CHS3 = 0;
    CHS4 = 0;
    
	// turn the ADC on
	ADON = 1;
    
    // PWM settings

    // PWM1 module is RC5
    TRISC5 = 0;
    
    // configure TIMER2
    PR2 = 0xff;
    T2CON = 0b100;
    
    // turn on PWM module
    PWM1EN = 1;
    PWM1OE = 1;
    
    // main loop
    while (1) {
       
		// read 10-bit ADC value
        GO = 1; while (GO);
        
        // set 10-bit duty cycle value for PWM module
        PWM1DCH = ADRESH;
		PWM1DCL = ADRESL;
    
        // wait a bit
        __delay_ms(1);
    
    }
    
    return;
    
}