18f4431; driving a stepper IN HARDWARE mode


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Location
    Kalamazoo
    Posts
    42

    Default 18f4431; driving a stepper IN HARDWARE mode

    hi everyone,
    i have the pic18fxx31 datasheet all printed out and bound into a book, and i read it every spare time i can get.
    so the other day, i was looking at the power control module (page 181) and started thinking about how to drive a stepper with it. i looked closely at the pwm output override section, (page 202), and an idea hit me. use the OVDCOND register to determine which pwm output pins turn on, irrespective of the duty cycle. in short, on/off control of any pwm pin, AND 14-bit duty cycle control of any pwm output pin AT THE SAME TIME.
    i tried it with this simple code and it works on a stepper and leds, in my QL200 dev board
    Code:
    main
         ADCIN 0, DUTY_ADJ            ;read value on pot
         DUTY = DUTY_ADJ*64           ;bring up 8 bit adc value to 14 bit duty value
         PDC0L = Duty.LowByte         ;load duty value into duty registers
         PDC0H = Duty.HighByte        ;
         PDC1L = DUTY.LOWBYTE         ;
         PDC1H = DUTY.HIGHBYTE        ;
         PDC2L = Duty.LowByte         ;
         PDC2H = Duty.HighByte        ;
         PDC3L = DUTY.LOWBYTE         ;
         PDC3H = DUTY.HIGHBYTE        ;
         OVDCOND = %10000000          ;use override control to switch pwm pins
         PAUSE 10                     ;pause
         OVDCOND = %00100000          ;
         PAUSE 10                     ;
         OVDCOND = %00001000          ;
         PAUSE 10                     ;
         OVDCOND = %00000010          ;
         pause 10                     ;
         goto main                    ;loop
    stepper power is controlled by the value of the duty cycle. in this example, all the duty registers have the same value (from the adcin 0),but its possible to have different duty cycles.
    for the 18f4431, you get four individual duty cycle control, thus perfect compatibility with a stepper motor. im highly interested in motor control for cnc applications. i would really appreciate all the help i can get, to implement into this idea:
    • ramping up/down ideas
    • current limiting by varying the duty cycle
    • current feedback
    • any other stepper optimization technology/ideas
    thanks for reading.
    Last edited by DDDvvv; - 19th January 2010 at 18:49.
    NAG CON WIFE!
    WIFE VAR MOOD

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Wow - nice catch on that one. It does appear to work. Cool idea, and thanks for the tip.

    I wish I had a stepper motor board to test this on, but it does look good compared to the
    UCN5804B datasheet for 2-phase & half-step drive sequences in MPSIM logic analyzer
    .. when simulated for an 18F2431.

    I already had a project in MPLAB using C18 for an 18F2431, so I tossed these values in
    using your OVDCOND approach, and it looks good. Do these waveforms look good for both
    2-phase & half-step?

    If they do, and it's worthwhile .. I'll work up an example in PBP. I'm a total newbie when it
    comes to steppers - so let me know if the control signals look OK.

    Here's the C18 example I used to test & simulate, which should be super easy to translate
    to PBP.
    Code:
    #include  p18f2431.h
    #include  delays.h 
    #pragma   config OSC=HS,LVP=OFF,WDTEN=OFF,MCLRE=OFF
    
    union DutyCycle
    {
      unsigned int Cycle;
      unsigned char wByte[2];
    } Duty;
    
    
    void main(void) 
    { 
       // Port init
       PORTB = 0;			      // clear port
       TRISB = 0;  		      // all outputs 
    
       // PCPWM init
       PTCON0 = 0;               // Free Running mode.
       PTPERL = 0;               //
       PTPERH = 1;               // PTPER = $0100 or 256d for ~19.45kHz
       PWMCON0 = 0b01011111;     // PWM[5:0] outputs enabled	
       PWMCON1 = 1;              // OVDCON synched to PWM time base
       DTCON = 0;                // zero dead-time
       PTCON1 = 0b10000000;      // PWM time base is ON
       
       Duty.Cycle = 1023;        // 19.5kHz has a 10-bit resolution.
    
       PDC0L=Duty.wByte[0];
       PDC0H=Duty.wByte[1];
       PDC1L=Duty.wByte[0];
       PDC1H=Duty.wByte[1];
       PDC2L=Duty.wByte[0];
       PDC2H=Duty.wByte[1];
    
       while(1)
        {
        /* // half step mode
        OVDCOND = 0b00000001;    // use override control to switch pwm pins
        Delay10TCYx(250);        // 500uS @ 20MHz
        OVDCOND = 0b00000011;
        Delay10TCYx(250);
        OVDCOND = 0b00000010;
        Delay10TCYx(250);
        OVDCOND = 0b00000110;
        Delay10TCYx(250);
        OVDCOND = 0b00000100;
        Delay10TCYx(250);
        OVDCOND = 0b00001100;
        Delay10TCYx(250);
        OVDCOND = 0b00001000;
        Delay10TCYx(250);
        OVDCOND = 0b00001001;
        Delay10TCYx(250); */
     
        // 2 phase mode
        OVDCOND = 0b00001001;
        Delay10TCYx(250);
        OVDCOND = 0b00000011;
        Delay10TCYx(250);
        OVDCOND = 0b00000110;
        Delay10TCYx(250);
        OVDCOND = 0b00001100;
        Delay10TCYx(250);
    
      } // End while(1) 
    
     } // End main
    Half-step is 1st graphic, 2-phase is 2nd.
    Attached Images Attached Images   
    Last edited by Bruce; - 20th January 2010 at 01:43.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Dec 2009
    Location
    Kalamazoo
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    thanks for the reply Bruce.
    im happy that someone is on the same wavelength as me. yes, the waveforms look great. i wish i had checked them with my scope, before dismantling the project from my computer desk. ill set it up again tomorrow.

    i was surprised that it worked first time, when i tried it with leds, turning on the individual leds, and varying the brightness using the pot

    my stepper could not accept anything under 3ms pauses between steps, but then, i was using 5v and the stepper module on the ql200 board leaves a lot to be desired. im trying to design a stepper output board, just for this particular idea.

    newbie in steppers? how about me, a newbie in everything? i stumbled on the picbasic language back in august 2009, after trying (unsuccessfully) to learn assembly. since then, pic programming has been easier for my brain cells.

    hoping to hear from you soon and other forum members.

    thanks
    NAG CON WIFE!
    WIFE VAR MOOD

  4. #4
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default Stepper motors

    Thanks for the thread - please keep posting your progress.

    There is an excellent application note on the problem of ramping up/down on:

    The ATMEL site: AVR446 'Linear Control of Stepper Motor'

    It comes up with a good approximation for linear acceleration without over-taxing the MCU. The simple business of making linear changes to the PWM 'sort of works' but is nothing like the smooth motion one get with MACH3.

    Regards Bill Legge

  5. #5
    Join Date
    Dec 2009
    Location
    Kalamazoo
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    thanks for the atmel app note. i've also been thinking about the trapezoid curves, and since all cnc software incorporate them, there's no need to take the trouble for them, unless one wants to control the stepper from serial/parallel, or standalone.
    i have a crude power driver on a bread board, and still experimenting on pwm chopping to limit the current. anyone know what's a good pulse frequency for chopping? i've been adjusting it around, ive tried frequencies between 5khz to 56 khz. conclusion; higher frequencies cause more heat dissipation on the power transistors, (which are not heatsinked).

    if i can get this pwm to limit/control current, then ill be happy.

    thanks for the idea, and back to experimenting.

    regards
    NAG CON WIFE!
    WIFE VAR MOOD

Similar Threads

  1. 18F4431 - Complementary mode PWM
    By AndrewC in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 15th December 2011, 12:23
  2. Driving two stepper motors at the same time
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 20th February 2010, 18:33
  3. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 22:07
  4. Driving Stepper Motor with PBL3717
    By crhomberg in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th November 2007, 21:59
  5. Driving a stepper motor bipolar
    By debutpic in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 18th June 2007, 18:34

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts