Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: lab4.c
- * Author: Aaron
- *
- * Created on February 28, 2017, 9:06 AM
- */
- #include "xc.h"
- _CONFIG1 (FWDTEN_OFF & JTAGEN_OFF)
- _CONFIG2 (POSCMOD_NONE & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_FRCPLL )
- int adcvalue=0;
- int value=0;
- int t2=0;
- void ADC_init(){
- //setup ADC configuration bits and TRISB
- AD1PCFG=0xFDFF;
- AD1CON1=0x20E6;
- AD1CON2=0;
- AD1CON3=0x0201;
- AD1CHS=0x0009;
- AD1CSSL=0x0000;
- TRISB=0x8000;
- }
- void setup(void){
- __builtin_write_OSCCONL(OSCCON & (~(1<<6))); // Release IOLOCK
- //RPOR3bits.RP7R = 0x12; //RB7
- RPOR2bits.RP4R = 0x12; //RB4
- __builtin_write_OSCCONL(OSCCON|(1<<6)); // Engage IOLOCK
- }
- void main(void) { //10khz, 75% duty cycle
- ADC_init(); //initialize ADC
- //enable interrupt
- //clear interrupt flag
- IEC0bits.AD1IE = 1;
- IFS0bits.AD1IF = 0;
- AD1CON1bits.ADON = 1; //turn on ADC
- //init PWM
- OC1CON = 0; //Clear OC1CON1
- OC1R=800;
- OC1RS=600;
- //OC1R = 79;
- //OC1RS = 39;
- //OC1CON2 = 0x020C; //Set value of OC1CON2
- OC1CON = 0x2000;
- OC1CONbits.OCM = 0x0006;
- //PR2=79;
- PR2=800;
- T2CONbits.TON = 0b1; //Initiate Timer 2
- setup();
- while(1){
- }
- }
- void __attribute__ ((interrupt, no_auto_psv)) _ADC1Interrupt(void){
- //Disable interrupt
- //Clear flag
- IEC0bits.AD1IE = 0;
- IFS0bits.AD1IF = 0;
- //adcvalue = ADC1BUF0;
- // OC1R=adcvalue*78/1023;
- //LATB=adcvalue;
- IEC0bits.AD1IE = 1;
- IFS0bits.AD1IF = 0;
- }
- void __attribute__ ((interrupt,no_auto_psv)) _T2Interrupt(void){
- IEC0bits.T2IE=0;
- IFS0bits.T2IF=0;
- t2=2;
- IEC0bits.T2IE=1;
- IFS0bits.T2IF=0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement