Create Timer loop(sec)

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

Moderator: Benj

Post Reply
Steven_SS
Posts: 46
Joined: Wed Jul 18, 2018 6:33 pm
Has thanked: 18 times
Been thanked: 3 times
Contact:

Create Timer loop(sec)

Post by Steven_SS »

So I have this sensor where through PORTB0 will be counting the number of pulses. I want to find frequency in Hz which is pulses/second.
Is there a way I can create some sort of loop like a while loop for 1000 ms(1 second) as a example?? To make that loop run for 1 second.
As in while the time has not reached("time" could be a variable starting at 0) that 1000 ms(1 second) or For 1 second, count the number of pulses there are(loop through the commands in the time being). Or a workaround to accomplish what I am trying to do. I hope I made sense.
Thanks a lot in advance!

Steven
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

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

Re: Create Timer loop(sec)

Post by medelec35 »

Hi Steven,
I created a simple frequency counter here
Can you let me know what version of flowcode you're using?
Basically all you need is a pin change and a timer interrupt.
The timer interrupt will retrieve, display then reset the pin count every second.
Martin

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

Re: Create Timer loop(sec)

Post by medelec35 »

Since Flowchart is using C code, depending on your target device and Flowcode version you may get a compile error.
If that's the case, post your flowchart and I can assist you further.
Martin

Steven_SS
Posts: 46
Joined: Wed Jul 18, 2018 6:33 pm
Has thanked: 18 times
Been thanked: 3 times
Contact:

Re: Create Timer loop(sec)

Post by Steven_SS »

Thank you Martin. I'm using Flowcode V8 and I'm targeting an ATMEGA328P, setting the clock speed to 16000000 Hz.
I am currently trying to fully understand your flowchart being not too familiar with interrupts. As you stated, I basically just need a timer interrupt and pin change but can you explain to me what is going on before the loop? Just with the interrupts part. The pin change part throws me off as well.
And regarding to your last message, I've attached a sample below. Thanks again
Attachments
EthanolSample.fcfx
(46.31 KiB) Downloaded 244 times
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

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

Re: Create Timer loop(sec)

Post by medelec35 »

Pin change interrupt is automatically calls the interrupt macro, when the pin voltage changes voltage from one logic level to another.
All you will be interested in are the two interrupt enables just before the main loop.
All they do is set up the interrupt condition for you.
E.g with timer1 interrupt, if you double click it, then select properties you will see that the interrupt frequency is 50Hz.
This means the TimerISR macro will be is accessed 50 times every second (or once every 1/50 = 2ms )
So if you use a counter within the timer interrupt, every time the counter reaches 50, one second has elapsed so the pin change count is read, and counter and pin count can be reset back to 0.
The Int interrupt is just enabling the pin interrupt that is triggered when the voltage level changes from 0 to 5V (logic 0 to logic 1)
Or the interrupt can be set to detect a change on the pin from logic 1 to logic 0.
Have a go at adding both interrupts.
Martin

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

Re: Create Timer loop(sec)

Post by medelec35 »

I have just checked with your target device.
If you want a more accurate frequency counter then you will need to run with a crystal frequency of 19660800Hz (timer interrupt set to 75.00Hz).
Otherwise is means running timer interrupt at maximum frequency which is wasteful in my opinion.
Martin

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

Re: Create Timer loop(sec)

Post by medelec35 »

For the 16MHz clock frequency I have put something together for you.
As INT0 is used, you will need to connect the pulses source to pin D2.
Attachments
EthanolSample v2.fcfx
(51.24 KiB) Downloaded 231 times
Martin

Steven_SS
Posts: 46
Joined: Wed Jul 18, 2018 6:33 pm
Has thanked: 18 times
Been thanked: 3 times
Contact:

Re: Create Timer loop(sec)

Post by Steven_SS »

I'm sorry for the late response. But thank you for the explanation Martin! I'm incorporating that flowchart into the main .fcfx at the moment. I want to get the frequency(Hz) 10x then average that and finally display it. So I would just place the two interrupts inside of a loop.
medelec35 wrote:For the 16MHz clock frequency I have put something together for you.
As INT0 is used, you will need to connect the pulses source to pin D2.
I was having the pulses source connected to pin B0, or would this not work?
Thanks again
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

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

Re: Create Timer loop(sec)

Post by medelec35 »

Hi Steven,
You're welcome.
Steven_SS wrote:So I would just place the two interrupts inside of a loop.
No, you only have interrupts enabled the once at the start.
The only time you enable again is if the interrupt has to be disabled for a reason.
Steven_SS wrote:I was having the pulses source connected to pin B0, or would this not work?
You will need to change from INT0 which is fixed on D2, to IOC PORT B
It gets a bit more complex as you need to select the pin to interrupt on.
Also, the interrupt will be triggered on both rising and falling edges so the frequency will be doubled.
So you could either Right shift the results by 1:

Code: Select all

Frequency = Frequency >>1
which is the same as

Code: Select all

Frequency = Frequency/2.
but much more efficiently.
Or check the pin level of B0 within IOC port interrupt.
If High then allow

Code: Select all

PinChange = PinChange + 1
, otherwise if its low don't allow

Code: Select all

PinChange = PinChange + 1
Martin

Steven_SS
Posts: 46
Joined: Wed Jul 18, 2018 6:33 pm
Has thanked: 18 times
Been thanked: 3 times
Contact:

Re: Create Timer loop(sec)

Post by Steven_SS »

Gotcha gotcha. I went ahead and changed INT0 to IOC PORT B.
So putting the interrupts at the start, could I still receive a frequency 10 times then?
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

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

Re: Create Timer loop(sec)

Post by medelec35 »

Steven_SS wrote: could I still receive a frequency 10 times then?
Not to sure what you mean?

