簡體   English   中英

STM32F4-PWM輸出以控制伺服

[英]STM32F4 - PWM output to control servo

因此,我嘗試使用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();
    }
}

如果配置TIM3但修改TIM2這是很正常的。

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

您已通過設置以下值將輸出配置為定時器3的通道2

TIM3->CCER    |= 0x01; 

對於CCER寄存器的該值,PWM輸出將位於PB8引腳上。

要獲得PC6引腳上的輸出,請設置值:

TIM3->CCER |= 0x1000;

並且,根據文檔,TIM3的通道1連接到引腳PA6,PB4,PC6。 檢查這三個引腳上的PWM輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM