From my main program I have cut out the components that relate specifically to TimerThree.
/*-------------------------------------------------------------------------
Timer3_test.cpp
Created on: Dec 14, 2012
Author: Paul Alting van Geusau
A very simple sketch to check the operation of the TimerThree Library
for reliable and continuous PWM operation.
Step 1. initialise the Timer3 for correct mode, PWM on pin 2 with a period of 100 milli second period
Step 2. call a function every 100 milli seconds to call the Timer3.setPwmDuty(..) function
*/
#include "timer3_test.h"
#include "TimerThree.h"
// const type variable = value comment
const uint8_t pin_PWM = 2; // PWM from OCR3B
uint32_t lastIntTime = 0; // last time through the timerInterrupt loop:
const uint32_t intPeriod = 100; // delay period to call timerInterrupt loop:
//-------------------------------------------------------------------------
// void setup()
//
void setup() {
// Serial.begin(115200);
pinMode(pin_PWM, OUTPUT); // configure pin 2 for output:
Timer3.initialize(100000); // initialize Timer3, and set a 100 milli second period:
Timer3.pwm(pin_PWM, 0); // pre-initialise the pwm on pin 2 and set for 0% duty cycle:
}
//-------------------------------------------------------------------------
// void loop()
//
void loop() {
if (millis() - lastIntTime >= intPeriod) { // is it time to call the process100mS routine:
process100mS();
lastIntTime = millis();
}
}
//-------------------------------------------------------------------------
// void process100mS()
// Instead of PID output, we just give it a steady value of 512 for the test
//
void process100mS() {
Timer3.setPwmDuty(pin_PWM, 512); // set the pwm with the output of the pid output to control the SSR:
}
And header file
#ifndef Timer3_test_
#define Timer3_test_
#include "Arduino.h"
// function definitions for the project
void loop();
void setup();
void process100mS();
#endif /* Timer3_test_ */
Edit: remember that I have also made a small change in the TimerThree library in the function setPeriod(..) as mentioned earlier and as follows;
void TimerThree::setPeriod(long microseconds)
{ // (F_CPU * microseconds) / 2000000 was original but changed to (F_CPU / 2000000) * microseconds due to potentail overflow condition.
long cycles = (F_CPU / 2000000) * microseconds;
// rest of function
}
Note: I am using the Freetronics Ether-Mega, so a 2560 based system.
Paul