#ifndef __TIM_H_
#define __TIM_H_
#include "stm32f0xx_tim.h"
void BSP_TIM6_Init(void);
#endif
#include "TIM.h"
void BSP_TIM6_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);
TIM_TimeBaseInitStructure.TIM_Period = (1000-1);
TIM_TimeBaseInitStructure.TIM_Prescaler = (48-1);
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM6,ENABLE);
}
void TIM6_IRQHandler(void)
{
if(TIM_GetITStatus(TIM6,TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM6,TIM_IT_Update); //清除更新中断标志位
MotorTick_Now ++;
}
}
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
uint32_t MotorTick_Now = 0;
uint32_t MotorTick_Trig = 0;
uint8_t MotorTrig_Flag = 0;
void delay(uint32_t xms)
{
__IO uint32_t i, j;
i = xms * 999;
while(i--)
{
j = 47;
while(j--);
}
}
/**
@brief Main program.
@param None
@retval None / int main(void) { /!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ BSP_GPIO_Init(); BSP_TIM6_Init(); SysTick_Init(8); //滴答定时器延时初始化
// __set_PRIMASK(1);
/* Infinite loop */
while(1)
{
if(GPIO_ReadInputDataBit(INPUT_Port,SW_Pin) == SET )
{
GPIO_ResetBits(JK_Port,JK_Pin);
}
else
{
if(GPIO_ReadInputDataBit(INPUT_Port, DS_Pin) == RESET)
{
MotorTrig_Flag = 1;
}
if(MotorTrig_Flag == 1)
{
GPIO_ResetBits(JK_Port, JK_Pin);
MotorTick_Trig = 0;
if(GPIO_ReadInputDataBit(SWs_Port, GPIO_Pin_2) ==0)
{
MotorTick_Trig = 1000 * 10;
}
if(GPIO_ReadInputDataBit(SWs_Port, GPIO_Pin_3) ==0)
{
MotorTick_Trig += 1000 * 60 * 2;
}
if(GPIO_ReadInputDataBit(SWs_Port, GPIO_Pin_4) == 0)
{
MotorTick_Trig += 1000 * 60 * 4;
}
if(GPIO_ReadInputDataBit(SWs_Port, GPIO_Pin_5) == 0)
{
MotorTick_Trig += 1000 * 60 * 8;
}
if(GPIO_ReadInputDataBit(SWs_Port, GPIO_Pin_6) == 0)
{
MotorTick_Trig += 1000 * 60 * 16;
}
if(GPIO_ReadInputDataBit(SWs_Port, GPIO_Pin_7) == 0)
{
MotorTick_Trig += 1000 * 60 * 32;
}
if(MotorTick_Now <= MotorTick_Trig)
{
GPIO_SetBits(JK_Port, JK_Pin); //引脚输出1 继电器吸合
}
}
else
{
MotorTick_Trig = 0;
}
GPIO_SetBits(JK_Port,JK_Pin);
if(GPIO_ReadInputDataBit(INPUT_Port, DS_Pin) == RESET) //压力
{
GPIO_ResetBits(JK_Port, JK_Pin);
}
if(GPIO_ReadInputDataBit(INPUT_Port, WS_Pin) == SET) //液位
{
GPIO_ResetBits(JK_Port, JK_Pin);
}
if(GPIO_ReadInputDataBit(INPUT_Port, DK_Pin) == RESET) //强制启动
{
GPIO_SetBits(JK_Port, JK_Pin);
}
}
Delay_ms(10); //滴答定时器延时
// delay(10); //while延时
// Delay_ms(10); //滴答定时器延时
}
} |