找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4509|回复: 1
收起左侧

STM32F103互补PWM带死区频率占空比可调源程序

[复制链接]
ID:85785 发表于 2019-8-29 08:35 | 显示全部楼层 |阅读模式
最近做了STM32F103互补PWM带死区频率占空比可调,现在来分享一下。
从STM32的datashee可以看到下图
B4DC28DB-6BBA-40f5-AB3E-2084D8E076BF.png
互补PWM我现在选择的是TIM1的通道1通道2通道3
上图可看出复用脚PA6 PA7 PB0 PB1
不复用的脚PB12 PB13 PB14 PB15,可以根据自己的引脚需求选择复用或者不复用,这些脚是输出互补的引脚,正向PWM引脚为PA8 PA9 PA10 PA11
这个功能其实也挺简单的,下面直接看附件代码。

单片机源程序如下:
  1. #include "PWM_Deadline.h"

  2. uint16_t CC1_Value = 1000,CC2_Value = 10000,CC3_Value = 20000;

  3. void TIM3_PWMShiftInit(void)
  4. {
  5.         TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  6.         TIM_OCInitTypeDef  TIM_OCInitStructure;
  7.         GPIO_InitTypeDef  GPIO_InitStruct;
  8.         TIM_BDTRInitTypeDef      TIM1_BDTRInitStruct;
  9.        
  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  11.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  12.        
  13.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  14.         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_6 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
  15.         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  16.         GPIO_Init(GPIOA,&GPIO_InitStruct);
  17.        
  18.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  19.         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  20.         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  21.         GPIO_Init(GPIOB,&GPIO_InitStruct);
  22.        
  23.         GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);
  24. ////        GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
  25.         //TIM1_TIM2_TIM16
  26.         TIM_TimeBaseStructure.TIM_Period = 65534;//计数1000
  27.         TIM_TimeBaseStructure.TIM_Prescaler = 71;//48/n
  28.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  29.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  30.         TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  31.         TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  32.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  33.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  34.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  35.         TIM_OCInitStructure.TIM_Pulse = 100;
  36.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  37.         TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  38.         TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
  39.         TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
  40.        
  41.         //配置死区
  42.         TIM1_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Disable;
  43.         TIM1_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Disable;
  44.         TIM1_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  45.         TIM1_BDTRInitStruct.TIM_DeadTime = 10;
  46.         TIM1_BDTRInitStruct.TIM_Break = TIM_Break_Disable;
  47.         TIM1_BDTRInitStruct.TIM_BreakPolarity = TIM_BreakPolarity_High;
  48.         TIM1_BDTRInitStruct.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
  49.         TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStruct);

  50. //        TIM_OCInitStructure.TIM_Pulse = 900;
  51. //        TIM_ICInit(TIM1, &TIM_OCInitStructure);
  52. //        TIM_CCPreloadControl(TIM1, TIM_OCPreload_Enable);

  53.   TIM_OCInitStructure.TIM_Pulse = CC1_Value;
  54.         TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  55.         TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
  56. //        TIM_Cmd(TIM1, ENABLE);
  57. //        TIM_CtrlPWMOutputs(TIM1, ENABLE);
  58.        
  59.         TIM_OCInitStructure.TIM_Pulse = CC2_Value;
  60.         TIM_OC2Init(TIM1, &TIM_OCInitStructure);//CH4可以正确输出,但CH1不行,全速转动,为什么?
  61.         TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
  62.        
  63.         TIM_OCInitStructure.TIM_Pulse = CC3_Value;
  64.         TIM_OC3Init(TIM1, &TIM_OCInitStructure);
  65.         TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
  66.        
  67.         TIM_Cmd(TIM1, ENABLE);
  68.         TIM_CtrlPWMOutputs(TIM1, ENABLE);
  69.        
  70. //        TIM_ITConfig(TIM1, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3, ENABLE);
  71.        
  72. //        TIM_ARRPreloadConfig(TIM1, ENABLE);

  73. }

  74. uint16_t Fre;
  75. void Dynami_Fre(void)
  76. {
  77.         TIM1->ARR = Fre;   //可调频率
  78.         if (Fre > 10000) Fre = 10000;
  79.         if (Fre < 1000) Fre = 1000;
  80.         Fre ++;
  81.         TIM_SetCompare1(TIM1, 100);  //占空比 = 100 / TIM1->ARR;
  82.         TIM_SetCompare2(TIM1, 200);  //占空比 = 200 / TIM1->ARR;
  83.         TIM_SetCompare3(TIM1, 300);  //占空比 = 300 / TIM1->ARR;
  84.        
  85. }


  86. uint16_t value1 = 0;
  87. void TIM1_CC_IRQHandler(void)
  88. {
  89.         uint16_t capture;
  90.         if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
  91.         {
  92.                 TIM_ClearITPendingBit(TIM1, TIM_IT_CC1 );
  93.                 capture = TIM_GetCapture1(TIM1);
  94.                 if (value1 > 20000) value1 = 20000;
  95.                 TIM_SetCompare1(TIM1, 10000);
  96.                 TIM1->ARR = 15000;
  97.         }
  98. //        if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)
  99. //        {
  100. //                TIM_ClearITPendingBit(TIM1, TIM_IT_CC2 );
  101. //                capture = TIM_GetCapture2(TIM1);
  102. //                if (value1 > 20000) value1 = 20000;
  103. //                TIM_SetCompare1(TIM1, capture + CC2_Value/2);
  104. //        }
  105. //        if (TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET)
  106. //        {
  107. //                TIM_ClearITPendingBit(TIM1, TIM_IT_CC3 );
  108. //                capture = TIM_GetCapture3(TIM1);
  109. //                if (value1 > 20000) value1 = 20000;
  110. //                TIM_SetCompare1(TIM1, capture + CC3_Value/2);
  111. //        }
  112. }

  113. void NVIC_Configuration(void)
  114. {
  115. NVIC_InitTypeDef NVIC_InitStructure;




  116. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);


  117. NVIC_InitStructure.NVIC_IRQChannel=TIM1_CC_IRQn;
  118. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  119. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  120. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  121. NVIC_Init(&NVIC_InitStructure);
  122. }
复制代码

所有资料51hei提供下载:
master.7z (303.03 KB, 下载次数: 97)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:71535 发表于 2019-9-16 19:36 | 显示全部楼层

看看是什么宝贝
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表