AC mains Hz meter

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

Moderator: Benj

Post Reply
Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

AC mains Hz meter

Post by Derrihj »

Hi guys ,really need your help on frequency counter flowchart only in Hz maybe from 0 Hz to 100Hz i want to use it to measure Ac mains and my dc-ac inverter output frequency in my country its 50Hz.I really need to see how its done with the help of a flowchart since am a slow learner . Thanks allot guys.

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: AC mains Hz meter

Post by jgu1 »

Hi Derrih!

I have not tryed to make it before but in my head I would do something like this:

First use an int in in this examble B0 everytime a puls is coming in this count a counter.
Then use a timer interrupt.
The timer interrrupt count up to 1sec. So when one second (Timecount =150) you clear display>write the new value (50hz hopefully) from input> then reset both counter.
I have not test the program, you could try. I am sure there are other way to make this and I mean Martin have created one aerlier try to search.

Hope this could help for a start :D

Br Jorgen
Attachments
FrqCounter.fcfx
(12.28 KiB) Downloaded 158 times

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Thanks allot Jorgen am going to build this and give it a try will let you know only that i think i will have to change one thing that is from rising edge to faling edge take a look at the set up i want to use, don't know if am right about the edge.
Attachments
Screenshot_20210909-161803.png
Screenshot_20210909-161803.png (338.85 KiB) Viewed 21658 times

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Can we tune this up to give us floating point numbers coz i want to get every bit of a change in the frequency example 49.8Hz or something of a kind, if yes how?

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: AC mains Hz meter

Post by jgu1 »

Hi Derrijh!

I see your point, But unfortunately I am not hardcore in programming for this. You need to ask some of the really good programmers here in fora, and I know there are some :wink:

Maybe you could use some calculation from project a guy have made in arduino ide:

https://simple-circuit.com/arduino-freq ... 0-380v-ac/

Br Jorgen

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: AC mains Hz meter

Post by medelec35 »

Hi,
I have created a flowchart by copying the code from the link Jorgen provided.
The LCD pins will need changing for your hardware.
I would not use D0 or D1 If I was you as they are used if UART is required e.g diagnostics, extra data etc.
I have not tested on hardware, so I have no guarantees it will work.
The trimmer looks like it's not wired correctly.
You will need the supply VDD and GND going to the outer ends of the trimmer and the middle wiper goes to VEE of the display.
The optocoupler must be connected to INT0 which is digital pin 2.
I would probably use two 68K resistors in series, instead of a single 47K resistor.
The optocoupler I would choose would be one with the highest CTR value that's reasonably priced.
Just make sure it is a reputable make as that is what is isolating the dangerous mains from the SELV side of the Arduino.
Attachments
AC Frequency Meter.fcfx
(13.76 KiB) Downloaded 168 times
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: AC mains Hz meter

Post by medelec35 »

Just tested the AC frequency meter with a signal generator.
I connected the LCD to pins 8 to 13.
It will perfectly detect frequencies from 31Hz ,in which the LCD displays 31.02 Hz
At 50Hz the LCD displays 50.04 Hz
At 60Hz the LCD displays 60.04 Hz
The Frequency meter will work in the 1000's Hz, but the accuracy is less.
e.g at 3KHz, the display shows 3007.52 Hz.
Martin

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Thanks Martin am going to try it out, am not going to use the Arduino, am to use a pic instead, so from that image above am to take only the mains isolator setup but on the pic not Arduino in my case let me go thru the flowchart and try to understand what you have coded.

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: AC mains Hz meter

Post by medelec35 »

Hi Derrihj.
Derrihj wrote:
Fri Sep 10, 2021 5:57 pm
am not going to use the Arduino, am to use a pic instead
As the image shows an Arduino UNO and the code was for that microcontroller, then the flowchart will only work for that.
If you change target to a PIC then compile you will get a error.
That is because C code is used.
The only way this will work is by reading the microcontroller registers via C code which might not be the same from one PIC to another.
So more work will be required.
Martin

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: AC mains Hz meter

Post by jgu1 »

