/* 
 * File:   Pic32PWM.c
 * Author: Cara
 *
 * Created on February 12, 2020, 1:10 PM
 * 
 * PWM test for PIC32
 */

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "configbits-16ex8.h"
#include "SDlib16.h"
#include <sys/attribs.h>


#define init_duty_cycle 3870   //(cw=994-3560; stall= <990 and 3562-3845 and >6405; ccw= 3848-6405)
#define period 49999;          //20ms
/*
 * 
 */
int main(int argc, char** argv) {
    
    //Servo 1 Setup
    T2CONbits.TCKPS=2;          //1:4 pre-scaler
    PR2=period;                 //Set the Period for Timer2
    TMR2=0;                     //Choose 32 bit count register for Timer2
    OC1CONbits.OCM=0b110;       //OC1 set for PWM without fault pin
    OC1RS=init_duty_cycle;      //Set secondary register duty cycle (update this register)
    OC1R=init_duty_cycle;       //Set primary register with initial duty cycle
    
    //Servo 2 Setup
    T3CONbits.TCKPS=2;          //1:4 pre-scaler
    PR4=period;                 //Set the period for Timer4
    TMR4=0;                     //
    OC2CONbits.OCM=0b110;       //OC2 set for PWM without fault pin
    OC2RS=init_duty_cycle;      //Set secondary register duty cycle (update this)
    OC2R=init_duty_cycle;       //Set primary register with initial duty cycle
    
    //Enable Timers and Output Compare Registers
    T2CONbits.ON=1;             //Turn on Timer 2
    OC1CONbits.ON=1;            //Turn on OC1
    T4CONbits.ON=1;             //Turn on Timer 4
    OC2CONbits.ON=1;            //Turn on OC2

    while(1);                    //infinite while loop                
return (EXIT_SUCCESS);
}