#ifndef __STEPMOTOR_H
#define __STEPMOTOR_H
#include "sys.h"
#include "stm32f4xx_tim.h"
#include "usart.h"
#define STEPMOTOR1_DIR PGout(5)
#define STEPMOTOR1_ENA PGout(6)
#define STEPMOTOR2_DIR PGout(7)
#define STEPMOTOR2_ENA PGout(8)
#define STEPMOTOR1_DIR_PIN GPIO_Pin_5
#define STEPMOTOR1_DIR_PORT GPIOG
#define STEPMOTOR1_DIR_CLK RCC_AHB1Periph_GPIOG
#define STEPMOTOR1_DIR_CLK_ENA() {RCC_AHB1PeriphClockCmd(STEPMOTOR1_DIR_CLK ,ENABLE);}
#define STEPMOTOR1_ENA_PIN GPIO_Pin_6
#define STEPMOTOR1_ENA_PORT GPIOG
#define STEPMOTOR1_ENA_CLK RCC_AHB1Periph_GPIOG
#define STEPMOTOR1_ENA_CLK_ENA() {RCC_AHB1PeriphClockCmd( STEPMOTOR1_ENA_CLK ,ENABLE);}
#define STEPMOTOR2_DIR_PIN GPIO_Pin_7
#define STEPMOTOR2_DIR_PORT GPIOG
#define STEPMOTOR2_DIR_CLK RCC_AHB1Periph_GPIOG
#define STEPMOTOR2_DIR_CLK_ENA() {RCC_AHB1PeriphClockCmd( STEPMOTOR2_DIR_CLK ,ENABLE);}
#define STEPMOTOR2_ENA_PIN GPIO_Pin_8
#define STEPMOTOR2_ENA_PORT GPIOG
#define STEPMOTOR2_ENA_CLK RCC_AHB1Periph_GPIOG
#define STEPMOTOR2_ENA_CLK_ENA() {RCC_AHB1PeriphClockCmd( STEPMOTOR2_ENA_CLK ,ENABLE);}
#define CW 0
#define CCW 1
extern u32 Toggle_Pulse[4];
extern u8 pwmflag;
void stepmotor1234_gpio_Init(void);
void stepmotor1234_timer8_Init(u32 arr,u32 psc);
#endif
step.c
#include "stepmotor.h"
u32 Toggle_Pulse[4]={900,500,500,500};
int32_t xend=0;
int32_t yend=0;
u32 steptotalnum=0;
int32_t fi=0;
u8 moveaxisflag=0;
u8 movingstatus=0;
void stepmotor1234_gpio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
STEPMOTOR1_DIR_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = STEPMOTOR1_DIR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(STEPMOTOR1_DIR_PORT, &GPIO_InitStructure);
STEPMOTOR1_ENA_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = STEPMOTOR1_ENA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(STEPMOTOR1_ENA_PORT, &GPIO_InitStructure);
STEPMOTOR2_DIR_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = STEPMOTOR2_DIR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(STEPMOTOR2_DIR_PORT, &GPIO_InitStructure);
STEPMOTOR2_ENA_CLK_ENA();
GPIO_InitStructure.GPIO_Pin = STEPMOTOR2_ENA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(STEPMOTOR2_ENA_PORT, &GPIO_InitStructure);
}
void stepmotor1234_timer8_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity= TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState= TIM_OCIdleState_Reset ;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;
TIM_OCInitStructure.TIM_Pulse =Toggle_Pulse[0];
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_OC1FastConfig(TIM8, TIM_OCFast_Disable);
TIM_OCInitStructure.TIM_Pulse=Toggle_Pulse[1];
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ARRPreloadConfig(TIM8,ENABLE);
TIM_ClearITPendingBit(TIM8, TIM_IT_CC1);
TIM_ClearITPendingBit(TIM8, TIM_IT_CC2);
TIM_Cmd(TIM8, DISABLE);
TIM_CtrlPWMOutputs(TIM8, DISABLE);
TIM_ITConfig(TIM8, TIM_IT_CC1 | TIM_IT_CC2 , DISABLE);
movingstatus=0;
}
void Interpolation_move(u32 xendtemp,u32 yendtemp,u16 speed)
{
fi=0;
xend=xendtemp;
yend=yendtemp;
steptotalnum=xendtemp+yendtemp;
moveaxisflag=0;
TIM_SetCompare1(TIM8, speed);
TIM_SetCompare2(TIM8, speed);
TIM_SetAutoreload(TIM8, speed);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_ITConfig(TIM8, TIM_IT_CC1 | TIM_IT_CC2 , ENABLE);
TIM_Cmd(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
TIM_CCxCmd(TIM8,TIM_Channel_1, ENABLE);
TIM_CCxCmd(TIM8,TIM_Channel_2, DISABLE);
movingstatus=1;
}
void Linear_move(int32_t xendtemp,int32_t yendtemp,u16 speedtemp)
{
if(movingstatus!=0)
return;
if(xendtemp<0)
{
STEPMOTOR1_DIR=CCW;
xendtemp= -xendtemp;
}
else
{
STEPMOTOR1_DIR=CW;
}
if(yendtemp<0)
{
STEPMOTOR2_DIR=CCW;
yendtemp= -yendtemp;
}
else
{
STEPMOTOR2_DIR=CW;
}
printf("xendtemp=%d, yendtemp=%d。\r\n",xendtemp,yendtemp);
Interpolation_move(xendtemp,yendtemp, speedtemp);
}
void TIM8_CC_IRQHandler(void)
{
static xi=0,yi=0;
u8 last_axis=0;
static u8 ii=0;
static u8 calflag=0;
last_axis = moveaxisflag;
if(TIM_GetFlagStatus(TIM8,TIM_FLAG_CC1)!=RESET)
{
TIM_ClearFlag(TIM8, TIM_FLAG_CC1);
ii=ii+1;
if(ii%2==0)
{
calflag=1;
ii=0;
}
}
if(TIM_GetFlagStatus(TIM8,TIM_FLAG_CC2)!=RESET)
{
TIM_ClearFlag(TIM8, TIM_FLAG_CC2);
ii=ii+1;
if(ii%2==0)
{
calflag=1;
ii=0;
}
}
if(calflag==1)
{
if(fi<0)
{
moveaxisflag=1;
fi=fi+xend;
if(STEPMOTOR2_DIR==CW)
{yi=yi+1;}
else
yi=yi-1;
xi=xi;
printf("y轴运动,第%d步。fi=%d,xi=%d, yi=%d\r\n",steptotalnum,fi,xi,yi);
}
else
{
moveaxisflag=0;
fi=fi-yend;
if(STEPMOTOR1_DIR==CW)
xi=xi+1;
else
xi=xi-1;
yi=yi;
printf("x轴运动,第%d步。fi=%d,xi=%d, yi=%d\r\n",steptotalnum,fi,xi,yi);
}
if(moveaxisflag==0)
{
TIM_CCxCmd(TIM8,TIM_Channel_1, ENABLE);
TIM_CCxCmd(TIM8,TIM_Channel_2, DISABLE);
}
else if(moveaxisflag==1)
{
TIM_CCxCmd(TIM8,TIM_Channel_1, DISABLE);
TIM_CCxCmd(TIM8,TIM_Channel_2, ENABLE);
}
steptotalnum=steptotalnum-1;
if(steptotalnum==1)
{
TIM_CCxCmd(TIM8,TIM_Channel_1, DISABLE);
TIM_CCxCmd(TIM8,TIM_Channel_2, DISABLE);
movingstatus=0;
printf("结束运动\r\n");
TIM_ITConfig(TIM8, TIM_IT_CC1 , DISABLE);
TIM_ITConfig(TIM8, TIM_IT_CC2 , DISABLE);
TIM_CtrlPWMOutputs(TIM8, DISABLE);
TIM_Cmd(TIM8, DISABLE);
}
calflag=0;
}
}
main
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "oled.h"
#include "key.h"
#include "EXTI.h"
#include "timer.h"
#include "pwm.h"
#include "dcmotor.h"
#include "linesensor.h"
#include "stepmotor.h"
extern u32 Toggle_Pulse[4];
u8 GLOKEYVAL=0;
u8 pwmflag=0;
u8 pwmaddsubflag=0;
int main(void)
{
int shuzi=987;
u8 keyval=0;
float aa=123.456;
float vvtemp=0.2;
delay_init(168);
LED_Init();
OLED_Init();
uart_init(115200);
KEY_Init();
delay_ms(30);
OLED_Clear();
stepmotor1234_gpio_Init();
stepmotor1234_timer8_Init(65535,168-1);
printf("逐点比较法\r\n");
delay_ms(10);
while(1)
{
keyval=KEY_Scan();
if(keyval==30)
{
printf("k3(x,y)\r\n");
Linear_move(5,-8,1500);
}
}
}