The display should show actual frequency X10?

Or Loop for 10 times displaying the frequency then stop displaying frequency?
Martin

Steven_SS
Posts: 46
Joined: Wed Jul 18, 2018 6:33 pm
Has thanked: 18 times
Been thanked: 3 times
Contact:

Re: Create Timer loop(sec)

Post by Steven_SS »

I'm trying to grab the frequency 10x then average those 10 times to get one averaged frequency.
So I have an array[10] taking in the frequency(Hz) value 10 times then adding up those frequencies and dividing it by 10 to get the average...
I hope that makes sense
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

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

Re: Create Timer loop(sec)

Post by medelec35 »

Yes, understood you just want to display the average.
If I was you I would use values to the power of 2, e.g *8 or *16
Also it depend on maximum expected frequency if in Hz then adding up the frequency you don't want to exceed the value of UInt (16bit) or Uong (32bit).
Suppose you use UInt, the maximum value is 65535.
65535/10 = 6553Hz
Or using Ulong maximum value = 42924267295 so if /10 maximum frequency is 4292426729 Hz = 429MHz and there is no way the microcontroller can ever get near to measuring that frequency.
So what is the maximum expected frequency?
Martin

Steven_SS
Posts: 46
Joined: Wed Jul 18, 2018 6:33 pm
Has thanked: 18 times
Been thanked: 3 times
Contact:

Re: Create Timer loop(sec)

Post by Steven_SS »

medelec35 wrote: So what is the maximum expected frequency?
Well 100% reading is 150 Hertz so I'm assuming 150 is the max expected frequency so 16bit would be suitable.
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

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

Re: Create Timer loop(sec)

Post by medelec35 »

Indeed, 16bit will be fine.
If you can post your flowchart that has the change from INT0 to IOC, then I will see what I can do for you.
Martin

Steven_SS
Posts: 46
Joined: Wed Jul 18, 2018 6:33 pm
Has thanked: 18 times
Been thanked: 3 times
Contact:

Re: Create Timer loop(sec)

Post by Steven_SS »

Okay, yessir the changes are made that we've discussed.
Again, the goal is to read in the frequency from the sensor 10 times to get 10 HZ values, each time being calculated to find ethanol % and placed into an array[10] and then divide it by 10 to get the total average Ethanol %("Ethanol_Avg").
Attachments
EthanolSample[rev].fcfx
(45.93 KiB) Downloaded 153 times
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

glennmk
Posts: 3
Joined: Sun Aug 26, 2018 1:24 am
Location: AUSTRALIA
Contact:

Re: Create Timer loop(sec)

Post by glennmk »

Hi all, I am new to flowcode8 and I am having issues with a timer 2 interrupt.
I require the timer to run on a 7 hr cycle activating 2 different relays. the pump 1 relay turns on at the start of the cycle, after 2 mins it turns of for the remainder. Pump 2 relay turns on 30 mins into the cycle and remains on for 6hrs and 30mins.

Attached are a schematic and my .fcfx with detailed description of the operations that are required.

Kind regards,
Glen
Attachments
Intellicycle360_Rev.A.pdf
(700.35 KiB) Downloaded 135 times
Intellicycle360_16F18855_fw_ver.1.1.fcfx
(31.7 KiB) Downloaded 125 times

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

Re: Create Timer loop(sec)

Post by medelec35 »

Hi Glen,
Welcome to the forums.
You have not stated what issue you are having.
First thing to do is remove the one second delay from the IOC interrupt macro.
Next make sure timing of hardware is correct.
One way is the one second flash test.
I have noticed on your circuit you have not got the two low value (about 22pF) capacitors that connect to each osc pin & GND
Martin

glennmk
Posts: 3
Joined: Sun Aug 26, 2018 1:24 am
Location: AUSTRALIA
Contact:

Re: Create Timer loop(sec)

Post by glennmk »

Hi Martin,
I did the one sec flash test and my HW timing is correct. 19.6608Mhz crystal, with a interrupt of 75Hz ( Btw, the xtal has inbuilt load caps).
With regards to the PWM in the IOC flow, i removed the 1 sec delay before disabling CCP3. This delay makes the piezo "beep" ...I am sure there is another way to do this, but this was my first shot at it :)

The thing that i am stuck with is setting up the timer to do the on/off cycles i mentioned. I think i have correctly initialised the timer, i just dont know how to put the "decisions" together to turn the 2 relays on & off at the required times?
Do i make the timer the "MAIN flow" diagram also?

I hope this makes sense..
and cheers in advance for your assistance!

Glen

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

Re: Create Timer loop(sec)

Post by medelec35 »

Hi Glen,
I have made some changes, but only covered :

Code: Select all

Both blower & sludge pump run on a 7 Hour timer cycle.
At the start of main loop timer cycle , the blower pump is turned off (Port RB1 = 0) and the sludge pump is turned on (Port RB0 = 1) for 2 mins and is then switched off (Port RB0 = 0) for the
remainder of the timer cycle (6Hrs and 58Mins).
30 mins into the timer cycle the blower pump is turned on (Port RB1 = 1) and will remain on for 6Hrs and 30Mins.
Just tot give you an idea on how I would tackle it.
There are different ways it can be done, attached is just one such way.
I have also modified IOC for the CCP3 1 second delay.
Not sure it all is correct, but will give you a general idea.
Attachments
Intellicycle360_16F18855_fw_ver.1.2.fcfx
(35.15 KiB) Downloaded 135 times
Martin

glennmk
Posts: 3
Joined: Sun Aug 26, 2018 1:24 am
Location: AUSTRALIA
Contact:

Re: Create Timer loop(sec)

Post by glennmk »

Hi Martin,
Thanks for modifying my flowcode...you certainly went above and beyond what i was expecting!

cheers,
Glen

Post Reply