Update PWM code to demo rotation

Tower Pro Micro Servo 9g SG90 was used for test.
This commit is contained in:
Sanchayan Maity 2017-05-09 13:02:34 +05:30
parent 0a057093ad
commit 31ba3f4f2b

View file

@ -11,14 +11,19 @@
* libsoc PWM API Documentation: https://jackmitch.github.io/libsoc/c/pwm/ * libsoc PWM API Documentation: https://jackmitch.github.io/libsoc/c/pwm/
*/ */
#define SERVO_PERIOD 20000000
#define DMIN 150000
#define DMAX 2000000
#define STEP_SIZE 5
int main(void) int main(void)
{ {
pwm* vf_pwm = NULL; pwm* vf_pwm = NULL;
uint32_t ret = EXIT_SUCCESS; uint32_t ret = EXIT_SUCCESS;
uint32_t pwm_period = 0;
uint32_t pwm_number = 0; uint32_t pwm_number = 0;
uint32_t pwm_chip_number = 0; uint32_t pwm_chip_number = 0;
uint32_t pwm_duty_cycle = 0; uint32_t count = 2;
uint32_t ms;
printf("Enter PWM chip number:\t"); printf("Enter PWM chip number:\t");
scanf("%d", &pwm_chip_number); scanf("%d", &pwm_chip_number);
@ -30,30 +35,38 @@ int main(void)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
printf("Enter PWM Period:\t"); /* As required for Tower Pro Micro Servo 9g SG90 */
scanf("%d", &pwm_period); ret = libsoc_pwm_set_period(vf_pwm, SERVO_PERIOD);
ret = libsoc_pwm_set_period(vf_pwm, pwm_period);
if (ret == EXIT_FAILURE) { if (ret == EXIT_FAILURE) {
perror("PWM set period failed"); perror("PWM set period failed");
goto exit_failure; goto exit_failure;
} }
printf("Enter PWM Duty Cycle:\t");
scanf("%d", &pwm_duty_cycle);
ret = libsoc_pwm_set_duty_cycle(vf_pwm, pwm_duty_cycle);
if (ret == EXIT_FAILURE) {
perror("PWM set duty cycle failed");
goto exit_failure;
}
ret = libsoc_pwm_set_enabled(vf_pwm, ENABLED); ret = libsoc_pwm_set_enabled(vf_pwm, ENABLED);
if (ret == EXIT_FAILURE) { if (ret == EXIT_FAILURE) {
perror("PWM enable failed"); perror("PWM enable failed");
goto exit_failure; goto exit_failure;
} }
printf("PWM will be running for 10 seconds\n"); while (count > 0) {
sleep(10); for (ms = DMIN; ms <= DMAX;) {
ret = libsoc_pwm_set_duty_cycle(vf_pwm, ms);
if (ret == EXIT_FAILURE) {
perror("PWM set duty cycle failed");
goto exit_failure;
}
ms = ms + STEP_SIZE;
}
for (ms = DMAX; ms >= DMIN;) {
ret = libsoc_pwm_set_duty_cycle(vf_pwm, ms);
if (ret == EXIT_FAILURE) {
perror("PWM set duty cycle failed");
goto exit_failure;
}
ms = ms - STEP_SIZE;
}
count--;
}
printf("Disabling PWM\n"); printf("Disabling PWM\n");
ret = libsoc_pwm_set_enabled(vf_pwm, DISABLED); ret = libsoc_pwm_set_enabled(vf_pwm, DISABLED);