Split LED functionality in seperate headers/sources

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

View File

@ -1,10 +0,0 @@
#ifndef PORTS_H
#define PORTS_H
/* DS18B20+ is configured in DS18B20 library */
/* Signal LEDs */
#define LED_1 PD3
#define LED_2 PD4
#endif

10
include/led.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef LED_H
#define LED_H
#define LED_DDR DDRD
#define LED_1_BIT PD3
#define LED_2_BIT PD4
void init_leds(void);
#endif /* LED_H */

View File

@ -11,8 +11,6 @@
#include "debug.h" #include "debug.h"
#endif /* DEBUG */ #endif /* DEBUG */
void init_led(void);
void read_temp(void); void read_temp(void);
uint16_t read_adc(uint8_t channel);
#endif #endif

9
src/led.c Normal file
View File

@ -0,0 +1,9 @@
#include <avr/io.h>
#include "common.h"
#include "led.h"
void init_leds(void)
{
SET(LED_DDR, LED_1_BIT);
SET(LED_DDR, LED_2_BIT);
}

View File

@ -2,23 +2,10 @@
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <util/delay.h> #include <util/delay.h>
#include "ds18b20.h" #include "ds18b20.h"
#include "led.h"
#include "servos.h" #include "servos.h"
#include "sonar.h" #include "sonar.h"
#include "main.h" #include "main.h"
#include "io.h"
void init_led(void)
{
DDRD |= 1 << LED_1;
DDRD |= 1 << LED_2;
}
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) void read_temp(void)
{ {
@ -28,29 +15,18 @@ void read_temp(void)
for (;;) { for (;;) {
d = ds18b20_gettemp(); d = ds18b20_gettemp();
if (d >= 23) if (d >= 23)
SET(PORTD, LED_1); ;
else else
CLR(PORTD, LED_1); ;
_delay_ms(500); _delay_ms(500);
} }
} }
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)
{ {
init_led(); init_leds();
init_servos(); init_servos();
init_adc();
run_servos(); run_servos();
read_temp(); read_temp();