各位帮我看下这个程序哪里有问题,实现不了pwm输出。
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_exti.h"
#include "system_stm32f10x.h"
#include "misc.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIMER_cfg(void);
void PWM_cfg(void);
int main()
{
RCC_Configuration();
GPIO_Configuration();
TIMER_cfg();
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_exti.h"
#include "system_stm32f10x.h"
#include "misc.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIMER_cfg(void);
void PWM_cfg(void);
int main()
{
RCC_Configuration();
GPIO_Configuration();
TIMER_cfg();
PWM_cfg();
TIM_SetCompare2(TIM3,20);
while(1)
{
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
void TIMER_cfg(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM3);
TIM_InternalClockConfig(TIM3);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 72 - 1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
}
void PWM_cfg(void)
{
TIM_OCInitTypeDef TimOCInitStructure;
TIM_OCStructInit(&TimOCInitStructure);
TimOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TimOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TimOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC2Init(TIM3, &TimOCInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM3,ENABLE);
}
一周热门 更多>