2021-09-06 09:09:59 索煒達(dá)電子 848
項(xiàng)目編號:E814
文件大?。?79K
源碼說明:帶中文注釋
開發(fā)環(huán)境:C編譯器
簡要概述:
一、本次使用的硬件
開發(fā)板:stm32f103c8t6核心板
電機(jī):ULN2003 步進(jìn)電機(jī)擴(kuò)展板+普通42電機(jī)
二、簡單原理及代碼
1.原理
四相步進(jìn)電機(jī)有兩種運(yùn)行方式 1.四相四拍;2.四相八拍。
拍數(shù)
指電機(jī)轉(zhuǎn)過一個(gè)齒距角所需脈沖數(shù),通俗的來講拍數(shù)指的是步進(jìn)電機(jī)運(yùn)行時(shí)每轉(zhuǎn)一個(gè)齒距所需的脈沖數(shù)。以四相電機(jī)為例,有四相單四拍運(yùn)行方式即A-B-C-D-A,有四相四拍運(yùn)行方式即AB-BC-CD-DA-AB,有四相八拍運(yùn)行方式即 A-AB-B-BC-C-CD-D-DA-A。
轉(zhuǎn)動
給四個(gè)管腳一定頻率的脈沖即可使其轉(zhuǎn)動,脈沖頻率越快,轉(zhuǎn)動越快。反之轉(zhuǎn)動越慢,脈沖次數(shù)越多,轉(zhuǎn)動角度越大。
實(shí)現(xiàn)方式
使用最簡單的延時(shí)控制脈沖,將數(shù)據(jù)放在數(shù)組內(nèi)通過for循環(huán)發(fā)給驅(qū)動模塊。不使用定時(shí)器或者復(fù)雜的計(jì)算,能實(shí)現(xiàn)短時(shí)間內(nèi)將電機(jī)驅(qū)動起來。
2.主要函數(shù)
main.c
#include "delay.h"
#include "sys.h"
#include "stdio.h"
#include "motor.h"
//IN4: PB9 d
//IN3: PB8 c
//IN2: PB7 b
//IN1: PB6 a
int main(void)
{
Moto_Init(); //電機(jī)
delay_init(); //延時(shí)函數(shù)初始化
while(1)
{
Motor_Ctrl_Direction_Angle(1,180);//正轉(zhuǎn)180度
delay_ms(500);
Motor_Ctrl_Direction_Angle(0,90);//反轉(zhuǎn)90度
}
}
簡單的執(zhí)行正轉(zhuǎn)180度,然后反轉(zhuǎn)90度。中間的delay_ms()按需要修改,但是注意如果delay_ms()太短可能出現(xiàn)卡頓現(xiàn)象。因?yàn)閳?zhí)行上一個(gè)轉(zhuǎn)動角度的任務(wù)需要一定的時(shí)間。所以要合理修改。
motor.c
#include "motor.h"
#include "delay.h"
unsigned short phasecw[4] ={0x0200,0x0100,0x0080,0x0040};// D-C-B-A 反轉(zhuǎn)
unsigned short phaseccw[4]={0x0040,0x0080,0x0100,0x0200};// A-B-C-D 正轉(zhuǎn)
/***************************************************/
// 電機(jī)模塊與單片機(jī)連接引腳
//IN4: PB9 d
//IN3: PB8 c
//IN2: PB7 b
//IN1: PB6 a
void Moto_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 ;//引腳按著INT1順序接就行了
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_6 | GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 );
}
引腳初始化的函數(shù),可以按照自己所需使用的引腳進(jìn)行修改。
void MotoRcw(void) //反轉(zhuǎn)
{
int i;
for(i=0;i<4;i++)
{
GPIO_Write(GPIOB,phasecw[ i]);
delay_ms(3);
}
}
正轉(zhuǎn)函數(shù),修改 delay_ms()即可以改變轉(zhuǎn)動速度,反轉(zhuǎn)函數(shù)依然同樣操作。
void Motor_Ctrl_Direction_Angle(int direction, int angle)
{
u16 j;
if(direction == 1)
{
for(j=0;j<64*angle/45;j++)
{
MotoRccw();//正轉(zhuǎn)
}
MotorStop();//停止
}
else
{
for(j=0;j<64*angle/45;j++)
{
MotoRcw();//反轉(zhuǎn)
}
MotorStop();//停止
}
}
控制電機(jī)轉(zhuǎn)動角度函數(shù)
direction為方向變量,輸入1為正轉(zhuǎn),0為反轉(zhuǎn)。
angle為角度變量,可為0-360具有實(shí)際意義。
**注意:
j<64*angle/45
這一句是轉(zhuǎn)動一度的數(shù)學(xué)公式,是根據(jù)這個(gè)步進(jìn)電機(jī)本身的轉(zhuǎn)子數(shù)計(jì)算出來的.
目錄│文件列表:
└ 步進(jìn)電機(jī)驅(qū)動程序
│ keilkilll.bat
│ README.TXT
├ CORE
│ │ core_cm3.c
│ │ core_cm3.h
│ │ startup_stm32f10x_hd.s
│ └ startup_stm32f10x_md.s
├ HARDWARE
│ └ LED
│ │ led.c
│ └ led.h
├ OBJ
│ └ LED.hex
├ STM32F10x_FWLib
│ ├ inc
│ │ │ misc.h
│ │ │ stm32f10x_adc.h
│ │ │ stm32f10x_bkp.h
│ │ │ stm32f10x_can.h
│ │ │ stm32f10x_cec.h
│ │ │ stm32f10x_crc.h
│ │ │ stm32f10x_dac.h
│ │ │ stm32f10x_dbgmcu.h
│ │ │ stm32f10x_dma.h
│ │ │ stm32f10x_exti.h
│ │ │ stm32f10x_flash.h
│ │ │ stm32f10x_fsmc.h
│ │ │ stm32f10x_gpio.h
│ │ │ stm32f10x_i2c.h
│ │ │ stm32f10x_iwdg.h
│ │ │ stm32f10x_pwr.h
│ │ │ stm32f10x_rcc.h
│ │ │ stm32f10x_rtc.h
│ │ │ stm32f10x_sdio.h
│ │ │ stm32f10x_spi.h
│ │ │ stm32f10x_tim.h
│ │ │ stm32f10x_usart.h
│ │ └ stm32f10x_wwdg.h
│ └ src
│ │ misc.c
│ │ stm32f10x_adc.c
│ │ stm32f10x_bkp.c
│ │ stm32f10x_can.c
│ │ stm32f10x_cec.c
│ │ stm32f10x_crc.c
│ │ stm32f10x_dac.c
│ │ stm32f10x_dbgmcu.c
│ │ stm32f10x_dma.c
│ │ stm32f10x_exti.c
│ │ stm32f10x_flash.c
│ │ stm32f10x_fsmc.c
│ │ stm32f10x_gpio.c
│ │ stm32f10x_i2c.c
│ │ stm32f10x_iwdg.c
│ │ stm32f10x_pwr.c
│ │ stm32f10x_rcc.c
│ │ stm32f10x_rtc.c
│ │ stm32f10x_sdio.c
│ │ stm32f10x_spi.c
│ │ stm32f10x_tim.c
│ │ stm32f10x_usart.c
│ └ stm32f10x_wwdg.c
├ SYSTEM
│ ├ delay
│ │ │ delay.c
│ │ └ delay.h
│ ├ sys
│ │ │ sys.c
│ │ └ sys.h
│ └ usart
│ │ usart.c
│ └ usart.h
└ USER
│ drv_systick.c
│ drv_systick.h
│ EventRecorderStub.scvd
│ JLinkSettings.ini
│ LED.uvguix.10238
│ LED.uvoptx
│ LED.uvprojx
│ main.c
│ motor.c
│ motor.h
│ step.c
│ step.h
│ stm32f10x.h
│ stm32f10x_conf.h
│ stm32f10x_it.c
│ stm32f10x_it.h
│ system_stm32f10x.c
│ system_stm32f10x.h
└ DebugConfig
│ LED_STM32F103C8_1.0.0.dbgconf
│ LED_STM32F103RC_1.0.0.dbgconf
└ LED_STM32F103VC_1.0.0.dbgconf