/*******************************************************
FILE:     l293.c

DESCRIPTION: PIC code to control small motor through L293D chip.
See schematic for hookup.
Designed for 16f88 PIC chip using internal oscillator.

Motor truth table:
Enable  IN1     IN2     Function
 H       H      L       turn right
 H       L      H       turn left
 H      L/H     L/H     fast stop
 L     either   either  slow stop

REVISION HISTORY:
*******************************************************/
//--Includes

#include <16f88.h>

//--Setup commands

#fuses INTRC_IO,NOWDT,NOPROTECT,PUT,NOWRT,NOLVP  // internal oscillator
#id 0x1234                     // 4 digit id
#use standard_io(A)
#use standard_io(B)
//#use fast_io(A)
//#use fast_io(B)
#use delay(clock=8000000)      // xx MHz resonator



//--Pin definitions

#define LED            PIN_B0   // LED for debugging
#define ENAB1          PIN_B1   // L293D pins
#define IN1            PIN_B2
#define IN2            PIN_B3


//--Motor functions

void turn_right(void)
{
  output_high(ENAB1);
  output_high(IN1);
  output_low(IN2);
}

void turn_left(void)
{
  output_high(ENAB1);
  output_low(IN1);
  output_high(IN2);
}

void slow_stop(void)
{
  output_low(ENAB1);
  output_low(IN1);
  output_low(IN2);
}

void fast_stop(void)
{
  output_high(ENAB1);
  output_low(IN1);
  output_low(IN2);
}




/*******************************************************
  Main starts here
*******************************************************/

void main(void)
{
  int i;
  setup_oscillator(OSC_8MHZ);

//-------Set direction registers

//  set_tris_a(0xFF);   // a is all inputs (for adc)
//  set_tris_b(0xC0);   // b7,b6 in, rest out


//-------Flash led a few times

  for (i=0;i<5;i++) {
    output_high(LED);
    delay_ms(100);
    output_low(LED);
    delay_ms(300);
  }

  delay_ms(1000);  //pause for effect

//-------Turn motor two directions

  for (i=0;i<5;i++) {
    turn_right();
    delay_ms(500);
    turn_left();
    delay_ms(500);
  }

  slow_stop();
  delay_ms(2000);

//-------Slow stop

  turn_right();
  delay_ms(1000);
  slow_stop();
  delay_ms(3000);

//-------Fast stop

  turn_right();
  delay_ms(1000);
  fast_stop();
  delay_ms(3000);


//-------That's all folks

  slow_stop();  //all lines low

  for (i=0;i<4;i++) { //flash the led
    output_high(LED);
    delay_ms(200);
    output_low(LED);
    delay_ms(200);
  }

  sleep;

}