Hey both!

I knew Martin could. Again our rescue. I look forward to testing. Also thanks for the help from here Martin. :wink:

Nice weekend Jorgen

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

That is because C code is used.
The only way this will work is by reading the microcontroller registers via C code which might not be the same from one PIC to another.
So more work will be required.

I want to use PIC18F442 since i don't have Atmel programmer how can we make this work for this pic.

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: AC mains Hz meter

Post by medelec35 »

Attached is a pic version for the 18F442
As I have not got a 18F442 to test with, I'm unable to verify if it works on hardware or not?
If you are changing the clock frequency then you must change for formula within the calculation box to the new frequency.
I don't recommend using the internal osc as that will affect the accuracy.
For example, I have tried the pic version at 50Hz with both 32MHz internal osc and 19.6608MHz crystal.
The internal osc displays 49.89Hz and the crystal version displays 50.01Hz
Both displays are very stable.
The frequency tracks to 0.01Hz resolution.
Attachments
AC Frequency Meter PIC18F442.fcfx
(16.13 KiB) Downloaded 159 times
Martin

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Wawo! wawo! Thanks allot Martin for your kindness and the time you invest in for our own good,right now i have a trip for 4 days up country, when i get back i have to build this then tell you the results.Thanks May God Bless you richly.

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Hi Martin, am going to build this when i get back, but to understand this clearly, i have some questions (1) What determines the maximum and minimum frequency this can measure? (2) Am asking kindly if it's ok with you to explain what is taking place in the code as in, which crystal to use and not to use (Trade OFFs), the set-ups involved and why they are done like that.Coz i want to use a 20mhz crystal ok you commented out the prescaler (3) Is using the word FLOAT before the variable name another way of converting that variable to float? (4) The calculation boxes and the C icon look appetizing and i really want to understand what is going on there too, Eg: (Frequency,2). I know Frequency = Cycles/ period
I love to understand how your int0 C code comes up with the period.Thanks my Teacher.

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: AC mains Hz meter

Post by medelec35 »

Hi, Derrihj.
(1) the minimum frequency is the frequency just before the timer1 interrupt has fired.
If you double-click the timer 1 interrupt, then select properties you will it shows

Code: Select all

Interrupt Frequency 37.500 Hz
Therefore the minimum frequency is about 37.501Hz
The maximum frequency is not as easy to answer.
The formula use for frequency is

Code: Select all

Frequency = (Fosc / 4.0 / Prescaler) / Timer 1 ticks
So the lower the ticks then the higher the frequency, but then has the less resolution.
For example with a few tick values:
Tickvalues vs Frequency.png
Tickvalues vs Frequency.png (22.76 KiB) Viewed 21477 times
Although the highest frequency for when the tick is just 1 = 1.2MHz, when the tick is 2 the frequency will need to be 614KHz.
This is not practical for a frequency meter.
It depends on the resolution required.
If the max resolution is 500Hz or less then the maximum frequency will be just under 25KHz
(2) It does not really matter what crystal frequency you use so long it has good accuracy.
20MHz will be OK.
19.6608MHz is better for dividing down within interrupts, but as some have post, pre and rollover values you can enter, it's not a great deal.
I just used 19.6608MHz as that is already on my BL0080 PIC Multiprogrammer.
(3)Yes that is exactly what it is doing. If not using the Keyword Floatthen the result would be typecast to an integer, so will be the completely wrong value as all the variables including floats will be treated as intergers!
There are cases when you can omit the Float keyword, but not worth tanking the risk.
(4)How it works is Timer1 is a16 bit register, so the value goes from 0 to 65535.
when the value 65535 is incremented by 1 the value changes to 0 and the interrupt is fired.
Timer 1 has two 8 bit registers (TMR1H & TMR1L).
So when you see the calculation FCV_TMR1 = (TMR1H << 8)+TMR1L;
that is converting the two registers and assigning the value to the 16bit Flowcode variable called tmr1 this will be known as the number of ticks.
The << is left shifting the TMR1H by 8 places which are the same as multiply by 256 then the TMR1L is added on to the result.
As soon as it's done that the timer 1 high and low byte registers are both cleared.
This is done on every falling edge of the input frequency within INT0 Interrupt Service Routine macro, therefore its timing the period of the waveform, but the result will be the frequency due to the calculations.
It also needs to be done before timer 1 interrupt is triggered otherwise tmr1 will be cleared.
If you look at the formula

