2.tim.c
#include "tim.h"
#include "bsp_usart.h"
long current_pos[2] = {0,0};
int motor_dir2 = 0;
u8 count[2] = {0,0};
u32 motor_speed = 0;
u8 rcr_remainder; //重复计数余数部分
u8 is_rcr_finish = 1; //重复计数器是否设置完成
long rcr_integer; //重复计数整数部分
long target_pos = 0; //有符号方向
/* 电机转速与方向函数 */
void Locate_Rle2(u32 frequency,DIR_Type dir)
{
motor_dir2 = dir;
DRIVER_DIR = motor_dir2;
if(motor_dir2 == CW){motor_speed = 3000;}/* motor_dir2高电平时转速3000Hz */
else if(motor_dir2 == CCW){motor_speed = 10000;}/* motor_dir2低电平时转速10000Hz */
frequency = motor_speed;
if(TIM2->CR1&0x01)
{
printf("\r\nThe last time pulses is not send finished,wait please!\r\n");
return;
}
if((frequency<20)||(frequency>100000))
{
printf("\r\nThe frequency is out of range! please reset it!!(range:20Hz~100KHz)\r\n");
return;
}
TIM2_Startup(frequency);
}
/* PA8 - DIR+*/
void Driver_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
}
/* PA1- PUL+ TIM2_CH2 */
void TIM2_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular);
TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = arr>>1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update ,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_Cmd(TIM2, DISABLE);
}
void TIM2_Startup(u32 frequency)
{
u16 temp_arr=1000000/frequency-1;
TIM_SetAutoreload(TIM2,temp_arr);
TIM_SetCompare2(TIM2,temp_arr>>1);
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE);
}
|
———————————————— 版权声明:本文为CSDN博主「小昌快跑」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_44690557/article/details/125607583