|
发表于 2011-9-9 15:29:44
|
显示全部楼层
楼主,我今天在用定时器1输出PWM,
但始终出来不了波型,不知为什么,
void TIM_Configuration(void)
{
/*ÒÔÏÂΪ60msÖжÏÒ»´Î*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period=60000-1; /*×Ô¶¯ÖØ×°ÔؼĴæÆ÷µÄÖµ*/
TIM_TimeBaseStructure.TIM_Prescaler= (36 - 1); /*ʱÖÓÔ¤·ÖƵÊý*/
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; /*²ÉÑù·ÖƵ*/
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; /*¼ÆÊý·½ÏòÏòÉϼÆÊý */
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /*¸ù¾ÝTIM_TimeBaseStructureÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIM2µÄʱ¼ä»ùÊýµ¥Î»*/
TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update); //Çå³ýTIM2µÈ´ýÖжϸüÐÂÖжϱê־λ
TIM_ClearFlag(TIM1, TIM_FLAG_Update); /*Çå³ýÒç³öÖжϱêÖ¾*/
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); /*ʼÄܶ¨Ê±Æ÷µÄÖжÏ*/
TIM_Cmd(TIM1, ENABLE); /*ʹÄܶ¨Ê±Æ÷*/
//////////////////////////////////////////////////////////////////////////////////////////////////
// ÆôÓÃARRµÄÓ°×ӼĴæÆ÷£¨Ö±µ½²úÉú¸üÐÂʼþ²Å¸ü¸ÄÉèÖã©
TIM_ARRPreloadConfig(TIM1, ENABLE); //ʹÄÜ»òÕßʧÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæÆ÷
//TIM1_OC1Ä£¿éÉèÖã¨ÉèÖÃ1ͨµÀÕ¼¿Õ±È£©
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM_OCModeÑ¡Ôñ¶¨Ê±Æ÷ģʽ TIMÂö³å¿í¶Èµ÷ÖÆģʽ1 TIM_OCMode_PWM1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ʹÄÜÕýÏòͨµÀ
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //ʹÄÜ·´ÏòͨµÀ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM_OCPolarityÊä³ö¼«ÐÔ
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //TIM_OCPolarity»¥²¹Êä³ö¼«ÐÔΪ¸ßµçƽ
TIM_OCInitStructure.TIM_Pulse = 120; //TIM_PulseÉèÖÃÁË´ý×°È벶»ñ±È½Ï¼Ä´æÆ÷µÄÂö³åÖµ¡£ËüµÄÈ¡Öµ±ØÐëÔÚ0x0000ºÍ0xFFFFÖ®¼ä¡£
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
//ÆôÓÃCCR1¼Ä´æÆ÷µÄÓ°×ӼĴæÆ÷£¨Ö±µ½²úÉú¸üÐÂʼþ²Å¸ü¸ÄÉèÖã©
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜ»òÕßʧÄÜTIMxÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷ TIMxÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷ʹÄÜ
//TIM1_OC2Ä£¿éÉèÖã¨ÉèÖÃ2ͨµÀÕ¼¿Õ±È£©
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 680;
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //ʹÄÜ»òÕßʧÄÜTIMxÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
//ÆôÓÃCCR2¼Ä´æÆ÷µÄÓ°×ӼĴæÆ÷£¨Ö±µ½²úÉú¸üÐÂʼþ²Å¸ü¸ÄÉèÖã©
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //ʹÄÜ»òÕßʧÄÜTIMxÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
//ËÀÇøÉèÖÃ
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 0x90; //ÕâÀïµ÷ÕûËÀÇø´óС0-0xff
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
//TIM1¿ªÆô
TIM_Cmd(TIM1, ENABLE);
//TIM1_OCͨµÀÊä³öPWM£¨Ò»¶¨Òª¼Ó£©
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/////////////////////////////////////////////////////////////////////////////////////////////////////
} |
|