choyhong88 WROTE:i have some doubt in the provided source code, filename adc.c
why all the adc result is put in (signed int)ADCBUF0? will each of the result overwrite the previous result, then in the end we will only obtain the adc result for si_adc_right_motor_2 and the rest of result is gone?
- CODE: SELECT_ALL_CODE
void adc_start_conversion(void)
{
// Turn off PWM and let the back EMF become stable.
// While waiting, we will sample the other channels first.
OC1CONbits.OCM = 0;
OC2CONbits.OCM = 0;
// Select AN0 (Vin).
ADCHSbits.CH0SA = 0;
// Start sampling.
ADCON1bits.SAMP = 1;
// Delay 100uS.
__delay32(_XTAL_FREQ/40000);
// Stop sampling and start conversion.
ADCON1bits.SAMP = 0;
// Wait for conversion to finish.
while (ADCON1bits.DONE == 0);
// Save the value.
sui_adc_supply = (signed int)ADCBUF0;
// Select AN1 (Acc - X).
ADCHSbits.CH0SA = 1;
// Start sampling.
ADCON1bits.SAMP = 1;
// Delay 100uS.
__delay32(_XTAL_FREQ/40000);
// Stop sampling and start conversion.
ADCON1bits.SAMP = 0;
// Wait for conversion to finish.
while (ADCON1bits.DONE == 0);
// Save the value.
sui_adc_acc_x = (signed int)ADCBUF0;
// Select AN3 (Gyro).
ADCHSbits.CH0SA = 3;
// Start sampling.
ADCON1bits.SAMP = 1;
// Delay 100uS.
__delay32(_XTAL_FREQ/40000);
// Stop sampling and start conversion.
ADCON1bits.SAMP = 0;
// Wait for conversion to finish.
while (ADCON1bits.DONE == 0);
// Save the value.
sui_adc_gyro = (signed int)ADCBUF0;
// Select AN4 (Left Motor Voltage - 1).
ADCHSbits.CH0SA = 4;
// Start sampling.
ADCON1bits.SAMP = 1;
// Delay 100uS.
__delay32(_XTAL_FREQ/40000);
// Stop sampling and start conversion.
ADCON1bits.SAMP = 0;
// Wait for conversion to finish.
while (ADCON1bits.DONE == 0);
// Save the value.
si_adc_left_motor_1 = (signed int)ADCBUF0;
// Select AN5 (Left Motor Voltage - 2).
ADCHSbits.CH0SA = 5;
// Start sampling.
ADCON1bits.SAMP = 1;
// Delay 100uS.
__delay32(_XTAL_FREQ/40000);
// Stop sampling and start conversion.
ADCON1bits.SAMP = 0;
// Wait for conversion to finish.
while (ADCON1bits.DONE == 0);
// Save the value.
si_adc_left_motor_2 = (signed int)ADCBUF0;
// Select AN6(Right Motor Voltage - 1).
ADCHSbits.CH0SA = 6;
// Start sampling.
ADCON1bits.SAMP = 1;
// Delay 100uS.
__delay32(_XTAL_FREQ/40000);
// Stop sampling and start conversion.
ADCON1bits.SAMP = 0;
// Wait for conversion to finish.
while (ADCON1bits.DONE == 0);
// Save the value.
si_adc_right_motor_1 = (signed int)ADCBUF0;
// Select AN7 (Right Motor Voltage - 2).
ADCHSbits.CH0SA = 7;
// Start sampling.
ADCON1bits.SAMP = 1;
// Delay 100uS.
__delay32(_XTAL_FREQ/40000);
// Stop sampling and start conversion.
ADCON1bits.SAMP = 0;
// Wait for conversion to finish.
while (ADCON1bits.DONE == 0);
// Save the value.
si_adc_right_motor_2 = (signed int)ADCBUF0;
// Turn on PWM.
OC1CONbits.OCM = 0b110;
OC2CONbits.OCM = 0b110;
// At last, before we exit, we start sampling AN0 (Vin) as it has a low output impedance.
ADCHSbits.CH0SA = 0;
// Start sampling.
ADCON1bits.SAMP = 1;
}
should we save the adc value in this way
sui_adc_supply = (signed int)ADCBUF0;
sui_adc_acc_x = (signed int)ADCBUF1;
sui_adc_gyro = (signed int)ADCBUF2;
si_adc_left_motor_1 = (signed int)ADCBUF3;
si_adc_left_motor_2 = (signed int)ADCBUF4;
si_adc_right_motor_1 = (signed int)ADCBUF5;
si_adc_right_motor_2 = (signed int)ADCBUF6;
Yup. The value in ADCBUF0 will be overwritten when the next conversion completed. Thus we need to save it before start the next conversion.
choyhong88 WROTE:And another doubt, the circuit diagram show that using 2 out of the 3 axis in accelerometer but in the source code only 1 axis is need. So, acually 1axis + 1 gyro rate are enough for balancing ..
For 2 wheel balancing robot, 1 accelerometer is already enough.