Merge pull request #6 from rhssk/servos

Use TC1 to drive an additional servo motor
This commit is contained in:
Rihards Skuja 2017-11-25 11:41:05 +00:00 committed by GitHub
commit 0e6fe01609
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 18 deletions

View File

@ -7,8 +7,10 @@
#define LED_1 PD2 #define LED_1 PD2
/* Servo motors */ /* Servo motors */
#define SERVO_L PB1 #define SERVO_L_PIN PB1
#define SERVO_R PD6 #define SERVO_L OCR1A
#define SERVO_R_PIN PB2
#define SERVO_R OCR1B
/* Photo sensors */ /* Photo sensors */
#define PH_1 PC1 #define PH_1 PC1

View File

@ -6,7 +6,8 @@
#define CHK(PORT, PIN) (PORT & (1 << PIN)) #define CHK(PORT, PIN) (PORT & (1 << PIN))
#define TOG(PORT, PIN) (PORT ^= (1 << PIN)) #define TOG(PORT, PIN) (PORT ^= (1 << PIN))
void initIO(void); void initLED(void);
void initPWM(void);
void blinkLed(void); void blinkLed(void);
void readTemp(void); void readTemp(void);
void readPhoto(void); void readPhoto(void);

View File

@ -9,21 +9,22 @@
#include "debug.h" #include "debug.h"
#endif /* DEBUG */ #endif /* DEBUG */
void initIO() void initPWM(void)
{ {
/* Servos */ DDRB |= 1 << SERVO_L_PIN | 1 << SERVO_R_PIN;
SET(DDRB, SERVO_L); /* Phase Correct PWM; Set/clear on Compare Match when down/up-counting */
/* Phase Correct PWM, 9-bit; Inverting mode */ TCCR1A |= 1 << WGM11 | 1 << COM1A1 | 1 << COM1B1;
TCCR1A |= 1 << WGM11 | 1 << COM1A1 | 1 << COM1A0;
/* "Clear Timer on Compare match" mode; Prescaler = 1 */ /* "Clear Timer on Compare match" mode; Prescaler = 1 */
TCCR1B |= 1 << WGM13 | 1 << WGM12 | 1 << CS10; TCCR1B |= 1 << WGM12 | 1 << WGM13 | 1 << CS10;
ICR1 = 19999; // F_CPU / 50Hz - 1 ICR1 = F_CPU / 50 - 1; // 50Hz required by servos
}
/* LED */ void initLED(void)
{
SET(DDRD, LED_1); SET(DDRD, LED_1);
} }
void readTemp() void readTemp(void)
{ {
double d; double d;
@ -39,27 +40,31 @@ void readTemp()
} }
} }
void runServos() void runServos(void)
{ {
/* Stop */ /* Stop */
OCR1A = ICR1 - 1500; SERVO_L = 1500;
SERVO_R = 1500;
_delay_ms(1500); _delay_ms(1500);
/* Reverse */ /* Reverse */
OCR1A = ICR1 - 2000; SERVO_L = 2000;
SERVO_R = 2000;
_delay_ms(1500); _delay_ms(1500);
/* Forwards */ /* Forwards */
OCR1A = ICR1 - 1000; SERVO_L = 1000;
SERVO_R = 1000;
_delay_ms(1500); _delay_ms(1500);
} }
int main(void) int main(void)
{ {
initIO(); initLED();
initPWM();
runServos(); runServos();
readTemp(); readTemp();
return 0; return 0;
} }