Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
#include "16f877a.h"#define adc=10#use delay(clock=4000000) // 4MHZ crystal#fuses xt,nowdt,put,nolvp,nocpd// define the variablesunsigned int i,j;int16 ch0_min, ch1_min; unsigned int16 ch1[10], ch2[10];// define struct for leds work as indicatorstypedef struct{unsigned int rled:1;unsigned int yled:1; unsigned int bled:1;}leds;#define PORTE (*(leds*) 0x09)// routaine for ADCvoid ADX() {setup_adc(adc_clock_div_32); setup_adc_ports(AN0_AN1_AN2_AN3_AN4); // set PORTA as analog, Vcc and GND as reference voltages set_adc_channel(0); // select channel zero delay_ms(50); ch0_min=read_adc(); // read channel zero set_adc_channel(1); // select channel one delay_ms(50); ch1_min=read_adc(); // read channel one setup_adc(adc_off); }// stop the motorsvoid stop() { setup_ccp1(ccp_pwm); set_pwm1_duty(500); setup_timer_2(t2_div_by_1,249,1); setup_ccp2(ccp_pwm); set_pwm2_duty(500); setup_timer_2(t2_div_by_1,249,1); output_d(0x03);} // make the motors always enable// move forward routainevoid movf() // move forward {setup_ccp1(ccp_pwm); set_pwm1_duty(800); setup_timer_2(t2_div_by_1,249,1); setup_ccp2(ccp_pwm); set_pwm2_duty(800); setup_timer_2(t2_div_by_1,249,1); output_d(0x03);}//move to leftvoid movl() // move left {setup_ccp1(ccp_pwm); set_pwm1_duty(500); setup_timer_2(t2_div_by_1,249,1); setup_ccp2(ccp_pwm); set_pwm2_duty(800); setup_timer_2(t2_div_by_1,249,1); output_d(0x03); }// move to right void movr() // move right {setup_ccp1(ccp_pwm); set_pwm1_duty(800); setup_timer_2(t2_div_by_1,249,1); setup_ccp2(ccp_pwm); set_pwm2_duty(500); setup_timer_2(t2_div_by_1,249,1); output_d(0x03);}// start the main programvoid main() { set_tris_e(0x00); // set PORTE as output (leds) while(1) { ADX(); ch0_min>>2; // shift to right (two bits) ch1_min>>2; output_e(0x00); // turn off all leds delay_ms(500); if(ch0_min>(ch1_min+25)) // if ch0 > ch1 by 0.5v turn left {porte.yled=1; // turn on yellow led for(i=0;i<=3;i++) // loop for moving left {movl(); // turn left delay_ms(500); } } else if(ch1_min>(ch0_min+25)) {porte.bled=1; // turn on the blue led for(i=0;i<=3;i++) {movr(); // turn right delay_ms(500); } } else { porte.rled=1; // turn on the red led for(i=0;i<=3;i++) {movf(); // move forward delay_ms(500); } }} }
On using the ADCs, after the ADC channel is selected you only need a few usec delay to let the sampling settle before starting a conversion. And it is better the have a function to set the channel and read the ADC without re-settting up the ADC.
I use a different C compiler so I don't know what the built in CCS functions that handle the PWM (CCP) and ADCs really do. I write my own functions for setting up and using the ADCs and PWM so I know exactly what they are doing at the PIC's register level. I'll assume that the CCS set-up functions are correct.
I see that you turn LEDs on or off which is a good idea for debugging. Do these LEDs change?Try doing the simplest ADC read and see if the motors come back. Once you eliminate the line of code that causes the problem you can then figure out why.
On the IR sensors: Did you measure their output voltage? Because according to the data sheet their output is either greater than 4.5V or less than 0.5V ( see the Vol & Voh in the Electro-Optical Characteristics). Also are you trying to detect an IR LED modulated at 38kHz? That's what these detect.