延时函数的配置
定时器中断配置
上图是大约10us一次中断
定时器硬件计数模式配置代码
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 /
timer_second++;
t++;
if(timer_second60)
{
timer_second=0;
timer_minute++;
}
if(timer_minute60)
{
timer_minute=0;
timer_hour++;
}
if(timer_hour==24)
{
timer_hour=0;
timer_day++;
}
/ USER CODE END TIM2_IRQn 0 /
HAL_TIM_IRQHandler(&htim2);
/ USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 /
}
以下是舵机具体进行运转的代码,通过定时器中断产生PWM波进行舵机控制的代码
#if 0
uint16_t a=30;
while(a!=240)
{
/舵机控制原理:pwm20ms的时基周期,0.5-2ms代表角度范围/
if(time<a)
{
IN1(ON);
}
else if(time>=a)
{
IN1(OFF);
}
/以下代码用来控制舵机角度改变的速度,即用控制旋转快慢/
if(time1 == 8)
{
a = a + 10;
time1 = 0;
}
}
#endif
void TIM2_IRQHandler(void)
{
/ USER CODE BEGIN TIM2_IRQn 0 /
// Read_ADC();
#if 0
/舵机程序/
time++;
i++;
if(time>2000)
{
time=0;
time1++;
}
#endif
/ USER CODE END TIM2_IRQn 0 /
HAL_TIM_IRQHandler(&htim2);
/ USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}