SN754410 Dual Motor Control

Program Download:

Main C File
Dual_Motor_Control.c
Dual_Motor_Control.hex

The Software
           There are two main portions of code that we are concerned with:
    -Trimpot Input A/D Converter Code
    -Input Eval. and Output Control


The first section of code we will look at concerns how we get the input from the trimpots. The analog to digital (A/D) converters will be used to convert the analog voltage from the trimpot to a 10-bit digital value. That means the value will be between 1023 and 0. 1023 means the trimpot is at +5v and 0 means the trimpot is at +0v.

Trimpot Input A/D Converter Code

------------« Begin Code »------------
void InterruptHandlerHigh()
{
     if(PIR1bits.ADIF == 1){
          PORTC = 0xFF;
          if(ad_chan == ADC_CH0){
               ad_val_0 = ReadADC();
               ad_ready = 1;
               ad_chan = ADC_CH1;
          }
          else{
               ad_val_1 = ReadADC();
               ad_ready = 1;
               ad_chan = ADC_CH0;
          }
          SetChanADC(ad_chan);
          PIR1bits.ADIF = 0;
     }
INTCONbits.GIEH = 1; //Re-enable all interrupts
return;
}
------------« End Code »------------

           After being able to take input, now we must evaluate it and create our output control. A big while loop in the main function of the code does just that. The input is is compared in a series of if statements and then if certain conditions are met, then the corresponding motor's speed or direction is changed. A counter variable is used to keep everything in sync.

Input Evaulation and Output Control

------------« Begin Code »------------
/************** Control Motor #1 -BEGIN *******
***********************************************/
    //Go Forward
if(ad_val_0>>3 > 63){
    if((ad_val_0>>3)-63 > cnt){
            PORTDbits.RD0 = 1;
            PORTDbits.RD1 = 0;
    }
    else{
        PORTDbits.RD0 = 0;
        PORTDbits.RD1 = 0;
    }
}
    //Go Reverse
else{
    if(63-(ad_val_0>>3) < cnt){
        PORTDbits.RD0 = 0;
        PORTDbits.RD1 = 0;
    }
    else{
        PORTDbits.RD0 = 0;
        PORTDbits.RD1 = 1;
    }
}
/************** Control Motor #1 -END *********
***********************************************/
------------« End Code »------------

           These two chunks of code are the 'meaty' parts of the software, however please download and view the entire source code for yourself. It will answer any questions that might have come up while reading through my abridged version above.



;