Merge branch 'master' into radio

This commit is contained in:
Rihards Skuja 2017-11-28 15:40:06 +02:00
commit fcf6b9564d
No known key found for this signature in database
GPG Key ID: 53FA13A3F7F8571B
3 changed files with 62 additions and 32 deletions

View File

@ -7,14 +7,9 @@
#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
/* Photo sensors */ #define SERVO_R OCR1B
#define PH_1 PC1
#define PH_2 PC2
#define PH_3 PC3
#define PH_4 PC4
#define PH_5 PC5
#endif #endif

View File

@ -1,15 +1,17 @@
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
#include <stdint.h>
#define SET(PORT, PIN) (PORT |= (1 << PIN)) #define SET(PORT, PIN) (PORT |= (1 << PIN))
#define CLR(PORT, PIN) (PORT &= (~(1 << PIN))) #define CLR(PORT, PIN) (PORT &= (~(1 << PIN)))
#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 init_led(void);
void blinkLed(void); void init_pwm(void);
void readTemp(void); void read_temp(void);
void readPhoto(void); void run_servos(void);
void runServos(void); uint16_t read_adc(uint8_t channel);
#endif #endif

View File

@ -10,21 +10,29 @@
#include "debug.h" #include "debug.h"
#endif /* DEBUG */ #endif /* DEBUG */
void initIO() void init_pwm(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 */
SET(DDRD, LED_1);
} }
void readTemp() void init_led(void)
{
DDRD |= 1 << LED_1;
}
void init_adc(void)
{
ADCSRA |= 1 << ADPS0 | 1 << ADPS1; // Prescaler = 8 => ADC clock = 125Hz
ADMUX |= 1 << REFS0; // AVcc with external cap as reference voltage
ADCSRA |= 1 << ADEN | 1 << ADSC;
}
void read_temp(void)
{ {
double d; double d;
@ -40,27 +48,52 @@ void readTemp()
} }
} }
void runServos() void run_servos(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);
} }
uint16_t read_adc(uint8_t channel)
{
ADMUX &= 0xF0;
ADMUX |= channel;
SET(ADCSRA, ADSC); // Start conversion
while (CHK(ADCSRA, ADSC)); // Wait for conversion to finish
return ADCW;
}
int main(void) int main(void)
{ {
initIO(); uint16_t adc_val;
runServos();
readTemp();
init_led();
init_pwm();
init_adc();
while (1) {
adc_val = read_adc(0);
if (adc_val <= 512) {
SERVO_R = 2000;
SERVO_L = 2000;
} else {
SERVO_R = 1000;
SERVO_L = 1000;
}
}
return 0; return 0;
} }