In my current project I am developing I recently moved up from using a Freetronics Ether-Ten(328) to a Freetronics Ether-Mega(2560) board.

In my software I make use of a timer library to do two things;

Generate a 100mSec interrupt for calling a routine,

and also to generate a PWM signal from the output of a PID control loop.

I use the library to generate a PWM output that is far slower than the standard core PWM as I use it to control a SSR (Solid State Relay).The SSR is a 240VAC 25 Amp unit and will not operate with the fast built-in PWM.As part of the 100mSec interrupt routine, I set the PWM value, so in essence, this is as fast as I switch it, PWM with a 100mSec period.

With the 328 board I used the TimerOne Library, and all appeared to be problem free.Using the 2560 Mega board I shifted to using the TimerThree Library, essentially the same, just catering for the 2560.TimerOne and TimerThree library http://arduino.cc/playground/Code/Timer1

What I am experiencing is the following;The 100mSec interrupt generated by the timer library works fine, no problems there at all.But, I do have a problem with the generation of PWM in that after a period, typically a few days it appears to cease generating any PWM output.This is a real problem for me as the PID control loop that drives the PWM which in turn operates the SSR which is dumping excess energy from my micro-hydro turbine.My PID setpoint is 27.6Vdc for the battery bank, so when the PWM sigal stops and the PID loop continues trying to drive it, of course my battery volts go up, potentially too high.

I have searched for anyone else having such a problem using the TimerOne / TimerThree libraries, but haven't come across anything.

Below is my main program, main.cpp which is the section that initialises the TimerThree interrupt and attaches a routine as well as sets up the PWM.Specifically, in the setup section, see the following code;

Timer3.initialize(100000);// initialize timer1, and set a 100 milli second period:Timer3.pwm(pin_TurbineDump, 0);// pre-initialise the pwm on pin 2 and set for 0% duty cycle:Timer3.attachInterrupt(timerInterrupt);// attach interrupt to timerInterrupt routine, used for PID and other timing control:

And then near the endof my code where I use the PID value to set it as the PWM value.The PID output value has already been scaled to fall in the range of 0 to 1023 which is the range of the PWM input.

udpStart();// start UDP service for getting NTP formatted time for software RTC:

Timer3.initialize(100000);// initialize timer1, and set a 100 milli second period:Timer3.pwm(pin_TurbineDump, 0);// pre-initialise the pwm on pin 2 and set for 0% duty cycle:Timer3.attachInterrupt(timerInterrupt);// attach interrupt to timerInterrupt routine, used for PID and other timing control:}

