基于STM32F103C8T6的同步电机驱动-PWM驱动代码以及SVPWM的实现
本系列文章:
- 基于STM32F103C8T6的同步电机驱动-CubeMX配置与IQmath调用
- 基于STM32F103C8T6的同步电机驱动-PWM驱动代码以及SVPWM的实现
一、PWM驱动代码的编写
在系列文章的第一篇中已经对主定时器TIM1进行了PWM相关的配置,对从定时器TIM4进行了配置并开启了中断。电机驱动程序相关代码也将写在TIM4的中断里。
/****file:main.c*****/
//Include Header Files
#include "main.h"
#include "adc.h"
#include "can.h"
#include "i2c.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "IQmathlib.h"
//Global Variable
//PWM相关参数
uint16_t PWM1_PULSE=0;
uint16_t PWM2_PULSE=0;
uint16_t PWM3_PULSE=0;
/************向上为参数定义,向下为运行函数*******************************/
//main函数
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_CAN_Init();
MX_I2C1_Init();
MX_SPI1_Init();
MX_TIM1_Init();
MX_USART1_UART_Init();
MX_I2C2_Init();
MX_TIM4_Init();
HAL_ADCEx_Calibration_Start(&hadc1); //AD校准
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, PWM1_PULSE);
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, PWM2_PULSE);
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_3, PWM3_PULSE);
__HAL_TIM_SET_AUTORELOAD(&htim4,2-1); //设置要输出的PWM脉冲数1000个
HAL_TIM_Base_Start_IT(&htim4); //启动从定时器
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
标签:PWM,HAL,SVPWM,TIM,Init,STM32F103C8T6,驱动,include,MX
From: https://blog.csdn.net/qq_45854134/article/details/140180625