欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
STM32簡(jiǎn)單的步進(jìn)電機(jī)動(dòng)作程序
[打印本頁(yè)]
作者:
KIKI123
時(shí)間:
2019-11-2 12:34
標(biāo)題:
STM32簡(jiǎn)單的步進(jìn)電機(jī)動(dòng)作程序
#include "motor.h"
#include "delay.h"
int motor_speed;
int motor_route;
int motor_time;
void motor_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE); //使能GPIO外設(shè)時(shí)鐘使能
//設(shè)置該引腳為復(fù)用輸出功能,輸出TIM1 CH1的PWM脈沖波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//PWM輸出初始化
//arr:自動(dòng)重裝值
//psc:時(shí)鐘預(yù)分頻數(shù)
void motor_pwm(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外設(shè)時(shí)鐘使能
//設(shè)置該引腳為復(fù)用輸出功能,輸出TIM1 CH1的PWM脈沖波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來(lái)作為T(mén)IMx時(shí)鐘頻率除數(shù)的預(yù)分頻值 不分頻
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時(shí)鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計(jì)數(shù)模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1
; //選擇定時(shí)器模式:TIM脈沖寬度調(diào)制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_Pulse = 0; //設(shè)置待裝入捕獲比較寄存器的脈沖值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主輸出使能
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1預(yù)裝載使能
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的預(yù)裝載寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM1
}
int motor_spe(void)
{
int i;
int n;
n=motor_speed*3/57.6; //電機(jī)轉(zhuǎn)輪是皮帶輪的三分之一,所以*3
i=72000000/(3200*n);
return i;
}
void motor_set(int sp,int re)
{
motor_speed=sp;
motor_route=re;
motor_time=re*1000/sp;
delay_ms(10);
}
void motor_run()
{
int i;
int n;
n=motor_spe();
motor_EN=1;
for(i=19999;i>n;i=i-100)//
{
TIM1->ARR=i;
delay_us(500);
TIM_SetCompare1(TIM1,i/2);
}
}
void motor_stop()
{
int i;
int n;
n=motor_spe();
for(i=n;i<19999;i=i+100)
{
TIM1->ARR=i;
delay_us(100);
TIM_SetCompare1(TIM1,i/2);
}
for(i=n;i<15999;i=i+100)
{
TIM1->ARR=i;
delay_us(100);
TIM_SetCompare1(TIM1,i/2);
}
motor_EN=0;
}
void motor_fwd(void)
{
motor_EN=0;
delay_us(100);
motor_DIR=1;
delay_us(100);
}
void motor_rev(void)
{
motor_EN=0;
delay_us(100);
motor_DIR=0;
delay_us(100);
}
復(fù)制代碼
motor.zip
2019-11-2 12:33 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
1.84 KB, 下載次數(shù): 17, 下載積分: 黑幣 -5
c和h文件
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1