#include "stm32f10x.h" void RCC_Config(void); void GPIO_Config(void); void TIM2_2_Config(void); void TIM3_Config(void); void delay_ms(int a); int main() { RCC_Config(); TIM2_2_Config(); TIM3_Config(); GPIO_Config(); u16 ledpwmval=0;u8 dir=1; while(1) { //u16 ledpwmval=0;u8 dir=1; if(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update)==SET) { TIM_ClearFlag(TIM3, TIM_FLAG_Update); GPIOC->ODR=~GPIO_ReadOutputData(GPIOC); } delay_ms(50); if(dir)ledpwmval++; else ledpwmval--; if(ledpwmval>200)dir=0; if(ledpwmval==0)dir=1; TIM_SetCompare2(TIM2,ledpwmval); } } void delay_ms(int a) { while(a--); } void RCC_Config(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); } void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_SetBits(GPIOC,GPIO_Pin_13); GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); } void TIM2_2_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_Prescaler=0; TIM_TimeBaseInitStructure.TIM_Period=899; TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE); /*------------------------------------------------------------------------*/ // NVIC_InitTypeDef NVIC_InitStructure; // NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; // NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; // NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; // NVIC_Init(&NVIC_InitStructure); /*------------------------------------------------------------------------*/ TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState=ENABLE; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; TIM_OC2Init(TIM2,&TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM2,ENABLE); TIM_Cmd(TIM2, ENABLE); } //void TIM2_IRQHandler(void) //{ // if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) // { // TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); // GPIOA->ODR=~GPIO_ReadOutputData(GPIOA); // } //// GPIOA->ODR=~GPIO_ReadOutputData(GPIOA); //} void TIM3_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_Prescaler=7199; TIM_TimeBaseInitStructure.TIM_Period=10000; TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); TIM_ARRPreloadConfig(TIM3,ENABLE); TIM_Cmd(TIM3, ENABLE); } 不知道问题出在哪里,,占空比一直不变,, |
评分
查看全部评分
评分
查看全部评分
兄弟,如果你要用PWM直接CubeMX設定一下。
開頭跑一下
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
要變更duty就用instance直接操作暫存器比較快
htim2.Instance->CCR1 = duty;
要變週期就
htim2.Instance->ARR = period;
用HAL,要直接操作暫存器也是很快的。
评分
查看全部评分