我用的是TIM3的CH1和CH2两路PWM输出,占空比设置一样,输出的电压却不一样,CH1输出接近3.3V。CH2输出只有1.1V
PWM初始化:
#include "timer.h"
#include "led.h"
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê±Öóê1Äü
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ ¼Æêyμ½5000Îa500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ 10KhzμļÆêyÆμÂê
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM3ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIMxíaéè
}
//¶¨ê±Æ÷3ÖD¶Ï·tÎñ3ìDò
void TIM3_IRQHandler(void) //TIM3ÖD¶Ï
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //¼ì2éÖ¸¶¨μÄTIMÖD¶Ï·¢éúóë·ñ:TIM ÖD¶ÏÔ′
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
LED1=!LED1;
}
}
void TIM3_PWM_Init(u16 arr,u16 psc)
{
TIM_OCInitTypeDef TIM_OCInitStruc;
GPIO_InitTypeDef GPIO_InitStruc;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê±Öóê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStruc.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruc.GPIO_Pin=GPIO_Pin_5 | GPIO_Pin_4;
GPIO_InitStruc.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruc);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ ¼Æêyμ½5000Îa500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ 10KhzμļÆêyÆμÂê
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_OCInitStruc.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStruc.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStruc.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC2Init(TIM3,&TIM_OCInitStruc);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
}
主函数:
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
void qianjin(void);
void houtui(void);
int main(void)
{
delay_init();
LED_Init();
TIM3_PWM_Init(300-1,0);
while(1)
{
GPIO_ResetBits(GPIOF,GPIO_Pin_4);
GPIO_ResetBits(GPIOF,GPIO_Pin_5);
qianjin();
delay_ms(1000);
houtui();
delay_ms(1000);
}
}
void qianjin()
{
GPIO_SetBits(GPIOF,GPIO_Pin_1);
GPIO_ResetBits(GPIOF,GPIO_Pin_0);
TIM_SetCompare2(TIM3,200);
GPIO_SetBits(GPIOF,GPIO_Pin_3);
GPIO_ResetBits(GPIOF,GPIO_Pin_2);
TIM_SetCompare1(TIM3,200);
}
void houtui()
{
GPIO_SetBits(GPIOF,GPIO_Pin_0);
GPIO_ResetBits(GPIOF,GPIO_Pin_1);
TIM_SetCompare2(TIM3, 200);
GPIO_SetBits(GPIOF,GPIO_Pin_2);
GPIO_ResetBits(GPIOF,GPIO_Pin_3);
TIM_SetCompare1(TIM3,200);
}[/mw_shl_code]1.1V
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>