步進電機驅動正反轉 其他 都可移植
#include "sys.h"
#include "delay.h"
#include "LED.h"
#include "step.h"
u8 zhengzhuan = 20;
u8 fanzhuan = 5;
int main()
{
LED_Init();
delay_init();
Step_Motor_GPIO_Init();
while(1)
{
motorNcircle(20,(bool)1);
motorNcircle(20,(bool)0);
}
}
- #include "sys.h"
- #include "delay.h"
- #include "step.h"
- //IN4: PB6 d
- //IN3: PB5 c
- //IN2: PB4 b
- //IN1: PB3 a
- //四相八拍
- u8 phasecw[8] ={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};// 逆時針100000001100000001000000011000000010000000110000000100001001
- u8 phaseccw[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};// 順時針100100000001000000110000001000000110000001000000110000001000
- //引腳初始化
- void Step_Motor_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//打開AFIO時鐘
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); //Full SWJ Disabled (JTAG-DP + SW-DP)
- // 改變指定管腳的映射 GPIO_Remap_SWJ_Disable SWJ 完全禁用(JTAG-DP+SW-DP)
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //JTAG-DP Disabled and SW-DP Enabled
- // 改變指定管腳的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_5|GPIO_Pin_4;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- }
- //引腳映射
- void SetMotor(char InputData)
- {
- if(InputData&0x01)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_4);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_4);
- }
- if(InputData&0x02)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_5);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_5);
- }
- if(InputData&0x04)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_6);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_6);
- }
- if(InputData&0x08)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_7);
- }
- else
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_7);
- }
- }
- //步距角5.625 360/5.625=64 減速比1/64
- //故64*64個脈沖轉一圈
- //n圈數
- //position 方向
- void motorNcircle(int n,bool position)
- {
- 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 == position)
- {
- SetMotor(phasecw[k]);
- }
- else if(0 == position)
- {
- SetMotor(phaseccw[k]);
- }
- delay_ms(2);
- }
- }
- }
- }
復制代碼
原理圖: 無
仿真: 無
代碼:
2_正反轉(success).7z
(181.6 KB, 下載次數: 18)
2024-3-15 02:52 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|