Timer counter erratic in '2343
2008-06-09 by John Johnson
Hello All,
I'm glad you all, and this group, are here. I hope I will be able to
contribute to this group myself.
I'm working on a small project to keep our coffee pots from boiling
dry at the office.
I'm using an AT90S2343-10PU (I think it's a PU) that I have left over
from previous piddling.
The problem is, sometimes it seems to run in slow motion, and
sometimes it is okay. I cycle the power, and it will run at the
correct speed one time, then a few times in "slow mode", then it's
okay again.
I reduced the WinAVR/gcc code down to a simple ISR that counts a
variable down. When it's 0, it changes an LED's state. It should be
one second on, one second off. When it goes into "slow mode" it is
more like 6 seconds on, 6 seconds off.
The code (from memory) goes something like:
#define F_CPU 7372800
#include // avr stuff
#define OVFS_PER_SEC (F_CPU / 64 / 256)
volatile int16_t ovfs;
volatile uint8_t secs;
volatile uint8_t mins;
volatile uint16_t hours;
ISR(timer overflow 0) // I forget the exact signal name
{
if (ovfs-- <= 0) {
ovfs = OVFS_PER_SEC;
secs++;
}
}
void ioinit()
{
TCCR0 = _BV(CS1) | _BV(CS0); // or whatever clk/64 is
TIMSK = TOIE0;
sei();
}
int
main(void)
{
uint8_t old_secs;
ioinit();
for(;;) {
_delay_ms(250);
if (secs != old_secs) {
if (secs >= 60) {
secs = 0;
mins++;
// mins and hours processing
}
if (secs & 0x01) {
PORTB |= 0x01;
}
else {
PORTB &= 0xFE;
}
}
}
}
That is the essence of the code.
As far as hardware, it's on a solderless breadboard. I've added bypass
caps all over the place, a 100nf from gnd, across the IC to Vcc. I
have 100nf, 1uf and 10uf on the supply strips (tried a 470uf too).
I've tried a linear, as well as a switching 5v supply. Added inductors
in series with the supply line. Added a bypass cap for the oscillator
(which I verified runs at the right frequency). Tied reset to Vcc.
Incidentally, grounding reset does not cure the problem. It starts
back in "slow mode" if it already was running slow.
Any help greatly appreciated!
--
Regards,
JJ
http://www.pcbgcode.org