打印
[STM32F0]

求教,STM32F030的PA6,PA7作PWM输出的设置问题

[复制链接]
14677|22
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
beamart|  楼主 | 2014-8-25 22:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void PWM_Init(void)
{       
        GPIO_InitTypeDef         GPIO_InitStructure;  
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef        TIM_OCInitStructure;

        // TIM3参数设置
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

       

        // 配置GPIO管脚参数设置
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
        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(GPIOA, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
        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(GPIOA, GPIO_PinSource6, GPIO_AF_1); // GPIO管脚复用设置
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_1);
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);
         
        TIM_TimeBaseStructure.TIM_Period = 2250;                    //自动重装载值,取值必须在0x0000~0xFFFF之间
        TIM_TimeBaseStructure.TIM_Prescaler =0;                     //预分频值,取值必须在0x0000~0xFFFF之间,+1为分频系数                                                                                                 
        TIM_TimeBaseStructure.TIM_ClockDivision = 0;                                 //时钟分割
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;        //向上计数模式       
        TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);                         //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位                

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;             //TIM脉冲宽度调制模式1
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
        TIM_OCInitStructure.TIM_Pulse =0;                           //设置待装入捕获比较寄存器的脉冲值,取值必须在0x0000~0xFFFF之间,占空比101/1001
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;     //输出极性:TIM输出比较极性高       
        TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;       
       
        TIM_OC1Init(TIM3, &TIM_OCInitStructure);          //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
        TIM_OC2Init(TIM3, &TIM_OCInitStructure);
        TIM_OC3Init(TIM3, &TIM_OCInitStructure);
        TIM_OC4Init(TIM3, &TIM_OCInitStructure);  
        TIM_Cmd(TIM3, ENABLE);
        TIM_CtrlPWMOutputs(TIM3, ENABLE);
}
这段程序的PWM输出CH3,CH4没有问题,但是CH1,CH2的PA6,PA7却得不到正确的输出,问题出在哪儿?
沙发
beamart|  楼主 | 2014-8-25 23:31 | 只看该作者
自己顶

使用特权

评论回复
板凳
21ID| | 2014-8-26 10:35 | 只看该作者
复用时钟开启了吗

使用特权

评论回复
地板
beamart|  楼主 | 2014-8-26 12:12 | 只看该作者
21ID 发表于 2014-8-26 10:35
复用时钟开启了吗

哪个复用时钟?如何开启?能说一下吗?

使用特权

评论回复
5
21ID| | 2014-8-27 11:11 | 只看该作者
beamart 发表于 2014-8-26 12:12
哪个复用时钟?如何开启?能说一下吗?

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;      // 分配系数
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  // 向上计数模式
TIM_TimeBaseStructure.TIM_Period = PWM.TimerPeriod;    // PWM 周期
TIM_TimeBaseStructure.TIM_ClockDivision = 0;     
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

你可以参考一下

使用特权

评论回复
6
gurong60| | 2014-12-4 11:00 | 只看该作者
本帖最后由 gurong60 于 2014-12-4 11:01 编辑

@香水城 @grant_jx @mmuuss586 @icecut
各位版主来看看,这个问题不知道哪里了   

使用特权

评论回复
7
icecut| | 2014-12-4 11:25 | 只看该作者
看5楼

使用特权

评论回复
8
gurong60| | 2014-12-4 11:33 | 只看该作者
icecut 发表于 2014-12-4 11:25
看5楼

stm32f030里面没有RCC_APB2Periph_AFIO这个定义

使用特权

评论回复
9
gurong60| | 2014-12-4 12:16 | 只看该作者
icecut 发表于 2014-12-4 11:25
看5楼

GPIO_PinAFConfig复用只需要配置这个函数,复用时钟是没有的

使用特权

评论回复
10
mmuuss586| | 2014-12-4 12:27 | 只看该作者
3,4有输出,1,2没输出;
说明PWM初始化没问题,也许引脚配置问题;

使用特权

评论回复
11
mmuuss586| | 2014-12-4 12:35 | 只看该作者
/**
  * @brief  Configure the TIM1 Pins.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* GPIOA Clocks enable */
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
  
  /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | 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(GPIOA, &GPIO_InitStructure);
  
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
  
  /* TIM1 Configuration ---------------------------------------------------
   Generate PWM signals with 4 different duty cycles:
   TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2)   
    => TIM1CLK = PCLK2 = SystemCoreClock
   TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
   SystemCoreClock is set to 48 MHz for STM32F0xx devices
   
   The objective is to generate 4 PWM signal at 17.57 KHz:
     - TIM1_Period = (SystemCoreClock / 17570) - 1
   The channel 1 and channel 1N duty cycle is set to 50%
   The channel 2 and channel 2N duty cycle is set to 37.5%
   The channel 3 and channel 3N duty cycle is set to 25%
   The channel 4 duty cycle is set to 12.5%
   The Timer pulse is calculated as follows:
     - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
   
   Note:
    SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
    Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
    function to update SystemCoreClock variable value. Otherwise, any configuration
    based on this variable will be incorrect.
  ----------------------------------------------------------------------- */
  /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
  TimerPeriod = (SystemCoreClock / 17570 ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
  Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
  /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 */
  Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
  /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 */
  Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
  /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
  Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);

  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
  
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
  TIM_OC4Init(TIM1, &TIM_OCInitStructure);

  /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);

  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

使用特权

评论回复
12
mmuuss586| | 2014-12-4 12:35 | 只看该作者
按官方的例程改下看看;

使用特权

评论回复
13
gurong60| | 2014-12-4 13:18 | 只看该作者
本帖最后由 gurong60 于 2014-12-4 14:06 编辑
mmuuss586 发表于 2014-12-4 12:35
按官方的例程改下看看;


就是按官方例程改的

使用特权

评论回复
14
fanxsd| | 2014-12-4 13:44 | 只看该作者
根据官方的例程修改啊!

使用特权

评论回复
15
icecut| | 2014-12-4 17:30 | 只看该作者
gurong60 发表于 2014-12-4 13:18
就是按官方例程改的

你看明白再改.别乱删

使用特权

评论回复
16
gurong60| | 2014-12-4 17:40 | 只看该作者
icecut 发表于 2014-12-4 17:30
你看明白再改.别乱删

看的不够仔细,问题在这里
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
我直接替换上面的RCC_APB2Periph_TIM1,实际前面也得改,总线时钟不是apb2

使用特权

评论回复
17
icecut| | 2014-12-4 17:51 | 只看该作者
gurong60 发表于 2014-12-4 17:40
看的不够仔细,问题在这里
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
我直接替换上面的RCC ...

恩.细心就好

使用特权

评论回复
18
mmuuss586| | 2014-12-4 17:55 | 只看该作者
gurong60 发表于 2014-12-4 13:18
就是按官方例程改的

改成AF2,PWM1试试看;
主要是手头没有030的板子,要不然可以帮你测试下;

使用特权

评论回复
19
gurong60| | 2014-12-4 18:04 | 只看该作者
mmuuss586 发表于 2014-12-4 17:55
改成AF2,PWM1试试看;
主要是手头没有030的板子,要不然可以帮你测试下; ...

解决了,谢谢,

使用特权

评论回复
20
mmuuss586| | 2014-12-4 20:11 | 只看该作者
gurong60 发表于 2014-12-4 18:04
解决了,谢谢,

:loveliness:
也不说怎么解决的;

使用特权

评论回复
评论
gurong60 2014-12-4 20:29 回复TA
16楼 
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

10

帖子

0

粉丝