本人刚开始入手pwm,看到给的定时器内容很多,由于时间紧,有些地方没大看明白,写了个程序,但是不知道哪里错了,也不知道步骤对不对,请前辈指点,本人菜鸟,请耐心谢谢
程序如下:
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
void IO_config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void TIM_config(void)
{
TIM_TimeBaseInitTypeDef timInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
timInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 使用系统基础时钟
timInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
timInitStruct.TIM_RepetitionCounter = 0;
timInitStruct.TIM_Prescaler = 0; // 不再对时钟预分频了
timInitStruct.TIM_Period = 999; // 用计数器来控制通道的输出频率
TIM_TimeBaseInit(TIM3, &timInitStruct); //基本初始化
}
void PWM_config(void)
{
TIM_OCInitTypeDef timOCInitStructure;
timOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // 输出模式
timOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
timOCInitStructure.TIM_Pulse = 500; // 占空比参数
timOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &timOCInitStructure);
// TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
}
int main(void)
{
// GPIOA->CRL&=0X0FFFFFFF;//PA7输出
//GPIOA->CRL|=0XB0000000;//复用功能输出
//SystemInit();
IO_config();
TIM_config();
PWM_config();
TIM_ARRPreloadConfig(TIM3, ENABLE); // 这个记得要开
TIM_Cmd(TIM3, ENABLE);
while(1);
}
还有函数库中的TIM_OCInitTypeDef 结构体定义中,没有定义 uint16_t TIM_Channel; 这个函数定义的是否有问题,求解,下面是TIM_OCInitTypeDef 结构体
typedef struct
{
uint16_t TIM_OCMode; /*!< Specifies the TIM mode.
This parameter can be a value of @ref TIM_Output_Compare_and_PWM_modes */
uint16_t TIM_OutputState; /*!< Specifies the TIM Output Compare state.
This parameter can be a value of @ref TIM_Output_Compare_state */
uint16_t TIM_OutputNState; /*!< Specifies the TIM complementary Output Compare state.
This parameter can be a value of @ref TIM_Output_Compare_N_state
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_Pulse; /*!< Specifies the pulse value to be loaded into the Capture Compare Register.
This parameter can be a number between 0x0000 and 0xFFFF */
uint16_t TIM_OCPolarity; /*!< Specifies the output polarity.
This parameter can be a value of @ref TIM_Output_Compare_Polarity */
uint16_t TIM_OCNPolarity; /*!< Specifies the complementary output polarity.
This parameter can be a value of @ref TIM_Output_Compare_N_Polarity
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
uint16_t TIM_OCNIdleState; /*!< Specifies the TIM Output Compare pin state during Idle state.
This parameter can be a value of @ref TIM_Output_Compare_N_Idle_State
@note This parameter is valid only for TIM1 and TIM8. */
} TIM_OCInitTypeDef;
此帖出自
小平头技术问答
void IO_config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void TIM_config(void)
{
TIM_TimeBaseInitTypeDef timInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
timInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 使用系统基础时钟
timInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
timInitStruct.TIM_RepetitionCounter = 0;
timInitStruct.TIM_Prescaler = 71; // 不再对时钟预分频了
timInitStruct.TIM_Period = 999; // 用计数器来控制通道的输出频率
//TIM_TimeBaseInit(TIM3, &timInitStruct); //基本初始化 //??????????????????????????????
}
void PWM_config(void)
{
TIM_OCInitTypeDef timOCInitStructure;
timOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // 输出模式
timOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
timOCInitStructure.TIM_Pulse = 30000; // 占空比参数
timOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &timOCInitStructure);
// TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
}
int main(void)
{
// GPIOA->CRL&=0X0FFFFFFF;//PA7输出
//GPIOA->CRL|=0XB0000000;//复用功能输出
//SystemInit();
IO_config();
TIM_config();
PWM_config();
//TIM_ARRPreloadConfig(TIM3, ENABLE); // 这个记得要开
TIM_Cmd(TIM3, ENABLE);
// while(1);
{
GPIO_InitTypeDef GPIO_InitStruct;
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void TIM_config(void)
{
TIM_TimeBaseInitTypeDef timInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
timInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 使用系统基础时钟
timInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
timInitStruct.TIM_RepetitionCounter = 0;
timInitStruct.TIM_Prescaler = 71; // 不再对时钟预分频了 Prescaler 给了psc
timInitStruct.TIM_Period = 999; // 用计数器来控制通道的输出频率 PERIOD 的值给了ARR
TIM_TimeBaseInit(TIM3, &timInitStruct); //基本初始化 //??????????????????????????????
}
void PWM_config(void)
{
TIM_OCInitTypeDef timOCInitStructure;
timOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // 输出模式
timOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
timOCInitStructure.TIM_Pulse = 500; // 占空比参数
timOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &timOCInitStructure);
// TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
}
int main(void)
{
// GPIOA->CRL&=0X0FFFFFFF;//PA7输出
//GPIOA->CRL|=0XB0000000;//复用功能输出
//SystemInit();
IO_config();
TIM_config();
PWM_config();
//TIM_ARRPreloadConfig(TIM3, ENABLE); // 这个记得要开
TIM_Cmd(TIM3, ENABLE);
// while(1);
}题,重新贴一下程序吧
一周热门 更多>