stepper motor and PIC 16F877

Discussion in 'Embedded Systems and Microcontrollers' started by Electronic.art, Feb 19, 2011.

  1. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
    Hello,

    Exuse my bad English

    Topic: I ordered two unipolar stepper motors 6 son, "using the two control circuits L297 and L298, under the control of PIC 16F877 that receives commands from a PC connected by a DB9-max 232,

    http://img4.hostingpics.net/pics/317592forum.png

    For this I must set the USART and timer1.

    Problem: I compile without problem, but when I simulate the linking COMUNICATION TERMINAL with Proteus engines do not run properly
     
  2. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
    Thank you for your help, here is my code written in mikroC for PIC version 8:

    Code ( (Unknown Language)):
    1.  
    2. unsigned int t1, t2, TTMP;
    3. unsigned char m;
    4. unsigned short int cmd_recu;
    5. unsigned short int tmoteur1_L;
    6. unsigned short int tmoteur1_H;
    7. unsigned short int tmoteur2_L;
    8. unsigned short int tmoteur2_H;
    9. unsigned short int speed [16];
    10. unsigned short int cmd_moteur;
    11.  
    12.  
    13.  
    14. / *
    15.  
    16.    Order Received:
    17.          b7: Controls 1: 0 Speed: Commands
    18.          b6: Engine Choice 1: 2 0 Engine: Engine 1
    19.          b5: Marche_Arret 1: On 0: Off
    20.          b4: Motor1 Sense 1: 0:
    21.          b0-b3: Speed Engine
    22.  
    23.  
    24.    Devices:
    25.          cmd_recu = 1: Judgement of the two engines (cmd_moteur = 1)
    26.          cmd_recu = 2: Engine 1 and Engine 2 in March (cmd_moteur = 2)
    27.          cmd_recu = 4: Engine 1 Engine 2 in March and off (cmd_moteur = 4)
    28.          cmd_recu = 8: Engine 1 and Engine 2 to Off in March (cmd_moteur = 8)
    29.  
    30.  
    31.    PortB Pin Assignment:
    32.          RC2: Enable Engine 2
    33.          RC1: Enable Engine 1
    34.          RB4: Sense Engine 2
    35.          RB0: Sense Engine 1
    36.          RB5: Motor Control 2
    37.          RB1: 1 Motor Control
    38.  
    39. * /
    40.  
    41.  
    42.  
    43.  
    44.  
    45. void interrupt ()
    46. {
    47.  
    48.  
    49.  
    50.   if (PIR1.RCIF == 1) / / if data IS Received
    51.   {
    52.      PIR1.RCIF = 0;
    53.      cmd_recu USART_Read = ();
    54. / / Usart_Write (cmd_recu);
    55.  
    56. asm {
    57.  
    58.         bcf STATUS, RP0 / / Bank 0
    59.         bcf STATUS, RP1
    60.  
    61.         movf cmd_recu, 0 / / Usart_Write (cmd_recu);
    62.         movwf TXREG
    63.  
    64.  
    65.         btfss cmd_recu, 7 / / Controls if bit7 = 0
    66.         goto command
    67.  
    68.  
    69.         btfsc cmd_recu, 6 / / Engine ...
    70.         goto moteur2
    71.  
    72.         btfss cmd_recu, 5 / / Marche_Arret Engine 1
    73.         bcf PORTC, 1 / / Stop Motor 1
    74.  
    75.         btfsc cmd_recu, 4 / / Direction of Engine ...
    76.         goto sens2_Moteur1
    77.  
    78.         bsf PORTB, 0 / / Motor 1 Direction 1
    79.         goto vitesse_moteur1
    80. sens2_Moteur1:
    81.         bcf PORTB, 0 / / Motor 1 Direction 2
    82.  
    83. vitesse_moteur1:
    84.         movlw 0x0f
    85.         andwf cmd_recu, 1 / / cmd_recu & 0x0f -> cmd_recu
    86.  
    87.         movlw speed
    88.         ADDWF cmd_recu, 0 / / address vitesse_L [cm_recu] -> W
    89.         movwf FSR / / W -> FSR: register indirect addressing
    90.         movf INDF, 0 / / vitesse_L [cmd_recu] -> W
    91.         movwf tmoteur1_L
    92.  
    93.         bsf STATUS, IRP / / Addressing the area 0x120 - 0x16f
    94.         movf INDF, 0 / / vitesse_H [cmd_recu] -> W
    95.         movwf tmoteur1_H
    96.         bcf STATUS, IRP
    97.  
    98.  
    99.         goto fin_vitesse
    100.  
    101.  
    102. moteur2:
    103.         btfss cmd_recu, 5 / / Marche_Arret Engine 2
    104.         bcf PORTC, 2 / / Stop Motor 2
    105.  
    106.         btfsc cmd_recu, 4
    107.         goto sens2_Moteur2
    108.  
    109.         bsf PORTB, 4 / / Direction 1 Engine 2
    110.         goto vitesse_moteur2
    111. sens2_Moteur2:
    112.         bcf PORTB, 4 / / Sens 2 Engine 2
    113.  
    114. vitesse_moteur2:
    115.         movlw 0x0f
    116.         andwf cmd_recu, 1 / / cmd_recu & 0x0f -> cmd_recu
    117.  
    118.         movlw speed
    119.         ADDWF cmd_recu, 0 / / address vitesse_L [cmd_recu] -> W
    120.         movwf FSR / / W -> FSR: register indirect addressing
    121.         movf INDF, 0 / / vitesse_L [cmd_recu] -> W
    122.         movwf tmoteur2_L
    123.  
    124.  
    125.         bsf STATUS, IRP / / Addressing the area 0x120 - 0x16f
    126.         movf INDF, 0 / / vitesse_H [cmd_recu] -> W
    127.         movwf tmoteur2_H
    128.         bcf STATUS, IRP
    129.  
    130.  
    131.  
    132.         goto fin_vitesse
    133.  
    134.  
    135.  
    136.  
    137. commands:
    138.         btfsc cmd_recu, 0
    139.         goto arret_total
    140.  
    141.         btfsc cmd_recu, 1
    142.         goto Start
    143.  
    144.         btfsc cmd_recu, 2
    145.         goto market.1
    146.  
    147.         btfsc cmd_recu, 3
    148.         goto market2
    149.  
    150.  
    151.         goto fin_vitesse / / Return if another command ...
    152.  
    153.  
    154. arret_total: / / Judgement of 2 Engines
    155.         bcf PORTC, 1 / / Enable disabled
    156.         bcf PORTC, 2
    157.         movlw 1
    158.         movwf cmd_moteur / / save the state of the command
    159.         bcf T1CON, 0 / / Stop the Timer 1: TMR1ON = 0
    160.         bsf STATUS, RP0 / / Bank 1
    161.         bcf PIE1, 0 / / Disable interr.Timer 1
    162.         bcf STATUS, RP0 / / Bank 0
    163.         Bcf PIR1, 0 / / Clear the flag of Timer 1
    164.         goto fin_vitesse
    165.  
    166. On: / / Starting of Motors 2
    167.         bsf PORTC, 1
    168.         bsf PORTC, 2
    169.         movlw 2
    170.         movwf cmd_moteur
    171.         tso T1CON, 0 / / Start Timer 1: TMR1ON = 0
    172.         bsf STATUS, RP0 / / Bank 1
    173.         bsf PIE1, 0 / / Enable interr.Timer 1
    174.         bcf STATUS, RP0 / / Bank 0
    175.                                     / / Add to comparison between t1 and t2
    176.                                     / / To determine the value of 'm' !!!!!!
    177.  
    178.         goto fin_vitesse
    179.  
    180. Market.1: / / Engine 1 start
    181.         bsf PORTC, 1
    182.         bcf PORTC, 2
    183.         movlw 4
    184.         movwf cmd_moteur
    185.         tso T1CON, 0 / / Start Timer 1: TMR1ON = 0
    186.         bsf STATUS, RP0 / / Bank 1
    187.         bsf PIE1, 0 / / Enable interr.Timer 1
    188.         bcf STATUS, RP0 / / Bank 0
    189.         goto fin_vitesse
    190.  
    191. Market2: / / motor 2 reverse
    192.         bcf PORTC, 1
    193.         bsf PORTC, 2
    194.         movlw 8
    195.         movwf cmd_moteur
    196.         tso T1CON, 0 / / Start Timer 1: TMR1ON = 0
    197.         bsf STATUS, RP0 / / Bank 1
    198.         bsf PIE1, 0 / / Enable interr.Timer 1
    199.         bcf STATUS, RP0 / / Bank 0
    200.         goto fin_vitesse
    201.  
    202.  
    203.  
    204. fin_vitesse:
    205.        nop
    206.  
    207.       }
    208.  
    209.  
    210.   }
    211.  
    212.  
    213.  
    214.  
    215.  
    216.  
    217.   if (PIR1.TMR1IF == 1)
    218.   {
    219.      PIR1.TMR1IF = 0, / / clear TMR1IF
    220.  
    221.  
    222.      asm {
    223.  
    224.             btfsc cmd_moteur, 1
    225.             goto Marche_
    226.  
    227.             btfsc cmd_moteur, 2
    228.             goto Marche_1
    229.  
    230.             btfsc cmd_moteur, 3
    231.             goto Marche_2
    232.  
    233.             btfss cmd_moteur, 0 / / Skip ...
    234.             goto retour_interr
    235.  
    236.             nop / / Disable the timer!
    237.             goto retour_interr
    238.  
    239.  
    240.      }
    241.  
    242. / / Control of both engines ...
    243.      asm {
    244. Marche_:
    245.            nop
    246.      }
    247.  
    248.  
    249.      if (m == 1) / / the Timer1 has been assigned to Engine 1
    250.      {
    251.         / / PortB.F1 PortB.F1 = ~ / / reverse the order of the engine 1
    252. / / T1 = tm1;
    253.  
    254.           asm {
    255.  
    256.               movlw 0x02
    257.               xorwf PORTB, 1
    258.  
    259.               movf tmoteur1_L, 0 / / reload t1 which was amended
    260.               movwf _t1
    261.               movf tmoteur1_H, 0
    262.               movwf _t1 1
    263.  
    264.           }
    265.      }
    266.      else if (m == 2) / / the Timer1 has been assigned to Engine 2
    267.      {
    268.         / / PortB.F5 PortB.F5 = ~ / / reverse motor control 2
    269. / / T2 = tm2;
    270.  
    271.           asm {
    272.          
    273.               movlw 0x20
    274.               xorwf PORTB, 1
    275.  
    276.               movf tmoteur2_L, 0 / / reload t2 which was amended
    277.               movwf _t2
    278.               movf tmoteur2_H, 0
    279.               movwf _t2 1
    280.  
    281.           }
    282.  
    283.  
    284.      }
    285.      else if (m == 3) / / Timer1 is assigned to the 2 engines
    286.      {
    287.       / / PortB.F1 PortB.F1 = ~ / / reverse the order of 2 engines
    288.       / / = ~ PortB.F5 PortB.F5;
    289. / / T1 = tm1;
    290. / / T2 = tm2;
    291.  
    292.           asm {
    293.          
    294.               movlw 0x02
    295.               xorwf PORTB, 1
    296.              
    297.               movlw 0x20
    298.               xorwf PORTB, 1
    299.  
    300.               movf tmoteur1_L, 0 / / reload t1 and t2
    301.               movwf _t1
    302.               movf tmoteur1_H, 0
    303.               movwf _t1 1
    304.  
    305.               movf tmoteur2_L, 0 / / reload t2 which was amended
    306.               movwf _t2
    307.               movf tmoteur2_H, 0
    308.               movwf _t2 1
    309.  
    310.           }
    311.      }
    312.  
    313.  
    314.      if (t1 <t2) / / Timer1 will be assigned to Engine 1
    315.      {
    316.         t2 = t2-t1 / / t2 modified
    317.         TTMP ~ = t1;
    318. / / = TMR1L TTMP;
    319. / / TMR1H TTMP =>> 8;
    320.  
    321.           asm {
    322.  
    323.               movf _ttmp, 0 / / Load Timer1 t1
    324.               movwf TMR1L
    325.               movf _ttmp 1.0
    326.               movwf TMR1H
    327.  
    328.           }
    329.  
    330.         m = 1;
    331.      }
    332.      else if (t1> t2) / / Timer1 will be assigned to Engine 2
    333.      {
    334.         t1 = t1-t2 / / modified t1
    335.         TTMP ~ = t2;
    336. / / = TMR1L TTMP;
    337. / / TMR1H TTMP =>> 8;
    338.  
    339.           asm {
    340.  
    341.               movf _ttmp, 0 / / Load Timer1 t1
    342.               movwf TMR1L
    343.               movf _ttmp 1.0
    344.               movwf TMR1H
    345.  
    346.           }
    347.  
    348.         m = 2;
    349.      }
    350.      else / / Timer1 will be awarded to 2 engines
    351.      {
    352.         TTMP ~ = t1;
    353. / / = TMR1L TTMP;
    354. / / TMR1H TTMP =>> 8;
    355.  
    356.           asm {
    357.  
    358.               movf _ttmp, 0 / / load Timer1 with t1 = t2
    359.               movwf TMR1L
    360.               movf _ttmp 1.0
    361.               movwf TMR1H
    362.  
    363.           }
    364.  
    365.         m = 3;
    366.      }
    367.  
    368.      asm {
    369.  
    370.         goto retour_interr
    371.      }
    372.  
    373.  
    374.  
    375.  
    376. / / Motor Control 1 only ...
    377.     asm {
    378.  
    379. Marche_1:
    380.  
    381.         movlw 0x02
    382.         xorwf PORTB, 1 / / = ~ PortB.F1 PortB.F1
    383.  
    384.  
    385.         COMF tmoteur1_L, 0
    386.         movwf TMR1L
    387.         COMF tmoteur1_H, 0
    388.         movwf TMR1H
    389.         goto retour_interr
    390.  
    391.    }
    392.  
    393.  
     
    Last edited by a moderator: Feb 19, 2011
  3. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
    Code ( (Unknown Language)):
    1.  
    2. / / Motor Control 2 only ...
    3.    asm {
    4. Marche_2:
    5.  
    6.         movlw 0x20
    7.         xorwf PORTB, 1 / / = ~ PortB.F5 Portb.F5
    8.  
    9.         COMF tmoteur2_L, 0
    10.         movwf TMR1L
    11.         COMF tmoteur2_H, 0
    12.         movwf TMR1H
    13.         goto retour_interr
    14.  
    15.    }
    16.  
    17.  
    18.  
    19.  asm {
    20.  
    21. retour_interr:
    22.        nop
    23.  
    24.       }
    25.  
    26.  
    27.  }
    28.  
    29. }
    30.  
    31.  
    32.  
    33.  
    34. void main ()
    35. {
    36.  
    37.  
    38.  
    39. tmoteur1_L = 0xff;
    40. tmoteur1_H = 0x55;
    41. tmoteur2_L = 0x98;
    42. tmoteur2_H = 0x00;
    43.  
    44. / *
    45. speed [0] = 0x10;
    46. speed [1] = 0x11;
    47. speed [2] = 0x12;
    48. speed [3] = 0x13;
    49. speed [4] = 0x14;
    50. speed [5] = 0x15;
    51. speed [6] = 0x16;
    52. speed [7] = 0x17;
    53. speed [8] = 0x18;
    54. speed [9] = 0x19;
    55. speed [10] = 0x1a;
    56. speed [11] = 0x1b;
    57. speed [12] = 0x1c;
    58. speed [13] = 0x1d;
    59. speed [14] = 0x1e;
    60. speed [15] = 0x1f * /
    61.  
    62. speed [0] = 0;
    63.  
    64.  
    65. asm {
    66.  
    67.  
    68.         bcf STATUS, RP0
    69.       bcf STATUS, RP1
    70.  
    71.         movlw 0x10 / / Loading of LSB
    72.         movwf _vitesse / / speed instructions in the address
    73.         movlw 0x11 / / _vitesse located in Bank 0
    74.         movwf _vitesse 1
    75.         movlw 0x12
    76.         movwf _vitesse 2
    77.         movlw 0x13
    78.         movwf _vitesse 3
    79.         movlw 0x14
    80.         movwf _vitesse 4
    81.         movlw 0x15
    82.         movwf _vitesse 5
    83.         movlw 0x16
    84.         movwf _vitesse 6
    85.         movlw 0x17
    86.         movwf _vitesse 7
    87.         movlw 0x18
    88.         movwf _vitesse 8
    89.         movlw 0x19
    90.         movwf _vitesse 9
    91.         movlw 0x1a
    92.         movwf _vitesse 10
    93.         movlw 0x1b
    94.         movwf _vitesse 11
    95.         movlw 0x1c
    96.         movwf _vitesse 12
    97.         movlw 0x1d
    98.         movwf _vitesse 13
    99.         movlw 0x1e
    100.         movwf _vitesse 14
    101.         movlw 0x1f
    102.         movwf _vitesse 15
    103.  
    104.         bcf STATUS, RP0
    105.       bsf STATUS, RP1
    106.  
    107.         movlw 0x20 / / Load the MSB of
    108.         movwf _vitesse / / speed instructions in the address
    109.         movlw 0x21 / / _vitesse located in Bank 2
    110.         movwf _vitesse 1
    111.         movlw 0x22
    112.         movwf _vitesse 2
    113.         movlw 0x23
    114.         movwf _vitesse 3
    115.         movlw 0x24
    116.         movwf _vitesse 4
    117.         movlw 0x25
    118.         movwf _vitesse 5
    119.         movlw 0x26
    120.         movwf _vitesse 6
    121.         movlw 0x27
    122.         movwf _vitesse 7
    123.         movlw 0x28
    124.         movwf _vitesse 8
    125.         movlw 0x29
    126.         movwf _vitesse 9
    127.         movlw 0x2A
    128.         movwf _vitesse 10
    129.         movlw 0x2B
    130.         movwf _vitesse 11
    131.         movlw 0x2c
    132.         movwf _vitesse 12
    133.         movlw 0x2d
    134.         movwf _vitesse 13
    135.         movlw 0x2e
    136.         movwf _vitesse 14
    137.         movlw 0x2f
    138.         movwf _vitesse 15
    139.  
    140.         bcf STATUS, RP0
    141.       bcf STATUS, RP1
    142.  
    143. }
    144.  
    145. cmd_moteur = 0;
    146.  
    147.   Trisca = 0b11000000;
    148.   TRISB = 0;
    149.   PORTB = 0b10001000;
    150.  
    151.   m = 3 / / to remove (m will be initialized in
    152.                                     / / Receiving the setting command
    153.                                     / / Start both engines simultaneously)!
    154.  
    155.  
    156.   Usart_Init (9600);
    157.  
    158.   PIR1.RCIF = 0, / / clear USART ROTI
    159.   PIR1.TMR1IF = 0, / / clear TMR1IF
    160.  
    161. / / T1CON.TMR1ON = 1 / / Timer1 Enabled internal clock is
    162.   Intcons = 0x80, / / enable general interrupt
    163.  
    164.   PIE1.RCIE = 1 / / enable USART interrupts Reception
    165. / / PIE1.TMR1IE = 1 / / enable TMR1 interrupts
    166.   INTCON.PEIE = 1 / / enable TMR1 interrupts and USART
    167.  
    168.  
    169.  
    170. while (1);
    171.  
    172.  
    173.  
    174. }
    175.  
     
    Last edited by a moderator: Feb 19, 2011
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    First this post should be in the embedded forum.

    Why are you programming in C but using so much assembler? There really should be no need for that. If you want to program in assembly then just use an assembler. But you really should use C it will be much easier.

    You are trying to do too much all at once. If I understand you correctly, you are having a problem communicating to the terminal in Proteus.

    Write a small program to do just that. Something simple like to display "Hello World" on the terminal. You can then move on from there.

    If you have a more simple program you will increase your chances that someone can help you. Programming in C might also help.
     
  5. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
    I already tried this kind of thing, Hello world ... and its market well, now my problem is this one and not another!
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    What have you done to debug your code? Are your certain your interrupt routine is working?
     
    Last edited: Feb 19, 2011
  7. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
    Yes normally that's right, here is the algorithm that I wrote before programming:

    Algorithm: For the USART:
    Communication takes place on two pins and RC6/TX RC7/RX.
    For transmission:
    1 - Ensure that the TXI interrupt is not enabled.
    2 - Configure TX/RC6 pin input.
    3 - Configure registry TXSTA.
    4 - Initialize the register SPBRG to set the baud rate
    5 - Place the data to be transmitted in TXREG.

    For reception:
    1 - Ensure that the RCI interruption is not validated .
    2 - Configure RX/RC7 pin input.
    3 - Initialize SPBRG register to set the speed of the reception.
    4 - Configure registry RCSTA.
    5 - Wait flag ROTI which gives the end of the reception. [1], [2], [6] [7]


    The flowchart of main program:
    Senior Program -> Configure I / O ports ---> Upload speeds > Setup > Configuration-USART-Timer1 interrupts > OK


    The flowchart of subroutine interrupt processing:
    Interruption -> A byte received ->
    If yes: The disruption caused by the USART Interrupt Processing   End of interrupt processing

    If no: Timer1 Overflow
    If yes: The disruption caused by Timer1 Interrupt Processing > End of interrupt processing
    If not: Back to the main program (main)

    The flowchart of subroutine treatment interruption caused by the USART

    The interruption caused by the USART > Return the flag to "0"
    PIR1.RCIF = 0 > cmd_recu = Data received
    If yes: The data received is a Command
    If no: The data received is a speed



    chart of the console command:
    Data received is a control>B = 6 mode control console
    The different orders start are determined as follows:
    cmd_recu = 00000001 Total Stop (Stop both engines).
    cmd_recu = 00000010 Walk (Start both engines).
    cmd_recu = 00000100 March 1 (Start and stop the moteur2 MOTOR 1).
    cmd_recu = 00001000 March 2 (Start and stop the moteur2 MOTOR 1).


    Console control >cmd_recu = 1
    If yes: Blackout cmd_moteur = 1 > Stop 2 Engines, Off TMR1> Fin_interr
    If no: cmd_recu = 2
    If yes: Start >total cmd_moteur = 2 Start the 2 motors, activated TMR1> Fin_interr
    If no: cmd_recu = 4
    If yes: Start > cmd_moteur = 4, moteurs2 Stop, Start MOTOR 1, off TMR1 > Fin_interr
    If yes: cmd_recu = 8> cmd_moteur Marche 2 = 4, moteurs1 Stop, Start moteur2, off TMR1 > Fin_interr
    If not: Blackout > cmd_moteur = 1, Stop 2, Engines, Off TMR1 >Fin_interr


    The flow chart for determining the speed
    > speed B6 = 1
    If yes: Engine 2 > B5 = 0
    If yes > Portc.F2 = 0, Judgement of moteur2 B4 = 1 >
    If yes Sens2 > Determination of moteur2 speed moteur2 > Load Timer1 Fin_vitesse
    If not> Direction 1 of moteur2 Determination speed moteur2 > Load Timer1 Fin_vitesse
    Same diagram for motor 2
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    Sorry should have been debug above. What have yo done to debug.

    And you never answered why you are using so much assembler in C. You are only making things a lot harder for yourself.
     
  9. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
    I use mikroC because at school we were taught to use mikroC, the program seems to work correctly, I think I have an error in the schema!
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    Then use C and not in line assembler.

    The code is not correct if it is nor working as expected.

    An error in the schema? What do you mean by schema?
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    991
    You need to convert to all C and simplify your code. Get rid of all of those gotos/ It is no wonder you are having problems, as we say it is spaghetti code.
     
  12. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
  13. Electronic.art

    Thread Starter New Member

    Feb 19, 2011
    8
    0
    schema is diagram or draw ! sorry !
     
Loading...