From ddf696e1da13ce068d61d1e69e114103c8ed270b Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Sat, 30 Dec 2017 22:48:10 +0200 Subject: [PATCH 1/9] New pin definitions --- include/ds18b20.h | 8 ++++---- include/io.h | 13 +++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/ds18b20.h b/include/ds18b20.h index 7df1ca4..0890599 100644 --- a/include/ds18b20.h +++ b/include/ds18b20.h @@ -20,10 +20,10 @@ References: //setup connection -#define DS18B20_PORT PORTD -#define DS18B20_DDR DDRD -#define DS18B20_PIN PIND -#define DS18B20_DQ PD7 +#define DS18B20_PORT PORTC +#define DS18B20_DDR DDRC +#define DS18B20_PIN PINC +#define DS18B20_DQ PC0 //commands #define DS18B20_CMD_CONVERTTEMP 0x44 diff --git a/include/io.h b/include/io.h index 3239a25..67186db 100644 --- a/include/io.h +++ b/include/io.h @@ -3,13 +3,14 @@ /* DS18B20+ is configured in DS18B20 library */ -/* Signal LED */ +/* Signal LEDs */ #define LED_1 PD3 +#define LED_2 PD4 /* Servo motors */ -#define SERVO_L_PIN PB1 -#define SERVO_L OCR1A -#define SERVO_R_PIN PB2 -#define SERVO_R OCR1B - +#define SERVO_DDR DDRD +#define SERVO_L_PIN PD5 +#define SERVO_L OCR0B +#define SERVO_R_PIN PD6 +#define SERVO_R OCR0A #endif From e96258cdb83c2aa62f8f99fdea0e5e7780ee100b Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Sat, 30 Dec 2017 22:49:18 +0200 Subject: [PATCH 2/9] Use OCR0x to generate PWM for servo motors --- src/main.c | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/src/main.c b/src/main.c index 5e7ba95..1618702 100644 --- a/src/main.c +++ b/src/main.c @@ -9,19 +9,25 @@ #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_pwm(void) { - DDRB |= 1 << SERVO_L_PIN | 1 << SERVO_R_PIN; - /* Phase Correct PWM; Set/clear on Compare Match when down/up-counting */ - TCCR1A |= 1 << WGM11 | 1 << COM1A1 | 1 << COM1B1; - /* "Clear Timer on Compare match" mode; Prescaler = 1 */ - TCCR1B |= 1 << WGM12 | 1 << WGM13 | 1 << CS10; - ICR1 = F_CPU / 50 - 1; // 50Hz required by servos + 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; + DDRD |= 1 << LED_2; } void init_adc(void) @@ -38,7 +44,7 @@ void read_temp(void) sei(); for (;;) { d = ds18b20_gettemp(); - if (d >= 21) + if (d >= 23) SET(PORTD, LED_1); else CLR(PORTD, LED_1); @@ -49,20 +55,9 @@ void read_temp(void) void run_servos(void) { - /* Stop */ - SERVO_L = 1500; - SERVO_R = 1500; - _delay_ms(1500); - - /* Reverse */ - SERVO_L = 2000; - SERVO_R = 2000; - _delay_ms(1500); - - /* Forwards */ - SERVO_L = 1000; - SERVO_R = 1000; - _delay_ms(1500); + SERVO_L = US2TIMER0(SERVO_MAX); + SERVO_R = US2TIMER0(SERVO_MID); + _delay_ms(10000); // 10s } uint16_t read_adc(uint8_t channel) @@ -77,22 +72,12 @@ uint16_t read_adc(uint8_t channel) int main(void) { - uint16_t adc_val; - 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; - } - } + run_servos(); + read_temp(); return 0; } From 7e2007a0baf8a44c185382b3793e6042ddfb7677 Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 13:45:56 +0200 Subject: [PATCH 3/9] Split common macros in seperate header --- include/common.h | 9 +++++++++ include/main.h | 17 +++++++++++++---- src/main.c | 10 ---------- 3 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 include/common.h diff --git a/include/common.h b/include/common.h new file mode 100644 index 0000000..e96b2d8 --- /dev/null +++ b/include/common.h @@ -0,0 +1,9 @@ +#ifndef COMMON_H +#define COMMON_H + +#define SET(X, Y) X |= (1 << Y) +#define CLR(X, Y) X &= ~(1 << Y) +#define CHK(X, Y) X & (1 << Y) +#define TOG(X, Y) X ^= (1 << Y) + +#endif /* COMMON_H */ diff --git a/include/main.h b/include/main.h index 58be55a..5743225 100644 --- a/include/main.h +++ b/include/main.h @@ -3,10 +3,19 @@ #include -#define SET(PORT, PIN) (PORT |= (1 << PIN)) -#define CLR(PORT, PIN) (PORT &= (~(1 << PIN))) -#define CHK(PORT, PIN) (PORT & (1 << PIN)) -#define TOG(PORT, PIN) (PORT ^= (1 << PIN)) +#ifndef F_CPU +#define F_CPU 1000000UL +#endif + +#ifdef DEBUG +#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); diff --git a/src/main.c b/src/main.c index 1618702..04022a2 100644 --- a/src/main.c +++ b/src/main.c @@ -5,16 +5,6 @@ #include "main.h" #include "io.h" -#ifdef DEBUG -#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_pwm(void) { SERVO_DDR |= _BV(SERVO_L_PIN) | _BV(SERVO_R_PIN); From 3dcbbfe205075d2bdd3df4305b290220bc4a3ed2 Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 13:46:54 +0200 Subject: [PATCH 4/9] Read distance from HC-SR04 ultrasonic sensor --- include/sonar.h | 45 ++++++++++++++++++++++++++++++++++++ src/sonar.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 include/sonar.h create mode 100644 src/sonar.c diff --git a/include/sonar.h b/include/sonar.h new file mode 100644 index 0000000..03a1199 --- /dev/null +++ b/include/sonar.h @@ -0,0 +1,45 @@ +#ifndef SONAR_H +#define SONAR_H + +#include "common.h" + +// Trigger +#define TRIG_DDR DDRC +#define TRIG_PORT PORTC +#define TRIG_PIN PINC +#define TRIG_BIT PC4 + +// Echo +#define ECHO_DDR DDRC +#define ECHO_PORT PORTC +#define ECHO_PIN PINC +#define ECHO_BIT PC5 + +#define SPEED_OF_SOUND 343 // (m/s) +#define US_PER_CM 58 // Time for sound to travel distance of 1cm +#define MAX_SONAR_RANGE 8 // Trigger + echo (m) +#define DELAY_BETWEEN_TESTS 500 // Timeout for return signal +#define TIMER_MAX 65535 // Depends on the timer used + +#define CYCLES_PER_US F_CPU / 1000000 +#define SONAR_TIMEOUT (F_CPU * MAX_SONAR_RANGE) / SPEED_OF_SOUND + +#define TRIG_ERROR -1 +#define ECHO_ERROR -2 + +#define TRIG_INPUT_MODE() CLR(TRIG_DDR, TRIG_BIT) +#define TRIG_OUTPUT_MODE() SET(TRIG_DDR, TRIG_BIT) +#define TRIG_LOW() CLR(TRIG_PORT, TRIG_BIT) +#define TRIG_HIGH() SET(TRIG_PORT, TRIG_BIT) + +#define ECHO_INPUT_MODE() CLR(ECHO_DDR, ECHO_BIT) +#define ECHO_OUTPUT_MODE() SET(ECHO_DDR, ECHO_BIT) +#define ECHO_LOW() CLR(ECHO_PORT, ECHO_BIT) +#define ECHO_HIGH() SET(ECHO_PORT, ECHO_BIT) + + +void init_sonar(void); +void trigger_sonar(void); +unsigned int read_sonar(void); + +#endif /* SONAR_H */ diff --git a/src/sonar.c b/src/sonar.c new file mode 100644 index 0000000..4f15e14 --- /dev/null +++ b/src/sonar.c @@ -0,0 +1,61 @@ +#include +#include +#include "sonar.h" + +volatile uint32_t overflow_counter = 0; +volatile uint32_t trig_counter = 0; +volatile uint32_t no_of_cycles = 0; + +void init_sonar() +{ + TRIG_OUTPUT_MODE(); + ECHO_INPUT_MODE(); +} + +void trigger_sonar() +{ + TRIG_LOW(); + _delay_us(1); + TRIG_HIGH(); + _delay_us(12); + TRIG_LOW(); + _delay_us(1); +} + +ISR(TIMER1_OVF_vect) +{ + ++overflow_counter; + TCNT1 = 0; +} + +unsigned int read_sonar() +{ + int dist_in_cm = 0; + init_sonar(); + trigger_sonar(); + + // While echo pin is low + while (!(CHK(ECHO_PIN, ECHO_BIT))) { + if (++trig_counter > SONAR_TIMEOUT) + return TRIG_ERROR; // Received no response from echo + } + + TCNT1 = 0; + TCCR1B |= (1 << CS10); + TIMSK1 |= (1 << TOIE1); + overflow_counter = 0; + sei(); + + // While echo pin is high + while (CHK(ECHO_PIN, ECHO_BIT)) { + if (((overflow_counter * TIMER_MAX) + TCNT1) > SONAR_TIMEOUT) + return ECHO_ERROR; // No echo within sonar range + } + + TCCR1B = 0x00; + cli(); + no_of_cycles = ((overflow_counter * TIMER_MAX) + TCNT1); + dist_in_cm = (no_of_cycles / (US_PER_CM * CYCLES_PER_US)); + + return dist_in_cm; +} From 3fc7ad4c8d5ce7ca6094f895f72b40e698c50016 Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 14:02:42 +0200 Subject: [PATCH 5/9] Split servo functionality in seperate headers/sources --- include/io.h | 6 ------ include/main.h | 8 -------- include/servos.h | 21 +++++++++++++++++++++ src/main.c | 20 +++----------------- src/servos.c | 28 ++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 31 deletions(-) create mode 100644 include/servos.h create mode 100644 src/servos.c diff --git a/include/io.h b/include/io.h index 67186db..4699712 100644 --- a/include/io.h +++ b/include/io.h @@ -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 diff --git a/include/main.h b/include/main.h index 5743225..adf9394 100644 --- a/include/main.h +++ b/include/main.h @@ -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 diff --git a/include/servos.h b/include/servos.h new file mode 100644 index 0000000..e316410 --- /dev/null +++ b/include/servos.h @@ -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 */ diff --git a/src/main.c b/src/main.c index 04022a2..b3a4e61 100644 --- a/src/main.c +++ b/src/main.c @@ -2,18 +2,11 @@ #include #include #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(); diff --git a/src/servos.c b/src/servos.c new file mode 100644 index 0000000..60257e2 --- /dev/null +++ b/src/servos.c @@ -0,0 +1,28 @@ +#include +#include +#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 +} From f04723704ea274bdef8c6066aafcdb9ac196bf3d Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 14:08:13 +0200 Subject: [PATCH 6/9] Split LED functionality in seperate headers/sources --- include/io.h | 10 ---------- include/led.h | 10 ++++++++++ include/main.h | 2 -- src/led.c | 9 +++++++++ src/main.c | 32 ++++---------------------------- 5 files changed, 23 insertions(+), 40 deletions(-) delete mode 100644 include/io.h create mode 100644 include/led.h create mode 100644 src/led.c diff --git a/include/io.h b/include/io.h deleted file mode 100644 index 4699712..0000000 --- a/include/io.h +++ /dev/null @@ -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 diff --git a/include/led.h b/include/led.h new file mode 100644 index 0000000..a22eff6 --- /dev/null +++ b/include/led.h @@ -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 */ diff --git a/include/main.h b/include/main.h index adf9394..2b2393e 100644 --- a/include/main.h +++ b/include/main.h @@ -11,8 +11,6 @@ #include "debug.h" #endif /* DEBUG */ -void init_led(void); void read_temp(void); -uint16_t read_adc(uint8_t channel); #endif diff --git a/src/led.c b/src/led.c new file mode 100644 index 0000000..4a2b702 --- /dev/null +++ b/src/led.c @@ -0,0 +1,9 @@ +#include +#include "common.h" +#include "led.h" + +void init_leds(void) +{ + SET(LED_DDR, LED_1_BIT); + SET(LED_DDR, LED_2_BIT); +} diff --git a/src/main.c b/src/main.c index b3a4e61..d3d468f 100644 --- a/src/main.c +++ b/src/main.c @@ -2,23 +2,10 @@ #include #include #include "ds18b20.h" +#include "led.h" #include "servos.h" #include "sonar.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) { @@ -28,29 +15,18 @@ void read_temp(void) for (;;) { d = ds18b20_gettemp(); if (d >= 23) - SET(PORTD, LED_1); + ; else - CLR(PORTD, LED_1); + ; _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) { - init_led(); + init_leds(); init_servos(); - init_adc(); run_servos(); read_temp(); From b7458e4bd3b927fe4b55633bc30fe125c50f9530 Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 14:10:10 +0200 Subject: [PATCH 7/9] Rename temperature headers/sources --- include/{ds18b20.h => temperature.h} | 0 src/{ds18b20.c => temperature.c} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename include/{ds18b20.h => temperature.h} (100%) rename src/{ds18b20.c => temperature.c} (100%) diff --git a/include/ds18b20.h b/include/temperature.h similarity index 100% rename from include/ds18b20.h rename to include/temperature.h diff --git a/src/ds18b20.c b/src/temperature.c similarity index 100% rename from src/ds18b20.c rename to src/temperature.c From e6ec28b04ca50f492bd834969b8a6013adf0c938 Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 14:18:30 +0200 Subject: [PATCH 8/9] Use predefined bit manipulation macros --- include/temperature.h | 7 ++--- src/temperature.c | 68 +++++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/include/temperature.h b/include/temperature.h index 0890599..c768ac2 100644 --- a/include/temperature.h +++ b/include/temperature.h @@ -18,14 +18,11 @@ References: #include -//setup connection - #define DS18B20_PORT PORTC #define DS18B20_DDR DDRC #define DS18B20_PIN PINC -#define DS18B20_DQ PC0 +#define DS18B20_BIT PC0 -//commands #define DS18B20_CMD_CONVERTTEMP 0x44 #define DS18B20_CMD_RSCRATCHPAD 0xbe #define DS18B20_CMD_WSCRATCHPAD 0x4e @@ -41,7 +38,7 @@ References: //stop any interrupt on read #define DS18B20_STOPINTERRUPTONREAD 1 -//functions + uint8_t ds18b20_reset(void); void ds18b20_writebit(uint8_t bit); uint8_t ds18b20_readbit(void); diff --git a/src/temperature.c b/src/temperature.c index 4ba3c77..9fa3349 100644 --- a/src/temperature.c +++ b/src/temperature.c @@ -10,8 +10,8 @@ Please refer to LICENSE file for licensing information. #include #include #include - -#include "ds18b20.h" +#include "common.h" +#include "temperature.h" /* * ds18b20 init @@ -20,20 +20,20 @@ uint8_t ds18b20_reset() { uint8_t i; - //low for 480us - DS18B20_PORT &= ~(1 << DS18B20_DQ); //low - DS18B20_DDR |= (1 << DS18B20_DQ); //output + // low for 480us + CLR(DS18B20_PORT, DS18B20_BIT); + SET(DS18B20_DDR, DS18B20_BIT); _delay_us(480); - //release line and wait for 60uS - DS18B20_DDR &= ~(1 << DS18B20_DQ); //input + // release line and wait for 60uS + CLR(DS18B20_DDR, DS18B20_BIT); _delay_us(60); - //get value and wait 420us - i = (DS18B20_PIN & (1 << DS18B20_DQ)); + // get value and wait 420us + i = CHK(DS18B20_PIN, DS18B20_BIT); _delay_us(420); - //return the read value, 0=ok, 1=error + // return the read value, 0=ok, 1=error return i; } @@ -42,18 +42,18 @@ uint8_t ds18b20_reset() */ void ds18b20_writebit(uint8_t bit) { - //low for 1uS - DS18B20_PORT &= ~(1 << DS18B20_DQ); //low - DS18B20_DDR |= (1 << DS18B20_DQ); //output + // low for 1uS + CLR(DS18B20_PORT, DS18B20_BIT); + SET(DS18B20_DDR, DS18B20_BIT); _delay_us(1); - //if we want to write 1, release the line (if not will keep low) + // if we want to write 1, release the line (if not will keep low) if (bit) - DS18B20_DDR &= ~(1 << DS18B20_DQ); //input + CLR(DS18B20_DDR, DS18B20_BIT); - //wait 60uS and release the line + // wait 60uS and release the line _delay_us(60); - DS18B20_DDR &= ~(1 << DS18B20_DQ); //input + DS18B20_DDR &= ~(1 << DS18B20_BIT); } /* @@ -63,20 +63,20 @@ uint8_t ds18b20_readbit(void) { uint8_t bit = 0; - //low for 1uS - DS18B20_PORT &= ~(1 << DS18B20_DQ); //low - DS18B20_DDR |= (1 << DS18B20_DQ); //output + // low for 1uS + CLR(DS18B20_PORT, DS18B20_BIT); + SET(DS18B20_DDR, DS18B20_BIT); _delay_us(1); - //release line and wait for 14uS - DS18B20_DDR &= ~(1 << DS18B20_DQ); //input + // release line and wait for 14uS + CLR(DS18B20_DDR, DS18B20_BIT); _delay_us(14); - //read the value - if (DS18B20_PIN & (1 << DS18B20_DQ)) + // read the value + if (CHK(DS18B20_PIN, DS18B20_BIT)) bit = 1; - //wait 45uS and return read value + // wait 45uS and return read value _delay_us(45); return bit; } @@ -119,17 +119,17 @@ double ds18b20_gettemp() cli(); #endif - ds18b20_reset(); //reset - ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM - ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion + ds18b20_reset(); // reset + ds18b20_writebyte(DS18B20_CMD_SKIPROM); // skip ROM + ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); // start temperature conversion - while (!ds18b20_readbit()); //wait until conversion is complete + while (!ds18b20_readbit()); // wait until conversion is complete - ds18b20_reset(); //reset - ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM - ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad + ds18b20_reset(); // reset + ds18b20_writebyte(DS18B20_CMD_SKIPROM); // skip ROM + ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); // read scratchpad - //read 2 byte from scratchpad + // read 2 byte from scratchpad temperature_l = ds18b20_readbyte(); temperature_h = ds18b20_readbyte(); @@ -137,7 +137,7 @@ double ds18b20_gettemp() sei(); #endif - //convert the 12 bit value obtained + // convert the 12 bit value obtained retd = ((temperature_h << 8) + temperature_l) * 0.0625; return retd; From 78a8b62142b81dcae98b8f888e24f1211b02659c Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 14:22:26 +0200 Subject: [PATCH 9/9] Fix wrong header name in main.c --- include/main.h | 8 -------- src/main.c | 11 ++++++++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/main.h b/include/main.h index 2b2393e..92b59e8 100644 --- a/include/main.h +++ b/include/main.h @@ -3,14 +3,6 @@ #include -#ifndef F_CPU -#define F_CPU 1000000UL -#endif - -#ifdef DEBUG -#include "debug.h" -#endif /* DEBUG */ - void read_temp(void); #endif diff --git a/src/main.c b/src/main.c index d3d468f..746263f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,21 @@ #include #include #include -#include "ds18b20.h" +#include "temperature.h" #include "led.h" #include "servos.h" #include "sonar.h" #include "main.h" +#ifdef DEBUG +#include "debug.h" +#endif /* DEBUG */ + +#ifndef F_CPU +#define F_CPU 1000000UL +#endif + + void read_temp(void) { double d;