⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 acindmain.c

📁 U15873EE1V0AN00 aplication note
💻 C
📖 第 1 页 / 共 2 页
字号:
 * Function:    setVFprofile
 * Description:  sets amplitude and frequency of PWM sine wave
 *                according to given VF profile for motor
 * Parameter:	None
 * Returns:     None
 ---------------------------------------------------------------------------*/
void setVFprofile(UINT16 setSpeed)
	{

   if(setSpeed<MIN_SPEED)
      setSpeed=MIN_SPEED; /*limit minimum speed of motor F_BOOST*/

   /*Determine sine table increment (frequency) based on demanded speed */
   tableInc = ((SINE_TABLE_SIZE * setSpeed * MAX_FREQ)/
                  (PWM_UPDATE_RATE * MAX_SET_SPEED));

   /*Determine amplitude based on demanded speed */
   amplitude = ((CM3_DATA * setSpeed)/(2 * MAX_SET_SPEED));
   
   if (amplitude < MIN_AMPLITUDE)
      /*limit minimum amplitude of motor voltage (V_BOOST)*/ 
      amplitude = MIN_AMPLITUDE; 

	} /*end of ainit*/

/*------------------------------------------------------------------------- 
 * Function:    display
 * Description:  LED display sub-routine using P2
 * Parameter:	data (display data)
 * Returns:     None
 --------------------------------------------------------------------------*/
void display(UINT32 rpmData)
	{
   UINT8 bcdData[4];

   /*convert rpm data to BCD*/
   bcdData[0] = rpmData/1000; /*number of thousands*/

   bcdData[1] = ((rpmData - (bcdData[0]*1000))/100); /*hundreds*/

   bcdData[2] = ((rpmData - (bcdData[0]*1000) - (bcdData[1]*100))/10); /*tens*/

   bcdData[3] = ((rpmData - (bcdData[0]*1000) - (bcdData[1]*100)- (bcdData[2]*10))); /*units*/

   /*Output BCD to display port*/
   P2 = (bcdData[0] | 0xF0);     /*set up*/
   P2 = (bcdData[0] | 0x70);     /*latch MS digit - thousands*/
   P2 = (bcdData[0] | 0xF0);     /*hold*/

   P2 = (bcdData[1] | 0xF0);     /*set up*/
   P2 = (bcdData[1] | 0xB0);     /*latch 3rd digit - hundreds*/
   P2 = (bcdData[1] | 0xF0);     /*hold*/

   /*NOTE wiring error on card means ls and next ls digit latches are swapped*/
   P2 = (bcdData[2] | 0xF0);     /*set up*/
   P2 = (bcdData[2] | 0xE0);     /*latch LS digit - tens - should be 0xD0*/
   P2 = (bcdData[2] | 0xF0);     /*hold*/

   P2 = (bcdData[3] | 0xF0);     /*set up*/
   P2 = (bcdData[3] | 0xD0);     /*latch 2nd digit - units - should be 0xE0*/
   P2 = (bcdData[3] | 0xF0);     /*hold*/
   
   
	return;
	}

/*------------------------------------------------------------------------- 
 * Function:    getDigitalOrderedSpeed
 * Description: reads P3 switches for ordered speed
 * Parameter:	None
 * Returns:     orderedSpeed (0-MAX_FREQ)
 ---------------------------------------------------------------------------*/
UINT16 getDigitalOrderedSpeed( void )
  {
  UINT8 digitalSpeed;
  UINT16 orderedSpeed; /*scaled ordered speed */
   
   digitalSpeed = (P3 & 0xC0); /*read switches*/
   
   switch(digitalSpeed)
    {
    case 0x00:  speedDemandAv = 192; /*1000 rpm*/
    break;
    
    case 0x40:  speedDemandAv = 383; /*2000 rpm*/
    break;
    
    case 0x80:  speedDemandAv = 575; /*3000 rpm*/
    break;
    
    case 0xC0:  speedDemandAv = 767; /*4000 rpm*/
    break;
    
    default:  speedDemandAv = 0; /*0 rpm*/
    break;
    }
    
   orderedSpeed = ((speedDemandAv*MAX_RPM)/MAX_ADC_READING); /*in units of rpm*/
   
   return(orderedSpeed);

   }


/*------------------------------------------------------------------------- 
 * Function:    getOrderedSpeed
 * Description: reads ADC input for speed demand and calcs running average
 * Parameter:	None
 * Returns:     orderedSpeed (0-MAX_FREQ)
 ---------------------------------------------------------------------------*/
