DC motor and Fan speed control using pic 16f877 Microcontroller

Here is a simple project on how to control fan or dc motor speed uisng pic 16f877 microcontroller. There are numerous ways to control the speed of motor(or fan). Varing current,voltage and resistance etc. But when it comes to controlling the speed using microcontrollers. Then the PWM(Pulse width modulation) technique is used. In Pulse width modulation the digital signal high and low time is chaged. Actually it is made variable. So if the digital signal is high for 70% of the time and low for 30% of the time then the motor rotates more quickly because the signal is high for most of the time. Setting the pwm for different times can make you to control the motor speed.

Project Requirements

• PIC 16f877 microcontroller
• L293d Motor driver Ic
• DC Motor or small fan
• Crystal 20MHz
• Push Buttons
• connecting wires
• Power Supply battery

Their are two pins dadicated for generating pwm output in pic 16f877 microcontroller. These two pins are of port c pin 1 and 2. They are named as CCP1 and CCP2 (CCP stands for Capture,compare and PWM). These two pins can be used in capture, compare and Pwm mode.They can also be used as digital I/O. We first have to configure them whether we want to use them in CCP mode or as digital I/O.

To Control and configure CCP1, CCP1 register is used. CCP1 is a 16-bit register which is further divided in to two 8-bit registers CCPR1H(Capture,compare,PWM register 1 high byte) and CCPR1L(Capture,compare,PWM register 1 LOW byte).These two registers are controlled by CCP1CON(Capture,compare,PWM 1 control) register.Alternatively CCP2CON is used to control the CCP2 module.
The PWM signal generated by PIC 16f877 provides 10-bit resolution. Which means it can calculate up to 2^10 = 1024. The 10 bit value is loaded in CCPR1L and CCP1CON<5:4> registers. Now Set the Period and duty cycle for the signal. The period is set in PR2 register. PR2 is an 8-bit register. Timer 2 runs from 0 upto the value loaded in PR2 register. Upon reaching the value it starts again from 0. When all the values are loaded in the registers then run the timer to generate the Pwm. Dont forget to set the timer prescaler. I set it to 4.