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:

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.

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

// DC Motor control using PIC12F1822 and L293D CCS C code

#include <12F1822.h>

#fuses NOMCLR INTRC_IO PLL_SW

#device ADC = 10

#use delay(clock=32000000)

#use fast_io(A)

int8s;// Used to know motor status

int16i;

voidmain(){

setup_oscillator(OSC_8MHZ|OSC_PLL_ON);// Set internal oscillator to 32MHz (8MHz and PLL)