4-Wire Fan PWM

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:

4-Wire Fan PWM

Post by Derrihj »

Hi am trying tocontrol a 4 wire Fan with PWM but am only getting 10% of its speed, what am i not doing right? Please help.
Attachments
Fan 25kHz With Display.fcfx
(26.39 KiB) Downloaded 158 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 4-Wire Fan PWM

Post by Benj »

Hello,

Not sure if this is a problem but you're using timer 2 for your interrupt and your PWM. They both have different overflow values 79 for the PWM vs 202 for the interrupt and the interrupt is called second and so will be overriding the PWM setting.

Could this be the cause of the problem?

You could instead use Timer 0 for your interrupt and preload the count register with a value using a C icon at the start of the interrupt macro to offer the same sort of overflow frequency.

Code: Select all

TMR0 = (256 - 202);

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Thanks Ben, can you please edit my flowchat so that i see clearly with a live example? Thanks again for the support.

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

Re: 4-Wire Fan PWM

Post by Derrihj »

I added an lcd to see if I get from 0 - 100%, well I do get my 100% pwm in simulator but on the real hardware I only get 10% maximum.
Attachments
Fan 25kHz With LCD.fcfx
(40.13 KiB) Downloaded 152 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: 4-Wire Fan PWM

Post by chipfryer27 »

Hi Derrihj

I have a couple of those chips kicking around and I'll see what I get with your code. Hope to get to it tomorrow but it may get pushed to the weekend.

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: 4-Wire Fan PWM

Post by chipfryer27 »

Hi Derrihj

Just noticed that your LCD is sharing pins with the PWM and other components. This may work in simulation but not in hardware. I'm guessing you added it for testing / simulating and it isn't actually in your hardware layout.

Also you have an issue or so with the 3 x 7-Segment LED displays. They too share pins (B7) and the first segment isn't common'd. I'll have a further look tomorrow or so.

Regards

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Yes just added the LCD for testing in simulation it's not on the hardware, B7 on the real hardware was used to drive a relay because I didnot use a decimal point on my 7seg I didnot need it since my interest is to display from 0 - 100% PWM, For example ( 007, 009, 10, 20 to 100% )

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Ok I've managed to fix the PWM speed, problem was in the calculation box, had forgotten to include some part of math, but now it's fixed the remaining problem is the display still shows only 10%. Ben mentioned something to do with timer2 which is used by both the PWM and the
3 digit 7Seg interrupt being with different overflow values. Can someone explain more on that and how go over it.

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: 4-Wire Fan PWM

Post by medelec35 »

Hi Derrihj ,
Derrihj wrote:
Fri Mar 12, 2021 7:15 am
Ben mentioned something to do with timer2 which is used by both the PWM and the
3 digit 7Seg interrupt being with different overflow values.
Benj wrote:
Tue Mar 09, 2021 6:15 pm
You could instead use Timer 0 for your interrupt and preload the count register with a value using a C icon at the start of the interrupt macro to offer the same sort of overflow frequency.
CODE: SELECT ALL

TMR0 = (256 - 202)
What you need to do is change the timer from timer2 to timer0.
Then within Timer function macro TmrMultiplex at the very start you add a C code block with a value and a semicolon.
E.g

Code: Select all

TMR0 = 54;
That will preload the timer with a fixed value instead of 0.
That have the effect of the timer interrupt being fired sooner as it will reach 256 quicker!

As for the displaying the 10.
Whenever I create a flowchart, I always add variables to the Simulation debugger
Especially if not going as expected.
I did that to your flowchart:
Simulation debugger.png
Simulation debugger.png (51.78 KiB) Viewed 7015 times
I could see straight away the variables don't look right.
Look at Seg0 for example, they should only be numbers from 0 to 9.
Also Code profiling (Within Debug ribbon) is useful as you can see which icons or accessed and which are not.
Martin

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

Re: 4-Wire Fan PWM

Post by Derrihj »

It works now but I used RA5/MCLR/VPP to drive my seg2 common pin, just found out that pin is not producing enough voltage for the seg2 common pin transistor.I can only see 0.22v there at a time it is supposed to be on I don't know why yet I configured it as an I/O pin.So that digit is not lit up at-all. So at 100% I get a 1 for seg0 , a zero for seg1 and nothing at-all for seg2 so at 100% I only see a 10.

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: 4-Wire Fan PWM

Post by medelec35 »

Derrihj wrote:
Fri Mar 12, 2021 5:09 pm
I used RA5/MCLR/VPP to drive my seg2 common pin, just found out that pin is not producing enough voltage for the seg2 common
That's because VPP/MCLR/RA5 is input only.
Martin

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Thanks Martin I think that is the problem tho in flowcode config bits they show it can be configured as I/O . Do you have some more time to explain in detail what we have just done about the (TMR0 256 - 202) just for learning purpose in detail coz after I changed to TMR0 overflow was no longer 202 but rather 244.141 please just for learning purpose in detail.

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: 4-Wire Fan PWM

Post by medelec35 »

What you need to do is look at the timer 0 interrupt properties, change Clock source to Internal.
Starting at the Prescaler Rate of 1:1, select different Prescaler Rates until the Interrupt Frequency showing is the nearest frequency below the one you want.
E.g. If you want 150Hz interrupt frequency and oscillator is 8MHz then choose a prescaler of 1:64 as 122.070Hz is below the 150.
Now calculate the TMR0 preload value:

Code: Select all

Preload value  =  256 - (Oscillator frequency/(4 * Precscaler * Required frequency))
With the example:

Code: Select all

Preload value  =  256 - (8000000/(4 * 64 * 150))

Code: Select all

Preload value = 48
Preload is calculated to 47.66 so round it up.
Therefore within the timer 0 interrupt macro you need a C code block at the start with

