Page 1 of 2

LED Flashing - delay problem

Posted: Sat Jun 12, 2021 12:51 pm
by Frank607
I’m trying to make a simple program to let flash some LEDs. I have LED flasher with four channels and must do the following: (all channels must start at the same time – power on)

Code: Select all

CH 1: On 1 min -  Off 3 min
CH 2: Off 1 min - On 1 min - Off 2 min
CH 3: Off 2 min - On 1 min flashing and 90 fpm - Off 1 min 
CH 4: Off 3 min - On 1 min
In my flowchart it is not working and I think this has to do with all the delays what I use. I can’t figure out on how to do this is in a other way.

Attached is my flowchart.
TEST LED Flash 01.fcfx
(14.6 KiB) Downloaded 149 times

Re: LED Flashing - delay problem

Posted: Sat Jun 12, 2021 5:20 pm
by mnf
The delays 'block' so - here you'll have one channel run - then the 2nd...

What I would suggest doing is having a timer interrupt incrementing a timer (for the sake of simplicity here lets say at 1s intervals)

Then have in the main loop:

if (tick < 60) Set Channel1 on, Set Channel2 off.
if (tick > 60) SetChannel1 off
If (tick < 120) Set channel3 off.

etc

Reset the 'tick' to 0 at 4 minutes (240s)

This is a (slightly) simplistic overview of what you need - and actually you'll need a higher resolution 'tick' for the flashing LED (so 90fpm = 60/180 = 1/3 second (90 times on and 90 times off)

I can create a simple Flowchart demonstrating this if it helps...

Martin

Re: LED Flashing - delay problem

Posted: Mon Jun 14, 2021 8:40 am
by Frank607
Hi Martin,

Yesterday the forum had a problem to access it.

Yes if you would me than I really appreciate that.

Re: LED Flashing - delay problem

Posted: Mon Jun 14, 2021 1:35 pm
by mnf
Yes, something odd with the forums (fora?) yesterday - powercut or DDOS???

I'll tray and make an example program this evening...

Martin

Re: LED Flashing - delay problem

Posted: Mon Jun 14, 2021 6:55 pm
by mnf
Okay - a simple demonstration (works okay in SIM and should work better on hardware - timings may vary in SIM depending on your PCs performance)
led test.fcfx
(19.18 KiB) Downloaded 158 times
Note that I've 'simplified' slightly for the sake of clarity - the timer actually runs at 30.518Hz - so the number of ticks in minute would actually be 1832 not the 1800 I've used here.
I think I got the maths right for the flash...

As an aside - this 'sets' or 'clears the pins many times per second (usually not changing the current state) - if this was undesirable, then set a flag for the LED state and just turn on or off if the state has changed.. I would suggest adding a delay to the loop in main too.


Martin

Re: LED Flashing - delay problem

Posted: Sat Jun 19, 2021 6:44 pm
by Frank607
Hi Martin,

I really appreciate your help. Today I had time to work with this but then I get a phone call for a job. So maybe tomorrow morning I have time.

I’m sorry for this.

Re: LED Flashing - delay problem

Posted: Mon Jun 21, 2021 12:08 pm
by Frank607
Hi Martin,

I have tested your flowchart in hardware but what I see is this:
LED1 turns on and stays on. It never goes to the next step for LED2.

Re: LED Flashing - delay problem

Posted: Mon Jun 21, 2021 1:41 pm
by mnf
Hi Frank,

What hardware are you using? - as per chart above or changed from that?

The program relies on ticks being incremented by the interrupt to mark the passage of time.... So if the interrupt isn't working for some reason (or clock is dramatically slower - say MCU running at 4MHz instead of 40) Does a 1s blinkie run correctly?

Martin

Re: LED Flashing - delay problem

Posted: Tue Jun 22, 2021 1:41 pm
by medelec35
I just had a look at the flowchart.
You will need to go into the interrupt properties (Double left-click on the Interrupt icon) and change Clock Source Select from Transition on T0CKI pin to Internal Clock (CLKO)

Re: LED Flashing - delay problem

Posted: Tue Jun 22, 2021 2:22 pm
by mnf
Thanks Martin,

Thought I'd gone with the clock interrupt... Interesting that the simulation worked 🤔

Martin

Re: LED Flashing - delay problem

Posted: Tue Jun 22, 2021 2:56 pm
by medelec35
mnf wrote:
Tue Jun 22, 2021 2:22 pm
Interesting that the simulation worked
This is something I have mentioned before.
The simulation will ignore the interrupt clock source and always treat it as internal.
it has been like that from the early days.
It has caught me out as well.

Re: LED Flashing - delay problem

Posted: Fri Jun 25, 2021 5:17 pm
by Frank607
I will try every sugestion tomorrow. Thanks !!

Re: LED Flashing - delay problem

Posted: Sat Jun 26, 2021 9:54 am
by Frank607
Hi Martin,

I have make the change in the timer interrupt. It looks much more better now.
( I use the PIC12F629)

Only the flashing LED is “out of control” See video on YouTube

https://youtu.be/TgyXV9eJYTw

Re: LED Flashing - delay problem

