CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

code help needed

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kazama



Joined: 01 Sep 2009
Posts: 10
Location: Malaysia

View user's profile Send private message

code help needed
PostPosted: Tue Sep 01, 2009 9:19 am     Reply with quote

Hi guys, I need some help on my coding. The robot that I'm doing is slam robot. But now I need to fix the sensor problem. I think the coding is alright but, after I burned the program to the PIC, only the left wheel is moving. Can someone check the program? Got blur at d moment..
Code:

#include <16f877a.h>               //declare a PIC header
#device adc=8                      //set the bit for ADC
#fuses hs,nowdt,noprotect          //set the PIC protection
#use delay (clock=20000000)        //set the clock frequency to 20 kHz
int val1;                          //val1 = middle sensor(ADC Value)
int val2;                        //val2 = left sensor(ADC Value)
int val3;                         //val3 = right sensor(ADC Value)
int vref;                          //voltage reference
void left_motor(int speed);         //left motor function
void right_motor(int speed);        //right motor function
#byte porta=5
#byte portc=7               
//motor_ra RC0 motor_rb RC3
//motor_la RC4 motor_lb RC5


void main()                        //main function
{
   vref=0x4d;                      //voltage reference = 2.5v
   set_tris_c(0);         //port b as output
   setup_port_a(all_analog);       //enable port a as ADC port
   setup_adc(adc_clock_internal);  //set ADC clock
   setup_ccp1(ccp_pwm);            //enable ccp1
   setup_ccp2(ccp_pwm);            //enable ccp2
   setup_timer_2(t2_div_by_4,100,1);    //set post scaler timer
   
  While(true)
  {
  portc=0b00010001;
  set_adc_channel(0); 
  val1=read_adc();
  set_adc_channel(1); 
  val2=read_adc();
  set_adc_channel(2);       
  val3=read_adc();
  if(val1>vref)
  {
      if((val2>vref)&&(val3>vref))
      {
         left_motor(70);
         right_motor(70);
      }
      else if ((val3<vref)&&(val2>vref))
      {
         left_motor(0);             
         right_motor(70);
      }
      else if ((val2<vref)&&(val3>vref))     
      {
         left_motor(70);
         right_motor(0);
      }
  } 
  else if (val1<vref)
  {
      if((val2>vref)&&(val3>vref))
      {                   
         left_motor(30);                     
         right_motor(30);
      }
      else if ((val3<vref)&&(val2>vref))
      {
         left_motor(0);                     
         right_motor(70);                     
      }
      else if ((val2<vref)&&(val3>vref))
      {
         left_motor(70);                     
         right_motor(0);                   
      }
  }
  else
  {
      left_motor(0);
      right_motor(0);
  }
  }
}

void left_motor(int gear)                       
{
   set_pwm1_duty(gear);                     
}

void right_motor(int gear)                     
{
   set_pwm2_duty(gear);                     
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 12:19 pm     Reply with quote

Quote:
#include <16f877a.h> //declare a PIC header
#device adc=8 //set the bit for ADC
#fuses hs,nowdt,noprotect,nolvp

Add NOLVP to the #fuses as shown above in bold. CCS does this
by default in vs. 4.xxx, but I don't know what version you're using.

Quote:

While(true)
{
portc=0b00010001;
set_adc_channel(0);
delay_us(20);
val1=read_adc();
set_adc_channel(1);
delay_us(20);
val2=read_adc();
set_adc_channel(2);
delay_us(20);
val3=read_adc();
if(val1>vref)
{

Add a short delay (20 us) after you change the ADC channel, as shown
above in bold. This is stated in the PIC data sheet:
Quote:

11.1 A/D Acquisition Requirements

After the analog input channel is selected (changed),
this acquisition [delay] must be done before the conversion
can be started.

TACQ = 19.72s
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 12:25 pm     Reply with quote

Just a quick observation here.... You need to insert a small delay after each channel select command to give the ADC holding capacitor time to charge to the new voltage level. Look at the spec. sheet to calculate the proper time delay. If you want a rough estimate, I believe 10uS was enough time.

Ronald

Edit: Too slow again...
kazama



Joined: 01 Sep 2009
Posts: 10
Location: Malaysia

View user's profile Send private message

PostPosted: Wed Sep 02, 2009 12:03 am     Reply with quote

still can't work. only left wheel run. do i need to add all portc=something to all looping?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 02, 2009 1:09 am     Reply with quote

I didn't look closely at your if-else code. I suggest that you make a
simple test program that only runs the right wheel. Don't use any if-else
statements. Just run the right wheel. See if that works.
kazama



Joined: 01 Sep 2009
Posts: 10
Location: Malaysia

View user's profile Send private message

PostPosted: Wed Sep 02, 2009 2:26 am     Reply with quote

I got left and right sensor works. Can move now. But having a problem on front sensor which is RA0. To move the robot forward I use portc=0b00010001 and portc=0b00101000 to reverse it. But still cannot reverse. How should I fix it? Do you think I need to change the vref value? Current value that I use is vref=0x4 which is 0.1v.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group