void ADCTemp_Init(uint16_t* ADCnum) { static uint8_t i=0; if(i>=8) { InitMeasCompCurr(hPhaseAOffset,hPhaseBOffset); VoltageOffset=VoltageOffset; SPOffset>>=5; //10bit Motor_State=WAIT; }
int main ( void ) { /** pin buttons locals */ volatile bool regPinButton1; volatile bool regPinButton2; /** speed local */ short iRefSpeed; // Configure Oscillator to operate the device at 40Mhz // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 // Fosc= 8*40/(2*2)= 80Mhz for 8M input clock PLLFBD = 38; // M=40 CLKDIVbits.PLLPOST = 0; // N1=2 CLKDIVbits.PLLPRE = 0; // N2=2 // while(OSCCONbits.LOCK != 1) {}; // Wait for PLL to loc /** setup dsPIC ports */ SetupPorts(); /** init user parameters */ InitUserParms(); /** init user specified parms and stop on error */ if( SetupPeripherals() ) { /* Error */ return 0; } uGF.Word = 0; // clear flags while(1) { // init Mode eStateControl = CNTRL_STOP; iLockLoopCnt = 0; /** clear the variables other than STOP command */ uGF.bit.TLock = 0; uGF.bit.Btn1Pressed = 0; uGF.bit.Btn2Pressed = 0; uGF.bit.DoSnap = 0; uGF.bit.SnapDone = 0; /** ============= Open Loop ======================*/ OpenLoopParm.qVelMech = dqOL_VelMech; CtrlParm.qVelRef = OpenLoopParm.qVelMech; iRefSpeed = CtrlParm.qVelRef; InitOpenLoop(); /** Inital offsets for currents */ InitMeasCompCurr( 450, 730 ); // init board SetupBoard(); // Enable ADC interrupt and begin main loop timing IFS0bits.AD1IF = 0; IEC0bits.AD1IE = 1; /** Initialize private variables used by CalcVelIrp. */ InitCalcVel(); /** Initialize private variables used by CurrModel */ InitCurModel(); // zero out i sums PIParmD.qdSum = 0; PIParmQ.qdSum = 0; PIParmQref.qdSum = 0; // zero out i out PIParmD.qOut = 0; PIParmQ.qOut = 0; PIParmQref.qOut = 0; /** if state is stop */ if(eStateControl == CNTRL_STOP) { //wait here until button 1 is pressed or stop time elapsed while(uGF.bit.TStop){}; while(!pinButton1) { /** make sure that the STOP do not occur due to this loop */ iStopLoopCnt = 0; // Start offset accumulation //and accumulate current offset while waiting MeasCompCurr(); } while(pinButton1); //when button 1 is released eStateControl = CNTRL_OPEN_LOOP; //then start motor in open loop } // Run the motor uGF.bit.ChangeMode = 1; // Enable the driver IC on the motor control PCB pinPWMOutputEnable_ = 0; //Run Motor loop while(1) { /** algorithm was stopped before*/ if(uGF.bit.TStop) { /* break the while */ break; } /* for logic of this application 2 buttons are used S3 and S6. If both buttons are pressed the state of the application is commuted between OPEN_LOOP, CLOSED_LOOP and CLOSED_LOOP_DOUBLE_SPEED. The touch of only one button will increase the speed in one of the states enumerated above */ /* read the buttons state */ /* while no buttons are pushed */ do{ regPinButton1 = pinButton1; /* pin button 1 */ /* retain if pressed */ if(regPinButton1 == 1) { uGF.bit.Btn1Pressed=1; } regPinButton2 = pinButton2; /* pin button 2 */ /* retain if pressed */ if(regPinButton2 == 1) { uGF.bit.Btn2Pressed=1; } /** make sure that the STOP do not occur due to this loop */ iStopLoopCnt = 0; }while((!regPinButton1)&&(!regPinButton2)); /* while buttons are released */ do{ regPinButton1 = pinButton1; /* pin button 1 */ /* retain if pressed */ if(regPinButton1 == 1) { uGF.bit.Btn1Pressed=1; } regPinButton2 = pinButton2; /* pin button 2 */ /* retain if pressed */ if(regPinButton2 == 1) { uGF.bit.Btn2Pressed=1; } }while(pinButton1||pinButton2); /** the counter for STOP is cleared - if buttons released */ iStopLoopCnt = 0; /* check if one of the buttons is pressed */ /* check if both buttons are pressed */ if ((uGF.bit.Btn1Pressed)&&(uGF.bit.Btn2Pressed)) { /* if yes */ /* clear both flags indicating the buttons were pressed */ uGF.bit.Btn1Pressed = 0; uGF.bit.Btn2Pressed = 0; /* and command not locked */ /* check if command not locked */ if(!uGF.bit.TLock) { /* lock command for Tlock_multiple */ uGF.bit.TLock = 1; /* command locked */ iLockLoopCnt = 0; /* reset counter */ /* state machine for control algorithm */ switch(eStateControl) { /* the previous state was STOP */ case CNTRL_OPEN_LOOP: { /* swich the state machine */ eStateControl = CNTRL_CLOSED_LOOP; /* first switch the global flag */ uGF.bit.ChangeMode = 1; break; } case CNTRL_CLOSED_LOOP: { /* double the speed of the motor */ /* and swich the state machine */ eStateControl = CNTRL_CLOSED_LOOP_DBL_SPEED; /* double the speed */ iRefSpeed += iRefSpeed; #ifdef SNAPSHOT uGF.bit.DoSnap = 1; SnapCount = 0; #endif break; } case CNTRL_CLOSED_LOOP_DBL_SPEED: { /* divide by 2 the speed of the motor */ /* and swich the state machine */ eStateControl = CNTRL_CLOSED_LOOP; /* divide the speed by 2 */ iRefSpeed -= iRefSpeed/2; #ifdef SNAPSHOT uGF.bit.DoSnap = 1; SnapCount = 0; #endif break; } default: { /* undefined state, stop */ eStateControl = CNTRL_STOP; /* disable the PWM module */ pinPWMOutputEnable_ = 1; break; } } } } /* check which button was pressed */ /* perhaps button 1 was the one */ else if(uGF.bit.Btn1Pressed) { /* if yes */ /* clear the button flag for future usage */ uGF.bit.Btn1Pressed = 0; /* and command not locked */ /* check if command not locked */ if(!uGF.bit.TLock) { /* lock command for Tlock_multiple */ uGF.bit.TLock = 1; /* command locked */ iLockLoopCnt = 0; /* reset counter */ /* increase speed with SPEED_STEP */ iRefSpeed += SPEED_STEP; } } /* it was button 2, but checking */ else if(uGF.bit.Btn2Pressed) { /* if yes */ /* clear the button flag for future usage */ uGF.bit.Btn2Pressed = 0; /* and command not locked */ /* check if command not locked */ if(!uGF.bit.TLock) { /* lock command for Tlock_multiple */ uGF.bit.TLock = 1; /* command locked */ iLockLoopCnt = 0; /* reset counter */ /* decrease speed with SPEED_STEP */ iRefSpeed -= SPEED_STEP; } } /** limit the reference to 60Hz */ if( iRefSpeed > MAX_SPEED_UP ) { iRefSpeed = MAX_SPEED_UP; } /** limit the reference to 60Hz */ if((signed short)iRefSpeed < (signed short)MAX_SPEED_DOWN ) { iRefSpeed = MAX_SPEED_DOWN; } /** update the global speed reference */ CtrlParm.qVelRef = iRefSpeed; if( uGF.bit.SnapDone ) { uGF.bit.SnapDone=0; } } // End of Run Motor loop } // End of Main loop // should never get here while(1){} }