Skip to content

Instantly share code, notes, and snippets.

@jakabo27
Last active October 25, 2018 01:35
Show Gist options
  • Save jakabo27/ececb72025f15f3b0ce49d1272e8e805 to your computer and use it in GitHub Desktop.
Save jakabo27/ececb72025f15f3b0ce49d1272e8e805 to your computer and use it in GitHub Desktop.
#include<plib.h>
int i;
//Interrupt Functions
void __ISR(3) ButtonInterrupt(void){
i = i+25;
if(i>100)
{//reset duty cycle
i = 0;}
OC1RS = i; //Cycle through duty cycle of 0%, 25%, 50%, 75%, 100%
IFS0bits.INT0IF = 0; // Clear Interrupt Flag
}
main()
{
//Map OC1 and 2
PPSOutput(1,RPA0,OC1);
ANSELAbits.ANSA0 = 0;
TRISAbits.TRISA0 = 0; //Output to motor
TRISBbits.TRISB7 = 1; //Input pushbutton
INTEnableSystemMultiVectoredInt();
//Set up button interrupt
INTCONbits.INT0EP = 1;
IEC0bits.INT0IE = 1; //enable interrupt
IFS0bits.INT0IF = 0; //set flag
IPC0bits.INT0IP = 1; //Set highest priority
//Setup Timer2
T2CONbits.TGATE = 0; //Don't use gate
T2CONbits.T32 = 0; //16 bit timer
T2CONbits.TCS = 0; //Internal clock
T2CONbits.TCKPS = 0; //1:1 prescale
TMR2 = 0; // Clear timer 2
PR2 = 99; // Period
T2CONbits.ON = 1;//Enable timer 2
//Setup Output Compare 1
OC1CONbits.OCM = 6; //Set PWM mode
OC1CONbits.OCTSEL = 0; //Timer 2 used
OC1CONbits.OC32 = 0; //16 bit mode
OC1CONbits.ON = 1; //Enable OC1
OC1RS = 0; //0% duty factor
while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment