add temperature reading on adc channels
This commit is contained in:
3
Makefile
3
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)
|
||||
|
||||
|
||||
@@ -97,4 +97,6 @@ void ioHelperEdgeDetector(void);
|
||||
#define IN_NOTAUS_ANLAGE BitPinF7
|
||||
#define IN_NOTAUS_SCHRANK BitPinK0
|
||||
|
||||
#define IN_KLATWASSER_DRAN BitPinF6
|
||||
|
||||
#endif
|
||||
|
||||
30
main.c
30
main.c
@@ -1,6 +1,7 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/wdt.h> // WatchDog
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#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<sizeof(ADC_reading)/sizeof(ADC_reading[0]);i++){
|
||||
char msg[64];
|
||||
char tpc[64];
|
||||
sprintf(msg, "%.1f", ADC_reading[i]);
|
||||
sprintf(tpc, "/Filamentanlage/02_Wasserbecken/state/temp%d", i);
|
||||
mqtt_pub(&mqtt_client, tpc, msg, strlen(msg));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// INIT MCU
|
||||
@@ -135,11 +149,11 @@ int main()
|
||||
while(1); //Reboot the board
|
||||
}
|
||||
|
||||
// Subscribe to all topics
|
||||
char SubString[] = "/Filamentanlage/02_Wasserbecken/#";
|
||||
//char SubString[] = "/Filamentanlage/03_Wasserbecken/#";
|
||||
mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived);
|
||||
printf("Subscribed (%s) %ld\r\n", SubString, mqtt_rc);
|
||||
//// Subscribe to all topics
|
||||
//char SubString[] = "/Filamentanlage/02_Wasserbecken/#";
|
||||
////char SubString[] = "/Filamentanlage/03_Wasserbecken/#";
|
||||
//mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived);
|
||||
//printf("Subscribed (%s) %ld\r\n", SubString, mqtt_rc);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -162,6 +176,9 @@ int main()
|
||||
if(millis() - timer_blink_outs > 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
|
||||
|
||||
|
||||
33
temperature.c
Normal file
33
temperature.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
||||
}
|
||||
9
temperature.h
Normal file
9
temperature.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _TEMPERATURE_H_
|
||||
#define _TEMPERATURE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern volatile float ADC_reading[5];
|
||||
void initADC(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user