UINT16 getAnalogOrderedSpeed( void )
   {
   static int i=0;
   int j;
   UINT16 orderedSpeed; /*scaled ordered speed */
   
   
   if(i==(SPEED_READS-1))    /*if max no of points reached*/
      i=0;                    /*reset read pointer*/
	 
   speedDemand[i++]=(ADCR01 & 0x03ff);   /*read ADC for speed demand (mask lower 10 bits)*/
                                         
   for(j=0;j<SPEED_READS;j++)     /*add all readings together*/
      {
      speedDemandAv+=speedDemand[j];
      }

   speedDemandAv/= SPEED_READS; /*calculate average of ADC reads*/
   
   if(speedDemandAv>0x1ff)   /****DEBUG*****/
      speedDemandAv=0x1ff;  /*****DEBUG*****/

   orderedSpeed = ((speedDemandAv*MAX_RPM)/MAX_ADC_READING); /*in units of rpm*/
   
   /*start ADC Conversion for when we return to this ISR*/
   ADSCM00 = 0x9801; /*select mode, A/D polling mode channel ANI01*/

   return(orderedSpeed);

   }

/*------------------------------------------------------------------------- 
 * Function:    getActualSpeed
 * Description:  reads Timer10 and calculates actual speed of motor
 * Parameter:	None
 * Returns:     actualSpeed (Hz)
 ---------------------------------------------------------------------------*/
UINT16 getActualSpeed( void )
	{
   static UINT16 previousEncoder =0;
   SINT16 encoderDiff=0;
	UINT16 currentEncoder;
   UINT16 rpmActual =0;                /*actual speed in rpm*/

	/******Current Speed Calculation********************/
                               
	/*Read Encoder to determine speed*/
	currentEncoder = TM10;

	if (direction==CCW) /*count will increment*/
		encoderDiff = (currentEncoder - previousEncoder);

	else /*motor direction CW and count will decrement*/
		encoderDiff = (previousEncoder - currentEncoder);
		
	if((encoderDiff<0)||(encoderDiff>(MAX_TM10_COUNT/2)))
		/*TMA01 has gone through zero*/
		{
		if (direction==CW)
			encoderDiff = ((MAX_TM10_COUNT - currentEncoder) + previousEncoder + 1);

		else
			encoderDiff = ((MAX_TM10_COUNT - previousEncoder) + currentEncoder + 1);
		}

	/*convert encoder value to RPM*/
	rpmActual=((encoderDiff * SAMPLE_RATE * 60)/ MAX_ENCODER_PULSES);

   /*current reading becomes previous reading*/
	previousEncoder = currentEncoder;

   return(rpmActual);  /*In Hz*/

   }

 /*------------------------------------------------------------------------- 
 * Function:    PIControl
 * Description:  modifies speed error (RPM) using P I coefficients
 * Parameter:	None
 * Returns:     PI speed demand
 ---------------------------------------------------------------------------*/
UINT16 PIControl(SINT32 speedError)
	{
	SINT32 proportional, integral;
   static SINT32 errorIntegral=0;
   SINT32 pi, speedDemand;

   proportional = KP(speedError);  /*calculate proportional term*/

   integral = KI(errorIntegral+=speedError); /*calculate integral term*/
      
   pi = proportional + integral;   /* calc PI product*/
   

   /*convert PI result from units of rpm to MAX_SET_SPEED scale*/
   speedDemand = ((pi * MAX_SET_SPEED)/MAX_RPM);

   if(speedDemand > MAX_SET_SPEED)
      speedDemand = MAX_SET_SPEED;  /*limit max value to max speed*/

   else if(speedDemand < 0)
      speedDemand = 0;  /*limit min value to 0*/
      
   return((UINT16)speedDemand);
   }
 

/****************************************************************************** 
 * Function:    basicConfig
 * Description: Performs the basic system configuration, particularly 
 *              settings of the PLL generator, internal bus spees and the 
 *              prescalers
 * Parameter:	None
 * Returns:     None
 *****************************************************************************/
void  basicConfig(void)
	{
	// Initialize CKC register
	// Bit 5: TBCS - Selects time base counter clock
	// 		0: TBC clock fxx/256
	// 		1: TBC clock fxx/512
	// Bit 3: CESEL - Specifies X1/X2 pin functions
	// 		0: crystal/resonator connected to X1-X2 pins
	//			1: external clock input at X1 pin
	// Bit 2 to 0: CKDIV2 to CKDIV0 - clock division in PLL mode
	unsigned char tempCKC = 0; 
	#ifdef PLL_MODE
		#if (PLL_DIV == 1)
			#if (PLL_MUL == 10)
			tempCKC |= 0x07;
			#elif (PLL_MUL == 5)
			tempCKC |= 0x03;
			#elif (PLL_MUL == 1)
			// bits 2-0 remain zero
			#else
			#error "No valid PLL factor defined."
			#endif
		#elif (PLL_DIV == 2)
			#if (PLL_MUL == 10)
			tempCKC |= 0x03;
			#elif (PLL_MUL == 5)
			tempCKC |= 0x01;
			#else
			#error "No valid PLL factor defined."
			#endif
		#endif
	PHCMD = tempCKC;
	CKC = tempCKC;
	#endif

	// Initialize VSWC register depending on the system freq.
	#if (SYS_FREQ < 25000000)
		VSWC = 0x11;
	#elif (SYS_FREQ < 33000000)
		VSWC = 0x12;
	#else // SYS_FREQ >= 33000000
		VSWC = 0x14;
	#endif

	} /*end of basicConfig*/



/*end of file*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -