Quote Originally Posted by markedwards
Hi Mengckid,

Your Pic has the hardware PWM built-in so it is your best choice.
Your program can read the ADC (pot) and update the speed variable
and the hardware PWM does the rest.
Depending on your motor voltage & current draw you may need to
consider your Mosfet circuit. Your Mosfet's voltage rating should be
at least twice your DC supply voltage. You need a clamp diode connected
across your motor (cathode to M+ anode to M-).
Your Mosfet has 2 losses that generate heat, conduction loss (dc current squared times the rds on resistance). example 2 Amps x 2 Amps x .1 ohm = .4 watts, requiring a heat sink. The Mosfet has a large gate capacitance that can give slow turn on & turn off times (switching losses). I prefer to use a mosfet driver IC such as TC4426 or TC4427.

Mark
HiHi,yaya, thanks u again

i got few problem here:

for the code above, I cannot compile this part: The code:
IF SPEED > SPD THEN SPD = SPD + 1 'accelerate
IF SPEED < SPD THEN SPD = SPD - 1 'de-accelerate

the error message is mismatching if loop..then...endif. Thus, i dunno how to fixed it. Thus, here is my code, hopefully you can help me a bit, thanks ya


INCLUDE "modedefs.bas"
DEFINE OSC 4

DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds
AD1 var byte
SPEED var byte
SPD var byte

TRISB = %10000000 ' set output for led portb.0
TRISC = %11111000 ' set output for pwm portc.2
TRISA = %111111 ' Set PORTA to all input

ADCON1 = 0 ' for 10-bit ADC and Set all PORTA is analog
ADCON0 =%10000101 'SET A/D Fosc/32, [4MHz], CHANNEL-0 = ON

main:

ADCIN 0,AD1 'read chanel 1...AN1
AD1 = AD1 >> 6 'justify data
gosub ADJUSTFAN
hpwm 2,SPD,300 ' PWM 20,000 Hz

IF SPEED > SPD THEN goto acce
endif
if speed <= SPD then goto deacce
endif

ADJUSTFAN:

IF AD1 < 2 then
SPEED = 153
endif '60% min speed
IF AD1 >= 3 then
SPEED = 178
endif '70%
IF AD1 >= 5 then
SPEED = 204
endif '80%
IF AD1 >= 7 then
SPEED = 229
endif '90%
IF AD1 >= 9 then
SPEED = 255
endif '100%
return

acce: SPD = SPD + 1 'accelerate
hpwm 2,SPD,300
return

deacce: SPD = SPD - 1 'de-accelerate
hpwm 2,SPD,300
return

P/s : The code above is Pic Basic Pro code...


Another problem is how do I get the ADC scaling. Actually, what is ADC scaling mean?? If I wanna do the ADC scaling from 1-32, so how to write in pic basic code? Is it neccessary to iniatiate first, the 1-32 values?

Thanks u,cheers

Regards
Mengckid