Code timing ideas

axeman22

Member
Hi all,

may I ask a question which is perhaps somewhat rudimentary but it's doing my head in anyway!

What I wonder is how do you run through a program and do something which takes some amount of time, without stalling the overall program execution to do it. So for example, I have a piece of code which makes a LED flash ON-OFF-ON for 80mS (see below) - there are times where I want to make the time periods 1 second, as opposed to 80mS.

The issue I have with the way I am doing it is that when I am in the middle of a pause etc(which might be 1 second) I'm not able to process other event, like a button push etc. I really want some way to fire off (aka spawn) a process and keep running program execution.

So.. just wondering - is this the challenged faced by all and you just have to deal with it best you can, or is there something fundamental I'm missing. I guess I could calculate the speed of the processor, the number of lines of code and run a loop to control the time periods, as opposed to using pause etc.?

appreciate all input.

Thanks!
for b8 = 1 to 4
high 2
pause 80
low 2
pause 80
next b8​
 

BeanieBots

Moderator
Two main options.
Break your 'pause' down into smaller time units and then 'test' more frequently.
Or, use interrupts. (see "setint").
 

axeman22

Member
hmm.. smaller time blocks makes sense.

I've played with interrupts but I find them to be a bit useless.. perhaps I just need to do more work with them!

I do also have a RTC chip available to me.. and they're cheap but I have to step up to an 18X before I can use them..

sounds silly but with an RTC I worry about missing the moment, it the right seconds etc.. but I guess even at 4Mhz you could read ti in multiple times in one second and practically never miss the moment..

thinking some more.. so I guess with something that you want time critical in some sense you have to have an RTC chip.. fire of events, with hooks to time started in variables.. make program code execution as fast as you can, ie no pauses etc.. and just check the clock often? or build an external circuit - ie a 555 timer which can just be fired from the picaxe etc.

am I on the right wavelength?
 
Last edited:

lbenson

Senior Member
>played with interrupts but I find them to be a bit useless

If you need to handle real-time events instantaneously, interrupts are essential. For insights into how to use the PICAXE to time-slice in small increments, and handle different timings for different simultaneous tasks, check out womai's thread, "Multitasking on Picaxe by using the time interrupt"--http://www.picaxeforum.co.uk/showthread.php?t=8541
 

BeanieBots

Moderator
I've played with interrupts but I find them to be a bit useless..
Oh dear!
IMHO, possibly one of the most powerful features there is!
certainly an obvious solution to acting apon a button press whilst your code is off doing something else even "something else" is only a long pause.
Certainly the BEST method of starting a process at precise time intervals.

Do you use an alarm clock in the morning or do you rely on being awake at about the right time and keep checking the clock periodically to see if it's time to get up?
(I do the latter and really doesn't work very well!)

Have another read about interrupts and another play.
If it's not clear, come back with some questions. They can be a bit daunting at first.
 
Last edited:

Buzby

Senior Member
I have always been a fan of interrupts, but will admit they ( usually ) have a very steep learning curve, and require you to visualise stack manipulation and pointer re-arrangement in your head.

However, those comments apply to 'real' interrupts, the kind you find in raw PICs etc., not to the 'house-trained' interrupts found in a PICaxe.

I would suggest you spend an hour with a PICaxe, two LEDs and a push-button. Have one LED flashing regularly, driven by a simple loop. Make the second LED turn on/off each time you press the button, using an interrupt.

When you get this working you will have mastered 90% of the PICaxe interrupt system, which will cover 99% of most people's requirements.

Have a go, it will only cost an hour !.
 

axeman22

Member
great idea, I will do this!

is it possible to use an interrupt to occur based on a certain time being reached on the RTC. just looking for a Yes/No - if yes I'll try and work it out myself.

:)
 

SD2100

New Member
is it possible to use an interrupt to occur based on a certain time being reached on the RTC.
If you are using the DS1307 RTC you can set the 1307 control register so the SQW output pin pulses once per second, connect the SQW pin to an interrupt pin on the picaxe and when an interrupt occurs then read the time from the 1307 using the i2c connection and compare it to your alarm time, when there's no interrupt then you can go off and do other things.

The SQW output pin on the 1307 is open collector so it has to be pulled high with a 10k resistor or similar so your interrupt needs to trigger when the input pin gets pulled down to 0v
 

BCJKiwi

Senior Member
@ axeman
If a different RTC is used, then alarms and delays can be set. If only one or two times periods need to be in play ant the same time, then these times can be set and acted onin the manner described by Phil.
 
Top