一、总体思路
二、GPIOA端口的设置
GPIO_InitTypeDef GPIO_InitStruct;
//开启电机0外设时钟
DJ_EnablePeriphClock_0();
//初始化电机0
GPIO_InitStruct.GPIO_Pin = DJ_EN_0 | DJ_DR_0 | DJ_MC_0;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(DJ_GPIO_0, &GPIO_InitStruct);
//设置电机0的初始化状态
DJ_DisEnable(DJ_GPIO_0, DJ_EN_0); //关闭电机0
三、SysTick设置
void Timer(__IO uint32_t nTime)
{
TimingDelay = nTime;
TimingLoad = nTime;
}
if (TimingDelay)
TimingDelay--;
else
{
TimingDelay = TimingLoad;
if (dj_GetMc()) //判断是否还需要发送脉冲
{
printfd("\r\nsend %dth pulse, %d", dj_GetMc(), 1 - GPIO_ReadOutputDataBit (DJ_GPIO_0, DJ_MC_0)); //用于调试
DJ_IO(1 - GPIO_ReadOutputDataBit (DJ_GPIO_0, DJ_MC_0), DJ_GPIO_0, DJ_MC_0); //用于发送脉冲
dj_DesMc();
}
else
SysTick_Shutdown(); //关闭系统时钟
}
四、按键设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
static void NVIC_SetVector( IRQn_Type IRQn, uint8_t PreemptionPriority, uint8_t SubPriority)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = IRQn;//EXTI0_IRQn | EXTI9_5_IRQn | EXTI3_IRQn | EXTI15_10_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = PreemptionPriority;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = SubPriority;
NVIC_Init(&NVIC_InitStruct);
}
//WAKEUP键
static void EXTI_PA0_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
NVIC_SetVector(EXTI0_IRQn, 0, 0); //配置NVIC
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); //配置GPIOA_Pin0为EXTI0线
EXTI_InitStruct.EXTI_Line = EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising ; //上升沿触发
EXTI_Init(&EXTI_InitStruct);
}
//WAKEUP
void EXTI0_IRQHandler(void)
{
if (EXTI_GetFlagStatus(EXTI_Line0) != RESET) //看是否产生了EXTI_Line0中断
{
printfd("\r\nexti0");
if (dj_GetMc() == 0)
{
dj_SetMc(1);
SysTick_Startup();
}
EXTI_ClearFlag(EXTI_Line0); //清除中断标志位
}
}