Line following robot

Discussion in 'Embedded Systems and Microcontrollers' started by samsoltats, Sep 3, 2010.

  1. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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:

    Code ( (Unknown Language)):
    1.  
    2. #include<16f877A.h>
    3. #fuses HS,NOWDT,NOPROTECT,NOLVP
    4. #use delay(clock = 8000000, INT_OSC)
    5. //page 130, datasheet
    6. //choosing frequency = 4.90khz -> period = 204us
    7. //t2DIV = 4
    8. //timer2 prescaler, PR2 = 0x65
    9. //to setup the value for the pwm function:
    10. //Duty = duty cycle/(t2DIV(1/8000000))
    11. void main()
    12. {
    13.    int remember = -1;
    14.    long DutyR = 204;   //50% duty cycle
    15.    long DutyL = 204;   //50% duty cycle
    16.    set_tris_B(0xFF);
    17.    set_tris_A(0x00);
    18.    set_tris_D(0x00);
    19.    setup_ccp1(CCP_PWM);   // Confiqure CCP1 as a PWM
    20.    setup_ccp2(CCP_PWM);   //
    21.    
    22.    //
    23.    setup_timer_2(T2_DIV_BY_4, 0x65, 1);   //sets the frequency of the pwm
    24.    
    25.    set_pwm1_duty(DutyL);      
    26.    set_pwm2_duty(DutyR);
    27.    //delay_ms(1000);
    28.    while(TRUE)
    29.      
    30.    {
    31.       //if DutyL>=DutyR remember is equal to 1 else remember = 0
    32. //stop All High    
    33. 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)
    34.       {
    35.           DutyR= 0;
    36.           DutyL= 0;
    37.           output_low(PIN_d3);
    38.           output_low(PIN_d2);
    39.          // remember = 1;
    40.            
    41.       }
    42. // Stop
    43.          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)
    44.           {
    45.           DutyR= 0;
    46.           DutyL= 0;
    47.           output_low (PIN_d3);
    48.           output_low(PIN_d2);
    49.           //remember = 0;
    50.           }
    51.    // sharp turn left "A"
    52.        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)
    53.       {
    54.           DutyR= 408;
    55.           DutyL= 408;
    56.           output_high(PIN_d3);
    57.           output_low (PIN_d2);
    58.          // remember = 1;
    59.        
    60.       }
    61. // sharp turn left "with no B3"
    62.        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)
    63.       {
    64.           DutyR= 408;
    65.           DutyL= 408;
    66.           output_high(PIN_d3);
    67.           output_low (PIN_d2);
    68.          // remember = 1;
    69.        
    70.       }
    71. //Sharp turn Right "B"
    72.           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)
    73.       {
    74.           DutyR= 408;
    75.           DutyL= 408;
    76.           output_low(PIN_d3);
    77.           output_high(PIN_d2);
    78.          // remember = 1;
    79.          
    80.      
    81.       }
    82. //Sharp turn Right "B"
    83.           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)
    84.       {
    85.           DutyR= 408;
    86.           DutyL= 408;
    87.           output_low(PIN_d3);
    88.           output_high(PIN_d2);
    89.          // remember = 1;
    90.          
    91.       }
    92. //   turn moderately to Left1 "C"
    93.       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)
    94.       {
    95.           DutyR= 306;
    96.           DutyL= 204;
    97.           output_high(PIN_d3);
    98.           output_low(PIN_d2);
    99.           remember = 0;
    100.       }
    101. //   turn moderately to Left2 "C"
    102.       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)
    103.       {
    104.           DutyR= 306;
    105.           DutyL= 204;
    106.           output_high(PIN_d3);
    107.           output_low(PIN_d2);
    108.          // remember = 0;
    109.       }
    110. //   Turn moderately to Right1   "D"
    111.         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)
    112.       {
    113.      
    114.           DutyR=204;
    115.           DutyL=306;
    116.           output_low(PIN_d3);
    117.           output_high(PIN_d2);
    118.           remember = 0;
    119.       }
    120. //   Turn moderately to Right2   "D"
    121.         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)
    122.       {
    123.      
    124.           DutyR=204;
    125.           DutyL=306;
    126.           output_low(PIN_d3);
    127.           output_high(PIN_d2);
    128.           remember = 0;
    129.       }
    130. //     turn Left1 "E"
    131.          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)
    132.           {
    133.           DutyR= 306;
    134.           DutyL= 102;
    135.           output_high(PIN_d3);
    136.           output_low(PIN_d2);
    137.           remember = 0;
    138.           }
    139. //     turn Left2 "E"
    140.          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)
    141.           {
    142.           DutyR= 306;
    143.           DutyL= 102;
    144.           output_high(PIN_d3);
    145.           output_low(PIN_d2);
    146.          // remember = 0;
    147.           }
    148. //     turn Left3 "E"
    149.          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)
    150.           {
    151.           DutyR= 306;
    152.           DutyL= 102;
    153.           output_high(PIN_d3);
    154.           output_low(PIN_d2);
    155.          // remember = 0;
    156.           }
    157. //     turn Left4 "E"
    158.          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)
    159.           {
    160.           DutyR= 306;
    161.           DutyL= 102;
    162.           output_high(PIN_d3);
    163.           output_low(PIN_d2);
    164.          // remember = 0;
    165.           }
    166. //turn Right1 "F"  
    167.         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)
    168.           {
    169.           DutyR= 306;
    170.           DutyL= 102;
    171.           output_low(PIN_d3);
    172.           output_high(PIN_d2);
    173.           remember = 0;
    174.           }
    175. //turn Right2 "F"  
    176.         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)
    177.           {
    178.           DutyR= 306;
    179.           DutyL= 102;
    180.           output_low(PIN_d3);
    181.           output_high(PIN_d2);
    182.           remember = 0;
    183.           }
    184. //turn Right3 "F"  
    185.         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)
    186.           {
    187.           DutyR= 306;
    188.           DutyL= 102;
    189.           output_low(PIN_d3);
    190.           output_high(PIN_d2);
    191.           remember = 0;
    192.           }
    193. //turn Right4 "F"  
    194.         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)
    195.           {
    196.           DutyR= 306;
    197.           DutyL= 102;
    198.           output_low(PIN_d3);
    199.           output_high(PIN_d2);
    200.           remember = 0;
    201.           }
    202. // Go Straight "D"
    203.          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)
    204.           {
    205.           DutyR= 204;
    206.           DutyL= 204;
    207.           output_high (PIN_d3);
    208.           output_high(PIN_d2);
    209.           remember = 0;
    210.           }
    211. // Go Straight "2" possibility
    212.          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)
    213.           {
    214.           DutyR= 204;
    215.           DutyL= 204;
    216.           output_high (PIN_d3);
    217.           output_high(PIN_d2);
    218.           remember = 0;
    219.           }
    220.          
    221.         /*else if (remember==1)
    222.          //{
    223.             DutyR = 306;
    224.             DutyL = 102;
    225.          }*/
    226.          set_pwm1_duty(DutyL);      
    227.          set_pwm2_duty(DutyR);
    228.      
    229.    }
    230. }  
    231.  
    232.  
    All regard
     
    Last edited by a moderator: Sep 3, 2010
  2. vantusaonho

    New Member

    Apr 28, 2010
    7
    0
    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:
    [​IMG]
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  5. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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.
     
  6. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    What chip type are you using? PDIP, PLCC, TQFP?

    The schematic pin numbers don't seem to match the PIC's pins.
     
  7. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  9. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    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.
     
  10. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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?
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
    Code ( (Unknown Language)):
    1. #include<16f877A.h>
    2. #fuses HS,NOWDT,NOPROTECT,NOLVP
    You have to use the correct include file, and set the configuration word correct.
     
  12. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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?
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Please read post #4:rolleyes:
     
  14. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    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.

    [​IMG]

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

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    Code ( (Unknown Language)):
    1.  
    2. #include<16f886.h>
    3. #fuses RC,NOWDT,NOPROTECT,NOLVP
    4. #use delay(clock = 8000000, INT_OSC)
    5. //page 130, datasheet
    6. //choosing frequency = 4.90khz -> period = 204us
    7. //t2DIV = 4
    8. //timer2 prescaler, PR2 = 0x65
    9. //to setup the value for the pwm function:
    10. //Duty = duty cycle/(t2DIV(1/8000000))
    11. void main()
    12. {
    13.    int remember = -1;
    14.    long DutyR = 0;   //50% duty cycle
    15.    long DutyL = 0;   //50% duty cycle
    16.    set_tris_B(0xFF);
    17.    set_tris_A(0xFF);
    18.    set_tris_c(0x00);
    19.    setup_ccp1(CCP_PWM);   // Confiqure CCP1 as a PWM
    20.    setup_ccp2(CCP_PWM);   //
    21.    
    22.    //
    23.    setup_timer_2(T2_DIV_BY_4, 0x65, 1);   //sets the frequency of the pwm
    24.    //delay_ms(1);
    25.    set_pwm1_duty(DutyL);      
    26.    set_pwm2_duty(DutyR);
    27.    while(true)
    28.      
    29.    {
    30.       //if DutyL>=DutyR remember is equal to 1 else remember = 0
    31. //stop    
    32. if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==1 )
    33.       {
    34.           DutyR= 0;
    35.           DutyL= 0;
    36.           output_low(PIN_c2);
    37.           output_low(PIN_c3);
    38.           remember = 1;
    39.            
    40.       }
    41.    // sharp turn left "A"
    42.        else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
    43.       {
    44.           DutyR= 408;
    45.           DutyL= 408;
    46.           output_high(PIN_c2);
    47.           output_low (PIN_c3);
    48.           remember = 1;
    49.        
    50.       }
    51. //Sharp turn Right "B"
    52.           else if (input(PIN_B0) == 1 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
    53.       {
    54.           DutyR= 408;
    55.           DutyL= 408;
    56.           output_low(PIN_c2);
    57.           output_high(PIN_c3);
    58.           remember = 1;
    59.          
    60.      
    61.       }
    62. //      turn Left "C"
    63.        else if (input(PIN_B0) == 0 && input(PIN_B1)==1 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
    64.       {
    65.           DutyR= 306;
    66.           DutyL= 204;
    67.           output_high(PIN_c2);
    68.           output_low(PIN_c3);
    69.           remember = 0;
    70.       }
    71. //   Turn Right   "D"
    72.          else if (input(PIN_B0) == 1 && input(PIN_B1)==1 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==0)
    73.       {
    74.      
    75.           DutyR=204;
    76.           DutyL=306;
    77.           output_low(PIN_c2);
    78.           output_high(PIN_c3);
    79.           remember = 0;
    80.       }
    81. //   Moderate turn Left "E"
    82.          else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B4)==0 && input(PIN_B5)==0)
    83.           {
    84.           DutyR= 306;
    85.           DutyL= 102;
    86.           output_high(PIN_c2);
    87.           output_low(PIN_c3);
    88.           remember = 0;
    89.           }
    90. //Moderate turn Right "F"  
    91.          else if (input(PIN_B0) == 1 && input(PIN_B1)==0 && input(PIN_B2)==1 && input(PIN_B4)==0 && input(PIN_B5)==0)
    92.           {
    93.           DutyR= 306;
    94.           DutyL= 102;
    95.           output_low(PIN_c2);
    96.           output_high(PIN_c3);
    97.           remember = 0;
    98.           }
    99. // Go Straight "G"
    100.          else if (input(PIN_B0) == 0 && input(PIN_B1)==1 && input(PIN_B2)==1 && input(PIN_B4)==0 && input(PIN_B5)==0)
    101.           {
    102.           DutyR= 204;
    103.           DutyL= 204;
    104.           output_high (PIN_c2);
    105.           output_high(PIN_c3);
    106.           remember = 0;
    107.           }
    108. // Stop
    109.          else if (input(PIN_B0) == 0 && input(PIN_B1)==0 && input(PIN_B2)==0 && input(PIN_B4)==0 && input(PIN_B5)==1)
    110.           {
    111.           DutyR= 204;
    112.           DutyL= 204;
    113.           output_low (PIN_c2);
    114.           output_low(PIN_c3);
    115.           remember = 0;
    116.           }
    117.          
    118.         /*else if (remember==1)
    119.          //{
    120.             DutyR = 306;
    121.             DutyL = 102;
    122.          }*/
    123.          set_pwm1_duty(DutyL);      
    124.           set_pwm2_duty(DutyR);
    125.      
    126.    }
    127. }
    128.  
    Sorry this "#" does not appeare on my block.Any way here the code.
     
    Last edited by a moderator: Sep 6, 2010
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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?
     
  17. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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.
     
  18. bertus

    Administrator

    Apr 5, 2008
    15,646
    2,345
    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:

    Code ( (Unknown Language)):
    1.  
    2. (put your code here)
    3.  

    Bertus
     
  19. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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?
     
  20. samsoltats

    Thread Starter New Member

    Jul 13, 2010
    16
    0
    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?
     
Loading...