There are two parts to this software/firmware section:
-The Controller Transmitter
-The Receiver Motor Controller
The transmitter is constantly polling the ADC values from RA0 and RA1, scaling the value and adding it to a single 8 bit data variable. Then, those 8 bits are sent out the USART and to the XBee module at 9600 BPS.
Transmitter Program
------------« Begin Code »------------
.. ... /************* Convert Channel 0 *****************/ OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD, ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0); //Convrt A/D Delay10TCYx( 5 ); // Delay for 50TCY ConvertADC(); // Start conversion //Read A/D while( BusyADC()); // Wait for completion result = (ReadADC()); // Read result Delay10TCYx(5); CloseADC(); // Disable A/D converter /************* ***************** *****************/ data += (result>>6); /************* Convert Channel 1 *****************/ OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD, ADC_CH1 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0); //Convrt A/D Delay10TCYx( 5 ); // Delay for 50TCY ConvertADC(); // Start conversion //Read A/D while( BusyADC() ); // Wait for completion result = (ReadADC()); // Read result Delay10TCYx(5); CloseADC(); // Disable A/D converter /************* ***************** *****************/ data += (result&0b1111000000)>>2; Delay10KTCYx(5); putcUSART( data ); ... ..
------------« End Code »------------
Surprisingly, the receiver firmware is even easier to write. The 8 bit USART command is received, decoded and used to set the PWM duty cycle for each motor individually.
Receiver Program
------------« Begin Code »------------
.. ... if(DataRdyUSART()){ data = getcUSART(); motor1_speed = data&0b00001111; motor2_speed = (data&0b11110000)>>4; } //For Motor #1 //Scale 500 by 16 using the lower 4 bits of data SetDCPWM1((500*(motor1_speed+1))/16); //For Motor #2 //Scale 500 by 16 using the upper 4 bits of data SetDCPWM2((500*(motor2_speed+1))/16); Delay1KTCYx(1); ... ..
------------« End Code »------------
So give the code a compile and download it to your PIC and then when you're ready, let's give it a test run and see how well the system works.