adjustable timer not behaving

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

adjustable timer not behaving

Post by Steve001 »

trying to make an adjustable interrupt driven timer, having an issue that I cannot seam to spot why not working

Think the issue is around the decision loop :? but don't know why

have tried
if Seconds_Count >=
but this didn't work either

is this because the interrupt is not jumping correctly ?
or do I place the seconds count decision within the interrupt ?

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

Hi Steve,
I would also advise to display variables within a new call macro on the LCD for diagnostic purposes.
When all is working you just disable the call macro.
I will take a look and see if I can spot the issue.

Martin

Edit:
There are a couple of things wrong with the timer interrupt.
1) The default for timer interrupt 0 Clock source is

Code: Select all

Transition on T0CKI external pin
.
If left that this, then timer 0 register will only increment by 1 with T0CKI pin changes logic.
You will need to change properties so Clock source is changed to

Code: Select all

Internal Clock
2) Interrupt is set to

Code: Select all

46875.000Hz
This means the interrupt macro
TMR0
is accessed 46875 times a second or once every 1/46875 sec = 21.33 microseconds.
So you will need to change interrupt frequency by selecting the highest prescaller rate.
In the case of timer0 = 1:256
That give an interrupt frequency of 183.105Hz
So you then change within timer interrupt macro

Code: Select all

Int_counter = 20
to

Code: Select all

Int_counter = 183
For much more accurate timing you may want to consider using Timer2 instead of Timer1 since you can alter

Code: Select all

prescaler
,

Code: Select all

post scaler
and

Code: Select all

rollover value
I can help you with that if required.

Martin
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Hi Martin

Will have a go doing that ans see what happens,

What i am trying to do at the moment is make an adjustable timer for my v drive, this timer is for the sample time 1 - 120 seconds when i get it working

For testing i was trying to energise then de energise a LED and when this was working was going to swap this for a stop watch that can be controlled remotely to double check the timings

what is happening at the moment if you set a time say 3 seconds

when simulating i dont get an output and the time = time select decision just jumps to "NO" even when it is true ?

have monitored al sorts of variables in the event viewer box and can see it just by passing the yes part

Hope this makes sense

Steve

Edit just seen your post above will have a look at those - more than likely will need your need help changing to timer 2
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

Hi Steve,
Steve001 wrote:more than likely will need your need help changing to timer 2
No problem.
Steve001 wrote:adjustable timer for my v drive, this timer is for the sample time 1 - 120 seconds when i get it working
I just wanted to see my project was working so I got seconds to increment all the the time and roll over when they reached 65535.
It is easy to set up to send data every fixed amout of seconds using the

Code: Select all

MOD 
function
The format for say every 15 seconds is:

Code: Select all

If: Seconds MOD 15 = 0
So the Yes of decision branch is accessed every 15 seconds.
So what you would need to do is:

Code: Select all

If: Seconds MOD SampleTime = 0
If you allow the seconds to keep ticking up then it can be used for other things as well.

Martin
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Thanks martin

will have a go , need to read up on MOD Function as i am not familiar with that one

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

Hi Steve,
Steve001 wrote:need to read up on MOD Function
Myself then Ben in much more detail covered it from this post.
Ben's post on

Code: Select all

Mon Feb 29, 2016 6:18 pm
will be more relevant to your application.
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Thanks for that martin

Steve
Success always occurs in private and failure in full view.

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Afternoon all

Had brain freeze with MOD and % functions have read bens and martins post mentioned above and got a bit lost with it. :?
has anyone got an ( Easy ) example that I could have a look at ? to try and get my head round what and why things are happening

Regards

Steve
Success always occurs in private and failure in full view.

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: adjustable timer not behaving

Post by LeighM »

Hi Steve,

MOD and % do the same, i.e. give the remainder

Try this ..
mod.jpg
mod.jpg (43.18 KiB) Viewed 14836 times

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

:idea: so if mod is checking for the remainder my decision box to exit the loop should be checking for 0 ?

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

