www.pudn.com > dsPIC_Advprog_Answer.rar > ex2.c, change:2011-05-11,size:4236b


// *********************************************************************** 
// File : dsPIC30F6010B.C 
// Purpose : 使用 Motor Control PWM 送出 60 Hz 的  sin wave  於 PWM1L/H 
// *********************************************************************** 
 
#define __dsPIC30F4011__ 
 
//#include <uart.h> 
//#include <adc12.h> 
#include 	<timer.h> 
#include 	<math.h> 
#include 	<p30F4011.h> 
#include	<uart.h> 
#include	<stdio.h> 
#include	"APP009V2_LCD.h" 
 
#define	pi	3.1415926  
#define	FCY	7372800 * 2 
 
#define	LED13		LATDbits.LATD0 
#define	LED14		LATDbits.LATD1 
#define	LED15		LATFbits.LATF0  
#define	LED16		LATFbits.LATF1  
 
#define	DIR_LED13	TRISDbits.TRISD0 
#define	DIR_LED14	TRISDbits.TRISD1 
#define	DIR_LED15	TRISFbits.TRISF0 
#define	DIR_LED16	TRISFbits.TRISF1 
 
void	Timer1_Initial( void ) ; 
void	CAN_Initial( void ) ; 
void	MotPWM_Initial( void ) ; 
void	Ports_Initial( void ) ; 
void	Vars_Initial( void ) ; 
void 	DelayNmSec(unsigned int ) ; 
void 	InitADC10(void); 
 
 
//--------------------------------------------------------------------------- 
// Configuration bits 
 
//   _FOSC(CSW_FSCM_OFF & EC_PLL4);   //XT with 4xPLL oscillator, Failsafe clock off 
//   _FWDT(WDT_OFF);                  //Watchdog timer disabled 
//   _FBORPOR(PBOR_OFF & MCLR_EN);    //Brown-out reset disabled, MCLR reset enabled 
//   _FGS(CODE_PROT_OFF);             //Code protect disabled 
 
unsigned int	ValuePDC ; 
 
int				SinTable[92] ; 
int				LoopVar1 ; 
int				U_Degree ; 
int				V_Degree ; 
int				W_Degree ; 
int				T1IF_Flag ; 
int				T1IF_Counter ; 
 
int				PWM_Factor ; 
 
unsigned int	Temp_Uint ; 
 
void __attribute__((__interrupt__)) _T1Interrupt(void) 
{ 
 
	T1IF_Counter += 1 ; 
 
	if (T1IF_Counter > 4000) 
		{ 
			T1IF_Counter = 0 ; 
			T1IF_Flag = 1 ; 
		}  
	IFS0bits.T1IF = 0 ; 
 
} 
 
void __attribute__((__interrupt__)) _PWMInterrupt(void) 
{ 
 
		IFS2bits.PWMIF = 0 ; 
} 
 
int	main( void ) 
 
{ 
 
	OpenLCD( ) ; 
 
	Timer1_Initial( ) ; 
	Ports_Initial( ) ; 
	Vars_Initial( ) ; 
	MotPWM_Initial( ) ; 
 
		putrsLCD("dsPIC30F4011 Dem") ; 
		setcurLCD(0,1) ; 
		putrsLCD("PWM 28.8k @ 50% ") ; 
 
 while (1)  
	{ 
		while ( T1IF_Flag == 0 ) ; 
 
			T1IF_Flag = 0 ; 
 
			IFS0bits.T1IF = 0 ; 
			LATDbits.LATD0 = ! LATDbits.LATD0 ; 
			LATDbits.LATD1 = ! LATDbits.LATD1 ; 
			LATFbits.LATF0 = ! LATFbits.LATF0 ; 
			LATFbits.LATF1 = ! LATFbits.LATF1 ; 
 
	} 
} 
 
void DelayNmSec(unsigned int N) 
{ 
unsigned int j; 
while(N--) 
 	for(j=0;j < 1000;j++); 
} 
 
 
void	Timer1_Initial( void ) 
{ 
		ConfigIntTimer1( T1_INT_PRIOR_7 & T1_INT_ON ) ; 
		OpenTimer1( T1_ON & T1_IDLE_STOP & T1_GATE_OFF & T1_PS_1_1 & T1_SYNC_EXT_OFF & T1_SOURCE_INT , 
					683 ) ; 
} 
 
 
void Ports_Initial(void) 
{ 
 
		TRISD &= 0x00ff ; 
 
		DIR_LED13 = 0 ; 
		DIR_LED14 = 0 ; 
 
		DIR_LED15 = 0 ; 
		DIR_LED16 = 0 ; 
 
			LED13 = 1 ; 
			LED14 = 0 ; 
 
} 
 
void	Vars_Initial(void) 
{ 
		T1IF_Flag = 0 ; 
		T1IF_Counter = 0 ; 
} 
 
void MotPWM_Initial(void) 
{ 
		IEC2bits.PWMIE = 0 ;	// Disable PWM Interrupt !! 
		IEC2bits.FLTAIE = 0 ;	 
 
		OVDCON = 0xff00 ;			// Inactive all PWM OUTPUT !! 
 
		TRISE = 0xffc0 ;		 
		PTCON = 0xa002 ;		// Configure as 0b1010 0000 0000 0000  
								// PWM Time Base OFF , PWM Time Base OP in free running Mode  
		PWMCON1 = 0x0077 ;		// Configure as 0b0000000000010001 
								// PWM I/O in complementary Mode and only PWM1L/H as PWM output  
		PWMCON2 = 0x0000 ;		// Configure as 0b0000000000000000  
 
		DTCON1 = 0x101F ;		// Configure as 0b000 10000 000 01000  
 
		FLTACON = 0x0000 ; 
 
		IPC9bits.PWMIP = 6 ; 
 
		// --------------------------------------------------------------------------------- 
		// The Switching Frequency !! 
		// PWM resolution = 10 bits ,  
		// PDCx[1:15] compare with PTMR [0:14] 
		// PDCx(0) compare with MSB of prescaler counter 
		// So, PTPER is 9 bit if resolution of PDCx is 10 bit 
		// Then .. PTPER = 512-1 = 511 
		// PWM Frequency will be Fcy/512 = 28.8K 
		// or ... PTPER = (Fcy/(FPWM*PTMR Prescaler)) - 1  
		// --------------------------------------------------------------------------------- 
		PTPER = 511 ;			// PWM Time Base Period Register  
		ValuePDC = 512 ; 
 
			PDC1 = ValuePDC ; 
			PDC2 = ValuePDC ; 
			PDC3 = ValuePDC ;		 
}