// // Main // void main(void) { // // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the F2837xS_SysCtrl.c file. // InitSysCtrl(); // // Step 2. Initialize GPIO: // This example function is found in the F2837xS_Gpio.c file and // illustrates how to set the GPIO to it's default state. // // InitGpio(); Skipped for this example // // 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 F2837xS_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 F2837xS_DefaultIsr.c. // This function is found in F2837xS_PieVect.c. // InitPieVectTable(); // // Step 4. User specific code: // #if EXAMPLE1 // // This example is a basic pinout // Gpio_setup1(); #endif // - EXAMPLE1 #if EXAMPLE2 // // This example is a communications pinout // Gpio_setup2(); #endif }
void main(void) { int i=0; // Loop Variable timeout_ctr = 0 ; //############################ Initialisation ###############################// // _SOURCE_ DeviceInit(); // DeviceInit.c InitGpio(); // GpioInit.c InitPieVectTable(); // PieVect.c InitPieCtrl(); // PieCtrl.c InitSci(); // Serial.c init_lcd_menu(); // LcdMenu.c lcd_init(); // LcdDriver.c SineRefInit(); // SineRefInit.c AdcInit(); // AdcInit.c PwmInit(); // PwmInit.c State.all = 0; State.bit.Start = 1; PeripheralEn.bit.Buttons= 1; // Allow button checks EnableInterrupts(); // PieCtrl.c DisableInverter(); // Ensure inverter is disabled on start-up lcd_position(0x5,0x0); lcd_puts("Kingfisher"); // Print introductory message on top line lcd_position(0x0,0x1); // Start Cursor From First Line lcd_puts(" Hydro Controller "); // Print introductory message on bottom line asm(" CLRC INTM, DBGM"); // Enable global interrupts and real-time debug CpuTimer0Regs.PRD.all = COUNTER0_PRD; // Initialise CPU timer0 CpuTimer1Regs.PRD.all = COUNTER1_PRD; // Initialise CPU timer0 //############################# Polling Loop ################################// while(1) { //#######################################################################// // STATES // //#######################################################################// // //############################ START STATE ##############################// if(State.bit.Start) { asm("NOP"); } //######################### EXCITATION STATE ############################// if (State.bit.Excitation) { data_out.bit.led_ctrl_green = 1; data_out.bit.led_ctrl_red = 0; if ( (AdcSignal.V_HVDC < 50) && (AdcSignal.Shaft_Velocity > 2000) && !(State.bit.Fault) ) { // PeripheralEn.bit.Buttons= 0; timeout_ctr++; if (timeout_ctr > 3) { State.all = 0; State.bit.Fault = 1; timeout_ctr = 0; } else { data_out.bit.startup = 1; shift_data_out(); if (CpuTimer0Regs.TCR.bit.TIF == 1) { for (i=1;i<=2;i++) // wait 2 seconds { CpuTimer0Regs.TCR.bit.TIF = 1; // Clear flag while(!CpuTimer0Regs.TCR.bit.TIF); } } data_out.bit.startup = 0; shift_data_out(); if (CpuTimer0Regs.TCR.bit.TIF == 1) { for(i=1;i<=3;i++) // wait 3 seconds { CpuTimer0Regs.TCR.bit.TIF = 1; // Clear flag while(!CpuTimer0Regs.TCR.bit.TIF); } } } } else if ( (AdcSignal.V_HVDC >= 50) && (AdcSignal.V_HVDC < 250) ) { timeout_ctr = 0; State.all = 0; State.bit.Excited = 1; // Perhaps change LCD Screen to message saying "Slowly increase turbine speed to increase voltage." } else if ( (AdcSignal.V_HVDC > 250) ) { timeout_ctr = 0; // Resets the timeout counter for excitation State.all = 0; State.bit.StartupSequence = 1; } } //############################ EXCITED STATE ############################// if (State.bit.Excited) { if (AdcSignal.V_HVDC > 250) { timeout_ctr = 0; // Resets the timeout counter for excitation State.all = 0; State.bit.StartupSequence = 1; } else { // !!!!! ADD SOME CODE FOR THIS CONDITION !!!!! } } //############################# FAULT STATE #############################// if (State.bit.Fault) { if (PeripheralEn.bit.Inverter) { DisableInverter(); data_out.bit.led_dump1 = 0; data_out.bit.led_dump2 = 0; data_out.bit.led_inverter = 0; data_out.bit.led_update = 0; } data_out.bit.protect_trig = 1; data_out.bit.led_ctrl_green = 0; data_out.bit.led_ctrl_red = 1; if ( !(LED_Fault_Ctr++ % 16384) ) { data_out.bit.led_dump1 = ~(data_out.bit.led_dump1); data_out.bit.led_dump2 = ~(data_out.bit.led_dump2); data_out.bit.led_inverter = ~(data_out.bit.led_inverter); data_out.bit.led_update = ~(data_out.bit.led_update); } } //########################### DC STABLE STATE ###########################// if (State.bit.DcStable) { if ( !(PeripheralEn.bit.Inverter) ) EnableInverter(); data_out.bit.led_ctrl_green = 1; data_out.bit.led_ctrl_red = 0; } //############################## TEST STATE #############################// if (State.bit.Test) { } //########################### TURNED OFF STATE ##########################// if (State.bit.TurnOff) { data_out.bit.led_ctrl_green = 0; data_out.bit.led_ctrl_red = 0; data_out.bit.led_dump1 = 0; data_out.bit.led_dump2 = 0; data_out.bit.led_inverter = 0; data_out.bit.led_update = 0; } //#######################################################################// // FLAGS // //#######################################################################// // //######################## SCREEN UPDATE FLAG #######################// if (flag_bc) { menu_update_lcd(); flag_bc = 0; } asm(" nop"); //########################### READ IN DATA FLAG #########################// if (flag.bit.DataIN) { shift_data_in(); check_buttons(); flag.bit.DataIN = 0; } //########################## SEND OUT DATA FLAG #########################// if (flag.bit.DataOUT) { shift_data_out(); flag.bit.DataOUT = 0; } //######################## HVDC VOLTAGE REGULATION ######################// if (flag.bit.V_HVDC) { if ( !(State.bit.TurnOff) && !(State.bit.Fault) ) { V_DcError = V_DC_REF - AdcSignal.V_HVDC; // Calculate the DC error from the reference DutyError = 0.75*K_DC*V_DcError + 0.25*DutyError; // Modify the dump load duty cycle with respect to the DC error if (V_DcError > 0) // If the DC voltage is less than the reference, dump no power { EPwm3Regs.CMPA.half.CMPA= 0; EPwm3Regs.CMPB = 0; } else if (DutyError > DUMP1_DUTY_MAX) { DutyFeedback = 1.0*(DUMP1_DUTY_MAX)*DUMP_PRD; EPwm3Regs.CMPA.half.CMPA= DutyFeedback; DutyFeedback = 1.0*(DutyError - DUMP1_DUTY_MAX)*DUMP_PRD; EPwm3Regs.CMPB = DutyFeedback; } else { DutyFeedback = 1.0*(DutyError)*DUMP_PRD; EPwm3Regs.CMPA.half.CMPA= DutyFeedback; EPwm3Regs.CMPB = 0; } }else // If there is a fault or the system is off, dump all power { EPwm3Regs.CMPA.half.CMPA = (DUMP1_DUTY_MAX)*DUMP_PRD; // Dump all power! EPwm3Regs.CMPB = DUMP_PRD; // Dump all power! } flag.bit.V_HVDC = 0; // Reset the flag } //###################### SHAFT VELOCITY MEASUREMENT #####################// if (ShaftVelocCtr >= 1000) // Updates the measured velocity every 1000*(1/EPWM8) = 0.5s { AdcSignal.Shaft_Velocity = shaftspeedtest; //0.8*(ShaftPulseCtr/0.5)*60 + 0.2*AdcSignal.Shaft_Velocity; // Where 1 pulse = 1 rotation ShaftPulseCtr = 0; // { Reset counters ShaftVelocCtr = 0; // { } //################### OUTPUT AC VOLTAGE FREQUENCY MEASURE ################// if (V_AcOutPrdCtr >= 10) { AdcSignal.V_AcOut_Freq = (SYSCLK/ADC_SAMP_PRD+1)*(V_AcOutPrdCtr/FreqOutDivCtr); // Approximate frequency over at least 10 periods AcOutFreqError = AC_OUT_FREQ - AdcSignal.V_AcOut_Freq; // Find frequency error V_AcOutReqFreq = V_AcOutReqFreq + 0.05*AcOutFreqError; // Frequency feedback loop sinGen.freq = V_AcOutReqFreq*(BIT31)*(2*BIT32*INVERTER_PWM_PRD)*(1/(sinGen.step_max*SYSCLK)); // Alter reference sine wave frequency V_AcOutPrdCtr = 0 ; // } FreqOutDivCtr = 0 ; // } Reset counters } //####################### INVERTER CONTROL #######################// if (flag.bit.Inverter) { /* BROKEN InverterMaxDuty = V_AC_OUT_REF*(1.0/(AdcSignal.V_HVDC)); // Find the maximum duty cycle necessary to create Vacoutpeak=325V DutyScaleMax = (BIT16*InverterMaxDuty-BIT15)*30.51850948e-6; // 30.51850948e-6 = 1/(BIT15-1) if ( !(State.bit.InvSoftStart) ) { DutyScaleError = DutyScaleMax - DutyScale; DutyScale = DutyScale + 0.01*DutyScaleError; if (DutyScale < 0.2) { DutyScale = 0.2; ADD SOME CODE IN CASE IT IS DANGEROUS TO GO BELOW 0.2 } } */ // ----- Toggle the onboard LED to show inverter control is active if (GPIO34_count >= 4000) // toggle_time = GPIO34_count_limit/fpwm { GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1; // Toggle the pin GPIO34_count = 0; // Reset the count } flag.bit.Inverter = 0; } //################### INPUT AC VOLTAGE FREQUENCY MEASURE ################// if (V_AcInPrdCtr >= 10) { AdcSignal.V_AcIn_Freq = 0.8*((SYSCLK/ADC_SAMP_PRD+1)*(V_AcInPrdCtr/FreqInDivCtr)) + 0.2*(AdcSignal.V_AcIn_Freq); // Approximate frequency over at least 10 periods genSlip = 100*((AdcSignal.V_AcIn_Freq*60) - (AdcSignal.Shaft_Velocity))/(AdcSignal.V_AcIn_Freq*60); // Calculates slip: s= 100%*((ns-nr)/ns) V_AcInPrdCtr = 0 ; // } FreqInDivCtr = 0 ; // } Reset counters } /* if(SendADCResult) { SendADCResult = 0; SerialSendStr("Voltage: "); SerialSendInt((int)V_DC_measured); SerialSendStr(" PWM1: "); SerialSendInt((int)(EPwm3Regs.CMPA.half.CMPA*100.0/(DUMP_PRD*1.0))); SerialSendCR(); SerialSendStr(" PWM2: "); SerialSendInt((int)((EPwm3Regs.CMPB*100.0)/(DUMP_PRD*1.0))); SerialSendCR(); }*/ } }
// // Main // void main(void) { // // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the F2837xS_SysCtrl.c file. // InitSysCtrl(); // // Step 2. Initialize GPIO: // This example function is found in the F2837xS_Gpio.c file and // illustrates how to set the GPIO to it's default state. // For this example, only enable the GPIO for McBSP-A // InitMcbspaGpio(); // // 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 F2837xS_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 F2837xS_DefaultIsr.c. // This function is found in F2837xS_PieVect.c. // InitPieVectTable(); // // Step 4. User specific code // init_mcbsp_spi(); sdata1 = 0x55aa; sdata2 = 0xaa55; // // Main loop to transfer 32-bit words through MCBSP in SPI mode periodically // for(;;) { mcbsp_xmit(sdata1,sdata2); // // Master waits until RX data is ready // while( McbspaRegs.SPCR1.bit.RRDY == 0 ) {} rdata2 = McbspaRegs.DRR2.all; // Read DRR2 first. rdata1 = McbspaRegs.DRR1.all; // Then read DRR1 to complete // receiving of data. // // Check that correct data is received. // if((rdata2 != sdata2)&&(rdata1 != sdata1)) { error( ); } delay_loop(); sdata1^=0xFFFF; sdata2^=0xFFFF; __asm(" nop"); // Good place for a breakpoint } }
void main(void) { // WARNING: Always ensure you call memcpy before running any functions from RAM // InitSysCtrl includes a call to a RAM based function and without a call to // memcpy first, the processor will go "into the weeds" #ifdef _FLASH memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize); #endif // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the f2802x_SysCtrl.c file. InitSysCtrl(); // Step 2. Initialize GPIO: // This example function is found in the f2802x_Gpio.c file and // illustrates how to set the GPIO to it's default state. // InitGpio(); // Skipped for this example InitEPwmGpio(); // Step 3. 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 f2802x_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 f2802x_DefaultIsr.c. // This function is found in f2802x_PieVect.c. InitPieVectTable(); // Step 4. Initialize all the Device Peripherals: // Not required for this example // For this example, only initialize the ePWM // Step 5. User specific code, enable interrupts: UpdateFine = 1; PeriodFine = 0; status = SFO_INCOMPLETE; // Calling SFO() updates the HRMSTEP register with calibrated MEP_ScaleFactor. // HRMSTEP must be populated with a scale factor value prior to enabling // high resolution period control. while (status== SFO_INCOMPLETE) // Call until complete { status = SFO(); if (status == SFO_ERROR) { error(); // SFO function returns 2 if an error occurs & # of MEP // steps/coarse step } // exceeds maximum of 255. } // Some useful PWM period vs Frequency values // TBCLK = 60 MHz 40 MHz //=============================== // Period Freq Freq // 1000 30 KHz 20 KHz // 800 37.5 KHz 25 KHz // 600 50 KHz 33 KHz // 500 60 KHz 40 KHz // 250 120 KHz 80 KHz // 200 150 KHz 100 KHz // 100 300 KHz 200 KHz // 50 600 KHz 400 KHz // 30 1 MHz 667 KHz // 25 1.2 MHz 800 KHz // 20 1.5 MHz 1 MHz // 12 2.5 MHz 1.7 MHz // 10 3 MHz 2 MHz // 9 3.3 MHz 2.2 MHz // 8 3.8 MHz 2.5 MHz // 7 4.3 MHz 2.9 MHz // 6 5.0 MHz 3.3 MHz // 5 6.0 MHz 4.0 MHz //==================================================================== // ePWM and HRPWM register initialization //==================================================================== HRPWM_Config(20); // ePWMx target for(;;) { // Sweep PeriodFine as a Q16 number from 0.2 - 0.999 for(PeriodFine = 0x3333; PeriodFine < 0xFFBF; PeriodFine++) { if(UpdateFine) { /* // Because auto-conversion is enabled, the desired // fractional period must be written directly to the // TBPRDHR (or TBPRDHRM) register in Q16 format // (lower 8-bits are ignored) EPwm1Regs.TBPRDHR = PeriodFine; // The hardware will automatically scale // the fractional period by the MEP_ScaleFactor // in the HRMSTEP register (which is updated // by the SFO calibration software). // Hardware conversion: // MEP delay movement = ((TBPRDHR(15:0) >> 8) * HRMSTEP(7:0) + 0x80) >> 8 */ EPwm1Regs.TBPRDHR = PeriodFine; //In Q16 format } else { // No high-resolution movement on TBPRDHR. EPwm1Regs.TBPRDHR = 0; } // Call the scale factor optimizer lib function SFO(0) // periodically to track for any change due to temp/voltage. // This function generates MEP_ScaleFactor by running the // MEP calibration module in the HRPWM logic. This scale // factor can be used for all HRPWM channels. HRMSTEP // register is automatically updated by the SFO function. status = SFO(); // in background, MEP calibration module continuously updates MEP_ScaleFactor if (status == SFO_ERROR) { error(); // SFO function returns 2 if an error occurs & # of MEP steps/coarse step } // exceeds maximum of 255. } // end PeriodFine for loop } // end infinite for loop } // end main
void main(void) { // 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: // This example function is found in the DSP2803x_Gpio.c file and // illustrates how to set the GPIO to it's default state. // InitGpio(); // Skipped for this example // Step 3. 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 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(); // 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 registers PieVectTable.EPWM1_INT = &epwm1_timer_isr; PieVectTable.EPWM2_INT = &epwm2_timer_isr; PieVectTable.EPWM3_INT = &epwm3_timer_isr; EDIS; // This is needed to disable write to EALLOW protected registers // Step 4. Initialize all the Device Peripherals: InitEPwmTimer(); // For this example, only initialize the ePWM Timers // Step 5. User specific code, enable interrupts: // Copy time critical code and Flash setup code to RAM // This includes the following ISR functions: epwm1_timer_isr(), epwm2_timer_isr() // epwm3_timer_isr and and InitFlash(); // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart // symbols are created by the linker. memcpy((uint16_t *)&RamfuncsRunStart,(uint16_t *)&RamfuncsLoadStart, (unsigned long)&RamfuncsLoadSize); // Call Flash Initialization to setup flash waitstates // This function must reside in RAM InitFlash(); // Initialize counters: EPwm1TimerIntCount = 0; EPwm2TimerIntCount = 0; EPwm3TimerIntCount = 0; LoopCount = 0; // Enable CPU INT3 which is connected to EPWM1-3 INT: IER |= M_INT3; // Enable EPWM INTn in the PIE: Group 3 interrupt 1-3 PieCtrlRegs.PIEIER3.bit.INTx1 = PWM1_INT_ENABLE; PieCtrlRegs.PIEIER3.bit.INTx2 = PWM2_INT_ENABLE; PieCtrlRegs.PIEIER3.bit.INTx3 = PWM3_INT_ENABLE; // Enable global Interrupts and higher priority real-time debug events: EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM // Step 6. IDLE loop. Just sit and loop forever (optional): EALLOW; GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0; GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1; EDIS; for(;;) { // This loop will be interrupted, so the overall // delay between pin toggles will be longer. DELAY_US(DELAY); LoopCount++; GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1; } }