Steve001 wrote:my decision box to exit the loop should be checking for 0
Yes indeed.
For example if you want to update vdrive every 120 seconds you can use:
update evey 120secs.png
(3.2 KiB) Downloaded 9533 times
Martin
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Cannot see what I have done wrong with this :(

Can someone have a look please and advise where I have gone wrong

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

Hi Steve,
I have made some corrections and added a new feature for you.
The main issue was if using any value for mod e.g

Code: Select all

If: Seconds MOD 19 = 0

Code: Select all

If: Seconds MOD 45 = 0

Code: Select all

If: Seconds MOD 200 = 0
etc.
Then condition will also be true for all of the above when seconds is = 0
So I added a condition to ignore 0 seconds:
Ignore 0 seconds.png
(12.14 KiB) Downloaded 9449 times
Also with simulator unassigned variables will be reset to 0
That is not the case with hardware.
They will be assigned with a random value.
if variable is read before it's written then seconds for example will will start off with a random number, so your project will not work as expected.
If that is the case then always set an initial value.
Hope the attached flowchart works better for you.
You also need to bear in mind that simulation is very fast indeed.
so seconds will take milliseconds.
Perhaps if running simulator at maximum speed, set ADC very high, e.g over 200.

Martin
Attachments
new Timer test 30.04.2016.fcfx
(20.02 KiB) Downloaded 258 times
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Hi Martin

Thanks for that will have a look later

Steve
Success always occurs in private and failure in full view.

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Hi Martin,

thank you for amending my chart

Can you please explain the loop

Code: Select all

if temp=200 
and the "no" branch as I don't under stand what it is doing. Is this something to do with the interrupts ?

Not too sure what this is doing either ( && ) , is this if the calculation reminder = 0 and the seconds count is 0 then goto "no"

Code: Select all

(Seconds MOD ADC_Val = 0) && (Seconds <> 0)
Program is working now but the timings are slightly out, I have played with the interrupt count but could only make it worse not better.
Would going to timer 2 as you suggested be better ?

Results of testing

30 sec delay = 29.71 seconds
60 sec delay = 58.58 seconds
120 sec delay = 1:58
180 sec delay = 2:57
255 sec delay = 4:11

I am using this stopwatch connected to my relay to test the timings

http://uk.rs-online.com/web/p/stopwatches/2355059/

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

Hi Steve,
Steve001 wrote:Can you please explain the loop

Code: Select all

if temp=200
There is a 1ms delay within main loop, so if looped 200 times then 200ms has elapsed.
So every 200ms the LCD gets refreshed.
That is all that is doing.
Steve001 wrote:Not too sure what this is doing either ( && ) , is this if the calculation reminder = 0 and the seconds count is 0 then goto "no"
&& is just logical AND
So

Code: Select all

if Seconds MOD ADC_Val = 0
AND seconds does not (<>) = 0 then condtion is true.
Steve001 wrote:Program is working now but the timings are slightly out
Yes that is expected.
Reason being if you look at the interrupt you will see that the frequency is 183.105Hz
1/183.105 * 183 = 0.9994 seconds
so 30 * 0.9994 = 29.82.
There are some overheads and port is not changed instantly so 29.71 can be expected.
With timer 2 you can try the following:

Code: Select all

Prescaler 16

Code: Select all

Postscaler 16

Code: Select all

Rollover 125
That should give an interrupt of 375Hz
Int_Count must be changed from a Byte to an Integer.
Then change

Code: Select all

If: Int_Count = 183

to

Code: Select all

If: Int_Count = 375
If still not accurate enough then increasing

Code: Select all

Rollover
will increase frequency, so less time will have elapsed.

Martin
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Hi Martin

Thanks for your help with my timer
Tuesday my next module starts at uni so packing up my bits today as I get too distracted :lol:
so I wont be able to have a go for about 6 - 8 weeks , will have another go later on

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

No problem Steve.

Hope your course goes well.

Martin
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

Thanks Martin

Just done a quick Autonomy calculation using some typical batteries and a load .

This came out at 13:06 Mins

I am wanting to plot this discharge with my vdrive, on a battery this size no point plotting every second so was thinking every min or so maybe more .

So after this length of time at 0.9994 seconds after 13 hours will be approx 280 seconds out ? or have i got this wrong

Would i be better after my time delay reading my RTC and time stamping on the vdrive ?

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

With a project I did, I used the RTC.
Sent date and time stamp to Vdrive.
Much more accurate than using timer interrupt as I wanted to log every 15mins on the dot.
Martin

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: adjustable timer not behaving

Post by Steve001 »

How would i check the rtc on a variable delay to decide to take a measurement and then write to the Vdrive ?

Not done anything with RTC's yet or i2c :(

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: adjustable timer not behaving

Post by medelec35 »

Hi Steve,
Steve001 wrote:How would i check the rtc on a variable delay to decide to take a measurement and then write to the Vdrive ?
Sent you a PM regarding that.

Martin
Martin

Post Reply