Pages

Thursday, May 5, 2016

DC motor control with PIC16F877A and L293D (Proteus simulation)

DC motor interfacing with PIC16F877A and L293D

This topic shows how to control DC motor rotation direction and speed using PIC16F877A microcontroller and L293D motor driver IC.
A pot is used to control motor speed and two pushbuttons are used to control rotation direction.
Related topics:
The following topic shows how to control speed and direct of a DC motor using PIC16F877A and H-bridge circuit:
DC motor speed and direction control with PIC16F877A and H-bridge

DC motor control with PIC16F877A and L293D circuit:
DC motor speed direction control circuit using PIC16F877A and L293D CCS PIC C
In the circuit there are 3 pushbuttons, 2 for selecting the direction and the other one for stopping the motor. The 2 LEDs are used to indicate the motor rotation direction, if LED1 is ON that means direction 1 has been chosen and the same thing for LED 2. If both LEDs are OFF that means the motor has been stopped.
The nominal voltage of the motor is 12V as well as L293D VS input voltage. Always L293D VS voltage is the same as the DC motor voltage and L293D VSS voltage is +5V.
A pot (pin AN0) is used to change the motor speed.
The microcontroller PIC16F877A reads analog data from channel 0 and use the digital value to set the PWM duty cycle. If direction 1 button is pressed the microcontroller starts PWM1 (RC2 pin) and stops PWM2 (RC1 pin) and if direction 2 button is pressed the microcontroller stops PWM1 (RC2 pin) and starts PWM2 (RC1 pin), when the stop button is pressed the microcontroller stops PWM1 and PWM2 signals and the motor will stop.
The PWM (PWM1 & 2) frequency is 488Hz.
DC motor control with PIC16F877A and L293D CCS PIC C code:
PIC16F877A Timer2 is configured to generate a PWM frequency of 488Hz and the microcontroller runs with 8MHz crystal oscillator.
The microcontroller PIC16F877A reads RA0 analog value and stores the digital value on variable (i), this variable is used to set duty cycle of the active PWM (PWM1 or PWM2).
// DC motor control using PIC16F877A and L293D CCS C code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC = 10
#use delay(clock = 8000000)
#use fast_io(B)
#use fast_io(C)

unsigned int16 i ;
void main(){
  port_b_pullups(TRUE);                  // Enable PORTB pull-ups
  output_b(0);                           // PORTB initial state
  set_tris_b(7);                         // Configure RB0, RB1 & RB2 as inputs
  output_c(0);                           // PORTC initial state
  set_tris_c(0);                         // Configure PORTC pins as outputs
  setup_adc(ADC_CLOCK_DIV_32);           // Set ADC conversion time to 32Tosc
  setup_adc_ports(AN0);                  // Configure AN0 as analog
  set_adc_channel(0);                    // Select channel AN0
  setup_timer_2(T2_DIV_BY_16, 255, 1);   // Set PWM frequency to 488Hz
  delay_ms(100);                         // Wait 100ms
  while(TRUE){
    i = read_adc();                      // Read from AN0 and store in i
    if(input(PIN_B3) == 1)               // If direction 1 is selected
      set_pwm1_duty(i);                  // Set pwm1 duty cycle
    if(input(PIN_B4) == 1)               // If direction 2 is selected
      set_pwm2_duty(i);                  // Set pwm2 duty cycle
    delay_ms(10);                        // Wait 10ms
    if(input(PIN_B0) == 0){              // If RB0 button pressed
      if(input(PIN_B3) == 0){            // If direction 1 not already selected
        output_b(0);                     // Both LEDs OFF
        setup_ccp1(CCP_OFF);             // CCP1 OFF
        setup_ccp2(CCP_OFF);             // CCP2 OFF
        output_c(0);                     // PORTC pins low
        delay_ms(100);                   // Wait 100ms
        setup_ccp1(CCP_PWM);             // Configure CCP1 as a PWM
        output_high(PIN_B3);             // RB3 LED ON
        }}
    if(input(PIN_B1) == 0){              // If RB1 button pressed
      if(input(PIN_B4) == 0){            // If direction 2 not already selected
        output_b(0);                     // Both LEDs OFF
        setup_ccp1(CCP_OFF);             // CCP1 OFF
        setup_ccp2(CCP_OFF);             // CCP2 OFF
        output_c(0);                     // PORTC pins low
        delay_ms(100);                   // Wait 100ms
        setup_ccp2(CCP_PWM);             // Configure CCP2 as a PWM
        output_high(PIN_B4);             // RB4 LED ON
        }}
    if(input(PIN_B2) == 0){              // If RB2 button pressed
      setup_ccp1(CCP_OFF);               // CCP1 OFF
      setup_ccp2(CCP_OFF);               // CCP2 OFF
      output_c(0);                       // PORTC pins low
      output_b(0);}                      // Both LEDs OFF
   }
}

DC motor control with PIC16F877A and L293D Proteus simulation video:

The following video shows the simulation using Proteus.


Proteus simulation file download:
You can download Proteus simulation file from the following link.
DOWNLOAD