赞
踩
头文件
- #ifndef __tim6_H
- #define __tim6_H
- #include "sys.h"
-
- #define TIM6_PWM_T 50
-
-
-
- //LED端口定义
- #define T_PORT1 PBout(12) // DS0
- #define T_PORT2 PBout(13) // DS1
-
-
- void TIM6_Pwm_Init(void);
-
-
- void Servo_Set_Angle(u8 channel , float angle);
-
- #endif
定时器代码:
- #include "tim6.h"
-
- /*
- 普通定时器中断功能生成舵机驱动波形
- */
-
-
-
- void Servo_Gpio_Init(){
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOF时钟
-
- //GPIOF9,F10初始化设置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
- GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
- }
-
-
-
- void TIM6_Pwm_Init()
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE); ///使能TIM7时钟
-
- TIM_TimeBaseInitStructure.TIM_Period = TIM6_PWM_T-1; //自动重装载值
- TIM_TimeBaseInitStructure.TIM_Prescaler=84-1; //定时器分频
- TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
-
- TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStructure);//初始化TIM7
-
- TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE); //允许定时器7更新中断
- TIM_Cmd(TIM6,ENABLE); //使能定时器3
-
- NVIC_InitStructure.NVIC_IRQChannel=TIM6_DAC_IRQn; //定时器3中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x04; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- Servo_Gpio_Init();
- }
-
-
- u8 time6_duty1 = 15;
- u8 time6_duty2 = 10000/TIM6_PWM_T;
-
- /*
- 理论上可以给任意引脚输出pwm舵机控制信号,此地pwm频率固定为50HZ
- */
-
- void TIM6_DAC_IRQHandler(void)
- {
- static u16 i=0;
- if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)
- {
- // 在这里添加你的代码逻辑
- i++;
- if(i<=time6_duty1) T_PORT1=1;
- else T_PORT1=0;
- if(i<=time6_duty2) T_PORT2=1;
- else T_PORT2=0;
- if(i==20000/TIM6_PWM_T) i=0;
- TIM_ClearITPendingBit(TIM6, TIM_IT_Update); // 清除中断标志位
- }
- }
-
- /*
- 函数功能:引脚输出舵机控制信号
- 参数: channel->通道 angle->角度
- 返回值:无
- */
- void Servo_Set_Angle(u8 channel , float angle){
- angle = 1500/TIM6_PWM_T+1000/TIM6_PWM_T/90.0*angle;
- if(channel==0){
- time6_duty1 = angle;
- }
- if(channel==1){
- time6_duty2 = angle;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。