Fixed WDT timer code, still not dynamic
This commit is contained in:
parent
2ba3c42a5e
commit
20023c48e0
5 changed files with 69 additions and 30 deletions
|
|
@ -1,24 +1,27 @@
|
|||
#ifndef HALCONFIGURATION_H
|
||||
#define HALCONFIGURATION_H
|
||||
|
||||
#define ENABLE_DEBUG // comment out to disable debug
|
||||
|
||||
#define TIMER_MILLISECOND 60*1000 // poling in minutes
|
||||
|
||||
// POWER CONSUMPTION SENSOR
|
||||
#define POWERCON_ENABLED // comment out to disable sensor
|
||||
#define POWERCON_SENSOR SensorBH1750()
|
||||
#define POWERCON_PROTOCOL ProtocolOregon(118)
|
||||
#define POWER_TIMER_MULTIPLIER 1 // poling in minutes
|
||||
#define POWER_TIMER_MULTIPLIER 1
|
||||
|
||||
// TEMPERATURE SENSOR
|
||||
#define TEMPERATURE_ENABLED // comment out to disable sensor
|
||||
#define TEMPERATURE_SENSOR SensorDHT11(5)
|
||||
#define TEMPERATURE_PROTOCOL ProtocolOregon(100)
|
||||
#define TEMPERATURE_TIMER_MULTIPLIER 1 // poling in minutes
|
||||
#define TEMPERATURE_TIMER_MULTIPLIER 1
|
||||
|
||||
// LIGHT SENSOR
|
||||
//#define LIGHT_ENABLED // comment out to disable sensor
|
||||
#define LIGHT_SENSOR SensorDH1750()
|
||||
#define LIGHT_PROTOCOL ?
|
||||
#define LIGHT_TIMER_MULTIPLIER 1 // poling in minutes
|
||||
#define LIGHT_TIMER_MULTIPLIER 1
|
||||
|
||||
|
||||
#endif // HALCONFIGURATION_H
|
||||
|
|
|
|||
|
|
@ -1,6 +1,22 @@
|
|||
#ifndef 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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,22 +2,17 @@
|
|||
A interrupt based sensor device that reads multiple sensors and transmits
|
||||
the data to a central location.
|
||||
*/
|
||||
#if (ARDUINO >= 100)
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#endif
|
||||
#include <Wire.h>
|
||||
|
||||
#include "HalInterfaces.h"
|
||||
#include "HalDefinitions.h"
|
||||
#include "HalConfiguration.h"
|
||||
#include "HalInterfaces.h"
|
||||
#include "HalInclude.h"
|
||||
#include "Interrupt.h"
|
||||
|
||||
|
||||
Interrupt* interrupt;
|
||||
|
||||
unsigned int timerMultiplierMAX;
|
||||
#define TIMER_MULTIPLIER_MAX \
|
||||
POWER_TIMER_MULTIPLIER * TEMPERATURE_TIMER_MULTIPLIER * LIGHT_TIMER_MULTIPLIER
|
||||
unsigned int timerMultiplier = 0;
|
||||
|
||||
// Sensors
|
||||
|
|
@ -36,12 +31,13 @@ void timerInterruptFunc();
|
|||
|
||||
void setup()
|
||||
{
|
||||
timerMultiplierMAX = POWER_TIMER_MULTIPLIER * TEMPERATURE_TIMER_MULTIPLIER * LIGHT_TIMER_MULTIPLIER; // Find a lowest common denominator
|
||||
Interrupt::setCallback(timerInterruptFunc);
|
||||
Interrupt::setupTimerInterrupt(60*1000); // one minute scheduled interrupt
|
||||
#ifdef ENABLE_DEBUG
|
||||
Serial.begin(9600);
|
||||
#endif
|
||||
|
||||
// Setup Sensors and protocols
|
||||
#ifdef POWERCON_ENABLED
|
||||
DEBUG("Setup POWERCON_SENSOR");
|
||||
powerSensor = new POWERCON_SENSOR;
|
||||
powerSensor->setup();
|
||||
powerProtocol = new POWERCON_PROTOCOL;
|
||||
|
|
@ -49,6 +45,7 @@ void setup()
|
|||
#endif
|
||||
|
||||
#ifdef TEMPERATURE_ENABLED
|
||||
DEBUG("Setup TEMPERATURE_SENSOR");
|
||||
tempSensor = new TEMPERATURE_SENSOR;
|
||||
tempSensor->setup();
|
||||
tempProtocol = new TEMPERATURE_PROTOCOL;
|
||||
|
|
@ -56,28 +53,38 @@ void setup()
|
|||
#endif
|
||||
|
||||
#ifdef LIGHT_ENABLED
|
||||
DEBUG("Setup LIGHT_SENSOR");
|
||||
lightSensor = new LIGHT_SENSOR;
|
||||
lightSensor->setup();
|
||||
lightProtocol = new LIGHT_PROTOCOL;
|
||||
lightProtocol->setup();
|
||||
#endif
|
||||
|
||||
DEBUG("Setup INTERRUPT");
|
||||
Interrupt::setCallback(timerInterruptFunc);
|
||||
Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt
|
||||
|
||||
DEBUG("Ready");
|
||||
}
|
||||
|
||||
|
||||
void timerInterruptFunc()
|
||||
{
|
||||
++timerMultiplier;
|
||||
if (timerMultiplier > timerMultiplierMAX)
|
||||
if (timerMultiplier > TIMER_MULTIPLIER_MAX)
|
||||
timerMultiplier = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
noInterrupts();
|
||||
|
||||
// Send power consumption
|
||||
#ifdef POWERCON_ENABLED
|
||||
if(timerMultiplier == POWER_TIMER_MULTIPLIER)
|
||||
{
|
||||
unsigned int consumption = powerSensor->getConsumption();
|
||||
DEBUGF("Read POWERCON_SENSOR= consumption:%d", consumption);
|
||||
powerSensor->reset();
|
||||
powerProtocol->setConsumption(consumption);
|
||||
powerProtocol->send();
|
||||
|
|
@ -90,6 +97,7 @@ void loop()
|
|||
{
|
||||
unsigned int temperature = tempSensor->getTemperature();
|
||||
unsigned int humidity = tempSensor->getHumidity();
|
||||
DEBUGF("Read TEMPERATURE_SENSOR= temperature:%d, humidity:%d", temperature, humidity);
|
||||
tempProtocol->setTemperature(temperature);
|
||||
tempProtocol->setHumidity(humidity);
|
||||
tempProtocol->send();
|
||||
|
|
@ -97,14 +105,20 @@ void loop()
|
|||
#endif
|
||||
|
||||
// Handle light sensor
|
||||
#ifdef TEMPERATURE_ENABLED
|
||||
#ifdef LIGHT_ENABLED
|
||||
if(timerMultiplier == LIGHT_TIMER_MULTIPLIER)
|
||||
{
|
||||
unsigned int lumen = lightSensor->getLuminosity();
|
||||
DEBUG("Read LIGHT_SENSOR= lumen:%d", lumen);
|
||||
lightProtocol->setLuminosity(lumen);
|
||||
lightProtocol->send();
|
||||
}
|
||||
#endif
|
||||
|
||||
interrupts();
|
||||
|
||||
DEBUG("Sleeping");
|
||||
Interrupt::sleep();
|
||||
DEBUG("Wakeup");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
#include <avr/power.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/wdt.h>
|
||||
|
||||
#include "HalInterfaces.h"
|
||||
|
||||
void emptyFunc(){}
|
||||
InterruptFunction Interrupt::callback = emptyFunc;
|
||||
|
|
@ -18,19 +18,26 @@ void __interruptHandler__() // the interrupt is handled here after wakeup
|
|||
noInterrupts(); // disable all interrupts
|
||||
(Interrupt::getCallback()) ();
|
||||
interrupts(); // enable all interrupts
|
||||
Interrupt::wakeUp();
|
||||
|
||||
}
|
||||
|
||||
ISR(Timer1_COMPA_vect) // timer compare interrupt service routine
|
||||
{
|
||||
//DEBUG("Timer1e Interrupt");
|
||||
__interruptHandler__();
|
||||
}
|
||||
|
||||
ISR(WDT_vect) { }
|
||||
ISR(WDT_vect)
|
||||
{
|
||||
//DEBUG("WDT Interrupt");
|
||||
//wdt_disable();
|
||||
__interruptHandler__();
|
||||
}
|
||||
|
||||
|
||||
void Interrupt::wakeUp()
|
||||
{
|
||||
|
||||
wakeUpNow = true;
|
||||
}
|
||||
|
||||
|
|
@ -61,15 +68,16 @@ void Interrupt::sleep()
|
|||
power_timer2_disable();
|
||||
power_twi_disable();
|
||||
*/
|
||||
while( ! Interrupt::wakeUpNow)
|
||||
{
|
||||
//while( ! Interrupt::wakeUpNow)
|
||||
//{
|
||||
wdt_reset();
|
||||
sleep_mode(); // here the device is actually put to sleep!!
|
||||
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
|
||||
}
|
||||
//}
|
||||
sleep_disable(); // first thing after waking from sleep:
|
||||
// disable sleep...
|
||||
|
||||
power_all_enable(); // during normal running time.
|
||||
//power_all_enable(); // during normal running time.
|
||||
}
|
||||
|
||||
void Interrupt::setupPinInterrupt(int pin)
|
||||
|
|
@ -112,8 +120,7 @@ void Interrupt::setupWatchDogInterrupt(unsigned int milliseconds)
|
|||
* 1 1 1 Interrupt first, reset on second trigger
|
||||
* 0 x x Reset
|
||||
*/
|
||||
WDTCSR = 0x00;
|
||||
WDTCSR |= (1 << WDCE) | (1 << WDIE);
|
||||
WDTCSR = (1 << WDCE) | (1<<WDE); // enable configuration
|
||||
/* 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 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 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();
|
||||
|
||||
|
|
@ -155,7 +161,7 @@ void Interrupt::setupTimerInterrupt(unsigned int milliseconds)
|
|||
*/
|
||||
TCCR1B |= (1 << CS12); // 256 prescaler
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue