单片机平台
正点原子探索者STM32F407
引脚
PUL+ <—> VCC(+3.3v)
PUL- <—> TIM1_CH1(PA8)
DIR+ <—> VCC(+3.3v)
DIR- <—> PE5
ENA+ <—> VCC(+3.3v)
ENA- <—> PE6
按键:
控制使能:PE4
控制方向:PE3
TIM1的设置
key_exit.h
#ifndef __KEY_EXTI_H__
#define __KEY_EXTI_H__
#include "MyApplication.h"
//定义结构体类型
typedef struct
{
uint8_t volatile KEY_Flag; //按键标志位
uint8_t Press_Num;
void (*KEY_EXTI_KEY_Detect)(void); //按键检测
} KEY_EXTI_t;
/* extern variables-----------------------------------------------------------*/
extern KEY_EXTI_t KEY_EXTI_KEY0;
extern KEY_EXTI_t KEY_EXTI_KEY1;
#endif
key_exit.c
/* Includes ------------------------------------------------------------------*/
#include "MyApplication.h"
volatile Steerper_Motor_Enable_Val_t Steerper_Motor_Enable_Status_Val = Steerper_Motor_No_EN_Val ;//默认不使能
volatile Steeper_Motor_DIR_Val_t Steerper_Motor_Dir_Status_Val = Steerper_Motor_FWD_Val; //默认正转
/* Private function prototypes------------------------------------------------*/
static void KEY_EXTI_KEY0_Detect(void); //按键0检测
static void KEY_EXTI_KEY1_Detect(void); //按键1检测
/* Public variables ----------------------------------------------------------*/
//结构体定义
KEY_EXTI_t KEY_EXTI_KEY0 = {FALSE,0,KEY_EXTI_KEY0_Detect};
KEY_EXTI_t KEY_EXTI_KEY1 = {FALSE,0,KEY_EXTI_KEY1_Detect};
/* Private function prototypes--------------------------*/
/*
* @ name KEY0_Detect
* @ brief 触摸按键0检测
* @ param None
* @ retval None
*/
static void KEY_EXTI_KEY0_Detect(void) //按键0检测
{
if(KEY_EXTI_KEY0.KEY_Flag == TRUE)
{
HAL_Delay(10);
if(HAL_GPIO_ReadPin(KEY_EXTI_KEY0_GPIO_Port,KEY_EXTI_KEY0_Pin) == GPIO_PIN_SET)
{
KEY_EXTI_KEY0.Press_Num++;
}
if(KEY_EXTI_KEY0.Press_Num == 3)
{
KEY_EXTI_KEY0.Press_Num =1;
}
if(KEY_EXTI_KEY0.Press_Num == 1)
{
printf("Steerper Motor Enable.\r\n");
Steerper_Motor.Steerper_Motor_Enable();
Steerper_Motor_Enable_Status_Val=Steerper_Motor_EN_Val;
}
if(KEY_EXTI_KEY0.Press_Num == 2)
{
printf("Steerper Motor Disable.\r\n");
Steerper_Motor.Steerper_Motor_Disable();
Steerper_Motor_Enable_Status_Val=Steerper_Motor_No_EN_Val;
}
//清除按键0状态
KEY_EXTI_KEY0.KEY_Flag = FALSE;
}
}
/*
* @ name KEY1_Detect
* @ brief 触摸按键1检测
* @ param None
* @ retval None
*/
static void KEY_EXTI_KEY1_Detect(void) //按键0检测
{
if(Steerper_Motor_Enable_Status_Val == Steerper_Motor_EN_Val)
{
if(KEY_EXTI_KEY1.KEY_Flag == TRUE)
{
HAL_Delay(10);
if(HAL_GPIO_ReadPin(KEY_EXTI_KEY1_GPIO_Port,KEY_EXTI_KEY1_Pin) == GPIO_PIN_SET)
{
KEY_EXTI_KEY1.Press_Num++;
}
if(KEY_EXTI_KEY1.Press_Num == 3)
{
KEY_EXTI_KEY1.Press_Num =1;
}
if(KEY_EXTI_KEY1.Press_Num == 1)
{
printf("Steerper Motor REV.\r\n");
Steerper_Motor_Dir_Status_Val=Steerper_Motor_REV_Val;
}
if(KEY_EXTI_KEY1.Press_Num == 2)
{
printf("Steerper Motor FWD.\r\n");
Steerper_Motor_Dir_Status_Val=Steerper_Motor_FWD_Val;
}
//清除按键1状态
KEY_EXTI_KEY1.KEY_Flag = FALSE;
}
}
}
steeper_motor.h
#ifndef __STEEPER_MOTOR_H__
#define __STEEPER_MOTOR_H__
#include "MyApplication.h"
//定义电机运转的方向
typedef enum
{
Steerper_Motor_FWD_Val = (uint8_t)1,
Steerper_Motor_REV_Val = (uint8_t)0
} Steeper_Motor_DIR_Val_t;
typedef enum
{
Steerper_Motor_EN_Val = (uint8_t)1,
Steerper_Motor_No_EN_Val = (uint8_t)0
} Steerper_Motor_Enable_Val_t;
//定义结构体类型
typedef struct
{
void (*Steerper_Motor_Enable)(void); //步进电机使能
void (*Steerper_Motor_Disable)(void); //步进电机失能
void (*Steerper_Motor_FWD)(void); //步进电机正转
void (*Steerper_Motor_REV)(void); //步进电机反转
} Steerper_Motor_t;
extern Steerper_Motor_t Steerper_Motor;
extern void Steerper_Motor_Work(Steeper_Motor_DIR_Val_t Steerper_Motor_DIR_Val);
/* 控制步进电机使能引脚 */ /* 高电平使能 */
#define Cotrol_Steerper_Motor_Enable() HAL_GPIO_WritePin(GPIOE, Control_Steeper_EN_Pin, GPIO_PIN_SET)
#define Cotrol_Steerper_Motor_Disable() HAL_GPIO_WritePin(GPIOE, Control_Steeper_EN_Pin, GPIO_PIN_RESET)
/* 低电平反能 */
/* 控制步进电机方向引脚 */ /* 高电平正转 */ // 高电平打开-高电平使能
#define Cotrol_Steerper_Motor_FWD() HAL_GPIO_WritePin(GPIOE, Control_Steeper_Dir_Pin, GPIO_PIN_SET)
#define Cotrol_Steerper_Motor_REV() HAL_GPIO_WritePin(GPIOE, Control_Steeper_Dir_Pin, GPIO_PIN_RESET)
/* 低电平反转 */
/* 控制步进电机脉冲引脚 */ /* 使能比较通道1 */
#define Cotrol_Steerper_Motor_PUL_Enable() TIM_CCxChannelCmd(TIM1, TIM_CHANNEL_1, TIM_CCx_ENABLE)
#define Cotrol_Steerper_Motor_PUL_Disable() TIM_CCxChannelCmd(TIM1, TIM_CHANNEL_1, TIM_CCx_DISABLE)
/* 失能比较通道1 */
#endif
steeper_motor.c
#include "MyApplication.h"
extern volatile Steerper_Motor_Enable_Val_t Steerper_Motor_Enable_Status_Val ;//默认不使能
extern volatile Steeper_Motor_DIR_Val_t Steerper_Motor_Dir_Status_Val ; //默认正转
void Steerper_Motor_Enable(void);
void Steerper_Motor_Disable(void);
void Steerper_Motor_FWD(void);
void Steerper_Motor_REV(void);
Steerper_Motor_t Steerper_Motor =
{
Steerper_Motor_Enable, //步进电机正能
Steerper_Motor_Disable, //步进电机失能
Steerper_Motor_FWD , //步进电机正转
Steerper_Motor_REV //步进电机反转
};
//步进电机正能
void Steerper_Motor_Enable(void)
{
Cotrol_Steerper_Motor_Enable();
Cotrol_Steerper_Motor_PUL_Enable();
}
//步进电机失能
void Steerper_Motor_Disable(void)
{
Cotrol_Steerper_Motor_PUL_Disable();
Cotrol_Steerper_Motor_Disable();
}
//步进电机正转
void Steerper_Motor_FWD(void)
{
Cotrol_Steerper_Motor_FWD();
}
//步进电机反转
void Steerper_Motor_REV(void)
{
Cotrol_Steerper_Motor_REV();
}
//电机工作方向选择
void Steerper_Motor_Work(Steeper_Motor_DIR_Val_t Steerper_Motor_DIR_Val)
{
if(Steerper_Motor_Enable_Status_Val == Steerper_Motor_EN_Val)
{
switch (Steerper_Motor_DIR_Val)
{
case Steerper_Motor_FWD_Val: Steerper_Motor.Steerper_Motor_FWD();
break;
case Steerper_Motor_REV_Val: Steerper_Motor.Steerper_Motor_REV();
break;
default : System.Assert_Failed();
break;
}
}
}
CallBack.c
#include "MyApplication.h"
__IO uint16_t OC_Pulse_num_Channel1 = 200; /* 通道1的比较值 */
//比较值为200时,整个脉冲周期的计数值应为400。
//168000000/168/400 = 10000/4 = 2500 Hz (分频系数为168,比较值为200)
//168000000/168/200 = 10000/2 = 5000 Hz (分频系数为168,比较值为100)
//168000000/6/400 = 280000 / 4 =70000 Hz(分频系数为6,比较值为200)
/**
* @brief 定时器比较中断
* @param htim:定时器句柄指针
* @note 无
* @retval 无
*/
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
__IO uint16_t count;
/*获取当前计数*/
count = __HAL_TIM_GET_COUNTER(htim);
/*判断触发中断的输出通道并设置新的比较数值*/
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
__HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, count + OC_Pulse_num_Channel1);
}
}
/**
* @brief EXTI line detection callbacks.
* @param GPIO_Pin Specifies the pins connected EXTI line
* @retval None
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
HAL_Delay(100); //消抖
switch(GPIO_Pin)
{
case KEY_EXTI_KEY0_Pin: KEY_EXTI_KEY0.KEY_Flag = TRUE;
break;
case KEY_EXTI_KEY1_Pin: KEY_EXTI_KEY1.KEY_Flag = TRUE;
break;
default:printf("Error\r\n");
break;
}
}
/********************************************************
End Of File
********************************************************/