STM32F4 - PWM输出控制伺服
问题描述:
所以我试图用STM32F4发现板移动伺服。我的代码如下。STM32F4 - PWM输出控制伺服
据我所见,一切都设置正确,但我没有得到任何输出引脚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;
+0
哎呀!修正了,仍然没有去:/ – 2014-11-25 20:59:25
答
你的输出配置为通道2定时器3的设定值:
TIM3->CCER |= 0x01;
对于CCER寄存器的这个值,PWM输出将在引脚PB8。
要获得销PC6输出,设定值:
TIM3->CCER |= 0x1000;
并且还,根据文档,TIM3的信道1连接到销 PA6,PB4,PC6。检查这三个引脚的PWM输出。
如果你必须定义'#define RCC_APB1ENR_TIM3EN((uint32_t)0x00000002)',那么你肯定会做一些非常错误的事情。其余的我没有阅读,头文件中有大量的#define,正好放在那里_NOT_用这些0x05,0x02000000等杀死其他人。 – 2014-11-25 15:16:45
这就是我们在嵌入式编程模块中教授的方式,当我将我的代码发送给我的项目主管时,他就是这样纠正它的。 – 2014-11-25 21:01:07