Fixed WDT timer code, still not dynamic

This commit is contained in:
Ziver Koc 2016-05-09 17:49:24 +02:00
parent 2ba3c42a5e
commit 20023c48e0
5 changed files with 69 additions and 30 deletions

View file

@ -1,24 +1,27 @@
#ifndef HALCONFIGURATION_H #ifndef HALCONFIGURATION_H
#define HALCONFIGURATION_H #define HALCONFIGURATION_H
#define ENABLE_DEBUG // comment out to disable debug
#define TIMER_MILLISECOND 60*1000 // poling in minutes
// POWER CONSUMPTION SENSOR // POWER CONSUMPTION SENSOR
#define POWERCON_ENABLED // comment out to disable sensor #define POWERCON_ENABLED // comment out to disable sensor
#define POWERCON_SENSOR SensorBH1750() #define POWERCON_SENSOR SensorBH1750()
#define POWERCON_PROTOCOL ProtocolOregon(118) #define POWERCON_PROTOCOL ProtocolOregon(118)
#define POWER_TIMER_MULTIPLIER 1 // poling in minutes #define POWER_TIMER_MULTIPLIER 1
// TEMPERATURE SENSOR // TEMPERATURE SENSOR
#define TEMPERATURE_ENABLED // comment out to disable sensor #define TEMPERATURE_ENABLED // comment out to disable sensor
#define TEMPERATURE_SENSOR SensorDHT11(5) #define TEMPERATURE_SENSOR SensorDHT11(5)
#define TEMPERATURE_PROTOCOL ProtocolOregon(100) #define TEMPERATURE_PROTOCOL ProtocolOregon(100)
#define TEMPERATURE_TIMER_MULTIPLIER 1 // poling in minutes #define TEMPERATURE_TIMER_MULTIPLIER 1
// LIGHT SENSOR // LIGHT SENSOR
//#define LIGHT_ENABLED // comment out to disable sensor //#define LIGHT_ENABLED // comment out to disable sensor
#define LIGHT_SENSOR SensorDH1750() #define LIGHT_SENSOR SensorDH1750()
#define LIGHT_PROTOCOL ? #define LIGHT_PROTOCOL ?
#define LIGHT_TIMER_MULTIPLIER 1 // poling in minutes #define LIGHT_TIMER_MULTIPLIER 1
#endif // HALCONFIGURATION_H #endif // HALCONFIGURATION_H

View file

