Line following robot

Thread Starter

samsoltats

Joined Jul 13, 2010
16
Good day
I am currently experiencing problem with the PIC, it can be recognized by the PIC-kit2 and programmed but I cant see anything at the output.
All the TRIS Reg for output and input pin are configured correctly, I even tried to write a simple blinking LED program it also does not work.
I am using CCS compiler C language with PIC 16f877A this was working correctly before i staterd with the Sensors.
The other thing I have realized is that the MPlab was not able to get to debugging mode when I tried it, then I uninstalled everything and installed it again but still setting at the problem. May you please help me, I have tried different new PIC already. Just for information I managed to make the sensor to work. I am using digital method.
Here is the program code:

Rich (BB code):
#include<16f877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000, INT_OSC)
//page 130, datasheet
//choosing frequency = 4.90khz -> period = 204us
//t2DIV = 4
//timer2 prescaler, PR2 = 0x65
//to setup the value for the pwm function:
//Duty = duty cycle/(t2DIV(1/8000000))
void main()
{
   int remember = -1;
   long DutyR = 204;   //50% duty cycle
   long DutyL = 204;   //50% duty cycle
   set_tris_B(0xFF);
   set_tris_A(0x00);
   set_tris_D(0x00);
   setup_ccp1(CCP_PWM);   // Confiqure CCP1 as a PWM
   setup_ccp2(CCP_PWM);   //
   
   //
   setup_timer_2(T2_DIV_BY_4, 0x65, 1);   //sets the frequency of the pwm
   
   set_pwm1_duty(DutyL);      
   set_pwm2_duty(DutyR);
   //delay_ms(1000);
   while(TRUE)
      
   {
      //if DutyL>=DutyR remember is equal to 1 else remember = 0
//stop All High    
if (input(PIN_B0) == 1 && input(PIN_B1)==1 && input(PIN_B2)==1 && input(PIN_B3)== 1 && input(PIN_B4)==1 && input(PIN_B5)==1 && input(PIN_B6 )==1)
      {
          DutyR= 0;
          DutyL= 0;
          output_low(PIN_d3);
          output_low(PIN_d2);
         // remember = 1;
           
      }
// Stop
         else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 0 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 0;
          DutyL= 0;
          output_low (PIN_d3);
          output_low(PIN_d2);
          //remember = 0;
          }
   // sharp turn left "A"
       else if (input(PIN_B0) == 1 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 1 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
      {
          DutyR= 408;
          DutyL= 408;
          output_high(PIN_d3);
          output_low (PIN_d2);
         // remember = 1;
        
      }
// sharp turn left "with no B3"
       else if (input(PIN_B0) == 1 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 0 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
      {
          DutyR= 408;
          DutyL= 408;
          output_high(PIN_d3);
          output_low (PIN_d2);
         // remember = 1;
        
      }
//Sharp turn Right "B"
          else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 1 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==1)
      {
          DutyR= 408;
          DutyL= 408;
          output_low(PIN_d3);
          output_high(PIN_d2);
         // remember = 1;
          
     
      }
//Sharp turn Right "B"
          else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 0 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==1)
      {
          DutyR= 408;
          DutyL= 408;
          output_low(PIN_d3);
          output_high(PIN_d2);
         // remember = 1;
          
      }
//   turn moderately to Left1 "C"
      else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B3)== 1 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
      {
          DutyR= 306;
          DutyL= 204;
          output_high(PIN_d3);
          output_low(PIN_d2);
          remember = 0;
      }
//   turn moderately to Left2 "C"
      else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B3)== 0 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
      {
          DutyR= 306;
          DutyL= 204;
          output_high(PIN_d3);
          output_low(PIN_d2);
         // remember = 0;
      }
//   Turn moderately to Right1   "D"
        else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 1 && input(PIN_B4)==1 && input(PIN_B5)==0 && input(PIN_B6 )==0)
      {
      
          DutyR=204;
          DutyL=306;
          output_low(PIN_d3);
          output_high(PIN_d2);
          remember = 0;
      }
//   Turn moderately to Right2   "D"
        else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 0 && input(PIN_B4)==1 && input(PIN_B5)==0 && input(PIN_B6 )==0)
      {
      
          DutyR=204;
          DutyL=306;
          output_low(PIN_d3);
          output_high(PIN_d2);
          remember = 0;
      }
//     turn Left1 "E"
         else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B3)== 1 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_high(PIN_d3);
          output_low(PIN_d2);
          remember = 0;
          }
//     turn Left2 "E"
         else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B3)== 0 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_high(PIN_d3);
          output_low(PIN_d2);
         // remember = 0;
          }
//     turn Left3 "E"
         else if (input(PIN_B0) == 0 && input(PIN_B1)==1 && input(PIN_B2)==1 && input(PIN_B3)== 1 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_high(PIN_d3);
          output_low(PIN_d2);
         // remember = 0;
          }
//     turn Left4 "E"
         else if (input(PIN_B0) == 0 && input(PIN_B1)==1 && input(PIN_B2)==1 && input(PIN_B3)== 0 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_high(PIN_d3);
          output_low(PIN_d2);
         // remember = 0;
          }
//turn Right1 "F"   
        else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 1 && input(PIN_B4)==1 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_low(PIN_d3);
          output_high(PIN_d2);
          remember = 0;
          }
//turn Right2 "F"   
        else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 0 && input(PIN_B4)==1 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_low(PIN_d3);
          output_high(PIN_d2);
          remember = 0;
          }
//turn Right3 "F"   
        else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 1 && input(PIN_B4)==1 && input(PIN_B5)==1 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_low(PIN_d3);
          output_high(PIN_d2);
          remember = 0;
          }
//turn Right4 "F"   
        else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 0 && input(PIN_B4)==1 && input(PIN_B5)==1 && input(PIN_B6 )==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_low(PIN_d3);
          output_high(PIN_d2);
          remember = 0;
          }
// Go Straight "D"
         else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B3)== 1 && input(PIN_B4)==0 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 204;
          DutyL= 204;
          output_high (PIN_d3);
          output_high(PIN_d2);
          remember = 0;
          }
// Go Straight "2" possibility
         else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B3)== 1 && input(PIN_B4)==1 && input(PIN_B5)==0 && input(PIN_B6 )==0)
          {
          DutyR= 204;
          DutyL= 204;
          output_high (PIN_d3);
          output_high(PIN_d2);
          remember = 0;
          }
         
        /*else if (remember==1)
         //{
            DutyR = 306;
            DutyL = 102;
         }*/
         set_pwm1_duty(DutyL);      
         set_pwm2_duty(DutyR);
      
   } 
}
All regard
 
Last edited by a moderator:

vantusaonho

Joined Apr 28, 2010
7
There is no anything at the output because of many reason.
1. Schematic
2. Board layout
3. This is code
You cant see anything at PWM, if you may see anything at direction
Try this circuit to control DC motor in PWM:
 

t06afre

Joined May 11, 2009
5,934
Go to the data sheet and read once more about PORTA and the ADCON1 register. Also mastering the art of debugging is very important. You can use a software simulator like MPLAB SIM to track down many errors. Then I have problems I often break the code into segments, and test each segment separate.
I will recommend that take some free Microchip Webseminars from this site
http://techtrain.microchip.com/webseminars/QuickList.aspx
Take the time to take at least these in the same order
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=46
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=61
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=137
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=153
If you take a look at this list you should be able to debug the 16f887(a) chip with Pickit2
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en027813
 

Thread Starter

samsoltats

Joined Jul 13, 2010
16
There is no anything at the output because of many reason.
1. Schematic
2. Board layout
3. This is code
You cant see anything at PWM, if you may see anything at direction
Try this circuit to control DC motor in PWM:
HELLO, attached is my circuits for bot the Pic and the sensors, the program was working fine before while i was using PIC 16f887, then it burned now its no longer working after replcaing it with PIC16f877A. and as with the H-bridge drive i am using LMD18200 it seems to work fine. I think the problem might be on the program or the circuit, may someone help please.
 

Attachments

Thread Starter

samsoltats

Joined Jul 13, 2010
16
Dont mind about the shape and all that, i using altium design program, is just mixing the pins for easy connections. To answere your question that Pic is 16f886 28 pin dip. thank you
 

t06afre

Joined May 11, 2009
5,934
This is somewhat confusing. Please post your current code, using code tgas. Also the 16f886 differ from the 16f877 regarding the configuration word setting. Read cap 14 in the data sheet. Attention to details are very important in this game. Like you must use the correct include file, as the two chips for sure differ.
 

elementalrage

Joined Jul 30, 2009
59
To answere your question that Pic is 16f886 28 pin dip. thank you
It is very difficult to help when you have 3 different chips that you're referring to. The schematic is drawn for a 16F886 (28 pin), but you were using a 16F887 (40 pin) and since that burned out you're now using a 16F877A.

I can't help but think you either have pins mixed up or the CONFIG bits are set wrong.
 

Thread Starter

samsoltats

Joined Jul 13, 2010
16
Yes i understand the confusion i have caused, and i apologize for that, suppose i work with the pic16f886 28 pin because is the one i have now amy someone help me with the code? and change the code i have posted as it refers to a different pic?
 

t06afre

Joined May 11, 2009
5,934
Yes i understand the confusion i have caused, and i apologize for that, suppose i work with the pic16f886 28 pin because is the one i have now amy someone help me with the code? and change the code i have posted as it refers to a different pic?
You must be the person who DO things. We in this forum are more than happy to discuss your problem and suggest solutions. But we can or will not do the work for you. If you do want help why do you not do as we ask you to do. Like you have not posted your current code. In order for you to have a working project. The two first line in your C code are very important like
Rich (BB code):
#include<16f877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
You have to use the correct include file, and set the configuration word correct.
 

Thread Starter

samsoltats

Joined Jul 13, 2010
16
I am afraid that now i dont know how to post the code with the "Tag", or does that mean i should commend my coding, i dont understand the document that i have been refered to, hence i can not yet post the new code, actually i am using the same code except that i will change the first statement to the releveant PIC and compare the datasheet, which look mostly the same exacept the output pins and the ccp pins are different otherewise code will be the same for different chip?
 
I am afraid that now i dont know how to post the code with the "Tag"...
It's actually "CODE" tag.

In the graphic below, the "CODE" tag (#) has been highlighted with a RED box and a RED glow around it. Press that and the "CODE" tags appear - shown here in ORANGE. Simply insert your 'code' in between these "CODE" tags and your 'code' will then be formatted properly for the forum readers to view.



Thank you, I'll be here all week and I do accept tips...:D
 

Thread Starter

samsoltats

Joined Jul 13, 2010
16
Rich (BB code):
#include<16f886.h>
#fuses RC,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000, INT_OSC)
//page 130, datasheet
//choosing frequency = 4.90khz -> period = 204us
//t2DIV = 4
//timer2 prescaler, PR2 = 0x65
//to setup the value for the pwm function:
//Duty = duty cycle/(t2DIV(1/8000000))
void main()
{
   int remember = -1;
   long DutyR = 0;   //50% duty cycle
   long DutyL = 0;   //50% duty cycle
   set_tris_B(0xFF);
   set_tris_A(0xFF);
   set_tris_c(0x00);
   setup_ccp1(CCP_PWM);   // Confiqure CCP1 as a PWM
   setup_ccp2(CCP_PWM);   //
   
   //
   setup_timer_2(T2_DIV_BY_4, 0x65, 1);   //sets the frequency of the pwm
   //delay_ms(1);
   set_pwm1_duty(DutyL);      
   set_pwm2_duty(DutyR);
   while(true)
      
   {
      //if DutyL>=DutyR remember is equal to 1 else remember = 0
//stop     
if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==1 )
      {
          DutyR= 0;
          DutyL= 0;
          output_low(PIN_c2);
          output_low(PIN_c3);
          remember = 1;
           
      }
   // sharp turn left "A"
       else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
      {
          DutyR= 408;
          DutyL= 408;
          output_high(PIN_c2);
          output_low (PIN_c3);
          remember = 1;
        
      }
//Sharp turn Right "B"
          else if (input(PIN_B0) == 1 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
      {
          DutyR= 408;
          DutyL= 408;
          output_low(PIN_c2);
          output_high(PIN_c3);
          remember = 1;
          
     
      }
//      turn Left "C"
       else if (input(PIN_B0) == 0 && input(PIN_B1)==1 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
      {
          DutyR= 306;
          DutyL= 204;
          output_high(PIN_c2);
          output_low(PIN_c3);
          remember = 0;
      }
//   Turn Right   "D"
         else if (input(PIN_B0) == 1 && input(PIN_B1)==1 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
      {
      
          DutyR=204;
          DutyL=306;
          output_low(PIN_c2);
          output_high(PIN_c3);
          remember = 0;
      }
//   Moderate turn Left "E"
         else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B4)==0 && input(PIN_B5)==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_high(PIN_c2);
          output_low(PIN_c3);
          remember = 0;
          }
//Moderate turn Right "F"   
         else if (input(PIN_B0) == 1 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B4)==0 && input(PIN_B5)==0)
          {
          DutyR= 306;
          DutyL= 102;
          output_low(PIN_c2);
          output_high(PIN_c3);
          remember = 0;
          }
// Go Straight "G"
         else if (input(PIN_B0) == 0 && input(PIN_B1)==1 && input(PIN_B2)==1 && input(PIN_B4)==0 && input(PIN_B5)==0)
          {
          DutyR= 204;
          DutyL= 204;
          output_high (PIN_c2);
          output_high(PIN_c3);
          remember = 0;
          }
// Stop
         else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==1)
          {
          DutyR= 204;
          DutyL= 204;
          output_low (PIN_c2);
          output_low(PIN_c3);
          remember = 0;
          }
         
        /*else if (remember==1)
         //{
            DutyR = 306;
            DutyL = 102;
         }*/
         set_pwm1_duty(DutyL);      
          set_pwm2_duty(DutyR);
      
   } 
}
Sorry this "#" does not appeare on my block.Any way here the code.
 
Last edited by a moderator:

t06afre

Joined May 11, 2009
5,934
If you look at the description for using CODE tags you will see that the start and end differ somewhat. Also going back to your configuration setting. It looks like you have a setup for RC osc. But I got the impression that you use internal osc. If you use internal osc the default setting is 4 MHz. Going through your postings I can not see any description about what is wrong (other than not working). Have you any debugging result you are able to share with us?
 

Thread Starter

samsoltats

Joined Jul 13, 2010
16
Hey t06afre, Yes i am using internal osc, the code is running correctly problem is the logic i guess, because i started with the PWM using CCP, it works perfectly. I am not good with programming but i guess i need to read the pins all at once and use a variable to store the sensor information, then do the operations lastly using calling methods, which i dont know how to do it, because the robot seems to get confused when there is rapid change and just stops, or etheir sequence of logic is not correct, i dont know.
 

bertus

Joined Apr 5, 2008
22,270
Hello,

You can use the code tags as followed:

to start the code use [code=rich] (put your code here)
at the end of the code use [/code]

The result will be:

Rich (BB code):
(put your code here)

Bertus
 

Thread Starter

samsoltats

Joined Jul 13, 2010
16
Oh! but isnt it that the output_high and output_low are for directional depending on how much % of the PWM(Duty R and Duty L) is?
 

Thread Starter

samsoltats

Joined Jul 13, 2010
16
Yes i understand, then how do you go reverse or forward because those statement "output_high() & output_high();"are to either go forward or backward by just reversing the polarities of the motor?
 
Top