spwm需要代码关注私发
stm32f103c8t6产生互补的pwm波
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Timer.h"
void bspTIMInit(void)
{
GPIOConfig();
TIM1Config();
}
/************************************************************************
* 接线:
* PA8 CH1 | PB13 CH1N | PB12 BKIN
* 功能:
* 通过 PA8 和 PB13 输出互补的 PWM 波形;当 PB12 为高时,停止 PWM 输出
* 描述:
* PWM 周期 100ms;死区时间 12.8ms
*/
int main(void)
{
bspTIMInit();
while (1);
}
Timer.c
#include "stm32f10x.h" // Device header
void GPIOConfig(void)
{
GPIO_InitTypeDef GPIOInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); /*使能GPIOA,GPIOB时钟*/
GPIOInitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIOInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIOInitStruct.GPIO_Pin = GPIO_Pin_8; /*PA8:CH1*/
GPIO_Init(GPIOA, &GPIOInitStruct);
GPIOInitStruct.GPIO_Pin = GPIO_Pin_13; /*PB13:CH1N*/
GPIO_Init(GPIOB, &GPIOInitStruct);
GPIOInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIOInitStruct.GPIO_Pin = GPIO_Pin_12; /*PB12:BKIN*/
GPIO_Init(GPIOB, &GPIOInitStruct);
GPIO_ResetBits(GPIOB, GPIO_Pin_12); /*设置PB12*/
}
void TIM1Config(void)
{
TIM_TimeBaseInitTypeDef TIMTimeBaseStruct;
TIM_OCInitTypeDef TIMOCInitStruct;
TIM_BDTRInitTypeDef TIMBDTRInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /*使能TIM1时钟*/
/*时基结构体配置*/
TIMTimeBaseStruct.TIM_Period = 1000 - 1; /*从0开始 一个信号周期计数1000次*/
/*定时器时钟:72MHz / 7200 = 10kHz;周期:(1 / 10kHz) * 1000 = 100us*/
TIMTimeBaseStruct.TIM_Prescaler = 72 - 1; /*计数器频率10kHz,每计数一次花费 100us*/
/*一个信号周期花费时间:1000 * 100us = 100ms*/
TIMTimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1; /*时钟分频因子 = 1,tDTS=tCKINT*/
TIMTimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up; /*向上计数*/
TIMTimeBaseStruct.TIM_RepetitionCounter = 0; /*禁用重复计数器*/
TIM_TimeBaseInit(TIM1, &TIMTimeBaseStruct);
/*输出比较结构体配置*/
TIMOCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; /*PWM1模式*/
TIMOCInitStruct.TIM_OutputState = TIM_OutputState_Enable; /*输出使能*/
TIMOCInitStruct.TIM_OutputNState = TIM_OutputNState_Enable; /*互补输出使能*/
TIMOCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; /*输出有效电平为高电平*/
TIMOCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High; /*互补输出有效电平为高电平*/
TIMOCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set; /*输出空闲时为高电平*/
TIMOCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Reset; /*互补输出空闲时为低电平*/
/*初始化TIM1的通道1*/
TIMOCInitStruct.TIM_Pulse = 500 ; /*占空比 = 250 / 1000 = 25%*/
TIM_OC1Init(TIM1,&TIMOCInitStruct);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); /*开启预装载,在更新时间后才会重新装载数值*/
/*刹车和死区结构体配置*/
TIMBDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Enable; /*运行模式下“关闭模式”选择 = 1*/
TIMBDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Enable; /*空闲模式下“关闭模式”选择 = 1*/
TIMBDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_1; /*锁定级别1,见参考手册*/
TIMBDTRInitStruct.TIM_DeadTime = 0x80; /*死区时间:12.8ms*/
TIMBDTRInitStruct.TIM_Break = TIM_Break_Enable; /*开启刹车功能*/
TIMBDTRInitStruct.TIM_BreakPolarity = TIM_BreakPolarity_Low; /*刹车输入低电平有效,如果引脚检测到高电平则会停止PWM的输出,不会产生任何波形*/
TIMBDTRInitStruct.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; /*开启自动输出*/
TIM_BDTRConfig(TIM1, &TIMBDTRInitStruct);
TIM_Cmd(TIM1, ENABLE); /*使能定时器,计数器开始计数*/
TIM_CtrlPWMOutputs(TIM1, ENABLE); /*开启主输出*/
}
Timer.h
#ifndef _TIMER_H
#define _TIMER_H
void GPIOConfig(void);
void TIM1Config(void);
#endif
spwm波
滤波效果不好,实际只需要一阶低通电路即可(10k,0.33uf)产生50HZ正弦波
实际效果
标签:TIM1,TIM,void,spwm,50hz,GPIOInitStruct,stm32f103c8t6,GPIO,TIMOCInitStruct From: https://blog.csdn.net/2301_76617986/article/details/143313492