void CalculateSModelLine(float fre[], unsigned short period[], float len, float fre_max, float fre_min, float flexible)
{
int i=0;
float deno ;
float melo ;
float delt = fre_max-fre_min;
for(; i<len; i++)
{
melo = flexible * (i-len/2) / (len/2);
deno = 1.0 / (1 + expf(-melo)); //expf is a library function of exponential(e)
fre[i] = delt * deno + fre_min;
period[i] = (unsigned short)(1000000.0 / fre[i]); // 10000000 is the timer driver frequency
}
return ;
}
//主函数
int main(void)
{
u8 t;
delay_init();
NVIC_Config();
LED_Init(); //初始化IO口
TIM1_Int_Init(10,72); //设置定时器的频率等
//PB8 9 12 13
//KEY_Init();
//SMG_io_init();
CalculateSModelLine(fre,period,ACCELERATED_SPEED_LENGTH,FRE_MAX,FRE_MIN,4);
TIM_Cmd(TIM1, ENABLE);
while(1)
{
}
}
定时器初始化
void TIM1_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1); /*¸´Î»TIM1¶¨Ê±Æ÷*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /*¿ªÊ±ÖÓ*/
TIM_TimeBaseStructure.TIM_Period = arr; /*ʱÖӵδðµÄ´ÎÊý£¬¹»ÊýÖжÏÕâÀïÊÇ1msÖжÏÒ»´Î*/
TIM_TimeBaseStructure.TIM_Prescaler = psc; /* ·ÖƵ720*/
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /*¼ÆÊý·½ÏòÏòÉϼÆÊý*/
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; //²»Ê¹ÓÃNµÄÊä³ö£¬ËùÒÔÒª¹Øµô
TIM_OCInitStructure.TIM_Pulse = 0; //µÚÒ»´ÎûÓÐÊä³öÂö³å
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* Clear TIM1 update pending flag Çå³ýTIM1Òç³öÖжϱêÖ¾] */
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; /*Òç³öÖжÏ*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* TIM1 counter enable */
TIM_Cmd(TIM1, DISABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
}
//初始化定时器IO
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
//设置PA11为复用推挽输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Âö³å¿Ú+·½Ïò
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* TIM1 Full remapping pins */
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //打开重映射
}
|
———————————————— 版权声明:本文为CSDN博主「你熊哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_40567368/article/details/81742938