Simple Motor Optical Encoder: Software

Program Download:

Main C File
Motor_Encoder.c

The Software
           The IR range software for this project has two main portions to it.
    -A/D Conversion
    -L298 Motor Control


A/D Conversion Code

------------« Begin Code »------------
..
..
OpenADC( ADC_FOSC_32
    & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH1 & ADC_INT_OFF
    & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0);
..
..
..
..
..
ConvertADC();
while( BusyADC() );
    total += ReadADC();
..
..
..
------------« End Code »------------

           This code enables the AN1 as an analog input so that it can take data from the IR detector. The resulting integer from the analog to digital conversion is used to determine whether we are seeing white or black. To reduce any error that might creep into the system, 20 ADC values are taken then the average of those is accepted as the true value.

L298 Motor Control
------------« Begin Code »------------
/**Setup Motors**/

OpenTimer2(TIMER_INT_OFF & T2_PS_1_1);
     //PWM period =[(period ) + 1] x 4 x TOSC x TMR2 prescaler
     // Fosc = 40 MHz (the default internal oscillator)
          // TMR2 prescaler = 1

OpenPWM1(0xFF);
OpenPWM2(0xFF);

SetOutputPWM1(SINGLE_OUT, PWM_MODE_3);
          //PWM_MODE_3 -> PWM2 Inverted Signal
move_center();
------------« End Code »------------

           We use C18 library functions to make our life easier for setting up the PWM. The PWM frequency itsself is around 7KHz depending on your input oscillator. Notice that we use two PWM registers because we're output two PWM signals. The 2nd signal output is the inverse of our main PWM signal. The L298 demands this seconds signal inorder to operate.

           So now that you have the software all coded, program it onto the PIC and let's go see how well it works!



;