我现在在做一个
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);
}