根据例程,我用通用定时器可以产生pwm波,为什么我换到高级定时器上面去就不行了
下面是我换到TIM8上面就不行了,我查到TIM8_CHI可以映射到PC6上,所以一下是我的程序,还请大神指教
以下是我的程序:
void TIM8_PWM_Init(u32 arr,u32 psc)
{
//′Ë2¿·ÖDèêÖ¶ˉDT¸ÄIO¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE); //TIM8ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //ê1ÄüPORTFê±Öó
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM8); //GPIOC6¸′óÃÎa¶¨ê±Æ÷TIM8_CH1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //GPIOC6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure); //3õê¼»ˉC6
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷8
//3õê¼»ˉTIM8 Channel1 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔμí
TIM_OC1Init(TIM8, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM8OC1
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //ê1ÄüTIM8ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM8,ENABLE);//ARPEê1Äü
TIM_CtrlPWMOutputs(TIM8,ENABLE);//PWMêä3öê1Äü
TIM_Cmd(TIM8, ENABLE); //ê1ÄüTIM14
}
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200);//3õê¼»ˉ′®¿ú2¨ìØÂêÎa115200
TIM8_PWM_Init(500-1,84-1); //84M/84=1MhzμļÆêyÆμÂê,ÖØ×°ÔØÖμ500£¬ËùòÔ
WMÆμÂêÎa 1M/500=2Khz.
while(1) //êμÏÖ±è½ÏÖμ′ó0-300μYÔö£¬μ½300oó′ó300-0μY¼õ£¬Ñ-»·
{
delay_ms(10);
if(dir)led0pwmval++;//dir==1 led0pwmvalμYÔö
else led0pwmval--; //dir==0 led0pwmvalμY¼õ
if(led0pwmval>300)dir=0;//led0pwmvalμ½′ï300o󣬷½ÏòÎaμY¼õ
if(led0pwmval==0)dir=1; //led0pwmvalμY¼õμ½0o󣬷½Ïò¸ÄÎaμYÔö
TIM_SetCompare1(TIM8,led0pwmval); //DT¸Ä±è½ÏÖ죬DT¸ÄÕ¼¿Õ±è
}
}
编译时没有问题的,我用示波器测得的结果是PC6一直是高电平,弄了好几天,我相信对这里的人来说程序很简单,所以还请走过路过的大侠看一眼,告诉我错在哪了!
一周热门 更多>