高中生
最后登录1970-1-1
在线时间 小时
注册时间2014-3-29
|
- #include "bsp_pwm_output.h"
- /*
- 1号舵机 PD12
- 2号舵机 PD13
- 3号舵机 PD14
- */
- static void TIM4_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 设置TIM3CLK 为 72MHZ */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- }
- static void TIM4_Mode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- TIM_TimeBaseStructure.TIM_Period = 19999;
- TIM_TimeBaseStructure.TIM_Prescaler = 71; //设置预分频:不预分频,即为72MHz
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频(这里用不到)
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
- }
- void Steering_1(int a)
- {
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = a; //设置跳变值,当计数器计数到这个值时,电平发生跳变
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
- TIM_OC1Init(TIM4, &TIM_OCInitStructure); //使能通道1
- TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_Cmd(TIM4, ENABLE);
- }
- void Steering_2(int b)
- {
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse =b; //设置通道2的电平跳变值,输出另外一个占空比的PWM
- TIM_OC2Init(TIM4, &TIM_OCInitStructure); //使能通道2
- TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_Cmd(TIM4, ENABLE);
- }
- void Steering_3(int c)
- {
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse =c; //设置通道3的电平跳变值,输出另外一个占空比的PWM
- TIM_OC3Init(TIM4, &TIM_OCInitStructure); //使能通道3
- TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
- TIM_Cmd(TIM4, ENABLE);
- }
- void TIM4_PWM_Init(void)
- {
- TIM4_GPIO_Config();
- TIM4_Mode_Config();
- }
复制代码 要控制机械臂,所以把光盘里面的PWM方波输出的程序改完后,很满意。项目中还需要NRF无线通信,发现TIM3的PA6,PA7同时也是SPI1的接口,于是打算换成定时器4的通道输出PWM方波控制3个舵机,在看了下原理图,打算使用PD12,PD13,PD14三个GPIO口作为舵机的信号输出,把光盘的程序改成TIM4的,编译通过,仿真不输出波形,查看了每一项的配置,没问题呀,火哥快来呀,下周就交中期检查报告了 |
|