Posted: Sat Jun 26, 2021 10:21 am
by mnf
Glad it's (sort of) working for you.

The flashing was a bit odd in simulation too - and I thought it was just timing issues there..

I'll have another play later today - but you'll maybe have it working by then.....

Martin

Re: LED Flashing - delay problem

Posted: Sat Jun 26, 2021 8:35 pm
by mnf
I think the problem is - the program might execute the main loop multiple times per increment of the timer... (this is less of problem in simulation as it runs a lot slower than the MCU)

One solution is to keep a record of where the 'change' time occurs and only change once...
led test 2.fcfx
(19.58 KiB) Downloaded 96 times
Martin

Re: LED Flashing - delay problem

Posted: Sun Jul 04, 2021 11:31 am
by Frank607
Hi Martin,

Thanks. But it seems that the modification didn’t help. See video:
https://youtu.be/wnuEeJsAlrU

Frank

Re: LED Flashing - delay problem

Posted: Mon Jul 05, 2021 1:32 pm
by Benj
Hello,

How about this, just a few slight changes to simplify the calculations and to change the clock source of the timer interrupt.
led test 2.fcfx
(22.95 KiB) Downloaded 145 times

Re: LED Flashing - delay problem

Posted: Mon Jul 05, 2021 5:55 pm
by Frank607
Hi Martin,

Thanks. I think there is no space any more in this PIC12629.

Code: Select all

using updated 32-bit floating-point libraries; improved accuracy might increase code size
led test 2.c: 498: (1360) no space for auto/param main@FCL_T_CHANGE
(908) exit status = 1
(908) exit status = 1

Error returned from [xc8.exe]
I will change this SMD chip for a PIC12F1840. (this one I have)

I will do this tomorrow.

Re: LED Flashing - delay problem

Posted: Sun Jul 11, 2021 10:39 am
by Frank607
Hi Martin,

I have changed the chip into a PIC12F1840.

The program was running very slow, so I decide to add an OSCCON value in the flowcode.

OSCCON=0x70 is going too fast.
OSCCON=0x60 is going too slow. (One minute is two minutes)

What can I chance more ?
led test 3.fcfx
(19.86 KiB) Downloaded 124 times

Re: LED Flashing - delay problem

Posted: Sun Jul 11, 2021 1:36 pm
by medelec35
Hi Frank.
I have modified your code to make timing more accurate.
First I set the internal OSC speed to 16MHz which is

Code: Select all

OSCCON=0x78;
Note. The oscillator speed of 12F1840 can go up to 32MHz.
If you use the component search and enter intosc, the Internal oscillator helper will be shown and can then be added to your panel.
Within properties of Internal oscillator helper, select the osc speed you want your microcontroller to run at.
After selecting 16000000 the results are:
Int osc.png
Int osc.png (14.5 KiB) Viewed 17646 times
You can just select, copy and paste the code section.
The next step is getting the timing more accurate.
For that, you will require the lowest whole number for the interrupt.
Timer 2 is very handy for that purpose.
If you download the spreadsheet from here
The quickest way to get a result is to enter the correct osc frequency of 16000000.
For the Req Val, if that is not known just enter any random number e.g 50
Select the drop-down arrow for whole No and unselect Blanks
Scroll down until you see the prescaler that matches the highest prescaler of your microcontroller.
I.e
Prescaler.png
Prescaler.png (7.65 KiB) Viewed 17646 times
.
These the result to enter into timer 2 interrupt properties:
Timer2 settings.png
Timer2 settings.png (5.68 KiB) Viewed 17646 times
100 is the lowest whole number which means higher accuracy.
Timer 2 interrupt.png
Timer 2 interrupt.png (38.96 KiB) Viewed 17646 times
I have attached a flowchart for you to try.

Re: LED Flashing - delay problem

Posted: Mon Jul 12, 2021 2:14 pm
by Frank607
Martin,

Thanks for the explanation. You have set the timer to 100Hz, that is the slowest what is possible if I understand it very well. Also in the excel sheet I don’t see any lower value than 100Hz with a Prescaler Rate of 16.

I ask this because the whole programs runs now in some three seconds. So, too fast.

Re: LED Flashing - delay problem

Posted: Mon Jul 12, 2021 3:53 pm
by medelec35
Hi Frank,
Sorry my mistake.
I got distracted and thought I had finished the flowchart when I had not.
Attached is a completed version.

Re: LED Flashing - delay problem

Posted: Wed Jul 14, 2021 12:06 pm
by Frank607
Ok, this looks beter :-)

The flash is also better but still has some errors.

See this movie:

https://youtu.be/M4aWtjW6UZU

Thanks.

Re: LED Flashing - delay problem

Posted: Wed Jul 14, 2021 4:01 pm
by medelec35
Hi Frank,
I have not modified that apart of the code from adjusting some numbers.
I could have a look in to that part.
I was just concentrating on the timing using Timer2.
How was the timing in minutes?

Re: LED Flashing - delay problem

Posted: Wed Jul 14, 2021 9:25 pm
by medelec35
Hi, Frank.
I have now modified the code to flash at 90FPM = 1.5Hz