help in programming pic 16f877a by mikro c

Discussion in 'General Electronics Chat' started by mastertiger, Aug 6, 2012.

  1. mastertiger

    Thread Starter New Member

    Jul 9, 2012
    24
    1
    my project is 4 axes cnc with robotic arm
    i finish cnc 4 axes (thank god )
    i work know in programming pic 16f877a for robotic arm
    i use dc motor for production line (portb.f7)
    drive for 3 stepper motor take 6 pin from pic for direction and step
    (portb from pin 6 to 1 )
    2 motor for robotic arm
    1 motor for fixture (to automatic fixed the work piece in cnc)
    1 servo motor for griper portb.f0
    1-start sw1 (portc.f0)
    1-sensor in production line (portc.f1)
    1-sensor in zero point (origin point of cnc)(portc.f2)
    1- stop sw2 (portc.f3)
    i make very large program to make it easy to any one to understand
    sequence
    {
    when i push sw1(portc.f0) dc motor for production line run
    when work piece cut sensor dc motor stop motor of robotic arm run (one by one ) griper close take work piece to fixture and put it then fixture close
    after cnc finish back to zero point robotic move again to take work piece
    then dc motor run again }
    problem
    sensor in zero point active before cnc run so robotic do not stop until cnc start it work to take work piece after put in fixture
    this is the program
    Code ( (Unknown Language)):
    1. ///////////////////////4 axes cnc machine with robotic arm//////////////////////
    2. ///////////////////////////////tiger team///////////////////////////////////////
    3.  
    4.  
    5. void main() {
    6.  
    7.  
    8. int a;                // variable a
    9. int b;                // variable b
    10. int c;                // variable c
    11. int d;                // variable d
    12. int e;                // variable e
    13. int f;                // variable f
    14. int h;                // variable i
    15. int j;                // variable j
    16. int k;                // variable k
    17. int i;                // variable l
    18. int m;                // variable m
    19. int n;                // variable n
    20. int o;                // variable o
    21. int p;                // variable p
    22. int q;                // variable q
    23. int r;                // variable r
    24. int s;                // variable s
    25. int t;                // variable t
    26. int u;                // variable u
    27. int v;                // variable v
    28. int w;                // variable w
    29. int x;                // variable x
    30. int y;                // variable y
    31. int z;               // variable z
    32. trisa=0;             // porta output
    33. trisb=0;             // portb output
    34. trisd=0;             // portd output
    35. portd=0;              // portd output zero
    36. trisc=0xff;           // portc input
    37. portb=0;              // portb output zero
    38. porta=0;                // porta output zero
    39. loop:                 // loop for all program
    40. if(portc.f0==0)        // if push start
    41.  
    42. portb=0b10000000;       // run dc motor
    43.  
    44. if(portc.f1==0)       // if work piece cut sensor
    45. {
    46.  
    47. portb=0;                     // dc motor stop
    48. for(a=0;a<10;a++)          //loop for link 2 down to take work piece
    49. {
    50. portb=0b01100000;
    51. delay_ms(100);
    52. portb=0b01000000;
    53. delay_ms(100);
    54.  
    55. }
    56.  
    57. portb=0;
    58. for(b=0;b<10;b++)  // graper close
    59. {
    60. portb=0b00000001;
    61. delay_us(3000);
    62. portb=0;
    63. delay_ms(20);
    64. }
    65.  
    66. for(c=0;c<10;c++)    //loop for link 2 up
    67. {
    68. portb=0b001000000;
    69. delay_ms(100);
    70. portb=0b000000000;
    71. delay_ms(100);
    72. }
    73. portb=0;
    74. for(d=0;d<10;d++)          //loop for motor base  go to fixture
    75. {
    76. portb=0b00011000;
    77. delay_ms(100);
    78. portb=0b00010000;
    79. delay_ms(100);
    80.  
    81. }
    82. portb=0;
    83. for(e=0;e<10;e++)    //loop for link2 down to put work piece in fixture
    84. {
    85. portb=0b01100000;
    86. delay_ms(100);
    87. portb=0b01000000;
    88. delay_ms(100);
    89.  
    90. }
    91.  
    92. portb=0;
    93. for(f=0;f<10;f++)      //motor of fixture close
    94. {
    95. portb=0b000000110;
    96. delay_ms(100);
    97. portb=0b000000010;
    98. delay_ms(100);
    99. }
    100. portb=0;
    101. for(h=0;h<20;h++)    // graper open to leave work piece in fixture
    102. {
    103. portb=0b00000001;
    104. delay_us(1000);
    105. portb=0b00000000;
    106. delay_ms(20);
    107. }
    108. for(j=0;j<10;j++)    // motor of base go back to production line
    109. {
    110. portb=0b000001000;
    111. delay_ms(100);
    112. portb=0b000000000;
    113. delay_ms(100);
    114. }
    115. portb=0;
    116. if(portc.f2==1){while(portc.f2==1){   // problem is here
    117.  
    118. if(portc.f2==0)     // if cnc finish
    119. {
    120. for(k=0;k<10;k++)       // motor base go to fixture
    121. {
    122. portb=0b00011000;
    123. delay_ms(100);
    124. portb=0b00010000;
    125. delay_ms(100);
    126. }
    127. portb=0;
    128. for(i=0;i<10;i++)  // link 2 down to take work piece from fixture
    129. {
    130. portb=0b01100000;
    131. delay_ms(100);
    132. portb=0b01000000;
    133. delay_ms(100);
    134. }
    135. portb=0;
    136. for(m=0;m<10;m++) // graper close to take work piece from fixture
    137. {
    138. portb=0b00000001;
    139. delay_us(3000);
    140. portb=0b00000000;
    141. delay_ms(20);
    142. }
    143. portb=0;
    144. for(n=0;n<10;n++)      //motor of fixture open
    145. {
    146. portb=0b00000010;
    147. delay_ms(100);
    148. portb=0b00000000;
    149. delay_ms(100);
    150. }
    151. portb=0;
    152. for(p=0;p<10;p++)      //loop for link 2 up
    153. {
    154. portb=0b00100000;
    155. delay_ms(100);
    156. portb=0b00000000;
    157. delay_ms(100);
    158. }
    159. portb=0;
    160. for(q=0;q<10;q++)    //motor of base take work piece to production line
    161. {
    162. portb=0b00001000;
    163. delay_ms(100);
    164. portb=0b00000000;
    165. delay_ms(100);
    166. }
    167. portb=0;
    168.  
    169. for(r=0;r<10;r++)     // link 2 down to put work piece in production line
    170. {
    171. portb=0b01100000;
    172. delay_ms(100);
    173. portb=0b01000000;
    174. delay_ms(100);
    175. }
    176. portb=0;
    177. for(s=0;s<25;s++) //graper open to leave work piece on production line
    178. {
    179. portb=0b00000001;
    180. delay_us(1000);
    181. portb=0b00000000;
    182. delay_ms(20);
    183. }
    184. portb=0;
    185. portb=0b10000000;
    186. delay_ms(9000);
    187. portb=0b00000000;
    188.  
    189.  
    190.  
    191. }}
    192. }
    193. goto loop;
    194. }
    i use this code for make robotic stop until cnc finish and back again
    if(portc.f2==1){while(portc.f2==1){

    if(portc.f2==0)
    but do not work
    then what i missed
    please help
    thanks
    this is Proteus file
    i connected led Instead of drive and motor for simulation
    http://www.mediafire.com/download.php?7zo7c2n5eniaenb
    thanks i solve my problem
    Code ( (Unknown Language)):
    1. ///////////////////////4 axes cnc machine with robotic arm//////////////////////
    2. ///////////////////////////////tiger team///////////////////////////////////////
    3. //////////////////////////////mohammed aly//////////////////////////////////////
    4. /////////////////////////////ahmed mohammed/////////////////////////////////////
    5. /////////////////////////////abdel rahman///////////////////////////////////////
    6. /////////////////////////////sherif/////////////////////////////////////////////
    7.  
    8. void main() {
    9.  
    10.  
    11. int a;                // variable a
    12. int b;                // variable b
    13. int c;                // variable c
    14. int d;                // variable d
    15. int e;                // variable e
    16. int f;                // variable f
    17. int h;                // variable i
    18. int j;                // variable j
    19. int k;                // variable k
    20. int i;                // variable l
    21. int m;                // variable m
    22. int n;                // variable n
    23. int o;                // variable o
    24. int p;                // variable p
    25. int q;                // variable q
    26. int r;                // variable r
    27. int s;                // variable s
    28. int t;                // variable t
    29. int u;                // variable u
    30. int v;                // variable v
    31. int w;                // variable w
    32. int x;                // variable x
    33. int y;                // variable y
    34. int z;               // variable z
    35. trisa=0;             // porta output
    36. trisb=0;             // portb output
    37. trisd=0;             // portd output
    38. portd=0;              // portd output zero
    39. trisc=0xff;           // portc input
    40. portb=0;              // portb output zero
    41. porta=0;                // porta output zero
    42. loop:                 // loop for all program
    43. if(portc.f0==0)        // if push start
    44.  
    45. portb=0b10000000;       // run dc motor
    46.  
    47. if(portc.f1==0)       // if work piece cut sensor
    48. {
    49.  
    50. portb=0;                     // dc motor stop
    51. for(a=0;a<10;a++)          //loop for link 2 down to take work piece
    52. {
    53. portb=0b01100000;
    54. delay_ms(100);
    55. portb=0b01000000;
    56. delay_ms(100);
    57.  
    58. }
    59.  
    60. portb=0;
    61. for(b=0;b<10;b++)  // graper close
    62. {
    63. portb=0b00000001;
    64. delay_us(3000);
    65. portb=0;
    66. delay_ms(20);
    67. }
    68.  
    69. for(c=0;c<10;c++)    //loop for link 2 up
    70. {
    71. portb=0b001000000;
    72. delay_ms(100);
    73. portb=0b000000000;
    74. delay_ms(100);
    75. }
    76. portb=0;
    77. for(d=0;d<10;d++)          //loop for motor base  go to fixture
    78. {
    79. portb=0b00011000;
    80. delay_ms(100);
    81. portb=0b00010000;
    82. delay_ms(100);
    83.  
    84. }
    85. portb=0;
    86. for(e=0;e<10;e++)    //loop for link2 down to put work piece in fixture
    87. {
    88. portb=0b01100000;
    89. delay_ms(100);
    90. portb=0b01000000;
    91. delay_ms(100);
    92.  
    93. }
    94.  
    95. portb=0;
    96. for(f=0;f<10;f++)      //motor of fixture close
    97. {
    98. portb=0b000000110;
    99. delay_ms(100);
    100. portb=0b000000010;
    101. delay_ms(100);
    102. }
    103. portb=0;
    104. for(h=0;h<20;h++)    // graper open to leave work piece in fixture
    105. {
    106. portb=0b00000001;
    107. delay_us(1000);
    108. portb=0b00000000;
    109. delay_ms(20);
    110. }
    111. for(j=0;j<10;j++)    // motor of base go back to production line
    112. {
    113. portb=0b000001000;
    114. delay_ms(100);
    115. portb=0b000000000;
    116. delay_ms(100);
    117. }
    118. portb=0;
    119. while(portc.f2!=1)
    120. {
    121. portb=0;
    122. portd=0b11111111;
    123. }
    124. if(portc.f2==1)     // if cnc finish
    125. {
    126. while(portc.f2!=0)
    127. {
    128. portb=0;
    129. portd=0b11111111;
    130. }
    131.  
    132. for(k=0;k<10;k++)       // motor base go to fixture
    133. {
    134. portb=0b00011000;
    135. delay_ms(100);
    136. portb=0b00010000;
    137. delay_ms(100);
    138. }
    139. portb=0;
    140. for(i=0;i<10;i++)  // link 2 down to take work piece from fixture
    141. {
    142. portb=0b01100000;
    143. delay_ms(100);
    144. portb=0b01000000;
    145. delay_ms(100);
    146. }
    147. portb=0;
    148. for(m=0;m<10;m++) // graper close to take work piece from fixture
    149. {
    150. portb=0b00000001;
    151. delay_us(3000);
    152. portb=0b00000000;
    153. delay_ms(20);
    154. }
    155. portb=0;
    156. for(n=0;n<10;n++)      //motor of fixture open
    157. {
    158. portb=0b00000010;
    159. delay_ms(100);
    160. portb=0b00000000;
    161. delay_ms(100);
    162. }
    163. portb=0;
    164. for(p=0;p<10;p++)      //loop for link 2 up
    165. {
    166. portb=0b00100000;
    167. delay_ms(100);
    168. portb=0b00000000;
    169. delay_ms(100);
    170. }
    171. portb=0;
    172. for(q=0;q<10;q++)    //motor of base take work piece to production line
    173. {
    174. portb=0b00001000;
    175. delay_ms(100);
    176. portb=0b00000000;
    177. delay_ms(100);
    178. }
    179. portb=0;
    180.  
    181. for(r=0;r<10;r++)     // link 2 down to put work piece in production line
    182. {
    183. portb=0b01100000;
    184. delay_ms(100);
    185. portb=0b01000000;
    186. delay_ms(100);
    187. }
    188. portb=0;
    189. for(s=0;s<25;s++) //graper open to leave work piece on production line
    190. {
    191. portb=0b00000001;
    192. delay_us(1000);
    193. portb=0b00000000;
    194. delay_ms(20);
    195. }
    196. portb=0;
    197. portb=0b10000000;
    198. delay_ms(9000);
    199. portb=0b00000000;
    200.  
    201.  
    202.  
    203. }
    204. }
    205. goto loop;
    206. }
     
    Last edited by a moderator: Aug 6, 2012
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    next time try something like this instead:

    Code ( (Unknown Language)):
    1.  
    2. if(portc.f2){while(portc.f2){};};  [COLOR=Red]When checking to see if it is ==1 or TRUE[/COLOR]
    3.  
    4. else if(!portc.f2){ };                 [COLOR=Red] When checking to see[/COLOR] [COLOR=Red]if it is ==0[/COLOR] [COLOR=Red]or False[/COLOR]
    5.  
     
  3. mastertiger

    Thread Starter New Member

    Jul 9, 2012
    24
    1
    thanks for replay
     
  4. mastertiger

    Thread Starter New Member

    Jul 9, 2012
    24
    1
    new problem help please
    i need griper be close all the time when motor move
    but pic read it sequence
    so can i make pin for servo motor of griper give pulse and at the same time motor run
    or i need to use pwm and how can i used it to do this
    Code ( (Unknown Language)):
    1. for(b=0;b<1000;b++)  // graper close
    2. {
    3. portb=0b00000001;
    4. delay_us(3000);
    5. portb=0;
    6. delay_ms(20);
    7. }
    and in the same time
    Code ( (Unknown Language)):
    1. for(c=0;c<10;c++)    //loop for link 2 up
    2. {
    3. portb=0b001000000;
    4. delay_ms(100);
    5. portb=0b000000000;
    6. delay_ms(100);
    7. }
    8. portb=0;
    9. for(d=0;d<10;d++)          //loop for motor base  go to fixture
    10. {
    11. portb=0b00011000;
    12. delay_ms(100);
    13. portb=0b00010000;
    14. delay_ms(100);
    15.  
    16. }
    17. portb=0;
    18. for(e=0;e<10;e++)    //loop for link2 down to put work piece in fixture
    19. {
    20. portb=0b01100000;
    21. delay_ms(100);
    22. portb=0b01000000;
    23. delay_ms(100);
    24.  
    25. }
    26.  
    27. portb=0;
    28. for(f=0;f<10;f++)      //motor of fixture close
    29. {
    30. portb=0b000000110;
    31. delay_ms(100);
    32. portb=0b000000010;
    33. delay_ms(100);
    34. }
    35. portb=0;
    thanks
     
    Last edited by a moderator: Aug 7, 2012
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Why don't you separate each one in its own subroutine??
    Example:
    Code ( (Unknown Language)):
    1.  
    2. void gripper_close(){
    3. for(b=0;b<1000;b++)  // graper close
    4. {
    5. portb=0b00000001;
    6. delay_us(3000);
    7. portb=0;
    8. delay_ms(20);
    9. }
    10. }//end gripper close
    11.  
    12. void fixture_close()
    13. {
    14.  for(f=0;f<10;f++)      //motor of fixture close
    15. {
    16. portb=0b000000110;
    17. delay_ms(100);
    18. portb=0b000000010;
    19. delay_ms(100);
    20. }
    21. portb=0;                      
    22. }//end fixture close
    23.  
    then you can just call that sub routine when needed from your main loop....

    example:

    Code ( (Unknown Language)):
    1.  
    2. void main_loop()
    3. {[INDENT]gripper_close();    //close gripper
    4. fixture_close();    //close fixture
    5. [/INDENT]}
    6.  
    OR

    You will definitely have to start using timers and interrupts to actuate your servos, then you can control multiple servos at once, take a look here.... >>> http://www.ermicro.com/blog/?p=771
     
  6. mastertiger

    Thread Starter New Member

    Jul 9, 2012
    24
    1
    thanks for replay
    if i do that gripper close then fixture close after it
    i need to do the both thing in the same time
     
Loading...