Page 1 of 1

4-Wire Fan PWM

Posted: Tue Mar 09, 2021 10:28 am
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.

Re: 4-Wire Fan PWM

Posted: Tue Mar 09, 2021 6:15 pm
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);

Re: 4-Wire Fan PWM

Posted: Thu Mar 11, 2021 5:37 am
by Derrihj
Thanks Ben, can you please edit my flowchat so that i see clearly with a live example? Thanks again for the support.

Re: 4-Wire Fan PWM

Posted: Thu Mar 11, 2021 10:18 am
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.

Re: 4-Wire Fan PWM

Posted: Thu Mar 11, 2021 9:43 pm
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

Re: 4-Wire Fan PWM

Posted: Thu Mar 11, 2021 11:28 pm
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

Re: 4-Wire Fan PWM

Posted: Fri Mar 12, 2021 3:53 am
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% )

Re: 4-Wire Fan PWM

Posted: Fri Mar 12, 2021 7:15 am
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.

Re: 4-Wire Fan PWM

Posted: Fri Mar 12, 2021 10:14 am
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 8424 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.

Re: 4-Wire Fan PWM

Posted: Fri Mar 12, 2021 5:09 pm
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.

Re: 4-Wire Fan PWM

Posted: Fri Mar 12, 2021 5:31 pm
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.

Re: 4-Wire Fan PWM

Posted: Fri Mar 12, 2021 7:09 pm
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.

Re: 4-Wire Fan PWM

Posted: Sat Mar 13, 2021 3:07 am
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.

Re: 4-Wire Fan PWM

Posted: Sat Mar 13, 2021 7:51 am
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.

Re: 4-Wire Fan PWM

Posted: Sat Mar 13, 2021 4:54 pm
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.

Re: 4-Wire Fan PWM

Posted: Sat Mar 13, 2021 5:04 pm
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.

Re: 4-Wire Fan PWM

Posted: Sat Mar 13, 2021 5:28 pm
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.

Re: 4-Wire Fan PWM

Posted: Sat Mar 13, 2021 11:35 pm
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

Re: 4-Wire Fan PWM

Posted: Mon Mar 15, 2021 7:58 am
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.

Re: 4-Wire Fan PWM

Posted: Mon Mar 15, 2021 8:00 am
by Derrihj
And more pics upto 100% thanks to everyone guys.

Re: 4-Wire Fan PWM

Posted: Tue Mar 16, 2021 7:50 am
by Derrihj
Clear pics

Re: 4-Wire Fan PWM

Posted: Wed Mar 24, 2021 10:20 am
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.

Re: 4-Wire Fan PWM

Posted: Wed Mar 24, 2021 10:21 am
by Derrihj
More

Re: 4-Wire Fan PWM

Posted: Wed Mar 24, 2021 7:32 pm
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

Re: 4-Wire Fan PWM

Posted: Fri Mar 26, 2021 11:45 am
by Derrihj
View toward night time.