Last active
August 29, 2015 14:01
-
-
Save gamazeps/b649c544450a929aaaba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ch.h" | |
#include "hal.h" | |
#include "servo.h" | |
#define SERVO_PWM PWMD3 | |
static PWMConfig pwm_servo = { | |
100000, /* 100Khz PWM clock frequency*/ | |
2000, /* PWM period of 2000 ticks ~ 20ms */ | |
NULL, /* No callback at the end of the timer count */ | |
{ | |
{PWM_OUTPUT_ACTIVE_HIGH, NULL}, | |
{PWM_OUTPUT_DISABLED, NULL}, | |
{PWM_OUTPUT_DISABLED, NULL}, | |
{PWM_OUTPUT_DISABLED, NULL} | |
}, | |
0, /* CR2 register */ | |
0 /* DIER register */ | |
}; | |
void servo_init(void){ | |
palSetPadMode(GPIOB, 4, PAL_MODE_ALTERNATE(2)); // used function : TIM3_CH1 | |
pwmStart(&SERVO_PWM, &pwm_servo); | |
} | |
/* | |
* Sets the servo angle between -90° and 90° | |
*/ | |
void set_servo(int angle){ | |
// Moves the target angle in the proper range as 0° is 1520µs | |
angle+=152; | |
pwmEnableChannel(&SERVO_PWM, 0, angle); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SERVO_H | |
#define SERVO_H | |
void servo_init(void); | |
void set_servo(int angle); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment