/* ÈýÏàPWMÒý½Å¶¨Òå */
#define PHASE_U_GPIO PortA
#define PHASE_U_Pin Pin08
#define PHASE_U_CHANEL Timer4OcoOuh
#define PHASE_AN_GPIO PortB
#define PHASE_AN_PIN Pin13
#define PHASE_AN_CHANEL Timer4OcoOul
#define PHASE_V_GPIO PortA
#define PHASE_V_Pin Pin09
#define PHASE_V_CHANEL Timer4OcoOvh
#define PHASE_BN_GPIO PortB
#define PHASE_BN_PIN Pin14
#define PHASE_BN_CHANEL Timer4OcoOvl
#define PHASE_W_GPIO PortA
#define PHASE_W_Pin Pin10
#define PHASE_W_CHANEL Timer4OcoOwh
#define PHASE_CN_GPIO PortB
#define PHASE_CN_PIN Pin15
#define PHASE_CN_CHANEL Timer4OcoOwl
/* ¶¨Ê±Æ÷Ïà¹Ø¶¨Òå */
#define PWM_TIMx (M4_TMR41)
#define MOTOR_TIM4_PERIOD (2000u)
int main(void)
{
SysClkConfig();
bsp_InitDWT();
stc_port_init_t stcPortInit;
stc_timer4_cnt_init_t stcCntInit;
stc_timer4_oco_init_t stcOcoInit;
stc_oco_high_ch_compare_mode_t stcHighChCmpMode;
stc_timer4_pwm_init_t stcPwmInit;
MEM_ZERO_STRUCT (stcPortInit);
MEM_ZERO_STRUCT (stcCntInit);
MEM_ZERO_STRUCT (stcOcoInit);
MEM_ZERO_STRUCT (stcHighChCmpMode);
MEM_ZERO_STRUCT (stcPwmInit);
/*¿ªÆôʱÖÓ*/
PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM41,Enable);//´ò¿ªtimer4¶¨Ê±Æ÷µÄʱÖÓ pclk1 84mhz
// stcPortInit.enPinMode = Pin_Mode_Out;
// stcPortInit.enPinDrv = Pin_Drv_H;
// stcPortInit.enPullUp = Enable;
// PORT_Init(PHASE_UN_PORT,PHASE_UN_PIN,&stcPortInit);
// PORT_Init(PHASE_VN_PORT,PHASE_VN_PIN,&stcPortInit);
// PORT_Init(PHASE_WN_PORT,PHASE_WN_PIN,&stcPortInit);
// PORT_ResetBits(PHASE_UN_PORT,PHASE_UN_PIN);
// PORT_ResetBits(PHASE_VN_PORT,PHASE_VN_PIN);
// PORT_ResetBits(PHASE_WN_PORT,PHASE_WN_PIN);
/*ÅäÖÃGPIO*/
PORT_SetFunc(PHASE_U_GPIO,PHASE_U_Pin,Func_Tim4,Disable);
PORT_SetFunc(PHASE_V_GPIO,PHASE_V_Pin,Func_Tim4,Disable);
PORT_SetFunc(PHASE_W_GPIO,PHASE_W_Pin,Func_Tim4,Disable);
/*ÅäÖÃʱ»ù ¼ÆÊýÆ÷Òç³ö = PSCÔ¤·ÖƵʱÖÓ£¬Ò»°ãÔÚ72mhz/PSC+1/ARR+1 */
stcCntInit.enBufferCmd = Disable; //ʧÄÜCPSR»º´æ
stcCntInit.enClk = Timer4CntPclk; //ʹÓÃpclk×÷Ϊtimer4µÄ¼ÆÊýʱÖÓ
stcCntInit.enClkDiv = Timer4CntPclkDiv1; //¶Ô¼ÆÊýʱÖÓ½øÐУ¨0+1£©·ÖƵ
stcCntInit.u16Cycle = MOTOR_TIM4_PERIOD; //¼ÆÊýÑ»·Öµ£¬cpsr£¬ARR¼ÆÊý·åÖµ 84Mhz/10Khz = 8400
stcCntInit.enCntMode = Timer4CntTriangularWave; //¼ÆÊýģʽΪÈý½Ç²¨Ä£Ê½
stcCntInit.enZeroIntMsk = Timer4CntIntMask0; //¼ÆÊýÁãµãÖжÏÆÁ±Î ÏÂÒçÖжϸöÊý
stcCntInit.enPeakIntMsk = Timer4CntIntMask0; //¼ÆÊý·åÖµÖжÏÆÁ±Î ÉÏÒçÖжϸöÊý
TIMER4_CNT_Init(PWM_TIMx,&stcCntInit); //³õʼ»¯¼ÆÊýµ¥Ôªtimer4
/*ÅäÖÃÖØ×°ÔØÖµ&±È½ÏÖµ*/
stcOcoInit.enOccrBufMode =OccrBufTrsfByCntZero; //ÏÂÒçʱдÈëOCCRµÄÖµ
stcOcoInit.enOcmrBufMode =OcmrBufTrsfByCntZero; //ÏÂÒçʱдÈëOCMRµÄÖµ
stcOcoInit.enPortLevel =OcPortLevelLow; //¶Ë¿ÚÊä³öÎÞЧʱ£¬Êä³öµÍµçƽ
stcOcoInit.enOcoIntCmd =Disable; //¼ÆÊýÆ¥ÅäÖжÏʧÄÜ
TIMER4_OCO_Init(PWM_TIMx,PHASE_U_CHANEL,&stcOcoInit);
TIMER4_OCO_Init(PWM_TIMx,PHASE_V_CHANEL,&stcOcoInit);
TIMER4_OCO_Init(PWM_TIMx,PHASE_W_CHANEL,&stcOcoInit);
/*ÅäÖøßͨµÀ±È½ÏÊä³ö¼Ä´æÆ÷¼´ÉÏÇű۵ÄÅäÖà Ï൱ÓÚÅäÖÃÁËpwm1ģʽ*/
stcHighChCmpMode.enCntZeroMatchOpState =OcoOpOutputHold; //b10~b11 occrxh = 0xoooo
stcHighChCmpMode.enCntZeroNotMatchOpState =OcoOpOutputHold; //b14~b15 ¹ÈÖµ£¬ÇÒ0x0000²»Îªccrʱ
stcHighChCmpMode.enCntUpCntMatchOpState =OcoOpOutputLow; //b8~b9 ÏòÉϼÆÊýµ½´ïccr
stcHighChCmpMode.enCntPeakMatchOpState =OcoOpOutputHold; //b6~b7 ·åÖµÇÒoccrxh = 0xffff
stcHighChCmpMode.enCntPeakNotMatchOpState =OcoOpOutputHold; //b12~b13 ·åÖµÇÒoccrxh £¡=0xffff
stcHighChCmpMode.enCntDownCntMatchOpState =OcoOpOutputHigh; //b4~b5 ÏòϼÆÊýµ½´ïccr
stcHighChCmpMode.enCntZeroMatchOcfState =OcoOcfHold; //b3ÏÂÒçµã¼ÆÊýÖµ²»Æ¥Åä ¸ßͨµÀOCF״̬ holdΪ0 setΪ1
stcHighChCmpMode.enCntUpCntMatchOcfState =OcoOcfSet; //b2 ÏòÉϼÆÊý ¼ÆÊýֵƥÅä
stcHighChCmpMode.enCntPeakMatchOcfState =OcoOcfHold; //b1 ÉÏÒçµã ¼ÆÊýÖµ²»Æ¥Åä
stcHighChCmpMode.enCntDownCntMatchOcfState =OcoOcfSet; //b0 ÏòϼÆÊý ¼ÆÊýֵƥÅä
stcHighChCmpMode.enMatchConditionExtendCmd =Disable; //²»À©Õ¹ÆäËû¼ÆÊýÌõ¼þ
TIMER4_OCO_SetHighChCompareMode(PWM_TIMx,PHASE_U_CHANEL,&stcHighChCmpMode);
TIMER4_OCO_SetHighChCompareMode(PWM_TIMx,PHASE_V_CHANEL,&stcHighChCmpMode);
TIMER4_OCO_SetHighChCompareMode(PWM_TIMx,PHASE_W_CHANEL,&stcHighChCmpMode);
TIMER4_OCO_WriteOccr(PWM_TIMx,PHASE_U_CHANEL,0);
TIMER4_OCO_WriteOccr(PWM_TIMx,PHASE_V_CHANEL,0);
TIMER4_OCO_WriteOccr(PWM_TIMx,PHASE_W_CHANEL,0);
TIMER4_OCO_OutputCompareCmd(PWM_TIMx,PHASE_U_CHANEL,Enable);
TIMER4_OCO_OutputCompareCmd(PWM_TIMx,PHASE_V_CHANEL,Enable);
TIMER4_OCO_OutputCompareCmd(PWM_TIMx,PHASE_W_CHANEL,Enable);
/*PWMÅäÖö˿ÚÊä³ö*/
stcPwmInit.enRtIntMaskCmd =Enable; //ÖØÔع¦ÄÜÓÐЧʱ£¬ÖØÔØÖжÏWÊä³öÎÞЧ
stcPwmInit.enClkDiv =PwmPlckDiv1; //¼ÆÊýʱÖÓ·ÖƵ
stcPwmInit.enOutputState =PwmHPwmLHold; //Êä³ö¼«ÐÔ°´ÕÕÉ趨µÄÀ´£¬²»·¢Éú¸Ä±ä
stcPwmInit.enMode =PwmThroughMode; //pwmËÀÇøģʽ
TIMER4_PWM_Init(PWM_TIMx,Timer4PwmU,&stcPwmInit);
TIMER4_PWM_Init(PWM_TIMx,Timer4PwmV,&stcPwmInit);
TIMER4_PWM_Init(PWM_TIMx,Timer4PwmW,&stcPwmInit);
/*Êä³ö*/
TIMER4_CNT_ClearCountVal(PWM_TIMx);
TIMER4_CNT_Start(PWM_TIMx);
while(1)
{
int i,j,k;
// for(i = 0;i <= 1500;i += 200)
// {
// TIMER4_OCO_WriteOccr(PWM_TIMx,PHASE_U_CHANEL,i);
// TIMER4_OCO_OutputCompareCmd(PWM_TIMx,PHASE_U_CHANEL,Enable);
// bsp_DelayMS(500);
// }
// i = 0;
//
for(j = 0;j <= 1500;j += 300)
{
TIMER4_OCO_WriteOccr(PWM_TIMx,PHASE_V_CHANEL,j);
TIMER4_OCO_OutputCompareCmd(PWM_TIMx,PHASE_V_CHANEL,Enable);
bsp_DelayMS(500);
}
j = 0;
// for(k = 0;k <= 1500;k += 400)
// {
// TIMER4_OCO_WriteOccr(PWM_TIMx,PHASE_W_CHANEL,k);
// TIMER4_OCO_OutputCompareCmd(PWM_TIMx,PHASE_W_CHANEL,Enable);
// bsp_DelayMS(500);
// }
// k = 0;
}
}
|