目录

一、原理图

二、配置STM32CubeMX

①TIM

三、Keil

四、VsCode

①int_motor.h

②int_motor.c

③App_freertos_task.c

④App_freertos_task.h


一、原理图

二、配置STM32CubeMX

①TIM

(1)在TIM1-4中分别选择通道,时钟源为内部时钟,预分频系数为4-1,自动重装载系数为1000-1,通道比较值为200,即PWM占空比初始值为20%(200/1000)

三、Keil

①在interface➡创建int_motor.c/.h文件

②Keil中添加.c/.h文件

四、VsCode

①int_motor.h

#ifndef __INT_MOTOR_H
#define __INT_MOTOR_H

#include "tim.h"
#include "Com_Debug.h"

typedef struct 
{
  TIM_HandleTypeDef *tim;
  uint16_t channel;
  int16_t speed;  //-32768-32767
}motor_struct;

//代入的参数其实是比较值,最大值是1000,默认初始值是200
void int_motor_set_speed(motor_struct *motor);

//启动电机
void int_motor_start(motor_struct *motor);

#endif

②int_motor.c

#include "int_motor.h"

//代入的参数其实是比较值,最大值是1000,默认初始值是200
void int_motor_set_speed(motor_struct *motor)
{
  if(motor->speed>1000)
  {
    printf("motor speed is so fast!\n");
  }
  __HAL_TIM_SET_COMPARE(motor->tim,motor->channel,motor->speed);
}

//启动电机
void int_motor_start(motor_struct *motor)
{
  __HAL_TIM_SET_COMPARE(motor->tim,motor->channel,0);
  HAL_TIM_PWM_Start(motor->tim,motor->channel);
}

③App_freertos_task.c

#include "APP_freertos_task.h"

//STM32F103C8T6  => SRAM 20K => 分配12K给操作系统

//电机结构体
motor_struct left_top_motor={.tim = &htim3,.channel = TIM_CHANNEL_1,.speed = 0};
motor_struct left_bottom_motor={.tim = &htim4,.channel = TIM_CHANNEL_4,.speed = 0};
motor_struct right_top_motor={.tim = &htim2,.channel = TIM_CHANNEL_2,.speed = 0};
motor_struct right_bottom_motor={.tim = &htim1,.channel = TIM_CHANNEL_3,.speed = 0};

//1、电源管理任务
void power_task(void *args);
//最小推荐填写128 => 128*4 = 512B
#define POWER_TASK_STACK_SIZE 128
//优先级,数值越小,优先级越小 => 最大时4 => 不推荐使用最小优先级0
#define POWER_TASK_PRIORITY 4
//任务句柄
TaskHandle_t power_task_handle;
//定义任务的周期
#define POWER_TASK_PERIOD 10000


//2、飞行控制任务
void flight_task(void *args);
#define FLIGHT_TASK_STACK_SIZE 128
#define FLIGHT_TASK_PRIORITY  3
TaskHandle_t flight_task_handle;
#define FLIGHT_TASK_PERIOD 6


/*启用freeRTOS操作系统*/
void App_freeRTOS_Start(void)
{
  //1、创建电源管理任务
  xTaskCreate(power_task, "power_task", POWER_TASK_STACK_SIZE, NULL, POWER_TASK_PRIORITY, &power_task_handle);

  //2、创建飞行控制任务
  xTaskCreate(flight_task, "flight_task", FLIGHT_TASK_STACK_SIZE, NULL, FLIGHT_TASK_PRIORITY, &flight_task_handle);

  //3、启动调度器
  vTaskStartScheduler();
}

/*-----------------------------------------------------------------------------------------------*/
//电源管理
void power_task(void *args)
{
  //获取当前的基准时间
  TickType_t xLastWakeTime = xTaskGetTickCount();
  while (1)

  {
    //每10s执行一次 => 启动电源 避免自动关机
    vTaskDelayUntil(&xLastWakeTime, POWER_TASK_PERIOD);
    
    //启动电源
    Int_IP5305T_Start();

}

/*-----------------------------------------------------------------------------------------------*/
//飞行控制
void flight_task(void *args)
{
    //获取当前的基准时间
  TickType_t xLastWakeTime = xTaskGetTickCount();
   while(1)
    {
        //1.设置电机的转速
        left_top_motor.speed=400;
        left_bottom_motor.speed=400;
        right_top_motor.speed=400;
        right_bottom_motor.speed=400;

        //2.直接启动电机
           int_motor_start(&left_top_motor);
           int_motor_start(&left_bottom_motor);
           int_motor_start(&right_top_motor);
           int_motor_start(&right_bottom_motor);

        vTaskDelayUntil(&xLastWakeTime, FLIGHT_TASK_PERIOD);
    }
  
}

④App_freertos_task.h

引用int_motor.h文件

Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