用的是原子哥的历程,只是修改了一下频率和IO口,输出幅值特别低100mV都没有,怎么回事?用的是电源口供电
这是主函数
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
uart_init(115200); //′®¿ú3õê¼»ˉÎa115200
LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
TIM2_PWM_Init(999,0); //2»·ÖÆμ¡£PWMÆμÂê=72000000/900=80Khz
while(1)
{
delay_ms(10);
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval>600)dir=0;
if(led0pwmval==0)dir=1;
TIM_SetCompare4(TIM2,led0pwmval);
}
}
pwm函数
void TIM2_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ê1Äü¶¨ê±Æ÷3ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM3 CH2μÄ
WMÂö3å2¨DÎ GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIO
//3õê¼»ˉTIM3
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM3 Channel2 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM3 OC2
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); //ê1ÄüTIM3ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_Cmd(TIM2, ENABLE); //ê1ÄüTIM3
}
输出频率和占空比都对 就是幅值太低
一周热门 更多>