DC motor speed control using PIC16F84A and CCS C compiler

Interfacing DC motor with PIC16F84A

DC motor speed can be easily controlled if the microcontroller has CCP module to generate PWM (Pulse Width Modulation) signal, the speed is controlled when the PWM signal duty cycle is varied and if the duty cycle is changed the power delivered to the motor will be changed also. The microcontroller PIC16F84A is a very old chip but it is good for beginners to start PIC programming and electronics, it doesn’t have CCP module and it has only 1 timer which is Timer0 module, this timer is an 8-bit timer. Using this timer we are going to see how to make a software PWM in order to control the DC motor.
DC motor speed control using PIC16F84A circuit schematic:
PIC16F84A DC motor speed control circuit
The nominal voltage of the DC motor is 12V. The PIC16F84A microcontroller must be supplied with 5V between pins VDD and VSS.
In the circuit there are two buttons to increase and decrease the speed of the motor, these buttons are connected to RB0 and RB1 respectively.
There is an LED connected to RB2 used to indicate that the motor is at full speed. The crystal frequency is 8MHz and the transistor is N-type mosfet.

DC motor speed control using PIC16F84A CCS C code:
The following CCS C command line is used to generate a software PWM frequency using Timer0 with 500Hz frequency on RA0 pin:

#use pwm(output = pin_a0, timer = 0, frequency= 500Hz, duty = 0)
And the following command is used to to set the duty cycle:
pwm_set_duty_percent( duty );                   // In tenths %
The complete CCS C code is as the following below:

DC motor speed control using PIC16F84A video:
The following video shows the project using a hardware circuit.

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.