Code: Select all

TMR0 = 48;
That will give you 150Hz
Of course change to suit your own values.
Last edited by medelec35 on Sat Mar 13, 2021 3:16 pm, edited 1 time in total.
Reason: Added extra set of brackets for clarity. Not really needed due to BODMAS
Martin

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Thanks a million times mr VC medelec35 for that info and thanks for always being a man of LIVE EXAMPLES that makes a slow learner like me to understand how things are done. For the help you give can be transferred right direct into a note book.

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

Re: 4-Wire Fan PWM

Post by Derrihj »

It works perfectly now am just going to change from RA5 to another pin for the seg2 common pin to make my display look cool thanks guys.
Attachments
IMG_20210312_172436_687.jpg
IMG_20210312_172436_687.jpg (438.82 KiB) Viewed 6927 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: 4-Wire Fan PWM

Post by medelec35 »

Derrihj wrote:
Sat Mar 13, 2021 4:54 pm
It works perfectly now
That what I love about Matrix TSL and the forums - Team work!
It's because you had information from Benj, chipfryer27 & myself, you got your project working.
That's what's it's all about.
Of course, not forgetting all the other contributors on the other threads

Thanks for letting us know.
Martin

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Yes Medelec35 that's the beauty of it all I will be back to show the final project with the display well lit-up. Thanks to all of those guys.One mentions a thing, and the other builds it up and in the end u get all the info well packaged. You know my friend Medelec35, I personally have a saying and it is " If you want to get very good and big knowledge from an expert then make a big mistake first" coz that is when the expert will know that you know nothing and he will teach you like a beginner then along that line you get to learn allot from him because if the expert is teaching someone who knows,he will skip out some useful info him thinking that you already know,but if the expert knows you know nothing, then he won't skip a thing. He will teach you step by step and that is the best way to learn.

chipfryer27
Valued Contributor
Valued Contributor
Posts: 617
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: 4-Wire Fan PWM

Post by chipfryer27 »

Hi Derrihj

Not sure I have the latest version of your code / chart but in mine you seem to be multiplexing the three LED displays by common cathode. However you have enable pins for Seg1 and Seg2 (albeit with the issues using RA5 mentioned earlier) but I don't see any enable for Seg0. This means it will display / mirror whatever Seg1 and Seg2 displays as it shares segment connections (probably showing garbage in reality).

You may have already addressed this.

Regards

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Thank you Chipfryer27, but you have un updated version of my code.You see sometimes when we are uploading these codes,some parts are not yet the final setup, so we only focus on our point of intrest and that was the PWM and interrupt. Here is the finished project and it works perfectly, I eliminated the decimal point that is why my percentages are displayed from the right with a zero infront in case of the ONES and TENS.
Attachments
IMG_20210315_094153_180.jpg
IMG_20210315_094153_180.jpg (382.69 KiB) Viewed 6895 times
IMG_20210315_094045_966.jpg
IMG_20210315_094045_966.jpg (378.08 KiB) Viewed 6895 times
IMG_20210315_094629_156.jpg
IMG_20210315_094629_156.jpg (384.16 KiB) Viewed 6895 times
Last edited by Derrihj on Mon Mar 15, 2021 9:08 am, edited 2 times in total.

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

Re: 4-Wire Fan PWM

Post by Derrihj »

And more pics upto 100% thanks to everyone guys.
Attachments
IMG_20210315_094256_483.jpg
IMG_20210315_094256_483.jpg (381.04 KiB) Viewed 6895 times
IMG_20210315_094434_611.jpg
IMG_20210315_094434_611.jpg (384.2 KiB) Viewed 6895 times

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Clear pics
Attachments
IMG_20210316_094301_591.jpg
IMG_20210316_094301_591.jpg (377.51 KiB) Viewed 6830 times
IMG_20210316_094408_943.jpg
IMG_20210316_094408_943.jpg (386.98 KiB) Viewed 6830 times
IMG_20210316_094615_065.jpg
IMG_20210316_094615_065.jpg (375.55 KiB) Viewed 6830 times

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

Re: 4-Wire Fan PWM

Post by Derrihj »

Just finished my project that i designed for some "NGO". It's a local stove with a fire size adjuster, as you increase the PWM of the 4 wire fan the fire increases there by cooking very fast and vise versa. take a look at the finished product.
Attachments
16165775565462022538166550640153.jpg
16165775565462022538166550640153.jpg (351.75 KiB) Viewed 6750 times
1616577513138718345002959201438.jpg
1616577513138718345002959201438.jpg (340.11 KiB) Viewed 6750 times
IMG_20210324_102346_177.jpg
IMG_20210324_102346_177.jpg (405.27 KiB) Viewed 6750 times

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

Re: 4-Wire Fan PWM

Post by Derrihj »

More
Attachments
16165776875957986306138604256307.jpg
16165776875957986306138604256307.jpg (363.9 KiB) Viewed 6750 times
16165776434164308848024857523890.jpg
16165776434164308848024857523890.jpg (340.75 KiB) Viewed 6750 times

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: 4-Wire Fan PWM

Post by AbhijitR »

Hello! Derrihj

Congratulations on your success, I am very happy to see your project running, truly FC forum is a very nice place to exchange and learn, cheers again.

Abhi

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

Re: 4-Wire Fan PWM

Post by Derrihj »

View toward night time.
Attachments
IMG_20210325_190758_853.jpg
IMG_20210325_190758_853.jpg (335.41 KiB) Viewed 6482 times
IMG_20210325_190818_534.jpg
IMG_20210325_190818_534.jpg (341.45 KiB) Viewed 6482 times
IMG_20210325_190849_877.jpg
IMG_20210325_190849_877.jpg (330.96 KiB) Viewed 6482 times

Post Reply