STM32F4 - PWM输出控制伺服

时间:2014-11-25 14:22:29

标签: c arm stm32

所以我试图用STM32F4发现板移动伺服器。我的代码如下。

据我所见,一切都设置正确,但我没有在PC6引脚上获得任何输出。任何人都可以发现我做错了什么/指向正确的方向吗?

谢谢!

#include <stm32f4xx.h>
//#include "stm32f4xx_tim.h"

#define  RCC_APB1ENR_TIM3EN          ((uint32_t)0x00000002)


void delay (void)                   //create simple delay loop
{
int d;
for (d=0; d<10000000; d++);
}

int main (void)
{

    RCC->APB1ENR     = RCC_APB1ENR_TIM3EN;  //enable timer 3
TIM3->CR1       |= 0x81;                    //enable timer 1 = 10000001
//TIM3->CR2         |= 0x40;                                    //                              = 01000000
TIM3->PSC        = 0x48;                                //set prescale to 72
TIM3->ARR        = 0x4E20;                          //set auto reload to 20000
TIM3->CCER    |= 0x01;                              //set timer to output
TIM3->CCMR1     |= 0x68;                                //Set PWM mode 1 = 01101000


//timer 3 now set to 50hz

RCC->AHB1ENR        |= 0x05;                        //IO Port A and C clock enable  = 00000101

GPIOC->MODER        |= 0x400;                   //set PC6 as alternate function = 0000 0100 0000 0000
GPIOC->AFR[0]      = 0x02000000;            //Set AF to timer 3 = 0000 0010 0000 0000 0000 0000 0000 0000
GPIOC->OTYPER    = 0;                           //Set output as Push-Pull mode
GPIOC->OSPEEDR   = 0;                           //Set output speed 2MHz low speed
GPIOC->PUPDR       = 0;                             //Set no pull up and pull down

GPIOA->MODER        &= 0xfffffffc;          // Set Port a Bit 0 Active input
GPIOA->OTYPER    = 0;                           //Set output as Push-Pull mode
GPIOA->OSPEEDR   = 0;                           //Set output speed 2MHz low speed
GPIOA->PUPDR       = 0;                             //Set no pull up and pull down




while(1)
    {
            TIM2->CCR1 |= 0x28A;            //650us pulses
            delay();
            TIM2->CCR1 |= 0x73A;                //1850us pulses
            delay();
    }
}

2 个答案:

答案 0 :(得分:0)

如果您配置TIM3但修改TIM2 ...

,这是很正常的
TIM3->CR1       |= 0x81;
...
TIM2->CCR1 |= 0x28A;

答案 1 :(得分:0)

您已通过设置值将输出配置为定时器3的通道2

TIM3->CCER    |= 0x01; 

对于CCER寄存器的该值,PWM输出将在引脚 PB8 上。

要在 PC6 引脚上输出,请设置值:

TIM3->CCER |= 0x1000;

并且根据文档,TIM3的通道1连接到引脚 PA6,PB4,PC6。检查这三个引脚的PWM输出。