赞
踩
注意:
1、供电:舵机需要至少 5.0V 供电;
2、使用TIM2,舵机A1。
直接上代码:
Timer.c:
- #include "stm32f10x.h" // Device header
- #include "Timer.h"
- void Timer_Init(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- TIM_TimeBaseInitStructure.TIM_Period = 50000+1;
- TIM_TimeBaseInitStructure.TIM_Prescaler = 7199+1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- TIM_Cmd(TIM2, ENABLE);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =15;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }

Timer.h:
- #ifndef __TIMER_H
- #define __TIMER_H
-
- void Timer_Init(void);
-
- #endif
PWM.c:
- #include "stm32f10x.h" // Device header
-
- void PWM_Init(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- TIM_InternalClockConfig(TIM2);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR
- TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
-
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0; //CCR
- TIM_OC2Init(TIM2, &TIM_OCInitStructure);
-
- TIM_Cmd(TIM2, ENABLE);
- }
-
- void PWM_SetCompare2(uint16_t Compare)
- {
- TIM_SetCompare2(TIM2, Compare);
- }
-
- void Servo_SetAngle(float Angle)
- {
- PWM_SetCompare2(Angle / 180 * 2000 + 500);
- }

PWM.h:
- #ifndef __PWM_H
- #define __PWM_H
- #include "stm32f10x.h" // Device header
-
- void PWM_Init(void);
- void PWM_SetCompare2(uint16_t Compare);
- void Servo_SetAngle(float Angle);
- #endif
main.c:
- #include "stm32f10x.h" // Device header
- #include "Delay.h"
- #include "PWM.h"
- #include "Timer.h"
- float Angle;//控制角度
-
- int main(void)
- {
- Timer_Init();
- PWM_Init();
- Angle=-16; //Angle的值可以修改
- while (1)
- {
- Servo_SetAngle(Angle);
- }
- }

中断服务程序:
- extern float Angle;
- unsigned char num=0;
- void TIM2_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- if(num == 0)
- {
- if(Angle < 195)
- {
- Angle=Angle+2;
- }
- else num=num+1;
- }
- if(num == 1)
- {
- if(Angle > -16)
- {
- Angle=Angle-2;
- }
- else num=num-1;
- }
- }
- }

能用的支持一波!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。