Countdown Timer Using MAX7221

elec_mech

New Member
Hello All,

I'm new to PICAXE but I've written several PBP programs in the past. I'm working with Tracecom to help someone on another forum with a project. The other member doesn't plan to get into uC's much but wants the ability to modify the code so a PICAXE seemed like the ideal choice.

I've written the code as I would have for PBP, with the exception of adding a subroutine to bit-bang the SPI data to a MAX7221. While everything appears to work swimmingly, the timing is off - 10 seconds of countdown requires 15 seconds or more on the display.

In researching this further on this forum, I believe part of the problem is the PICAXE is taking more time than I realized to run the code because of the BASIC interpretation (vs. compiled code that I'm used to working with).

I didn't realize the frequency could be increased on the fly with the setfreq command. I gave that a shot and increased the frequency from 4MHz to 32MHz, the maximum frequency allowed on the 20M2 chip I'm using. This helped immensely, but when tested with a 60 second countdown, actual time was ~62 seconds. Not bad, but the timer could be set up to 99 minutes, so I'd really like to get the timing as close to spot on as possible.

I've attached my code and schematic. Code was too big to post in body of post.

I'm using the button commands in a subroutine and checking them during the countdown. When commented out, a 60 second countdown takes ~61 seconds. I wonder if it would be better set up the buttons as interrupts?

Tracecom has suggested using a 20X2 and I tried rewriting the code to use the SPIOUT command, but display only shows the all segments on test and stays there though the program continues to run (evident by the flashing LEDs). In reading the manual further, the SPIOUT command does the same thing as the bit banging routine I use for the 20M2, so I suppose if it did work, the timing would be about the same. Tracecom brought up the HSPIOUT command which uses a hardware SPI, so I'll give that a shot too. I am hoping to get away with the 20M2 if possible though.

I found a great post by Marks whose done a project very similar to what we are trying to accomplish which does not require a MAX7221, so if all else fails, we may use that (http://www.picaxeforum.co.uk/showthread.php?16020-20X2-4-digit-7-segment-Countdown-timer&highlight=countdown+timer).

Any insight or advice is greatly appreciated. Thank you.
 

Attachments

nick12ab

Senior Member
Welcome to the PICAXE Forum.

The problem is caused by use of the pause command - for this application, the time variable should be used instead. The time variable will be much more accurate.
 

MartinM57

Moderator
"time" variable isn't that accurate either when using the internal oscillator. Could be a few % out - so 62 seconds vs 60 could be all you'd get anyway.

Option is to put a resonator on the PICAXE to fix the PICAXE freq accurately if you're going to stick with a software solution, and then work at getting it more accurate.

Or if hardware is possible, create a 1pps signal and use that to drive an interrupt. In increasing cost and accuracy:
- 555 timer
- DS1307 clock
- DS3232 clock
- GPS unit (the techsupplies uBlox produces a very nice 10ms (or is it 100ms) pulse whose leading edge is within a few nS of UTC GPS time :))
(other h/w solutions no doubt available)

On the other hand, if it's "only" traffic lights, isn't 62 secs close enough to 60 secs anyway?
 

elec_mech

New Member
Wow, thank you everyone! The person we're doing this for is using the traffic light as a sort of fancy kitchen timer in a classroom with the traffic light used to indicate how much time is left. Since the display is in MM:SS format, the total time could be as high as 99 minutes, hence the need for relative accuracy.

Someone else mentioned the use of the seconds pulse out of a DS1307 as an interrupt. I think we'll be giving that a shot. I'll look into the DS3232 - haven't come across that one before.

I've read here and been told by Tracecom that the even among the same models, the timing can vary from chip to chip. An external clock certainly seems like the simplest, most accurate option. I may look to replace the MAX7221 and wire everything from the PICAXE. Of course, now looking at the circuit again, I'd need a larger chip with more I/O's so maybe food for thought in the future.

Thank you all again!
 
Top