初中生
最后登录1970-1-1
在线时间 小时
注册时间2017-1-24
|
楼主 |
发表于 2017-1-30 21:36:46
|
显示全部楼层
谢谢,我自己研究了下,可是舵机转不了,可以帮忙看看吗?
/**
******************************************************************************
* @file main.c
* @author fire
* @version V1.0
* @date 2015-xx-xx
* @brief 用1.5.1版本库建的工程模板
******************************************************************************
* @attention
*
* 实验平台:野火 STM32 F429 开发板
* 论坛 :http://www.firebbs.cn
* 淘宝 :http://firestm32.taobao.com
*
******************************************************************************
*/
#include "stm32f4xx.h"
/**
* @brief 主函数
* @param 无
* @retval 无
*/
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; //TIM3的1、2通道 ,产生PWM
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //PA的1,2口对应TIM3的1、2通道,设置为复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; //TIM4的1、2、3、4通道
GPIO_Init(GPIOB,&GPIO_InitStructure); //B口的6,7,8,9对应TIM4的1、2、3、4通道,设置为复用推挽输出
TIM_TimeBaseStructure.TIM_Period =9999; //自动重载周期值
TIM_TimeBaseStructure.TIM_Prescaler =71; //预分频值 ,这里是50HZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数摸式为向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //TIM3,和TIM4用的相同配置,写入配置 ,PWM频率为50HZ
//设定占空比
TIM_OCStructInit(& TIM_OCInitStructure); //恢复初始
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //定时器模式为pwm模式1
TIM_OCInitStructure.TIM_Pulse =0; //脉冲值,即输出都是低电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性为高
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //将配置数据写入TIM3的通道1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //预装载使能
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);//TIM4的4个通道都用相同的配置
TIM_Cmd(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4, ENABLE); //使能PWM模式
}
void Delay(__IO uint32_t nCount) //简单的延时函数
{
for(; nCount != 0; nCount--);
}
int main(void)
{
/* 程序来到main函数之前,启动文件:statup_stm32f429xx.s已经调用
* SystemInit()函数把系统时钟初始化成180MHZ
* SystemInit()在system_stm32f4xx.c中定义
* 如果用户想修改系统时钟,可自行编写程序修改
*/
/* add your code here ^_^. */
TIM_Configuration();
TIM_SetCompare1(TIM3,500);
Delay(100);
TIM_SetCompare2(TIM3,600);
Delay(100);
while(1);
{
}
}
|
|