@ -1,6 +1,22 @@
#ifndef HALINTERFACES_H #ifndef HALINTERFACES_H
#define HALINTERFACES_H #define HALINTERFACES_H
#include "HalConfiguration.h"
#ifdef ENABLE_DEBUG
#define DEBUG(msg) \
Serial.println(msg); \
Serial.flush();
#define DEBUGF(msg, ...) \
static char buffer[80];\
snprintf(buffer, sizeof(buffer), msg, __VA_ARGS__);\
Serial.println(buffer);\
Serial.flush();
#else
#define DEBUG(msg)
#define DEBUGF(msg, ...)
#endif
class Sensor class Sensor
{ {

View file

@ -2,22 +2,17 @@
A interrupt based sensor device that reads multiple sensors and transmits A interrupt based sensor device that reads multiple sensors and transmits
the data to a central location. the data to a central location.
*/ */
#if (ARDUINO >= 100)
#include <Arduino.h> #include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <Wire.h> #include <Wire.h>
#include "HalInterfaces.h"
#include "HalDefinitions.h"
#include "HalConfiguration.h" #include "HalConfiguration.h"
#include "HalInterfaces.h"
#include "HalInclude.h"
#include "Interrupt.h" #include "Interrupt.h"
Interrupt* interrupt; #define TIMER_MULTIPLIER_MAX \
POWER_TIMER_MULTIPLIER * TEMPERATURE_TIMER_MULTIPLIER * LIGHT_TIMER_MULTIPLIER
unsigned int timerMultiplierMAX;
unsigned int timerMultiplier = 0; unsigned int timerMultiplier = 0;
// Sensors // Sensors
@ -36,12 +31,13 @@ void timerInterruptFunc();
void setup() void setup()
{ {
timerMultiplierMAX = POWER_TIMER_MULTIPLIER * TEMPERATURE_TIMER_MULTIPLIER * LIGHT_TIMER_MULTIPLIER; // Find a lowest common denominator #ifdef ENABLE_DEBUG
Interrupt::setCallback(timerInterruptFunc); Serial.begin(9600);
Interrupt::setupTimerInterrupt(60*1000); // one minute scheduled interrupt #endif
// Setup Sensors and protocols // Setup Sensors and protocols
#ifdef POWERCON_ENABLED #ifdef POWERCON_ENABLED
DEBUG("Setup POWERCON_SENSOR");
powerSensor = new POWERCON_SENSOR; powerSensor = new POWERCON_SENSOR;
powerSensor->setup(); powerSensor->setup();
powerProtocol = new POWERCON_PROTOCOL; powerProtocol = new POWERCON_PROTOCOL;
@ -49,6 +45,7 @@ void setup()
#endif #endif
#ifdef TEMPERATURE_ENABLED #ifdef TEMPERATURE_ENABLED
DEBUG("Setup TEMPERATURE_SENSOR");
tempSensor = new TEMPERATURE_SENSOR; tempSensor = new TEMPERATURE_SENSOR;
tempSensor->setup(); tempSensor->setup();
tempProtocol = new TEMPERATURE_PROTOCOL; tempProtocol = new TEMPERATURE_PROTOCOL;
@ -56,28 +53,38 @@ void setup()
#endif #endif
#ifdef LIGHT_ENABLED #ifdef LIGHT_ENABLED
DEBUG("Setup LIGHT_SENSOR");
lightSensor = new LIGHT_SENSOR; lightSensor = new LIGHT_SENSOR;
lightSensor->setup(); lightSensor->setup();
lightProtocol = new LIGHT_PROTOCOL; lightProtocol = new LIGHT_PROTOCOL;
lightProtocol->setup(); lightProtocol->setup();
#endif #endif
DEBUG("Setup INTERRUPT");
Interrupt::setCallback(timerInterruptFunc);
Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt
DEBUG("Ready");
} }
void timerInterruptFunc() void timerInterruptFunc()
{ {
++timerMultiplier; ++timerMultiplier;
if (timerMultiplier > timerMultiplierMAX) if (timerMultiplier > TIMER_MULTIPLIER_MAX)
timerMultiplier = 1; timerMultiplier = 1;
} }
void loop() void loop()
{ {
noInterrupts();
// Send power consumption // Send power consumption
#ifdef POWERCON_ENABLED #ifdef POWERCON_ENABLED
if(timerMultiplier == POWER_TIMER_MULTIPLIER) if(timerMultiplier == POWER_TIMER_MULTIPLIER)
{ {
unsigned int consumption = powerSensor->getConsumption(); unsigned int consumption = powerSensor->getConsumption();
DEBUGF("Read POWERCON_SENSOR= consumption:%d", consumption);
powerSensor->reset(); powerSensor->reset();
powerProtocol->setConsumption(consumption); powerProtocol->setConsumption(consumption);
powerProtocol->send(); powerProtocol->send();
@ -90,6 +97,7 @@ void loop()
{ {
unsigned int temperature = tempSensor->getTemperature(); unsigned int temperature = tempSensor->getTemperature();
unsigned int humidity = tempSensor->getHumidity(); unsigned int humidity = tempSensor->getHumidity();
DEBUGF("Read TEMPERATURE_SENSOR= temperature:%d, humidity:%d", temperature, humidity);
tempProtocol->setTemperature(temperature); tempProtocol->setTemperature(temperature);
tempProtocol->setHumidity(humidity); tempProtocol->setHumidity(humidity);
tempProtocol->send(); tempProtocol->send();
@ -97,14 +105,20 @@ void loop()
#endif #endif
// Handle light sensor // Handle light sensor
#ifdef TEMPERATURE_ENABLED #ifdef LIGHT_ENABLED
if(timerMultiplier == LIGHT_TIMER_MULTIPLIER) if(timerMultiplier == LIGHT_TIMER_MULTIPLIER)
{ {
unsigned int lumen = lightSensor->getLuminosity(); unsigned int lumen = lightSensor->getLuminosity();
DEBUG("Read LIGHT_SENSOR= lumen:%d", lumen);
lightProtocol->setLuminosity(lumen); lightProtocol->setLuminosity(lumen);
lightProtocol->send(); lightProtocol->send();
} }
#endif #endif
interrupts();
DEBUG("Sleeping");
Interrupt::sleep();
DEBUG("Wakeup");
} }

View file

@ -3,7 +3,7 @@
#include <avr/power.h> #include <avr/power.h>
#include <avr/sleep.h> #include <avr/sleep.h>
#include <avr/wdt.h> #include <avr/wdt.h>
#include "HalInterfaces.h"
void emptyFunc(){} void emptyFunc(){}
InterruptFunction Interrupt::callback = emptyFunc; InterruptFunction Interrupt::callback = emptyFunc;
@ -18,19 +18,26 @@ void __interruptHandler__() // the interrupt is handled here after wakeup
noInterrupts(); // disable all interrupts noInterrupts(); // disable all interrupts
(Interrupt::getCallback()) (); (Interrupt::getCallback()) ();
interrupts(); // enable all interrupts interrupts(); // enable all interrupts
Interrupt::wakeUp();
} }
ISR(Timer1_COMPA_vect) // timer compare interrupt service routine ISR(Timer1_COMPA_vect) // timer compare interrupt service routine
{ {
//DEBUG("Timer1e Interrupt");
__interruptHandler__(); __interruptHandler__();
} }
ISR(WDT_vect) { } ISR(WDT_vect)
{
//DEBUG("WDT Interrupt");
//wdt_disable();
__interruptHandler__();
}
void Interrupt::wakeUp() void Interrupt::wakeUp()
{ {
wakeUpNow = true; wakeUpNow = true;
} }
@ -61,15 +68,16 @@ void Interrupt::sleep()
power_timer2_disable(); power_timer2_disable();
power_twi_disable(); power_twi_disable();
*/ */
while( ! Interrupt::wakeUpNow) //while( ! Interrupt::wakeUpNow)
{ //{
wdt_reset();
sleep_mode(); // here the device is actually put to sleep!! sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
} //}
sleep_disable(); // first thing after waking from sleep: sleep_disable(); // first thing after waking from sleep:
// disable sleep... // disable sleep...
power_all_enable(); // during normal running time. //power_all_enable(); // during normal running time.
} }
void Interrupt::setupPinInterrupt(int pin) void Interrupt::setupPinInterrupt(int pin)
@ -112,8 +120,7 @@ void Interrupt::setupWatchDogInterrupt(unsigned int milliseconds)
* 1 1 1 Interrupt first, reset on second trigger * 1 1 1 Interrupt first, reset on second trigger
* 0 x x Reset * 0 x x Reset
*/ */
WDTCSR = 0x00; WDTCSR = (1 << WDCE) | (1<<WDE); // enable configuration
WDTCSR |= (1 << WDCE) | (1 << WDIE);
/* WDP3 WDP2 WDP1 WDP0 Number of cycles Typical Time-out time (VCC = 5.0V) /* WDP3 WDP2 WDP1 WDP0 Number of cycles Typical Time-out time (VCC = 5.0V)
* 0 0 0 0 2K (2048) 16 ms * 0 0 0 0 2K (2048) 16 ms
* 0 0 0 1 4K (4096) 32 ms * 0 0 0 1 4K (4096) 32 ms
@ -126,8 +133,7 @@ void Interrupt::setupWatchDogInterrupt(unsigned int milliseconds)
* 1 0 0 0 512K (524288) 4.0 s * 1 0 0 0 512K (524288) 4.0 s
* 1 0 0 1 1024K (1048576) 8.0 s * 1 0 0 1 1024K (1048576) 8.0 s
*/ */
WDTCSR = (1<< WDP0) | (1 << WDP1) | (1 << WDP2); // set the prescalar = 7 WDTCSR = (1 << WDIE) | (1 << WDP3) | (1 << WDP0);
//wdt_disable(); //wdt_disable();
@ -155,7 +161,7 @@ void Interrupt::setupTimerInterrupt(unsigned int milliseconds)
*/ */
TCCR1B |= (1 << CS12); // 256 prescaler TCCR1B |= (1 << CS12); // 256 prescaler
TCNT1 = 34286; // preload timer 65536-16MHz/256/2Hz TCNT1 = 34286; // preload timer 65536-16MHz/256/2Hz
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt //TODO: TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts interrupts(); // enable all interrupts
} }