1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Stepper motor control with PIC16F84A

Discussion in 'Microcontrollers' started by netbug, May 6, 2004.

  1. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    I am trying to control a stepper motor with PIC16F84A. The goal is to run the motor for 40 minutes and stop it for 20 minutes. These times must be adjustable using switches. Instead of 40 and 20, you may have 50 minutes on and 10 of, and so on.

    I am using L293B to drive the stepper and the stepper is a motor taken from and old HP 690C printer.

    Can I step the motor once in a second, or the steps must be smaller ?

    Is there any page you might know with a similar project ?

    Thank you,

    Pedro Cardoso
     
  2. falleafd

    falleafd New Member

    Joined:
    May 13, 2003
    Messages:
    412
    Likes:
    0
    Location:
    Vietnam
    Yes, but in this case, you should use full step with 2 winding held at a time.


    Using switch like this

    temp1 = 50
    temp2 = 40

    if button1, move temp1 to "timertemp"
    if button2, move temp2 to "timertemp"

    the "timertemp" here is not the real timer of PIC, because you cannot use timer to wait 1mins. But you use timer overflow interrupt and decrement another variable untill it meet 0.

    For example, timer1 can wait 65ms at prescaler 1:1. You set a variable to count from 1000/65 down to 0. If it meet 0, reset it to 1000/65 and decrement "timertemp". Until "timertemp" meet 0, reset the timertemp and step the motor. Do this loop forever.
     
  3. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    I am trying something similar but my code must have some error, that I am not able to find. As soon as I solve this I'll post again.

    Thank you very much for your help

    Best regards,

    Pedro
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal

    Hi,

    I am trying to use a L293 IC to drive an unipolar stepper motor, but it is not working. Does this driver only work with bippolar motors ?

    When I connect the stepper motor it runs, for example, 3 steps in one direction and another in the oposite direction. I can't understand what is going on. I verified the connections and even the code, but the motor keeps going forward and backwards ....

    Regards,

    Pedro Cardoso
     
  6. motion

    motion New Member

    Joined:
    Jul 14, 2003
    Messages:
    560
    Likes:
    0
    Location:
    Quezon City.PH
    This is usually the result of bad wiring, an open winding or bad sequence in signals to the stepper. That's all I can say with the information you're giving.
     
  7. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi, thank you all. I tried several ways to solve this problem but I didn't succeed.

    Well, I connected 4 diodes (with a resistor of course) to the L293 outputs, so I could monitor the outputs. When I power up the circuit, with the stepper disconnected, the leds flash in sequence but when I connect the stepper, one led is permanently on(actually the 3rd led in the sequence), while other 3 flash normally. This results on a bad functioning of the stepper (2 steps forward and 1 bacwards).

    The code seems fine because the leds flash in the right sequence. So I think it must be the motor or the fact I am using a L293 with a unipolar stepper.
    I tried every position of the windings, and I think I decteted successfully every windings.
    I am trying to solve this problem but I am not able to do it !!!

    Any ideas ?

    Thanks,

    Pedro Cardoso
     
  8. falleafd

    falleafd New Member

    Joined:
    May 13, 2003
    Messages:
    412
    Likes:
    0
    Location:
    Vietnam
    1) draw out your scheme and post it on to here

    2) The L293 does not have the internal diodes to protect the internal transistors. That is you have to put the diodes to your board. But you should use Schooky diodes. You can try with 4001, but you must run at low speed. And I think 4001 is enough. However, why don't u use L293D???

    3) Did you connect the right windings to the board? I posted somewhere in here how to know the windings of unipolar steppers.

    4) With l293, dual H bridges IC, you make your unipolar stepper run as identically as the bipolar motor. And you have to let the center wire(s) free.

    5) In this case, you have to know exactly the right wires of the motor. If you connect wrong wires, the motor will always powered at different endwire of different windings. And because the internal positions of the windings, the motor will not move, and it seem to not pwred (it isn't holding mode but no pwred).
     
  9. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Thank you again for your fast reply. It seems I made an huge error, I connected the centre wire of my stepper to 12 V. I'll try, as you said, to let this cable free.

    I am using a L293B with 20 pins. This quite strange because the datasheet of this component tells that the L293 B only has 16 pins, the one with 20 pins is L293 E, so I don't know if my circuit has the diodes integrated. Anyway, I'll connect the diodes (1N4001) to the circuit, then I'll reconnect the stepper and finally I will tell you if it works.

    Thank you,

    Pedro Cardoso
     
  10. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    I disconnected the common wire and it worked !!! However, I need once more your help. Please take a look at my code. I wanted to keep te stepper running for 5 seconds and stopped for another 5 seconds, but instead of this, the stepper runs forever.

    Thank you again

    Pedro

    PROCESSOR 16F84A
    RADIX DEC
    INCLUDE "P16F84A.INC"
    ERRORLEVEL -302


    __CONFIG 0x3FF1

    ORG 0x0C
    count1 RES 1
    counta RES 1
    countb RES 1
    countc RES 1
    time_off RES 1
    seconds RES 1

    LED Equ PORTB ;set constant LEDPORT = 'PORTB'

    ORG 0x00
    BSF STATUS, RP0

    MOVLW B'00011111'
    MOVWF TRISA & 7FH

    MOVLW B'11110000'
    MOVWF TRISB & 7FH


    MOVLW B'00000100'
    MOVWF OPTION_REG & 0x7F
    BCF STATUS, RP0

    ;***** Main code ******
    Loop1

    movlw d'5'
    ; time in seconds
    movfw seconds
    Loop3
    movlw b'00000001'
    movwf LED
    call Delay ;Delay of 250 ms
    movlw b'00000010'
    movwf LED
    call Delay ;Delay of 250 ms
    movlw b'00000100'
    movwf LED
    call Delay ;Delay of 250 ms
    movlw b'00001000'
    movwf LED
    call Delay ;Delay of 250 ms
    decfsz seconds,1
    goto Loop3 ; Ciclo interior

    movlw d'5'
    movwf time_off
    movlw b'0000000'
    movwf LED

    Loop4
    ; 1 second delay
    call Delay ;Delay of 250 ms
    call Delay ;Delay of 250 ms
    call Delay ;Delay of 250 ms
    call Delay ;Delay of 250 ms

    decfsz time_off,f
    goto Loop4 ; Ciclo interior

    goto Loop1

    ; ******* END Main code *******

    ;250 ms delay
    Delay movlw d'250' ;delay 250 ms (4 MHz clock)
    movwf count1
    d1 movlw 0xC7
    movwf counta
    movlw 0x01
    movwf countb
    Delay_0
    decfsz counta,f
    goto $+2
    decfsz countb,f
    goto Delay_0

    decfsz count1,f
    goto d1
    retlw 0x00

    end
     
  11. falleafd

    falleafd New Member

    Joined:
    May 13, 2003
    Messages:
    412
    Likes:
    0
    Location:
    Vietnam
    Sorry, these days I'm so busy, and I cannot help to test your codes. Perhaps nigel or someone else will help. Don't worry.
     
  12. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,214
    Likes:
    640
    Location:
    Derbyshire, UK
    At the start of Loop1 you're not initialising the 'seconds' register, you used 'movfw seconds' rather than 'movwf seconds'.
     
  13. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    Thank you for your correction. I made a big mistake .... Well I will try to put everything to work.

    Regards,

    Pedro
     
  14. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    The stepper is working fine, but when I connect a dip switch block to RA0...RA3, the PIC freezes. If I remove these connections it works again !!! My software is the one I posted above. I configured TRISA as intputs and TRISB as outputs.

    Pedro
     
  15. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,214
    Likes:
    640
    Location:
    Derbyshire, UK
    Well you don't seem to be accessing PortA at all in the software above, but I'm confused by the way you appear to be declaring your variables?.
    Code (text):

    ORG 0x0C
    count1 RES 1
    counta RES 1
    countb RES 1
    countc RES 1
    time_off RES 1
    seconds RES 1
     
    ORG (as far as I'm aware) sets an address in program memory, not in data registers.

    Try this instead:
    Code (text):

    cblock 0x0C
        count1
        counta
        countb
        countc
        time_off
        seconds
    endc
     
    However, I've assembled your code, and it seems to assemble correctly.

    Are you sure you're not shorting something out with your switch?.
     
  16. TKS

    TKS New Member

    Joined:
    Sep 1, 2003
    Messages:
    560
    Likes:
    0
    Location:
    -----
    idea...

    wire up a lcd screen and watch your self wha tis going on inside the pic..:D

    TKS
     
  17. falleafd

    falleafd New Member

    Joined:
    May 13, 2003
    Messages:
    412
    Likes:
    0
    Location:
    Vietnam
    Oh Nigel,

    It's the same way.

    We can use ORG 00xx
    with

    REGISTER RES 1

    that is REGISTER is 1 byte variable.

    I usually do this.

    cblock is a different way, and it's the same.
     
  18. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi everybody,

    Thanks to all of you my stepper is working fine !!! :D

    I need some more help on this project.
    I can divide this project in two:

    1- Stepper motor control
    2- LCD control

    So I need some help on point 2. What I want to do is to control several motors at the same time and with diferent speeds. So each motor will have separate control.

    The problem is I want to display on LCD the following information:
    - Running time of a specific motor
    - Speed of that motor
    - Actual time

    So with only one LCD, I would like to see the running time of each motor. I expect doing this with a press button that allows me to choose the motor which running time I want to display.

    I need some help to find the better solution from the hardware point of view. Should I make the clock with the PIC ?? If I need to count the running time of 4 motors, do I need 4 clocks and then 4 PIC's ? Is there a simpler way to do this ?

    Best regards,

    Pedro Cardoso
     
  19. falleafd

    falleafd New Member

    Joined:
    May 13, 2003
    Messages:
    412
    Likes:
    0
    Location:
    Vietnam
    Take a look to Nigel LCD tutorial, you will find many helpful codes.

    To control multi stepper, you need a decoder like 74LS85? Each time you output, you use 4 MSb to send the address of your motor, and use 4 LSb to send the control signal. You can see it so clearly in Douglas W. Jones tutorial, the final chapter (as I remember, it's chapter 6 in old version).

    You can search his name on google and get the tutorial for free.

    He used 74LS194 as a shift register, and LS85 to decode.

    For time control, you don't have to display to see how it work. You can use IDE to debug and see it in registers view.

    For LCD, Nigel's tutorial will help.
     
  20. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    But this way all motors will run identically, don't they ?

    I want to control several motors completely idependent one from another. The first motor can start running now, at 100 rpm CW. The second motor starts running 20 minutes later at 200 rpm CCW. You see, I have two motors running in oposite directions and diferent speeds, so we should use one pic for each motor, don't you think so?

    From the software point of view, how can I do this control with the same PIC ?

    Regards,

    Pedro Cardoso
     
  21. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,214
    Likes:
    640
    Location:
    Derbyshire, UK
    Using a seperate PIC for each motor would probably be easier to do, and more versatile. It would be quite easy to program a PIC to accept simple serial commands and control a stepper motor accordingly - the low price of PIC's makes it a fairly cost effective option.

    To do it with one PIC would also be easily possible, simply generate interrupts with a timer at a rate as fast (or faster) than your highest stepping rate, in your case every 0.3 seconds (for 200RPM). Step the fastest motor every interrupt and the slower motor every second interrupt.

    You could obviously do this at a higher rate, say every 0.03 seconds, and step one motor every 10 interrupts, and the other every 20.

    Which method to use really depends on the application, if the motors are far apart it's probably easier to use seperate PIC's (so you don't need to run as many wires). If they are close together it doesn't really matter which way you go.

    My preference though would be seperate PIC's, design yourself a generic stepper driver design that accepts a serial input - you can then use it over and over again, fed from almost anything!.
     

Share This Page