Code: Select all

Frequency = (19660800.0 / 4.0 / 2.0) / FLOAT value
,its just converting the number of timer1 ticks to a frequency.
Now the frequency is known, the period in seconds = 1/frequency.
The period is then x 1000 to get period in ms
Martin

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Wawo! Thanks Martin, really for if the forum is full of information well detailed like this, people will love Flowcode as such explanations makes it really easy to completely understand how things are done.Good job done Mr Martin i rate you a 10 star in this job, not because I like you but because you do your job really really well no more words hands up.

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Am sorry Martin am asking allot yet you have allot to do somewhere else, I have two more i would love to know here that will also help others like me with this detailed info. (1) What is the meaning of this (Frequency,2) and why it's used here. (2)-Why is the High bit multiplied by 256 and the Low bit is just added?

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: AC mains Hz meter

Post by medelec35 »

Derrihj wrote:
Mon Sep 13, 2021 6:03 am
(1) What is the meaning of this (Frequency,2) and why it's used here.
The 2 in FloatToString$(Float_Variable,2) is how many decimal places to round to.
That way you don't see F=50.012532356 Hz etc.
Derrihj wrote:
Mon Sep 13, 2021 6:03 am
(2)-Why is the High bit multiplied by 256 and the Low bit is just added?
It's the way the two bytes are converted to an integer.
Another way is to shift the high bit 8 places to the left (*256) then do a bitwise OR (|= Pipe character next to the Z on a UK keyboard)
So it will be

Code: Select all

 FCV_TMR1 = (TMR1H << 8)|TMR1L;
The reason we need to do this is that the registers we are reading from on the PIC are 8 bits (0 to 255) but some functions like the timer 1 register are 16-bits (i.e using 2 * 8-bit registers), so its value could be between 0 to 65535.
So we need to convert the 2 * 8 bits to 1 * 16 bit
The FCV_VARIABLE_NAME = C_CODE_VARIABLE is the way C to read the value of the PIC register and assign it to a FlowCode Variable.
It does not matter if the Variable assigned within your flowchart has upper or lower case characters, when using FCV_ all the characters must be in uppercase.

I hope I have cleared things up?
Martin

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

I hope I have cleared things up?
Very well cleared thanks sir, you see that is the beauty of asking as many questions as to the point for any slowest learner can get it in a blink of an eye.Thanks mr Martin no more questions on this have a Blessed day.

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Is this:- FCV_TMR1 = (TMR1H << 8) | TMR1L; same as

FCV_TMR1 = (TMR1H << 8) + TMR1L;

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: AC mains Hz meter

Post by medelec35 »

Derrihj wrote:
Mon Sep 13, 2021 9:43 pm
Is this:- FCV_TMR1 = (TMR1H << 8 | TMR1L; same as

FCV_TMR1 = (TMR1H << 8 + TMR1L;
Yes, They will give the same results.
Martin

Derrihj
Posts: 348
Joined: Mon Jul 09, 2018 12:43 pm
Has thanked: 67 times
Been thanked: 33 times
Contact:

Re: AC mains Hz meter

Post by Derrihj »

Like i said, that i will build this and test it when i get back so i had to do this really fast, at the moment the PCB is not the best but the frequency meter works really well thanks Martin and the rest, guys peace. Oh one more thing, I really need help guys with the flowchart on the link below:. viewtopic.php?f=76&t=21441
Attachments
IMG_20211002_143640_231.jpg
IMG_20211002_143640_231.jpg (428.95 KiB) Viewed 21189 times
Screenshot_20211002-192753.png
Screenshot_20211002-192753.png (202.54 KiB) Viewed 21190 times

Post Reply