#include "stm32f10x.h"
#include "BJMotor.h"
#include "delay.h"
int flag=0;
int i=0;
int main(void)
{
EXTI_Config();//中断初始化
Motor_GPIO_Config();//电机驱动口初始化
while(1)
{
if(flag==0)
for(i=0;i<8;i++)
{
GPIO_Write(GPIOB,F_Rotation<<12);//电机正转
delay_us(1000);//延时1ms
}
else if(flag==1)
for(i=0;i<8;i++)
{
GPIO_Write(GPIOB,B_Rotation<<12);//电机反转
delay_us(1000);
}
else if(flag==2) //电机停止转动
GPIO_Write(GPIOB,0x00<<12);
}
}
#ifndef __BJMotor_H__
#define __BJMotor_H__
static unsigned char F_Rotation[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};//正转表格
static unsigned char B_Rotation[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};//反转表格
void NVIC_Configuration(void);//中断线及优先级配置
void EXTI_Config(void);//中断形式配置
void Motor_GPIO_Config(void);//电机驱动口配置
#endif
#include "stm32f10x.h"
#include "BJMotor.h"
#include "misc.h"
#include "delay.h"
void NVIC_Configuration(void)//中断线及优先级配置
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级组1
/* 配置P[A|B|C|D|E]5为中断源 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //中断使能
NVIC_Init(&NVIC_InitStructure); //用结构体初始化
}
void EXTI_Config(void)//中断形式配置
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* config the extiline(PE5) clock and AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO,ENABLE);//使能GPIOE及打开端口复用功(外部中断)
/* config the NVIC(PE5) */
NVIC_Configuration();
/* EXTI line gpio config(PE5) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //选择GPIO5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // 上拉输入
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* EXTI line(PE5) mode config */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource5);
EXTI_InitStructure.EXTI_Line = EXTI_Line5; //选择第5线作中断输入
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //模式选择Interrupt,Event
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void Motor_GPIO_Config(void)//电机驱动口配置
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14
| GPIO_Pin_13 | GPIO_Pin_12; //高四位驱动作线
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速率50Mhz
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
|