#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();
        
        
    }
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