if (hydroPID.cvPID > 0) {loadStateHydro = true;// code for displaying load state on web and on led:}else {loadStateHydro = false;}digitalWrite(pin_TurbineDumpLed, loadStateHydro);Timer3.setPwmDuty(pin_TurbineDump, hydroPID.cvPID);// set the pwm with the output of the pid output to control the SSR: *** problem appears to be here***}

This happens in quite a few places, it's obviously copy/paste from the Timer 1 code.

Having said that though, the bits are probably the same so it is probably not the issue. However to be neat the library should be corrected so that the Timer 3 bit names are used for Timer 3 and not the Timer 1 ones.

Quote

But, I do have a problem with the generation of PWM in that after a period, typically a few days it appears to cease generating any PWM output.

This sounds to me like an overflow issue in your code. How many days exactly?

Please post technical questions on the forum, not by personal message. Thanks!

No string class is used anymore, after experiencing the often spoke of problems with string class, I threw them all out.

I'm uncertain that it is an overflow, which was my original thought, which is what typically happens with issues that pop up after a time period.Quite simply, I set the PWM with a value between 0 and 1023 which is what it requires.I don't see anywhere where I am accumulating to lead to overflow.

But, what I have noticed, is a difference between Timer1 and Timer3 libraries apart from the differences you have noted Nick.

Spot the diffeence near the top, timer3 has "(F_CPU * microseconds) / 2000000" which I spoted a while back as I could see that that would lead to overflow conditions.In fact, initially having Timer3 in original form, I noticed that the things were not working, which lead me to make that change.So I changed it to how it was in Timer1 where I do the division before multiplication.

So, with this change, things started to work, but the problem I posted about exists.

I haven't noticed that the problem occurs at precise periods as yet or due to any observable event, just that I get no PWM output.When the issue occurs, I do know that the 100mSec interrupt routine continues to be called as I can still see updated real-time data via my web page as I read sensor data in that interrupt period, so I am thinking that all other functions are performing normally.Even the LED output on digital pin 8, "pin_TurbineDumpLed" called just before I set the PWM operates and tells me that I should be getting PWM output.

Call me old-fashioned but I prefer to work with the actual values in situations like this.

In this particular case 16000000 / 2000000 will be 8, which is nice and even, so no problems there.

Yes I agree Nick, though if we use Timer3 with how it is originally we will not be doing the division first, but multiplying 16000000 by the value of microseconds and then dividing by 2000000.In my setup function I do the following;

Timer3.initialize(100000);Thereby setting the value of the variable in Timer3 to 100000 microseconds being the 100 milliseconds for my interrupt period.I am guessing that if we were to take the original Timer3 code where it would multiply 16000000 by 100000 giving 1.6 x 10^12 which in this case does not fit withing the declared data 'long' type of microseconds.Long data type is 2^32 which is only 2 and a wee bit x 10^9, so we are out by a long shot on the original code.Changing it so that effectively we have as you say 8 x 100000 does fit and does work, and I have seen this to be the case.This is just an error in the 'Timer3' library that others should be aware of.

But for other people following, remember the above is nothing to do with my existing problem, whereby the PWM signal from OCR3B simply ceases after some period of days of good operation.

I do this so as be certain that on startup I am not presenting a value to the PWM output.You may well be correct in thinking that it is not needed, as the PID control loop will feed the required value to the PWM function.I guess belt and braces here a little bit doesn't hurt, and for me it is definite.

Rob,

Quote

It seems to me that it's either in the timer PWM code or the PID code. Can you verify one way or the other by checking that the timer is getting a correct value, say printing the value.I guess that means waiting a few days.Another option is to apply a sanity test to the PWM value, might be difficult though of the range is 0 to 100%.

Where I do perform range checks in the PID function.You can see that my data structure holds the range, min of 0 and max of 1023 which is used in the last part of the above code.So the PID output value, 'PID.cvPID' will then be constrained to those limits.On the web page you see it as 0 - 100% on that dial guage you see, where as in the Arduino it is processed as 0 to 1023.

To wait a few days will mean I will need to cut that trace on the pcb that resets the cpu everytime I connect with the terminal program.That might be a good thing anyhow for me, as it's generally a nuisance to have the Arduino reset everytime I want serial stuff.More long term, I am looking more to developing debugs through the Ethernet connection.

Nick,

Quote

Second, after shifting dutyCycle (a 32 bit field) right 10 bits we have 22 bits left. But OCR3B is a 16 bit field. Now maybe the numbers are OK. Can you provide actual figures here?

I don't have any numbers to provide from the 'Timer3' library functions at present,I will look at putting some debug points in the Timer3 function to get closer to the problem area.

Good question, I'm afraid all that PID code is beyond me, in fact I was planning to pick your brains about it when the time comes

I was thinking of applying a sanity test to the value sent to the PWM which I assume is the yellow bit

Timer3.setPwmDuty(pin_TurbineDump, hydroPID.cvPID);

Your problem is that it turns full on and stays that way, (or doesn't turn on when it should) is that the case?

So you can't test for 1023 because that's valid. How about limiting the valid values to say 1000 and trapping anything over that. At least that will tell you that the PID code has a problem.

Maybe test for 1023 over a long period when the voltage is already at 30.

NOTE: Maybe the numbers are the other way around as you are controlling a load dump, so limit to say 20-1023 and test for < 20.

In other words general defensive coding, but despite being entertained by the graphs for the last couple of weeks I don't know your system well enough offer very targeted advice. You and Nick seem to have well analysed the actual timer code.

Hey Rob,I think you know my system pretty well actually, you keeping a midnigh eye on the trend graphs, much like watching the glowing embers of the late night fire eh?

Your suggestions are good and I will think more about putting that conditional test in to see if that helps, maybe this evening when the solar fire dims.

Edit:

Quote

applying a sanity test to the value sent to the PWM which I assume is the yellow bit

Yes, you are correct, that is what I do do in the PID section of code, which I pasted a few posts up.The value of hydroPID.cvPID is clamped to be within 0 to 1023.

Quote

Your problem is that it turns full on and stays that way, (or doesn't turn on when it should) is that the case?

Yes, the problem is that it does not turn on when it is supposed to turn on, thus my battery bank voltage goes up.If the PID ouput is close to zero, then I dump less energy, when it approaches the max of 1023, then I am putting max energy into the dumpload element.The PID works quite well, I will look at tweeking the tuning parameters later on if need be.So, my dump-load LED or watching the web page for turbine PID dumpload % which tells me that there should be PWM being applied to the dump load when there is no PWM is my problem.

Presently it is still dumping energy to the load, around 10% as the sun sets on the Australian bush horizon.

I find that debugging displays usually help. Also in a case like this, isolate it down to a simple sketch with just the Timer3 library in it. Now either that fails after a few days or not. If it fails, there is an issue with the timer (library) or your use of it. If not, the issue is elsewhere, and it just looks like the timer is to blame.

Please post technical questions on the forum, not by personal message. Thanks!

What you say Nick is perfectly true,and my brain tends to think that way when in diagnostic mode.My preference is to debug the real system as from my experience finding tricky problems on half baked systems designed to trap the error usually does not trap the error.

What I have done is to bring some of the values of Timer3, specifically function "setPwmDuty(char pin, int duty)" to be viewable on the web.The debug code now looks like this;

T3 PWM Period should stay as a constant at 12500.The value for T3 Duty Cycle should always equate tothe same value of T3 OCR3B, if not then we have a problem.

Also, I took on your suggestion Nick and modified those values you mentioned in post #4 and right away I noticed no PWM and the PID max'd out at 1023 trying to bring down the battery volts back to setpoint.

Then restoring the back to original and it works again, the PID settles back as the PWM does its thing

So, seemingly no go there. I am reading the datasheet on OCR3 but it is a bit to wade through,especially at 01h15.

At least I can now see the actual OCR3B register value in next to real-time on the web so I can keep an eye on it if it decides to fall over.

I'm just suspicious of all those calculations in the library. Fundamentally the timers are very simple. You have two numbers:

The "count" which gives the period (and thus, the frequency) of the PWM output. Depending on the mode of the timer this is either fixed or in OCRxA.

The duty cycle, which gives the number of "ticks" within the "count" for which the output is high rather than low. The duty cycle has to be lower than the count. Depending on the mode, again, this duty cycle could be in OCRxB.

Example:

Count = 199. Prescaler = 1.

Therefore the period is 12.5 uS (200 * 0.0625 uS). I say 200 because the count is zero-relative.

That being the period the frequency is: 80 KHz. That is: 1 / (200 * 62.5e-9)

(Caveat: this is fast-PWM mode. Phase-correct PWM mode has twice the period and therefore half the frequency).

So far so good. Now valid duty cycles in this example are 0 to 199 where 0 would be "all off" and 199 would be "all on". If, for example, the duty cycle was 220 the timer would not behave as expected because it will never reach the duty cycle count.

However if the duty cycle was 99 then you have a 50% duty cycle (you get 100 out of 200 counts in the "on" state).

Please post technical questions on the forum, not by personal message. Thanks!