Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<plib.h>
- //delay function
- delay(){
- int i;
- for(i = 0; i < 100; i++);
- }
- //ADC interrupt
- void __ISR(23) ADC() {
- //set next duty cycle (average buffers and divide by 10)
- OC2RS = ((ADC1BUF0 + ADC1BUF1 + ADC1BUF2 + ADC1BUF3 + ADC1BUF4)/50);
- OC1RS = ((ADC1BUF0 + ADC1BUF1 + ADC1BUF2 + ADC1BUF3 + ADC1BUF4)/50);
- //reset interrupt flag
- IFS0bits.AD1IF = 0;
- }
- //define motor direction
- int forward = 1;
- //push button interrupt
- void __ISR(3) button(void){
- delay();
- //switch motor direction
- if (forward == 1){
- forward = 0;
- //I have no idea what to do here
- }
- else {
- forward = 1;
- //I also have no idea what to do here
- }
- //reset interrupt flag
- IFS0bits.INT0IF = 0;
- }
- main() {
- INTEnableSystemMultiVectoredInt();
- TRISAbits.TRISA0 = 1; //Set FSR as input
- TRISBbits.TRISB1 = 0; //Set PWM Output pin
- TRISBbits.TRISB7 = 1; //Set button as input
- PPSOutput(2,RPB1,OC2); //Set first PWM output
- //set timer period to 100
- PR2 = 99;
- //set initial duty cycle to 0
- OC2R = 0;
- OC2RS = 0;
- OC1R = 0;
- OC1RS = 0;
- //configure button interrupt
- IPC0bits.INT0IP = 1;
- IFS0bits.INT0IF = 0;
- IEC0bits.INT0IE = 1;
- //configure first output compare module
- OC2CONbits.OC32 = 0; //set for 16-bit timer
- OC2CONbits.OCTSEL = 0; //use timer 2
- OC2CONbits.OCM = 6; //PWM mode
- OC2CONbits.ON = 1; //turn on output compare
- //configure second output compare module
- OC1CONbits.OC32 = 0; //set for 16-bit timer
- OC1CONbits.OCTSEL = 0; //use timer 2
- OC1CONbits.OCM = 6; //PWM mode
- OC1CONbits.ON = 0; //turn off output compare
- //configure timer module
- T2CONbits.TCS = 0; //use internal clock
- T2CONbits.TGATE = 0; //gated time accumulation disabled
- T2CONbits.TCKPS = 0; //1:1 prescale value
- T2CONbits.ON = 1; //turn on timer 2
- //set up ADC
- AD1CON1bits.ON = 0;
- IEC0bits.AD1IE = 0;
- // 1 configure analog port pins
- ANSELA = 0xFF;
- ANSELB = 0;
- // 2 Select analog inputs
- AD1CHSbits.CH0SA = 0;
- // 3 select the format of the ADC
- AD1CON1bits.FORM = 0;
- // 4 Sample clock source
- AD1CON1bits.SSRC = 7;
- // 5 voltage reference source
- AD1CON2bits.VCFG = 0;
- // 6 scan mode using CSCNA
- AD1CON2bits.CSCNA = 0;
- // 7 Set the number of conversions per interrupt
- AD1CON2bits.SMPI = 4;
- // 8 Set buffer Fill mode
- AD1CON2bits.BUFM = 0;
- // 9 Select the MUX
- AD1CON2bits.ALTS = 0;
- // 10 Select the ADC clock source
- AD1CON3bits.ADRC = 0;
- // 11 Select the sample time
- AD1CON3bits.SAMC = 10;
- // 12 Select the ADC clock prescaler
- AD1CON3bits.ADCS = 15;
- // 13 Turn the ACD module on
- AD1CON1bits.ON = 1;
- // 14 configure the ADC interrupt
- IFS0bits.AD1IF = 0;
- IPC5bits.AD1IP = 1;
- IEC0bits.AD1IE = 1;
- // 15 Start the conversion sequence
- AD1CON1bits.ASAM = 1;
- while(1) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement