基于STM32F103C8T6单片机做的控制舵机的程序,用PWM精准驱动
程序如下:
- /***************STM32F103C8T6**********************
- * 文件名 :main.c
- * 描述 : 舵机测试
- * 实验平台:STM32F103C8T6
- * 备注 :使用定时器来产生pwm波,控制舵机动作
- * 接口 :TIM1, CH1-PB13, CH2-PB14, CH3-PB15
- ****************STM32F103C8T6**********************/
- #include "pwm_output.h"
- /* 配置TIM1复用输出PWM时用到的I/O */
- static void TIM1_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//定时器1时钟使能
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//GPIOB时钟使能
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- /*配置TIM1输出的PWM信号的模式,如周期、极性、占空比 */
- static void TIM1_Mode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* PWM信号电平跳变值 */
- u16 CCR1_Val = 0;
- u16 CCR2_Val = 0;
- //u16 CCR3_Val = 50000;
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 20000 - 1;//定时器周期,20ms
- TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;//时钟预分频
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数方式:向上计数
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- /* PWM1 Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//选择定时器模式:tim脉宽调制2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//极性
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//开反向通道
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel2 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//开反向通道
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC2Init(TIM1, &TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
- /* PWM1 Mode configuration: Channel3 */
- /*
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;//开反向通道
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC3Init(TIM1, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); */
- TIM_ARRPreloadConfig(TIM1, ENABLE);
- /* TIM1 enable counter */
- TIM_Cmd(TIM1, ENABLE);
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
- }
- /*配置和初始化TIM1 */
- void TIM1_PWM_Init(void)
- {
- TIM1_GPIO_Config();
- TIM1_Mode_Config();
- }
- //粗略延时 不精确!
- void Delay_us(unsigned int i)
- {
- unsigned char t = 0;
- for(;i>0;i--)
- {
- for(t=0;t<10;t++)
- {
- }
- }
- }
- void Delay_ms(unsigned int i)
- {
- for(;i>0;i--)
- {
- Delay_us(1000);
- }
- }
- //舵机摆动
- void Servo_out (void)
- {
- TIM1->CCR1 = 2000;//pwm波高电平持续时间,2ms
- Delay_ms(2000);
- while(1)
- {
- TIM1->CCR1 = 1500;
- Delay_ms(1000);
- TIM1->CCR1 = 2500;
- Delay_ms(1000);
- }
- }
复制代码
|