DC Motor control using PIC12F1822 and L293D – CCS C compiler

With the help of L239D we can easily control DC motor speed and direction of rotation.
The L293D can control two motors with the same nominal voltage independently and in this project we are going to use half of the chip to control just one motor.
The microcontroller used in this project is PIC12F1822, it’s an 8-bit and 8 pins microcontroller. This microcontroller has 1 CCP module which can be used to generate a PWM signal.
The following topic shows how to use PIC12F1822 ADC and PWM modules:
PIC12F1822 ADC and PWM modules

DC Motor control using PIC12F1822 and L293D circuit:

PIC12F1822 DC motor control L293D circuit

Motor speed is controlled from a 10k potentiometer connected to RA0 pin. Two push buttons connected to RA1 and RA3 are used to select direction of rotation and another push button connected to RA4 is used to stop the motor.
The DC motor voltage is 12V which is the same as L293D VS voltage (pin 8).
The PWM signal output pin is selected by the software (RA2 or RA5).
PIC12F1822 internal oscillator is used and internal pull-ups is enabled for the inputs.
Here the microcontroller reads the analog data comes from the potentiometer and uses the corresponding digital value to set the PWM duty cycle.

DC Motor control using PIC12F1822 and L293D CCS C code:
In this project PIC12F1822 internal oscillator is used @ 8MHz with PLL enabled (gives 32MHz) and internal pull-ups are enebled for the inputs RA1, RA3 and RA4.
Timer2 is configured to generate a PWM signal of 1.95KHz with a resolution of 10-bit.
setup_timer_2(T2_DIV_BY_16, 255, 1);
CCP Module is configured to work as PWM module and the output pin of the PWM signal can be selected using the following two lines:
setup_ccp1(CCP_PWM | CCP1_A2);
setup_ccp1(CCP_PWM | CCP1_A5);
The complete CCS C code is below.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top