Looping for desire timing by interrupt needs advice

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
oookey
Posts: 42
Joined: Thu Mar 08, 2012 3:00 pm
Has thanked: 17 times
Contact:

Looping for desire timing by interrupt needs advice

Post by oookey »

Hi all,

I tried to use this circuit to observe the output ON & OFF for 30sec each, but what I see is the output always ON, never OFF. :|

Please advice how should I loop to achieve it, by using the interrupt method? What is the mathematic way to calculate the desire output ON/OFF time?
Thanks :)
Attachments
TryInterruptCycle.fcf
(5.5 KiB) Downloaded 237 times
InterruptOnOff.png
(42.92 KiB) Downloaded 3428 times

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

Re: Looping for desire timing by interrupt needs advice

Post by medelec35 »

Hi oookey,
I have modified your flowchart to toggle LED (Via PWM )every 30 seconds.
You can alter the value of PWM varaible to alter the brighness of LED when it's on.
E.g with PWM = 20 the LED will be dim.
If PWM = 230 then LED will be bright.
I don't have PWM at 100% when controlling LED's at full brightness for the simple reason is pulsed current allows more brightness using less power and less heating of LED's

X Y is not necessary for the toggling, but left them in just in case you require it in your application for doing something else.
If not they both can be deleted.

Let me know if there is anything you're stuck with.

You should be registered with Flowcode V4 section since you're not using Flowcode V4 free version.
But at this time I'm not sure if that still stands as support for V4 has official ended.
I will get clarification on Monday about this.

Martin
Attachments
TryInterruptCycle2.fcf
(6.5 KiB) Downloaded 247 times
Martin

oookey
Posts: 42
Joined: Thu Mar 08, 2012 3:00 pm
Has thanked: 17 times
Contact:

Re: Looping for desire timing by interrupt needs advice

Post by oookey »

Hi Martin,

Thanks for your advice, however i'm still using the FC3, so unable to see the changes u made , please if u could display the code in png format? Thanks.

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

Re: Looping for desire timing by interrupt needs advice

Post by medelec35 »

oookey wrote:i'm still using the FC3,
Oops sorry, I only tried your flowchart with FC4 since you did not state the version.
Since it opened, I wrongly assumed you were running FC4.
So I take back what I said:
medelec35 wrote:You should be registered with Flowcode V4 section
I have attaached a modifed V3 version.
The simulation is very slow compared with other version of flowcode (V6 it lightning fast :lol: )
So for simulation purposes only (defiantly not for sending to hardware), you can just add a call ISR macro to speed simulation up.

Hope this helps,

Martin
Attachments
TryInterruptCycle V2 FC3.fcf
(6.5 KiB) Downloaded 229 times
Martin

oookey
Posts: 42
Joined: Thu Mar 08, 2012 3:00 pm
Has thanked: 17 times
Contact:

Re: Looping for desire timing by interrupt needs advice

Post by oookey »

medelec35 wrote:
So for simulation purposes only (defiantly not for sending to hardware), you can just add a call ISR macro to speed simulation up.

Martin
Thanks Martin! :D

Now i can read the content, i'll try out to the chip later, if it workings on the hardware. :mrgreen:

oookey
Posts: 42
Joined: Thu Mar 08, 2012 3:00 pm
Has thanked: 17 times
Contact:

Re: Looping for desire timing by interrupt needs advice

Post by oookey »

Hi Martin, :)

I replicate to program into the chip, it stays ON all the while, just the same as my early version, in simulation it works ON/OFF according to program, but not on hardware. :?
May be the chip is spoilt :( , i'll come back after try on a new piece.
Thanks

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

Re: Looping for desire timing by interrupt needs advice

Post by medelec35 »

Hi oookey,
I Believe I have spotted the issue.

PPP on Flowcode 3 was not set up, so I did not check configuration settings.

I have now set it up since got a bit more time spare, and it looks like the configuration settings are wrong.
Can you try the attached version, to see if its any better?

Martin
Attachments
TryInterruptCycle V3 FC3.fcf
(6.5 KiB) Downloaded 223 times
Martin

oookey
Posts: 42
Joined: Thu Mar 08, 2012 3:00 pm
Has thanked: 17 times
Contact:

Re: Looping for desire timing by interrupt needs advice

Post by oookey »

Hi Martin,
Thanks alot, you been very helpful, :D
The LED is ON/OFF at the rate of 32sec each mode.

But i 'm curious, where went wrong to my version that did not work accordingly, any pointing from you please?

Thanks

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

Re: Looping for desire timing by interrupt needs advice

Post by medelec35 »

Hi oookey ,
Your welcome.
oookey wrote:where went wrong to my version that did not work accordingly, any pointing from you please?
First your configuration settings were not correct.
1) Your microcontroller will be in the permanent reset state if Master CLeaR (MCLR ) is set to external and you have not got a pull-up resistor connected from MCLR pin to +5V (note MCLR can be tied directly to +supply but this is not recommended if programming via ICSP).
You can disable the reset function by changing MCLR in your configuration settings from External to Internal.

2) Watchdog was enabled within configuration settings.
Normally with watchdog enabled, you need place commands to reset WatchDog Timer WDT within a short time period e.g. less than every 18ms or your chip will reset.
This is to prevent lockups.
Your hardware was not running due to 1)
You will need to set Watchdog Timer to off.

Looking at your Flowchart if configuration settings were correct then:
Timer interrupt is set so ISR is accessed every 1/7812.5 = 128uS
all that's happening is while count<1500 (1500*128uS = 0.12sec) the LED is switching on and off for the duty cycle of PWM at the frequency of PWM. 50% duty at 7.8125KHz will mean that LED will look like is on all the time but at about 1/2 its full brightness.
Count2 will increase by 1 every 0.12secs until Count2 is = 5 = 5 * 0.12 = 0.6 seconds.
So after 0.6 seconds both Count and Count2 are reset, PWM is disabled for 3000*128uS = 0.384sec and cycle will start again.

But since settings were not correct then the above will not even take place.
If it did all will be too fast to see.

I believe the best way is set timer interrupt for a much slower interrupt time.
Then within ISR, increment a count that will reset every second, then just increment seconds by 1.
Its not a good idea to keep enabling and disabling PWM.
What I would do is enable PWM before main loop then if LED is to be off set PWM duty to 0.
If LED is to be on at full brightness then set PWM duty for approx 85- 95 %
PWM only has to be faster than persistence of vision frequency (or flicker fusion threshold) .
so it can be any frequency above 60Hz
Personally I use about 1KHz so I'm well above but not too much to cause other issues.

Hope this helped.

Martin
Martin

Post Reply