• STM32F1高级定时器做普通PWM输出配置(例TIM1)


    TIM1的四个通道做PWM输出配置

    void pwm_time1()

    {  

    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; 
    GPIO_Init(GPIOA,&GPIO_InitStructure);

    TIM_TimeBaseInitStructure.TIM_Period = 1999;

    TIM_TimeBaseInitStructure.TIM_Prescaler = 7199;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter =0;
    TIM_TimeBaseInit(TIM1, & TIM_TimeBaseInitStructure);

    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse=0;
    TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
    TIM_OC1Init(TIM1,&TIM_OCInitStructure);

    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse=0;
    TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
    TIM_OC2Init(TIM1,&TIM_OCInitStructure);

    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse=0;
    TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
    TIM_OC3Init(TIM1,&TIM_OCInitStructure);

    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse=0;
    TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
    TIM_OC4Init(TIM1,&TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);

    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
    TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);

    TIM_Cmd(TIM1,ENABLE);

    TIM_CtrlPWMOutputs(TIM1, ENABLE);

    }

    配置完成之后使用以下代码即可(可用示波器或逻辑分析仪检测方波)

    TIM_SetCompare4(TIM1, 100);
    TIM_SetCompare3(TIM1, 200);
    TIM_SetCompare2(TIM1, 300);
    TIM_SetCompare1(TIM1, 400);

  • 相关阅读:
    Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    跨平台将终结
    一万字详解 Redis Cluster Gossip 协议
    Java实现简单的计算器
    CSDN开发者周刊第 22期:谷歌 DeepMind 第四代:不学规则就可以玩游戏;图灵奖得主 Edmund Clarke 因感染“新冠”逝世;
    理解Python闭包,这应该是最好的例子
    sscanf函数用法详解
    web项目中配置多个数据源
    web项目中配置多个数据源
    动态表格之查看、删除、编辑
  • 原文地址:https://www.cnblogs.com/pacexdong/p/12124074.html
Copyright © 2020-2023  润新知