const char MaxBright = 50;
const char MinBright = 1;
const char GlowSlow = 7;

char i;                                 //Counter
bit dir;                                //Remembers up/down counting direction

void main() {
  OSCCON = 0b01110000;                  //8MHz
  PLLEn_bit = 1;                        //Enable PLL (Now 48MHz)
  TRISC2_bit = 0;                       //Make PortC.2 an output
  LATC2_bit = 0;                        //Default LED to off
  i = MinBright;
  dir = 0;
  PPS_Mapping(13, _OUTPUT, _CCP1_P1A);  //Assign CCP1 to RP13
  PWM1_Init(5000);                      //Initialise PWM at 5kHz
  PWM1_Start();
  while (1) {
    //Set PWM
    PWM1_Set_Duty(i);
    delay_ms(GlowSlow);
    //Count up/down
    if (dir == 0) {
      i++;
    } else {
      i--;
    }
    //Change counting direction?
    if (i == MaxBright) {
      dir = 1; //Count down
    }
    if (i == MinBright) {
      dir = 0; //Count up
      //Give an "off" delay
      PWM1_Stop();
      delay_ms(1000);
      PWM1_Start();
    }
  }
}