Quote Originally Posted by markedwards
Hi Mengckid,

You did't mention which PIC you were using or what application.
Here is code that I use with hardware pwm (HPWM).
My application requires 5 speeds and a 60% minimum speed setting.
You can add more comparisons or calculate speed settings based on AD1.
There are 2 lines that control acceleration & de-acceleration
I like the hardware pwm best, it runs consistent speed
while your program executes other code. It will work with PWM if you change the last line.

ADCIN 1,AD1 'read channel 1...AN1
AD1 = AD1 >> 6 'justify data

ADJUSTFAN:
IF AD1 < 2 then FANSPEED = 153 '60% min speed
IF AD1 >= 3 then SPEED = 178 '70%
IF AD1 >= 5 then SPEED = 204 '80%
IF AD1 >= 7 then SPEED = 229 '90%
IF AD1 >= 9 then SPEED = 255 '100%
IF SPEED > SPD THEN SPD = SPD + 1 'accelerate
IF SPEED < SPD THEN SPD = SPD - 1 'de-accelerate
hpwm 2,SPD,32760 ' PWM 20,000 Hz

Mark
Thanks u,anyway... you are so great,ur code help me much,thanks u

The PIC i am using is PIC16F874A, and the application is used the single turn 10k potentiometer to increase or decrease the speed of motor fan in another pin in PIC. Meaning,i will use a N-channel MOSFET to drive the current to motor fan.

Sorry for that, if I willing to use PWM command to control speed of motor fan, meaning accelerate or de-accelerate it, can I do that?

Hopefully, you can help me, thanks a million