void main() { // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the DSP2803x_SysCtrl.c file. InitSysCtrl(); // Step 2. Initialize GPIO: // Enable XCLOCKOUT to allow monitoring of oscillator 1 EALLOW; GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 3; //enable XCLOCKOUT through GPIO mux SysCtrlRegs.XCLK.bit.XCLKOUTDIV = 2; //XCLOCKOUT = SYSCLK // Step 3. Clear all interrupts and initialize PIE vector table: // Disable CPU interrupts DINT; // Initialize PIE control registers to their default state. // The default state is all PIE interrupts disabled and flags // are cleared. // This function is found in the DSP2803x_PieCtrl.c file. InitPieCtrl(); // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; // Initialize the PIE vector table with pointers to the shell Interrupt // Service Routines (ISR). // This will populate the entire table, even if the interrupt // is not used in this example. This is useful for debug purposes. // The shell ISR routines are found in DSP2803x_DefaultIsr.c. // This function is found in DSP2803x_PieVect.c. InitPieVectTable(); // Step 4. Initialize all the Device Peripherals: // Configure and Initialize the ADC: InitAdc(); AdcOffsetSelfCal(); EALLOW; AdcRegs.ADCCTL1.bit.TEMPCONV = 1; //Connect channel A5 internally to the temperature sensor AdcRegs.ADCSOC0CTL.bit.CHSEL = 5; //Set SOC0 channel select to ADCINA5 AdcRegs.ADCSOC1CTL.bit.CHSEL = 5; //Set SOC1 channel select to ADCINA5 AdcRegs.ADCSOC0CTL.bit.ACQPS = 36; //Set SOC0 acquisition period to 37 ADCCLK AdcRegs.ADCSOC1CTL.bit.ACQPS = 36; //Set SOC1 acquisition period to 37 ADCCLK AdcRegs.INTSEL1N2.bit.INT1SEL = 1; //Connect ADCINT1 to EOC1 AdcRegs.INTSEL1N2.bit.INT1E = 1; //Enable ADCINT1 // Note: two channels have been connected to the temp sensor // so that the first sample can be discarded to avoid the // ADC first sample issue. See the device errata. // Set the flash OTP wait-states to minimum. This is important // for the performance of the temperature conversion function. FlashRegs.FOTPWAIT.bit.OTPWAIT = 1; //Main program loop - continually sample temperature for(;;) { //Sample the temp sensor... //Force start of conversion on SOC0 and SOC1 AdcRegs.ADCSOCFRC1.all = 0x03; //Wait for end of conversion. while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){} //Wait for ADCINT1 AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; //Clear ADCINT1 //Get temp sensor sample result from SOC1 temp = AdcResult.ADCRESULT1; //Convert the raw temperature sensor measurement into temperature degC = GetTemperatureC(temp); degK = GetTemperatureK(temp); } }
main() { // Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the F2806x_SysCtrl.c file. InitSysCtrl(); // Clear all interrupts and initialize PIE vector table: // Disable CPU interrupts DINT; // Initialize the PIE control registers to their default state. // The default state is all PIE interrupts disabled and flags // are cleared. // This function is found in the F2806x_PieCtrl.c file. InitPieCtrl(); // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; // Initialize the PIE vector table with pointers to the shell Interrupt // Service Routines (ISR). // This will populate the entire table, even if the interrupt // is not used in this example. This is useful for debug purposes. // The shell ISR routines are found in F2806x_DefaultIsr.c. // This function is found in F2806x_PieVect.c. InitPieVectTable(); // Interrupts that are used in this example are re-mapped to // ISR functions found within this file. EALLOW; // This is needed to write to EALLOW protected register PieVectTable.ADCINT1 = &adc_isr; EDIS; // This is needed to disable write to EALLOW protected registers // Initialize all the Device Peripherals: // This function is found in F2806x_InitPeripherals.c // InitPeripherals(); // Not required for this example InitAdc(); // For this example, init the ADC AdcOffsetSelfCal(); // User specific code, enable interrupts: // Enable ADCINT1 in PIE PieCtrlRegs.PIEIER1.bit.INTx1 = 1; // Enable INT 1.1 in the PIE IER |= M_INT1; // Enable CPU Interrupt 1 EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM LoopCount = 0; ConversionCount = 0; // Configure ADC EALLOW; AdcRegs.ADCCTL2.bit.ADCNONOVERLAP = 1; // Enable non-overlap mode AdcRegs.ADCCTL1.bit.INTPULSEPOS = 1; // ADCINT1 trips after AdcResults latch AdcRegs.INTSEL1N2.bit.INT1E = 1; // Enabled ADCINT1 AdcRegs.INTSEL1N2.bit.INT1CONT = 0; // Disable ADCINT1 Continuous mode AdcRegs.INTSEL1N2.bit.INT1SEL = 1; // setup EOC1 to trigger ADCINT1 to fire AdcRegs.ADCSOC0CTL.bit.CHSEL = 4; // set SOC0 channel select to ADCINA4 AdcRegs.ADCSOC1CTL.bit.CHSEL = 2; // set SOC1 channel select to ADCINA2 AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 5; // set SOC0 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1 AdcRegs.ADCSOC1CTL.bit.TRIGSEL = 5; // set SOC1 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1 AdcRegs.ADCSOC0CTL.bit.ACQPS = 6; // set SOC0 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1) AdcRegs.ADCSOC1CTL.bit.ACQPS = 6; // set SOC1 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1) EDIS; // Assumes ePWM1 clock is already enabled in InitSysCtrl(); EPwm1Regs.ETSEL.bit.SOCAEN = 1; // Enable SOC on A group EPwm1Regs.ETSEL.bit.SOCASEL = 4; // Select SOC from CMPA on upcount EPwm1Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event EPwm1Regs.CMPA.half.CMPA = 0x0080; // Set compare A value EPwm1Regs.TBPRD = 0xFFFF; // Set period for ePWM1 EPwm1Regs.TBCTL.bit.CTRMODE = 0; // count up and start // Wait for ADC interrupt for(;;) { LoopCount++; } }
void main(void) { memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize); // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks InitSysCtrl(); SysCtrlRegs.PCLKCR1.bit.EQEP2ENCLK = 0; // eQEP2 SysCtrlRegs.PCLKCR0.bit.SPIBENCLK = 0; // SPI-B InitFlash(); // Step 2. Initalize GPIO: // InitGpio(); // Skipped; not needed InitEQep1Gpio(); //InitEPwm1Gpio(); //InitEPwm2Gpio(); //InitEPwm3Gpio(); InitECap1Gpio(); InitECap2Gpio(); InitECap3Gpio(); EALLOW; GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; GpioCtrlRegs.GPBMUX2.bit.GPIO50 = 0; GpioCtrlRegs.GPBMUX2.bit.GPIO51 = 0; GpioCtrlRegs.GPBDIR.bit.GPIO50 = 1; setupDrv8301(); setupSpiA(); //DRV8301_setupSpi(); EDIS; DINT; InitPieCtrl(); // The default state is all PIE interrupts disabled and flags are cleared. // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; // Initialize the PIE vector table with pointers to the shell ISRs. // This will populate the entire table, even if the interrupt // is not used in this example. This is useful for debug purposes. // The shell ISR routines are found in F2806x_DefaultIsr.c. InitPieVectTable(); // Interrupts that are used in this example are re-mapped to our ISR functions EALLOW; PieVectTable.ECAP1_INT = &ecap1_isr; // Group 4 PIE Peripheral Vectors PieVectTable.ECAP2_INT = &ecap2_isr; // '' PieVectTable.ECAP3_INT = &ecap3_isr; // '' PieVectTable.ADCINT1 = &adc_isr; // //PieVectTable.SCIRXINTA = &scia_isr; EDIS; // Step 4. Initialize all the Device Peripherals: InitECapRegs(); scia_init(); epwmInit(1,2,0); //10kHz, 50% duty, no chop InitAdc(); AdcOffsetSelfCal(); // Step 5. Enable interrupts: IER |= M_INT1; // Enable CPU Interrupt 1 (connected to ADC) IER |= M_INT4; // Enable CPU INT4 which is connected to ECAP1-4 INT IER |= M_INT3; // Enable CPU INT1 which is connected to CPU-Timer 0: PieCtrlRegs.PIEIER1.bit.INTx1 = 1; // INT1.1 for ADC PieCtrlRegs.PIEIER4.bit.INTx1 = 1; // INT4.1 for ecap1 PieCtrlRegs.PIEIER4.bit.INTx2 = 1; // INT4.2 for ecap2 PieCtrlRegs.PIEIER4.bit.INTx3 = 1; // INT4.3 for ecap3 // Enable global Interrupts and higher priority real-time debug events: EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM qep_data.init(&qep_data); int printData = 10001; readHallStateFlag = 1; char writeBuffer[80] = {0}; int lastPhase = 0; gogo = 1; DRV8301_enable(); DRV8301_setupSpi(); i = 0; while(1) { qep_data.calc(&qep_data); if (readHallStateFlag) updateHallState(); if ((lastPhase != Phase) && (printData)) { //\033[2J\033[0;0H\r sprintf(writeBuffer, "Hall State: %d\n\r", (int)Phase); scia_msg(writeBuffer); sprintf(writeBuffer, "Velocity: %d rpm\n\r", qep_data.SpeedRpm_fr); scia_msg(writeBuffer); //sprintf(writeBuffer, "Mechanical Angle: %f degrees\n\r", qep_data.theta_mech*360); //scia_msg(writeBuffer); //sprintf(writeBuffer, "Electrical Angle: %f\n\r", qep_data.theta_elec); //scia_msg(writeBuffer); lastPhase = Phase; } //DRV8301_readData(); //if (!Phase /*|| drv8301.fault || drv8301.OverTempShutdown || drv8301.OverTempWarning*/) { //while (!Phase || drv8301.fault || drv8301.OverTempShutdown || drv8301.OverTempWarning){ //GpioDataRegs.GPBCLEAR.bit.GPIO50 = 1; ///DELAY_US(32000); //DELAY_US(32000); //sprintf(writeBuffer, "\aERROR DECTECTED: \n\r Hall State: %d %d %d\n\r", CoilA, CoilB, CoilC); //scia_msg(writeBuffer); //sprintf(writeBuffer, "Fault Bit: %d\n\rOverTempShutdown: %d\n\rOverTempWarning%d\n\r", drv8301.fault, drv8301.OverTempShutdown, drv8301.OverTempWarning); //scia_msg(writeBuffer); //} //} //else { //GpioDataRegs.GPBSET.bit.GPIO50 = 1; //} } }