Create Timer loop(sec)

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

Moderator: Benj

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)

Postby Steven_SS » Fri Nov 30, 2018 11:20 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Fri Nov 30, 2018 11:28 pm

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Fri Nov 30, 2018 11:40 pm

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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)

Postby Steven_SS » Sat Dec 01, 2018 12:09 am

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 52 times
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Sat Dec 01, 2018 12:27 am

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.
These users thanked the author medelec35 for the post:
Steven_SS (Mon Dec 03, 2018 4:20 pm)
Rating: 5%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Sat Dec 01, 2018 12:37 am

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Sat Dec 01, 2018 8:43 am

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 57 times
These users thanked the author medelec35 for the post:
Steven_SS (Mon Dec 03, 2018 6:03 pm)
Rating: 5%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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)

Postby Steven_SS » Mon Dec 03, 2018 6:02 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Mon Dec 03, 2018 9:09 pm

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
These users thanked the author medelec35 for the post (total 2):
Benj (Mon Dec 03, 2018 10:14 pm) • Steven_SS (Mon Dec 03, 2018 11:42 pm)
Rating: 10%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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)

Postby Steven_SS » Mon Dec 03, 2018 11:42 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Thu Dec 06, 2018 10:18 am

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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)

Postby Steven_SS » Thu Dec 06, 2018 5:44 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Thu Dec 06, 2018 6:37 pm

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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)

Postby Steven_SS » Thu Dec 06, 2018 8:33 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8597
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2468 times
Been thanked: 3555 times
Contact:

Re: Create Timer loop(sec)

Postby medelec35 » Fri Dec 07, 2018 3:36 pm

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.
These users thanked the author medelec35 for the post:
Steven_SS (Fri Dec 07, 2018 7:24 pm)
Rating: 5%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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)

Postby Steven_SS » Fri Dec 07, 2018 4:36 pm

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 23 times
I'm just trying to keep learning & growing in a infinite loop... unless I wanna break. Cheers :)
Steven