- #include "sys.h"
- #include "delay.h"
- #include "diver.h"
- #include "stdbool.h"
- //IN4: PB8 d
- //IN3: PB7 c
- //IN2: PB6 b
- //IN1: PB5 a
- //四相八拍
- u8 phasecw[8] ={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};// 逆時針
- u8 phaseccw[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};// 順時針
- //引腳初始化
- void Step_Motor_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- }
- //引腳映射
- void SetMotor(unsigned char InputData)
- {
- if(InputData&0x01)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_5);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_5);
- }
- if(InputData&0x02)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_6);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_6);
- }
- if(InputData&0x04)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_7);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_7);
- }
- if(InputData&0x08)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_8);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_8);
- }
- }
- //步距角5.625 360/5.625=64 減速比1/64
- //故64*64個脈沖轉一圈
- //n圈數
- //direction 方向
- void motorNcircle(int n,bool direction)
- {
- int i,j,k=0;
- for(j=0;j<n;j++)
- {
- for(i=0;i<64*8;i++)
- //for(i=0;i<8;i++)
- {
- for(k=0;k<8;k++)
- {
- if(1 == direction)
- {
- SetMotor(phasecw[k]);
- }
- else
- {
- SetMotor(phaseccw[k]);
- }
- delay_ms(2);
- }
- }
- }
- }
- //speed 速度
- //direction 方向
- void motorNspeed(int speed,bool direction)
- {
- unsigned char k=0;
- for(k=0;k<8;k++)
- {
- if(direction==1)
- SetMotor(phasecw[k]); //取數據
- else
- SetMotor(phaseccw[k]);
- delay_ms(speed); //調節轉速
- }
- }
復制代碼
全部資料51hei下載地址:
步進電機.7z
(179.69 KB, 下載次數: 29)
2019-11-13 14:55 上傳
點擊文件名下載附件
|