Split servo functionality in seperate headers/sources

This commit is contained in:
Rihards Skuja 2018-01-02 14:02:42 +02:00
parent 3dcbbfe205
commit 3fc7ad4c8d
No known key found for this signature in database
GPG Key ID: 53FA13A3F7F8571B
5 changed files with 52 additions and 31 deletions

View File

@ -7,10 +7,4 @@
#define LED_1 PD3
#define LED_2 PD4
/* Servo motors */
#define SERVO_DDR DDRD
#define SERVO_L_PIN PD5
#define SERVO_L OCR0B
#define SERVO_R_PIN PD6
#define SERVO_R OCR0A
#endif

View File

@ -11,16 +11,8 @@
#include "debug.h"
#endif /* DEBUG */
#define SERVO_MIN 930 // (us). Go backwards
#define SERVO_MAX 1930 // (us). Go forwards
#define SERVO_MID (SERVO_MIN + SERVO_MAX) / 2 // Stop
#define PWM_PERIOD 16384 // (1000000 / F_CPU) * PRESCALER * 256) / F_CPU (us)
#define US2TIMER0(us) (255 * (uint32_t)us) / PWM_PERIOD
void init_led(void);
void init_pwm(void);
void read_temp(void);
void run_servos(void);
uint16_t read_adc(uint8_t channel);
#endif

21
include/servos.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef SERVOS_H
#define SERVOS_H
#define SERVO_DDR DDRD
#define SERVO_L_REG OCR0B
#define SERVO_R_REG OCR0A
#define SERVO_L_BIT PD5
#define SERVO_R_BIT PD6
#define SERVO_MIN 930 // (us). Go backwards
#define SERVO_MAX 1930 // (us). Go forwards
#define SERVO_MID (SERVO_MIN + SERVO_MAX) / 2 // Stop
#define PWM_PERIOD 16384 // (1000000 / F_CPU) * PRESCALER * 256) / F_CPU (us)
#define US2TIMER0(us) (255 * (uint32_t)us) / PWM_PERIOD
void init_servos(void);
void run_servos(void);
#endif /* SERVOS_H */

View File

@ -2,18 +2,11 @@
#include <avr/interrupt.h>
#include <util/delay.h>
#include "ds18b20.h"
#include "servos.h"
#include "sonar.h"
#include "main.h"
#include "io.h"
void init_pwm(void)
{
SERVO_DDR |= _BV(SERVO_L_PIN) | _BV(SERVO_R_PIN);
TCCR0A |= _BV(COM0A1) | // Clear OC0A on Compare Match, set OC0A at BOTTOM
_BV(COM0B1) | // Clear OC0B on Compare Match, set OC0B at BOTTOM
_BV(WGM00) | _BV(WGM01); // Fast PWM, 0xFF TOP
TCCR0B |= _BV(CS00) | _BV(CS01); // clkPWM = clkIO / 64
}
void init_led(void)
{
DDRD |= 1 << LED_1;
@ -43,13 +36,6 @@ void read_temp(void)
}
}
void run_servos(void)
{
SERVO_L = US2TIMER0(SERVO_MAX);
SERVO_R = US2TIMER0(SERVO_MID);
_delay_ms(10000); // 10s
}
uint16_t read_adc(uint8_t channel)
{
ADMUX &= 0xF0;
@ -63,7 +49,7 @@ uint16_t read_adc(uint8_t channel)
int main(void)
{
init_led();
init_pwm();
init_servos();
init_adc();
run_servos();

28
src/servos.c Normal file
View File

@ -0,0 +1,28 @@
#include <avr/io.h>
#include <util/delay.h>
#include "common.h"
#include "servos.h"
void init_servos(void)
{
SET(SERVO_DDR, SERVO_L_BIT);
SET(SERVO_DDR, SERVO_R_BIT);
SET(TCCR0A, COM0A1); // Clear OC0A on Compare Match, set OC0A at BOTTOM
SET(TCCR0A, COM0B1); // Clear OC0B on Compare Match, set OC0B at BOTTOM
// Fast PWM, 0xFF TOP
SET(TCCR0A, WGM00);
SET(TCCR0A, WGM01);
// Prescaler. clkPWM = clkIO / 64
SET(TCCR0B, CS00);
SET(TCCR0B, CS01);
}
void run_servos(void)
{
SERVO_L_REG = US2TIMER0(SERVO_MAX);
SERVO_R_REG = US2TIMER0(SERVO_MID);
_delay_ms(10000); // 10s
}