Ejemplo n.º 1
0
void InitProx(void)
{
    InitAd();				// init AD converter module
    InitTMR1();				// init timer 1 for ir processing
}
/******************************************************************************
* Function:     main(void)
*
* Output:		None
*
* Overview:		Main function used to init the ADC, PWM and TIMER2 modules. 
*				It also inits the global variables used in the interrupts and 
*				PI controller.
*				The main task executed here is to start and stop the motor 
*				as well as setting the ramp-up initial parameters to 
*				spin the motor
*
* Note:			None
*******************************************************************************/
int main(void)
{

	// Configure Oscillator to operate the device at 30Mhz
	// Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
	// Fosc= 7.37*32/(2*2)= 58.96Mhz for 7.37 input clock
	/****************** Clock definitions *********************************/
	PLLFBD=30;					// M=32
	CLKDIVbits.PLLPOST=0;		// N1=2
	CLKDIVbits.PLLPRE=0;		// N2=2
	OSCTUN=0;					// Tune FRC oscillator, if FRC is used

	// Disable Watch Dog Timer
	RCONbits.SWDTEN=0;

	// Clock switch to incorporate PLL
	__builtin_write_OSCCONH(0x01);		// Initiate Clock Switch to
										// FRC with PLL (NOSC=0b001)
	__builtin_write_OSCCONL(0x01);		// Start clock switching
	while (OSCCONbits.COSC != 0b001);	// Wait for Clock switch to occur

	// Wait for PLL to lock
	while(OSCCONbits.LOCK!=1) {};
		   
	/****************** PID init **************************************/	
#ifdef CLOSELOOPMODE
	ReferenceSpeed = (MIN_DUTY_CYCLE*2)/3;								
	
	// load the PID gain coeffecients into an array;
	PIDGainCoefficients[0] = SpeedControl_P;
	PIDGainCoefficients[1] = SpeedControl_I;
	PIDGainCoefficients[2] = SpeedControl_D;				

	// Initialize the PIDStructure variable before calculation the K1, K2, and K3 terms	
	PIDStructure.abcCoefficients = abcCoefficients;	
	PIDStructure.controlHistory = controlHistory;
	PIDCoeffCalc(PIDGainCoefficients, &PIDStructure);
	// initialize control history
	PIDInit(&PIDStructure);
	PIDStructure.controlOutput = MIN_DUTY_CYCLE;
	/****************************************************************/							
#endif	
	
	/****************** I/O port Init *********************************/
	/* Configuring Digital PORTS multiplexed with PWMs as outputs*/
	LATBbits.LATB10 = 0;	//PWM1H3
	TRISBbits.TRISB10 = 0;
	LATBbits.LATB11 = 0;	//PWM1L3
	TRISBbits.TRISB11 = 0;
	LATBbits.LATB12 = 0;	//PWM1H2
	TRISBbits.TRISB12 = 0;
	LATBbits.LATB13 = 0;	//PWM1L2
	TRISBbits.TRISB13 = 0;	
	LATBbits.LATB14 = 0;	//PWM1H1
	TRISBbits.TRISB14 = 0;	
	LATBbits.LATB15 = 0;	//PWM1L1
	TRISBbits.TRISB15 = 0;	

	/*Push Buttons ports*/
	LATBbits.LATB4 = 0;
	TRISBbits.TRISB4 = 1;
	LATAbits.LATA8 = 0;
	TRISAbits.TRISA8 = 1;
	
	/****************** Functions init *********************************/
	INTCON1bits.NSTDIS = 0;		    // Enabling nested interrupts	
	InitMCPWM();					//Configuring MC PWM module
	InitADC10();					//Configuring ADC
	InitTMR2();						//Configuring TIMER 3, used to measure speed
	InitTMR1();						//Configuring TIMER 1, used for the commutation delay

	Flags.RotorAlignment = 0;		// TURN OFF RAMP UP
	Flags.RunMotor = 0;				// indication the run motor condition
	
	/****************** Infinite Loop *********************************/
	for(;;)
		{	

		while (!S3);					// wait for S3 button to be hit
		while (S3)						// wait till button is released
			DelayNmSec(DEBOUNCE_DELAY);
		
		T2CONbits.TON = 1;				// Start TIMER , enabling speed measurement
		PWM1CON1 = 0x0777;				// enable PWM outputs
			
		/*ROTOR ALIGNMENT SEQUENCE*/
		Flags.RotorAlignment = 1;		// TURN ON rotor alignment sequence
		Flags.RunMotor = 1;				// Indicating that motor is running
		CurrentPWMDutyCycle = MIN_DUTY_CYCLE;	//Init PWM values
		DesiredPWMDutyCycle = MIN_DUTY_CYCLE;	//Init PWM values
		PWMticks = 0;					//Init Rotor aligment counter
		
		/************* Rotor alignment sequence and ramp-up sequence ************/
		for(RampUpCommState=1;RampUpCommState<7;RampUpCommState++)
			{
            
			while(++PWMticks<MAX_PWM_TICKS)
				P1OVDCON=PWM_STATE[RampUpCommState];
			PWMticks = 0;				
			}   
		Flags.RotorAlignment = 0;		// TURN OFF rotor alignment sequence
		PWMticks = MAX_PWM_TICKS+1;		// RAMP UP for breaking the motor IDLE state
		DelayNmSec(RAM_UP_DELAY);		// RAMP UP DELAY
                Sub_timer=Sub_timer++;
		/****************** Motor is running *********************************/
		while(Flags.RunMotor)			// while motor is running
			{
 
		/****************** Stop Motor *********************************/
                    


			//if (S3)							// if S3 is pressed
				//{
                    Sub_timer=0;                        // Musses
                    while ( Sub_timer=Sub_timer++<=600) // Musses
				//while (S3)					// wait for key release
					DelayNmSec(DEBOUNCE_DELAY);		
		
				PWM1CON1 = 0x0700;			// disable PWM outputs
  			    P1OVDCON = 0x0000;			// override PWM low.
				Flags.RotorAlignment = 0;	// turn on RAMP UP
				Flags.RunMotor = 0;			// reset run flag
				CurrentPWMDutyCycle = 1;	// Set PWM to the min value
				
				//Initi speed measurement variables & timer
				T2CONbits.TON = 0;	// Stop TIMER2
				TMR2 = 0;			//Clear TIMER2 register
				Timer2Value = 0;
				Timer2Average = 0;

#ifdef CLOSELOOPMODE					
				//Init PI controller variables
				DesiredSpeed = (int)((ReferenceSpeed*3)/2);
				CurrentSpeed = 0;

				// load the PID gain coeffecients into an array;
				PIDGainCoefficients[0] = SpeedControl_P;
				PIDGainCoefficients[1] = SpeedControl_I;
				PIDGainCoefficients[2] = SpeedControl_D;				

				// Initialize the PIDStructure variable before calculation the K1, K2, and K3 terms	
				PIDStructure.abcCoefficients = abcCoefficients;	
				PIDStructure.controlHistory = controlHistory;
				PIDCoeffCalc(PIDGainCoefficients, &PIDStructure);
				// initialize control history
				PIDInit(&PIDStructure);
				PIDStructure.controlOutput = MIN_DUTY_CYCLE;
#endif
								
               // }
			}//end of motor running loop
		}//end of infinite loop

 return 0;
}//end of main function