From c5b6ba804381cb40c0040fba371c3cc2730ee301 Mon Sep 17 00:00:00 2001 From: agsler Date: Wed, 9 Nov 2022 17:23:19 +0100 Subject: [PATCH] add temperature reading on adc channels --- Makefile | 3 ++- avrIOhelper/io-helper.h | 2 ++ main.c | 30 ++++++++++++++++++++++++------ temperature.c | 33 +++++++++++++++++++++++++++++++++ temperature.h | 9 +++++++++ 5 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 temperature.c create mode 100644 temperature.h diff --git a/Makefile b/Makefile index e5279be..2db348b 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,8 @@ BUILDDIR = Builds DEFINES = -I . -IInternet/MQTT -I Internet/MQTT/MQTTPacket/src -I Ethernet/W5500 -I Ethernet -DF_CPU=16000000UL -D_WIZCHIP_=W5100 CFLAGS =-mmcu=$(MCU) -O2 -Wall -Wpedantic $(DEFINES) -std=c99 -ffunction-sections -fdata-sections -LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections +#LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections +LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections,-u,vfprintf -lprintf_flt LDFILES = $(foreach FILE,$(FILES),$(BUILDDIR)/$(FILE).o) diff --git a/avrIOhelper/io-helper.h b/avrIOhelper/io-helper.h index 71071f1..fce607b 100644 --- a/avrIOhelper/io-helper.h +++ b/avrIOhelper/io-helper.h @@ -97,4 +97,6 @@ void ioHelperEdgeDetector(void); #define IN_NOTAUS_ANLAGE BitPinF7 #define IN_NOTAUS_SCHRANK BitPinK0 +#define IN_KLATWASSER_DRAN BitPinF6 + #endif diff --git a/main.c b/main.c index 7ca309c..7168eba 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include #include #include // WatchDog +#include #include #include "Ethernet/socket.h" @@ -14,10 +15,11 @@ #include "uart.h" #include "spi.h" #include "mqtt.h" +#include "temperature.h" #include "util/delay.h" -#define PLC_MQTT_ENABLED 0 +#define PLC_MQTT_ENABLED 1 Client mqtt_client; @@ -88,6 +90,18 @@ void do_notaus(){ } } +void send_values(void){ + char msg[10]; + for(uint8_t i=0; i 500){ outStates[0] ^= outStatesBlinking[0]; outStates[1] ^= outStatesBlinking[1]; +#if PLC_MQTT_ENABLED + send_values(); +#endif timer_blink_outs = millis(); } @@ -217,6 +234,7 @@ static void avr_init(void) timer0_init();// Timer0 millis engine init uart_init(); + initADC(); sei(); //re-enable global interrupts diff --git a/temperature.c b/temperature.c new file mode 100644 index 0000000..173b9a8 --- /dev/null +++ b/temperature.c @@ -0,0 +1,33 @@ +#include +#include +#include + +volatile float ADC_reading[5] = {0,0,0,0,0}; + +void initADC(void) +{ + ADMUX = 1 << REFS0 | 0 << REFS1; //Select external Vref + ADCSRA = _BV(ADEN) | _BV(ADIE); // enable adc, enable interrupt + ADCSRA |= 1 << ADPS2 | 1 << ADPS1 | 1 << ADPS0; // set clock-prescaler to 128 + ADCSRA |= 1 << ADSC; // start conversion +} + +void adc_set_channel(uint8_t ch){ + ADMUX = (ADMUX & 0xE0) | (ch & 0x1F); +} + +ISR(ADC_vect) +{ + static uint8_t ch = 0; + //Reading 10bit conversion result + uint16_t tmp = ADCL; //copy the first LSB bits + tmp |= ADCH << 8; //copy remaing byte + + ADC_reading[ch] = (((float)tmp * 0.3)); + //ADC_reading[ch] = (((float)tmp * 0.03)/0.092)-2.3; + + ch = (ch + 1)%5; + adc_set_channel(ch); + + ADCSRA |= (1 << ADSC); //Start next conversion +} diff --git a/temperature.h b/temperature.h new file mode 100644 index 0000000..b2a1d7b --- /dev/null +++ b/temperature.h @@ -0,0 +1,9 @@ +#ifndef _TEMPERATURE_H_ +#define _TEMPERATURE_H_ + +#include + +extern volatile float ADC_reading[5]; +void initADC(void); + +#endif