This commit is contained in:
andreask 2020-09-05 22:57:07 +02:00
parent 616a3af2c0
commit 6d4965095d

View file

@ -66,7 +66,7 @@
SHT21 sensor; SHT21 sensor;
#endif #endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
volatile boolean alarm = false; volatile boolean alarm = false;
// Get the Interrupt Mask for the selected Alarm PIN // Get the Interrupt Mask for the selected Alarm PIN
@ -93,9 +93,23 @@
#endif #endif
#ifdef HAS_MANNLCOUNTER #ifdef HAS_MANNLCOUNTER
volatile uint16_t count_min = 0; #include <util/delay.h>
volatile uint32_t count_all = 0; uint16_t count_min = 0;
volatile unsigned long alteZeit=0, entprellZeit=20; //ms uint32_t count_all = 0;
unsigned long lastTime = millis();
void entprellung( volatile uint8_t *port, uint8_t maske ) {
uint8_t port_puffer;
uint8_t entprellungs_puffer;
for( entprellungs_puffer=0 ; entprellungs_puffer!=0xff ; ) {
entprellungs_puffer<<=1;
port_puffer = *port;
_delay_us(150);
if( (*port & maske) == (port_puffer & maske) )
entprellungs_puffer |= 0x01;
}
}
#endif #endif
// Global Variable to Track Deep Sleep // Global Variable to Track Deep Sleep
@ -140,6 +154,7 @@ int16_t brightness()
} }
#endif #endif
#ifndef HAS_MANNLCOUNTER
// Setup Wakeup Interrupt Timer // Setup Wakeup Interrupt Timer
void init_wdt() void init_wdt()
{ {
@ -179,29 +194,20 @@ ISR(WATCHDOG_vect) {
// enable WDT interrupt // enable WDT interrupt
WDTCSR = (1<<WDIE)|(1<<WDP3)|(1<<WDP0); WDTCSR = (1<<WDIE)|(1<<WDP3)|(1<<WDP0);
} }
#endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
// innterrupt handler pin change // innterrupt handler pin change
ISR(PCINT0_vect) ISR(PCINT0_vect)
{ {
#ifdef HAS_MANNLCOUNTER //counter mode
if((millis() - alteZeit) > entprellZeit) {
// innerhalb der entprellZeit nichts machen
count_min++;
count_all++;
alteZeit = millis(); // last button push
}
#else //Alarm mode
sleep_disable(); sleep_disable();
#ifdef HAS_LED #ifdef HAS_LED
blink(1); blink(1);
#endif #endif
alarm = true; alarm = true;
#endif
} }
#endif #endif
// Get Battery Voltage // Get Battery Voltage
int32_t readVcc() { int32_t readVcc() {
bitClear(PRR, PRADC); bitClear(PRR, PRADC);
@ -237,9 +243,11 @@ uint8_t calcEepromAddr(uint16_t framecounter) {
void setup() void setup()
{ {
#ifndef HAS_MANNLCOUNTER
// Initialize Sleep Timer // Initialize Sleep Timer
init_wdt(); init_wdt();
PRR = bit(PRTIM1); PRR = bit(PRTIM1);
#endif
#ifdef RF_LORA #ifdef RF_LORA
// Setup LoraWAN // Setup LoraWAN
@ -281,7 +289,7 @@ void setup()
radio.sleep(); radio.sleep();
#endif #endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
MCUCR = (MCUCR & ~(bit(ISC01)|bit(ISC00))) | bit(ISC01); // fallende Flanke MCUCR = (MCUCR & ~(bit(ISC01)|bit(ISC00))) | bit(ISC01); // fallende Flanke
GIMSK = (1<<PCIE0); GIMSK = (1<<PCIE0);
PCMSK0 = (1<<ALARM_INT); PCMSK0 = (1<<ALARM_INT);
@ -388,11 +396,6 @@ void loop()
alarm = false; alarm = false;
#endif #endif
#ifdef HAS_MANNLCOUNTER
data.count_all=count_all;
data.count_min=count_min;
#endif
#if defined DS18B20_PIN #if defined DS18B20_PIN
#ifdef DS18B20_POWER #ifdef DS18B20_POWER
@ -411,6 +414,23 @@ void loop()
#endif #endif
#endif #endif
#ifdef HAS_MANNLCOUNTER
while(millis() - lastTime < 60000){
entprellung( &PINA, (1<<PINA0) ); // ggf. Prellen abwarten
if( PINA & (1<<PINA0) ) // dann stabilen Wert einlesen
{
count_all++;
count_min++;
}
}
lastTime = millis();
// nach 60 sek
data.count_all=count_all;
data.count_min=count_min;
#endif
// Add Battery Voltage, 20mv steps, encoded into 1 Byte // Add Battery Voltage, 20mv steps, encoded into 1 Byte
uint32_t batv = readVcc(); uint32_t batv = readVcc();
data.bat = (uint8_t)(batv/20); data.bat = (uint8_t)(batv/20);
@ -436,9 +456,6 @@ void loop()
#else #else
// Send LoRa Packet, Increment Frame Counter // Send LoRa Packet, Increment Frame Counter
lora.Send_Data((unsigned char *)&data, sizeof(data), Frame_Counter_Tx, SF7BW125, 0x01); lora.Send_Data((unsigned char *)&data, sizeof(data), Frame_Counter_Tx, SF7BW125, 0x01);
#ifdef HAS_MANNLCOUNTER
count_min = 0;
#endif
#endif #endif
// Save the next FrameCounter to EEPROM // Save the next FrameCounter to EEPROM
Frame_Counter_Tx++; Frame_Counter_Tx++;
@ -455,6 +472,8 @@ void loop()
digitalWrite(LED_PIN, 0); digitalWrite(LED_PIN, 0);
#endif #endif
#ifndef HAS_MANNLCOUNTER
// Sleep until next Measurement // Sleep until next Measurement
sleep(SLEEP_TIME); sleep(SLEEP_TIME);
#endif
} }