採用定時器tim3來控制pwm的輸出來產生頻率變化的脈衝。
過程:加速——勻速——減速——停止
檔案:stepmotor.c 定義步進電機控制程式
//用到的tim2為了實現另外的功能可以忽視
#include "stepmotor.h"
#include
u32 pul_cnt;
// tim3脈衝計數
vu32 step_done;
vu32 run_state;
#define run_state_stop 0
#define run_state_acc 1
#define run_state_run 2
#define run_state_dec 3
void stepmotor_ctrl_init(void)
void tim3_configuration(u32 period)
//void motor_run(u32 acc, u32 dec, u32 topspeed, u32 dis)
//步進電機執行引數
//acc -- 加速度,單位: round/min/s
//dec -- 減速度,單位: round/min/s
//topspeed -- 最高速度,單位: round/min
//dis -- 總角位移,單位: round/10000
void motor_run(u32 acc, u32 dec, u32 topspeed, u32 dis)
step_acc = n_motor * ((float)topspeed*topspeed/(acc*120));
step_dec = n_motor * ((float)topspeed*topspeed/(dec*120));
if(step_all > step_acc + step_dec)
step_run = step_all - step_acc - step_dec;
else
step_run = 0;
//tim_cnt = 5.2363 * ft / (sqrt(acc*n_motor/2)); //(ft * sqrt(60)*0.676) / sqrt(acc*n_motor/2);
tim_cnt = 7.7460 * ft / (sqrt(acc*n_motor/2));
tim_rest = 0;
i = 0;
tim3_configuration(tim_cnt);
run_state = run_state_acc;
tim_cmd(tim3,enable);
step_done = 0;
while(step_done==0);
while(i
if(step_run > 0)
}run_state = run_state_dec;
tim_rest = 0;
i=0;
tim_cnt = tim_cnt + (2*tim_cnt+tim_rest) / (4*(step_dec-i)-1);
while(i
run_state = run_state_stop;
tim_cmd(tim3,disable);
}void tim2_irqhandler(void)
void tim3_irqhandler(void)
檔案:stepmotor.h 宣告步進電機控制標頭檔案
#define n_motor 10000 //步進電機細分
#define ft 24000000
void stepmotor_ctrl_init(void);
void motor_run(u32 acc, u32 dec, u32 topspeed, u32 dis);
檔案:main.c 主函式,設定加速度,減速度,最大速度和步數的引數值
#include "main.h"
#define led_set() gpio_setbits(gpiob,gpio_pin_8)
#define led_rst() gpio_resetbits(gpiob,gpio_pin_8)
#define set_dir_cw() gpio_setbits(gpioa,gpio_pin_6)
#define set_dir_ccw() gpio_resetbits(gpioa,gpio_pin_6)
void nvic_configuration(void);
void led_init(void);
void soft_delayms(u16 t);
int main(void)
return 0;
}void nvic_configuration(void)
void led_init(void)
void soft_delayms(u16 t)
}#ifndef _main_h
#define _main_h
#include "stm32f10x.h"
#include "stepmotor.h"
#endif
STM32定時器 6步PWM輸出
stm32的只有兩個高階定時器tim1與tim8才能互補輸出。這裡我們說的六步pwm輸出要用到互補輸出,所以只有tim1與tim8才能實現。什麼是六步pwm輸出?下面就來解釋下。當乙個定時器需要互補輸出的時候,可以預先設定輸出比較模式位ocxm 如tim ocmode timing,tim ocmo...
高階定時器 PWM互補輸出
一 程式設計要點 1 初始化tmi相關gpio 2 初始化時基結構體 3 初始化比較輸出結構體 4 初始化死區剎車結構體 5 編寫呼吸燈函式 二 結構體 庫函式 1 時基結構體 typedef struct tim timebaseinittypedef 2 比較輸出結構體 typedef stru...
定時器pwm輸出模式小結
對於pwm輸出模式共有以下幾種模式 tim ocmode timing tim ocmode active tim ocmode inactive tim ocmode toggle tim ocmode pwm1 tim ocmode pwm2 首先是 看說明書中的解釋 tim ocmode 函式...