#include "stm32f10x.h"
#include "Delay.h"
#include "stm32f10x_tim.h"
//#include "Encoder.h"
//#include "OLED.h"
static u16 i=0;
u8 Flag=1;
void TIMCH_UserConfig(u16 Period,u16 Prescaler ){
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_InitStructure;
TIM_OCInitTypeDef PWM_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_InitStructure.TIM_Period=Period; //重载值
TIM_InitStructure.TIM_Prescaler=Prescaler; //预分频
TIM_InitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_InitStructure.TIM_RepetitionCounter=DISABLE;
TIM_InitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_InitStructure);
PWM_InitStructure.TIM_OCMode=TIM_OCMode_PWM1;
PWM_InitStructure.TIM_OutputState=TIM_OutputState_Enable;
PWM_InitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM2,&PWM_InitStructure);
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_Cmd(TIM2,ENABLE);
}
void TIMCH_PWM(void){
if(Flag==1)
{
i++;
}
else
{
i--;
}
if(i>899)Flag=0;
if(i==0)Flag=1;
TIM_SetCompare1(TIM2,i);
}
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "Encoder.h"
#include "timch.h"
#include "LED.h "
#include "stm32f10x_tim.h"
int16_t Num;
int main(void)
{
SystemInit();
TIMCH_UserConfig(899,0 );
while (1)
{
TIMCH_PWM();
}
}
版权声明:本文为CSDN博主「wbx1123」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wbx1123/article/details/121944734
暂无评论