老师你好,请教一个新的延展性问题,我本案中,是利用TIM1的4路比较输出模式,控制一个步进电机的转到,这个功能现在已基本实现。
新的问题:现在想进一步控制电机正反转的切换。我已把程序和波形附图,我的做法是,新建一个user_app_motorcontrol()的函数,然后给这个函数附一个float的数值(带正负之分)。如果是正数,就让电机顺时转,如果是负数,就逆时针转动。然后main.c里写了一个测试函数如下。
user_app_motorcontrol(10);//+10顺时转
HAL_Delay(2000);
user_app_motorcontrol(-10);//-10逆时转
程序编译没有问题,下载程序,结果是,电机只朝一个方向转动(这里是顺时转,因为初始4个通道的数值设置成顺时),没有正反转的切换,大概仿真看了一下 , 发现 CCR1 CCR2 CCR3 CCR4的数值没有变换,不知道是不是程序哪里编写的不对,请帮忙看看。万分感谢。
这里特别说明一下,在main.c一开始,我默认给4个通道的数组里,写的是顺时针转动的数组,完整程序如下。
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "iwdg.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern DMA_HandleTypeDef hdma_tim1_ch1;
extern DMA_HandleTypeDef hdma_tim1_ch2;
extern DMA_HandleTypeDef hdma_tim1_ch3;
extern DMA_HandleTypeDef hdma_tim1_ch4_trig_com;
/* USER CODE END PTD */
/* USER CODE BEGIN PV */默认初始值为顺时转,后边应该在user_app_motorcontrol()切换数值,但是失败了???
uint16_t Data1_to_Comp[]={99,599}; //ch1
uint16_t Data2_to_Comp[]={499,199}; //ch2
uint16_t Data3_to_Comp[]={699,399}; //ch3
uint16_t Data4_to_Comp[]={299,799}; //ch4
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void user_app_motorcontrol(float pid_input);
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_IWDG_Init();
MX_USART2_UART_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
__HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_1);
__HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_2);
__HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_3);
__HAL_TIM_DISABLE_OCxPRELOAD(&htim1,TIM_CHANNEL_4);
TIM_CCxChannelCmd(TIM1,TIM_CHANNEL_1, TIM_CCx_ENABLE);
TIM_CCxChannelCmd(TIM1,TIM_CHANNEL_2, TIM_CCx_ENABLE);
TIM_CCxChannelCmd(TIM1,TIM_CHANNEL_3, TIM_CCx_ENABLE);
TIM_CCxChannelCmd(TIM1,TIM_CHANNEL_4, TIM_CCx_ENABLE);
hdma_tim1_ch1.State = HAL_DMA_STATE_READY;
HAL_DMA_Start(&hdma_tim1_ch1,(uint32_t)Data1_to_Comp, (uint32_t)&TIM1->CCR1, 2);
__HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_CC1);
hdma_tim1_ch2.State= HAL_DMA_STATE_READY;
HAL_DMA_Start(&hdma_tim1_ch2, (uint32_t)Data2_to_Comp, (uint32_t)&TIM1->CCR2,2);
__HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_CC2);
hdma_tim1_ch3.State= HAL_DMA_STATE_READY;
HAL_DMA_Start(&hdma_tim1_ch3, (uint32_t)Data3_to_Comp,(uint32_t)&TIM1->CCR3, 2) ;
__HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_CC3);
hdma_tim1_ch4_trig_com.State=HAL_DMA_STATE_READY;
HAL_DMA_Start(&hdma_tim1_ch4_trig_com, (uint32_t)Data4_to_Comp,(uint32_t)&TIM1->CCR4, 2) ;
__HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_CC4);
/* Enable the main output */
__HAL_TIM_MOE_ENABLE(&htim1);
/* Enable the Peripheral */
__HAL_TIM_ENABLE(&htim1);
user_app_motorcontrol(10);
HAL_Delay(2000);
user_app_motorcontrol(-10);
while (1)
{
}
}
void user_app_motorcontrol(float pid_input)
{
TIM_OC_InitTypeDef sConfigOC = {0};
if (pid_input>0) //顺时
{
TIM1->CCR1 = 599;
TIM1->CCR2 = 199;
TIM1->CCR3 = 399;
TIM1->CCR4 = 799;
uint16_t Data1_to_Comp[]={99,599}; //ch1
uint16_t Data2_to_Comp[]={499,199}; //ch2
uint16_t Data3_to_Comp[]={699,399}; //ch3
uint16_t Data4_to_Comp[]={299,799}; //ch4
}
if (pid_input<0) //逆时
{
TIM1->CCR1 = 699;
TIM1->CCR2 = 299;
TIM1->CCR3 = 99;
TIM1->CCR4 = 499;
uint16_t Data1_to_Comp[]={199,699}; //ch1
uint16_t Data2_to_Comp[]={599,299}; //ch2
uint16_t Data3_to_Comp[]={399,99}; //ch3
uint16_t Data4_to_Comp[]={799,499}; //ch4
}
}
|