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

📄 pwmcalc2.c

📁 U15873EE1V0AN00 aplication note
💻 C
字号:
/*******************************************************************
 *	File: Pwmcalc2.c
 *
 *	DESCRIPTION:	AC Induction Motor control demonstration
 *                (using constant V/F principle)
 *
 *   PWMCalc() produces the PWM timer values modulated 
 *   by a sine/cosine infinite numerical series. 2 pi radians of the sine wave
 *   represents one revolution of the motor and hence encoder which in this 
 *   example is defined as MAX_ENCODER_PULSES.   
 *   Three timers values are calculated with an effective displacement
 *   of 120deg between each value to produce a 3 phase waveform.
 *   The external variable tableInc represents the required repetition rate 
 *   and hence the frequency of the sine wave. The external variable 'amplitude' 
 *   is the required amplitude of the sine wave.
 *
 *	 AUTHOR:	S.Redpath 
 *
 *	 HISTORY:   Original	5th October 2001
 *				V1.1		31st October 2001
 *							changed debug port from 2 to 4
 *          V1.2     corrected typo errors in comments
 *
 *******************************************************************/
 

/** include files **/
#include <stdlib.h>
#include "tools.h"

#ifdef IAR
#include <intrv850.h>
#include <io_v850e_df3116.h>
#else
#include "df3116.h"
#endif

#include "acind.h"



/* Global Variables (External) */
extern UINT8 amplitude;	/* 0 to BFCMDATA/2 gives 0 to 100% modulation*/
extern UINT8 tableInc;	/* increment value for wave pointer update*/	

/* Local Variables*/
unsigned int Wave_ptr_a = 0;	/* wave pointer for phase A */	
unsigned int Wave_ptr_b = 684;		/* wave pointer for phase B */
unsigned int Wave_ptr_c = 341;          /*wave pointer for phase C*/
int sineValue;			/* sine of wave pointer*/
int Pwmmod_wave;		/* wave modulus */

/*local functions*/
void PWMcalc (void);
int sin( int x );
int sins( int x );


void PWMcalc (void)
  {			
   /* PHASE A */	

   sineValue = sin(Wave_ptr_a);   /*sine of radian*/
   Pwmmod_wave = ((sineValue * amplitude)>> SINE_OFFSET);	/* scale by amplitude */	
   #ifdef SINE_INVERT
   	BFCM00 = (BFCM_DATA/2) - Pwmmod_wave;	/* update BFCM00 register for phase A */
   #else
	BFCM00 = Pwmmod_wave + (BFCM_DATA/2);	/* update BFCM00 register for phase A */
   #endif
          
#ifdef DEBUG
   if (BFCM00 == 250)
	  P4BIT1 ^= 1;  /*toggle ouput DEBUG*/
#endif

   /* PHASE B */	

	sineValue = sin(Wave_ptr_b);   /*sine of radian*/
   Pwmmod_wave = ((sineValue * amplitude)>> SINE_OFFSET);	/* scale by amplitude */
    #ifdef SINE_INVERT
   	BFCM01 = (BFCM_DATA/2) - Pwmmod_wave;	/* update BFCM01 register for phase B */
    #else		
	BFCM01 = Pwmmod_wave + (BFCM_DATA/2);	/* update BFCM01 register for phase B */	
    #endif
										
   /* PHASE C */	

	sineValue = sin(Wave_ptr_c);   /*sine of radian*/
   Pwmmod_wave = ((sineValue * amplitude)>> SINE_OFFSET);	/* scale by amplitude */	
   #ifdef SINE_INVERT
   	BFCM02 = (BFCM_DATA/2) - Pwmmod_wave;	/* update BFCM00 register for phase C */
   #else	
	BFCM02 = Pwmmod_wave + (BFCM_DATA/2);	/* update BFCM00 register for phase C */	
   #endif										
	
	Wave_ptr_a += tableInc;		/* load new wave pointer for phase A */
	Wave_ptr_b += tableInc;		/* load new wave pointer for phase B */
	Wave_ptr_c += tableInc;		/* load new wave pointer for phase C */
	
  }

/*------------------------------------------------------------------------- 
 * Function:    sin x
 * Description:  sine pwm conversion
 * Parameter:	pulse units
 * Returns:     sine value*16384 (SINE_OFFSET)
 --------------------------------------------------------------------------*/
int sin( int x )
	{
	x = x % MAX_ENCODER_PULSES;

	if ( x < 0 ) x += MAX_ENCODER_PULSES;

	if ( x < (MAX_ENCODER_PULSES/4) )    /*first quadrant*/
		{
		return sins( x );
		}
		 
	else if ( x < (MAX_ENCODER_PULSES/2) )    /*second quadrant*/
		{
		return sins( (MAX_ENCODER_PULSES/2) - x );
		}
		 
	else if ( x < (MAX_ENCODER_PULSES*3/4) )    /*3rd quadrant*/
		{
		return -sins( x - (MAX_ENCODER_PULSES/2) );
		}
		 
	else 
		{
		return -sins( MAX_ENCODER_PULSES - x ) ;    /*4th quadrant*/
		}
	}

int sins( int x )
	{
	short z1, z2, z3, z4, z5 ;

	if ( x <= (MAX_ENCODER_PULSES/8) ) 
		{
		z1 = (x << SINE_OFFSET) / (MAX_ENCODER_PULSES/8) ;
		z2 = z1 * z1 >> SINE_OFFSET ;
		z3 = z1 * z2 >> SINE_OFFSET ;
		z5 = z2 * z3 >> SINE_OFFSET ;

		return ( (12868*z1) - (1322*z3) + (40*z5) ) >> SINE_OFFSET ;     /*sine series*/
		} 
	
	else 
		{
		x = (MAX_ENCODER_PULSES/4) - x ;
		z1 = (x << SINE_OFFSET) / (MAX_ENCODER_PULSES/8) ;
		z2 = z1 * z1 >> SINE_OFFSET ;
		z4 = z2 * z2 >> SINE_OFFSET ;
		return ( (268432772) - (5050*z2) + (252*z4) ) >> SINE_OFFSET ;   /*cosine series*/
		}
	}		
		
/*end of file*/


⌨️ 快捷键说明

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