简体   繁体   English

STM32F4-PWM输出以控制伺服

[英]STM32F4 - PWM output to control servo

So I'm trying to move a servo with an STM32F4 discovery board. 因此,我尝试使用STM32F4发现板移动伺服器。 My code is below. 我的代码如下。

As far as I can see, everything is set up correctly, but I'm not getting any output on pin PC6. 据我所知,一切都已正确设置,但PC6引脚上没有任何输出。 Can anyone spot what I've done wrong/point me in the right direction? 谁能发现我做错了什么/为我指明正确的方向?

Thanks! 谢谢!

#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();
    }
}

It's pretty normal if you configure TIM3 but modify TIM2 ... 如果配置TIM3但修改TIM2这是很正常的。

TIM3->CR1       |= 0x81;
...
TIM2->CCR1 |= 0x28A;

You have configured the output to channel 2 of Timer 3 by setting the value: 您已通过设置以下值将输出配置为定时器3的通道2

TIM3->CCER    |= 0x01; 

for this value of CCER register, the PWM outptut will be on pin PB8 . 对于CCER寄存器的该值,PWM输出将位于PB8引脚上。

To get the output on pin PC6 , set the value: 要获得PC6引脚上的输出,请设置值:

TIM3->CCER |= 0x1000;

and also, according to the documentation, channel 1 of TIM3 is connected to pins PA6, PB4, PC6. 并且,根据文档,TIM3的通道1连接到引脚PA6,PB4,PC6。 Check for PWM output on these three pins. 检查这三个引脚上的PWM输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM