我现在在做一个STM32F0的实验,板子的电路图如下,当TIM2开启输出PWM波形后,OLED会变暗,别外一个板子还直接重启,是不是我哪里没有配置好的?找了好久一直没有找到原因,代码和电路图如下: uint32_t ohm_freq = 238000; uint16_t ohm_percent = 500; void OHM_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //Charge_PWM GPIO Port GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_2); //DIO_Ctrl_Pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_ResetBits(GPIOB, GPIO_Pin_14); } void OHM_TIM_PWM_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; uint32_t TimerPeriod = 0; uint32_t ChannelPulse = 0; TimerPeriod = (SystemCoreClock/ohm_freq)-1; ChannelPulse = ohm_percent*(TimerPeriod - 1)/1000; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频系数(48-1), t=48/48000000=1us, 即TIMx->CNT每1us加一次 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseStructure.TIM_Period = TimerPeriod;//TimerPeriod; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_Pulse = ChannelPulse;//ChannelPulse;; //TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC4Init(TIM2, &TIM_OCInitStructure); //放到Enable函数里面 //TIM_Cmd(TIM2, ENABLE); //TIM_CtrlPWMOutputs(TIM2, ENABLE); } void OHM_Chk_Timer_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM16_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03; NVIC_Init(&NVIC_InitStructure); TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 50; //50ms TIM_TimeBaseStructure.TIM_Prescaler = 47999; //分频,1ms计数一次 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x03; TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure); TIM_ClearITPendingBit(TIM16, TIM_IT_Update); TIM_ITConfig(TIM16, TIM_IT_Update, ENABLE); TIM_Cmd(TIM16, ENABLE); } void OHM_Init(void) { OHM_GPIO_Init(); OHM_TIM_PWM_Init(); OHM_Chk_Timer_Init(); } void OHM_Start(void) { TIM_Cmd(TIM2, ENABLE); TIM_CtrlPWMOutputs(TIM2, ENABLE); GPIO_SetBits(GPIOB, GPIO_Pin_14); } void OHM_Stop(void) { GPIO_ResetBits(GPIOB, GPIO_Pin_14); TIM_CtrlPWMOutputs(TIM2, DISABLE); TIM_Cmd(TIM2, DISABLE); } |
é¨åçµè·¯å¾
stlink V3mini 调试器不能识别stm32f030F4P6芯片,有解决办法吗?
STM32F0 SMBus 中断怎么开启
STM32F072 无法清除EXTI4_15_IRQn pending标志,导致无法进入stop模式
基于GCC编译器的initcall在STM32如何实现?
想找一款pin to pin的MCU(参考STM32F105RC),支持CAN FD的,64PIN封装,各位小伙伴们有推荐吗
L9663如何使用上行缓冲区发送同步脉冲
如何使用定时器在任意IO输出PWM
下载到flash无法运行,开调试就能成功,是什么问题?
STM32是小端模式,但为什么Keil中memory窗口和反汇编窗口的32位指令不一致?
各位朋友,有没有STM32F0系列教程,最好是汇编语言的。
评分
查看全部评分
评分
查看全部评分