void initPIC() { SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); initLEDs(); initSerialNU32v2(); // Setup and turn off electromagnets EMAG1 = 0; EMAG2 = 0; TRISEbits.TRISE7 = 0; TRISCbits.TRISC1 = 0; // Direction Output DIR = 1; TRISAbits.TRISA9 = 0; setup_counters(); CloseADC10(); #define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF #define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31 #define PARAM4 ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA OpenADC10(PARAM1, PARAM2, PARAM3, PARAM4, 0); EnableADC10(); // 20kHz PWM signal, duty from 0-1000, pin D3 OpenTimer2(T2_ON | T2_PS_1_4, MAX_DUTY); OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); SetDCOC4PWM(0); // 200 Hz ISR OpenTimer3(T3_ON | T3_PS_1_256, (6250/4 - 1)); //OpenTimer3(T3_ON | T3_PS_1_256, (62500 - 1)); mT3SetIntPriority(1); mT3ClearIntFlag(); mT3IntEnable(1); INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableSystemMultiVectoredInt(); }
int main(void) { timesec=0; // set PIC32 to max computing power SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableSystemMultiVectoredInt(); initLEDs(); LED0 = 1; LED1 = 1; initSerialNU32v2(); setup_counters(); CloseADC10(); #define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF #define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31 #define PARAM4 ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4,0); EnableADC10(); // Setup and turn off electromagnets EMAG1 = 0; EMAG2 = 0; TRISEbits.TRISE7 = 0; TRISCbits.TRISC1 = 0; //Direction Output DIR = 1; TRISAbits.TRISA9 = 0; //g-select Outputs GSEL1 = 0; GSEL2 = 0; TRISEbits.TRISE2 = 0; TRISCbits.TRISC13= 0; //0g Inputs TRISAbits.TRISA0 = 1; TRISAbits.TRISA4 = 1; // 20kHz PWM signal, duty from 0-1000, pin D3 OpenTimer2(T2_ON | T2_PS_1_4, 1000); OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); HBridgeDuty = 0; SetDCOC4PWM(HBridgeDuty); // 20Hz ISR OpenTimer3(T3_ON | T3_PS_1_256, 15625); mT3SetIntPriority(1); mT3ClearIntFlag(); mT3IntEnable(1); while(1) { if(start){ EMAG2=0; SetDCOC4PWM(100); delaysec(delay1); SetDCOC4PWM(1000); delaysec(delay2); SetDCOC4PWM(500); delaysec(delay3); EMAG2=1; SetDCOC4PWM(0); // EMAG1=0; // SetDCOC4PWM(900); // DIR = 0; // delaysec(delay1); // SetDCOC4PWM(0); // delaysec(delay2); // SetDCOC4PWM(700); // delaysec(delay1); // SetDCOC4PWM(1000); // EMAG1=1; start=0; } } }