Sample code for PCA9629 operation

Dependencies:   mbed PCA9629

What is "PCA9629" ?

Information

The stepper motor controller PCA9629A is available now!
This "A-version (PCA9629A)" is a feature extended version on "non-A (PCA9629)".
If you are thinking new design, the PCA9629A can be a good option.

The PCA9629A has several new features like on-the-fly speed change, extended ramp controls, filter settings for GPIO port inputs, braking and releasing by timeout.. and more.

Please find detail information here. The "A-version" chip is pin-compatible to "non-A". So it can be replaced easily.
The register mapping and bit fields are re-defined. Those should be cared by software.
A sample code and a class library are available for mbed.

Import programPCA9629A_Hello

Hello program for PCA9629A. Demonstrates basic operations of PCA9629A component library.

Import libraryPCA9629A

PCA9629A component library

A Stepper motor controller

/media/uploads/nxp_ip/9629x5-bd-s.jpg
PCA9629 (x5) demo board: PCA9629, 4x FETs and stepper motor

The PCA9629 is an I2C-bus controlled low-power CMOS device that provides all the logic and control required to drive a four phase stepper motor. PCA9629 is intended to be used with external high current drivers to drive the motor coils. The PCA9629 supports three stepper motor drive formats: one-phase (wave drive), two-phase, and half-step. In addition, when used as inputs, four General Purpose Input/Outputs (GPIOs) allow sensing of logic level output from optical interrupter modules and generate active LOW interrupt signal on the INT pin of PCA9629. This is a useful feature in sensing home position of motor shaft or reference for step pulses. Upon interrupt, the PCA9629 can be programmed to automatically stop the motor or reverse the direction of rotation of motor.

/media/uploads/nxp_ip/block-diagram0.png
Communicate with mbed through I2C. The motor can be operated by register settings

Output wave train is programmable using control registers. The control registers are programmed via the I2C-bus. Features built into the PCA9629 provide highly flexible control of stepper motor, off-load bus master/micro and significantly reduce I2C-bus traffic. These include control of step size, number of steps per single command, number of full rotations and direction of rotation. A ramp-up on start and/or ramp-down on stop is also provided.

/media/uploads/nxp_ip/pca9629_16steps_w_ramp_control.png
Waveforms: Lower 2 lines are I2C bus. Upper 4 lines are output for motor driver stage
Motor moving 16 steps with acceleration and deceleration after register setting via I2C

The PCA9629 is available in a 16-pin TSSOP package and is specified over the -40 deg-C to +85 deg-C industrial temperature range.

/media/uploads/nxp_ip/dsc_0103_-1-.jpg
TSSOP16 package

Datasheet is available at next URL.
http://www.nxp.com/documents/data_sheet/PCA9629.pdf

Hardware

A sample schematic diagram is shown in next picture.

/media/uploads/nxp_ip/sample-sch.png

This schematic diagram shows a sample of the PF35T-48C stepper motor. It is using 12V supply voltage for motor and driving by SI3812DV FETs.
Pull-up resisters: For /RESET and /INT pins : 4.7K-ohms, for I2C signals (SDA and SCL) : >1.3k-ohms.

NOTE

For the correct operation of the "auto interrupt" demo, the GPIO port P0(pin1) and P1(pin2) must be connected to sensor input. signal edge of those pins are treated as the interrupt.

Sample code

This sample code operates the PCA9629 using its class library.
For the details of the library, please find information in library page.

Import libraryPCA9629

PCA9629 a stepper motor controller class library

API details are available in here

This program targets PCA9629 which has I2C address of 0x42 on I2C bus on pin28 and pin27 of mbed. 4 types motor actions are demonstrated in sequence (following 1..4 steps in loop).

  1. Simple motor turn demo
    • Motor moved 96 steps with 100pps (pulse persecond) speed
    • Then the motor turn reversed, move 48 steps by 50pps.
  2. Ramp control demo
    • total operation time = 2 seconds, initial speed = 40.69pps, rotations = 5turns+0steps, ramp-up enable = enable, ramp-down enable = enable, rotate direction = CW
    • The ramp control can be done with several register settings. The setting value calculation can be done by register setting tool (an Excel worksheet) from NXP.
  3. Auto interrupt demo
    • The GPIO inputs can be used as interrupt for PCA9629. The operation can be stopped or change the direction of turn.
    • This is turn direction auto reversing demo. Motor will switch the direction 12 steps after interrupt signal assertion.
    • The register setting of interrupt operation can be done by register setting tool also.
    • This demo works correctly only when sensor signals available on P0(pin1) and P1(pin2) input pins.
  4. Speed change demo
    • Changing motor speed in 5 steps from 55pps to 275pps.

main.cpp

Committer:
nxp_ip
Date:
2012-02-03
Revision:
0:4f93f6ecf23c
Child:
1:cab53217fcc1

File content as of revision 0:4f93f6ecf23c:

/** A sample/demo code for PCA9629
 *
 *  @author  Tedd OKANO, NXP Semiconductors
 *  @version 1.0
 *  @date    03-Feb-2011
 *
 *  Released under the MIT License: http://mbed.org/license/mit
 *
 *  An operation sample of PCU9629 stepper motor controller. 
 *  The mbed accesses the PCU9629 registers through I2C. 
 *
 *  This sample code demonstrates 4 type of PCA9629 operation
 */

#include "mbed.h"
#include "PCA9629.h"

BusOut      leds( LED4, LED3, LED2, LED1 );
PCA9629     motor_controller( p28, p27, 0x42 );

void motor_action_0( void );
void motor_action_1( void );
void motor_action_2( void );
void motor_action_3( void );
void motor_action_CDE( void );

int main() {
    printf( "PCA9629 simple sample program\r\n" );

    while ( 1 ) {
        leds    = 0x1;
        motor_action_0();

        leds    = 0x3;
        motor_action_1();

        leds    = 0x7;
        motor_action_2();

        leds    = 0xF;
        motor_action_3();
    }
}

void motor_action_0( void ) {

    //  simple motor operation
    //  the motor spins 2 times (100pps) for clockwise (CW), 1 time (50pps) for counter-clockwise. 

    motor_controller.stop();
    motor_controller.init_registers();

    motor_controller.pps( PCA9629::CW, PCA9629::PRESCALER_FROM_40_TO_333333, 100 );
    motor_controller.steps( PCA9629::CW, 96 );

    motor_controller.pps( PCA9629::CCW, PCA9629::PRESCALER_FROM_40_TO_333333, 50 );
    motor_controller.steps( PCA9629::CCW, 48 );

    for ( int i = 0; i < 2; i++ ) {
        motor_controller.start( PCA9629::CW );
        wait( 2.0 );

        motor_controller.start( PCA9629::CCW );
        wait( 2.0 );
    }
}

void motor_action_1( void ) {

    //  ramp control demo

    motor_controller.stop();
    motor_controller.init_registers();

    //  auto generated example code for PCA9629 on mbed
    //  setting: total operation time = 2, initial speed = 40.69, rotations = 5, steps = 0, ramp-up enable = enable, ramp-down enable = enable, rotate direction = CW

    //  motor_controller.write( PCA9629::STEPS_PER_ROATION, 48  );
    motor_controller.write( PCA9629::CW__STEP_WIDTH, 1175  );
    motor_controller.write( PCA9629::CW__STEP_COUNT, 35  );
    motor_controller.write( PCA9629::CW__ROTATION_COUNT, 2  );
    motor_controller.write( PCA9629::RAMPCNTL, 0x37  );

    for ( int i = 0; i < 2; i++ ) {
        motor_controller.write( PCA9629::MCNTL, 0xA8  );  //  start
        wait( 2.5 );
    }
}

void motor_action_2( void ) {

    //  interrupt operation demo

    motor_controller.stop();

    //  this array is a sample for PCA9629 local interrupt operation
    //  the P0 and P1 inputs enabled for interrupt in.
    //  motor rotation will be reversed by interrupt signals.
    //  the motor direction will be changed 12 steps after when the interrupt happened
    //
    //  CW = 40.69pps, CCW = 81.38pps

    char init_array2[] = {    0x80,
                              0x20, 0xE2, 0xE4, 0xE6, 0xE0, 0xFF, 0x10,             //  for registers MODE - WDCNTL (0x00 - 0x06
                              0x00, 0x00,                                           //  for registers IP and INTSTAT (0x07, 0x08)
                              0x0F, 0x03, 0x0C, 0x0F, 0x03, 0x01, 0x03, 0x03, 0x01, //  for registers OP - INT_AUTO_CLR (0x09 - 0x11)
                              0x00, 0x00, 0x30, 0x00, 0xFF, 0x1F, 0xFF, 0x0F,       //  for registers SETMODE - CCWPWH (0x12 - 0x19)
                              0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x00, 0x0A, 0x00,       //  for registers CWSCOUNTL - CCWRCOUNTH (0x1A - 0x21)
                              0x0C, 0x0C,                                           //  for registers EXTRASTEPS0 and EXTRASTEPS1 (0x22, 0x23)
                              0x00, 0x00, 0xA8                                      //  for registers RMPCNTL - MCNTL (0x24 - 0x26)
                         };

    motor_controller.set_all_registers( init_array2, sizeof( init_array2 ));
    wait( 5.0 );

    motor_controller.stop();
    wait( 0.5 );

}

void motor_action_3( void ) {

    //  speed change sample while the motor is running

    motor_controller.stop();
    motor_controller.init_registers();

    motor_controller.rotations( PCA9629::CCW, 0xFFFF );

    for ( int i = 0; i < 2; i++ ) {
        for ( int s = 1; s <= 5; s++ ) {
            motor_controller.pps( PCA9629::CCW, PCA9629::PRESCALER_FROM_40_TO_333333, 55 * s );
            motor_controller.start( PCA9629::CCW );
            wait( 1.0 );
            motor_controller.stop();
        }
    }
}

void motor_action_CDE( void ) {
    char    scale[ 8 ]  = { 4, 6, 8, 9, 11, 13, 15, 16 };  //   CDEFGABC from A
    float   freq[ 8 ];

    for ( int i = 0; i < 8; i++ )
        freq[ i ]   = 55.0 * pow( 2.0, (float)(scale[ i ]) / 12.0 );

    motor_controller.stop();
    motor_controller.init_registers();
    motor_controller.rotations( PCA9629::CCW, 0xFFFF );
    motor_controller.write( PCA9629::PHCNTL,0x1 );

    for ( int i = 0; i < 2; i++ ) {
        motor_controller.stop();
        for ( int s = 0; s < 8; s++ ) {
            motor_controller.pps( PCA9629::CCW, PCA9629::PRESCALER_FROM_40_TO_333333, (int)(freq[ s ]) );
            motor_controller.start( PCA9629::CCW );
            wait( 1 );

            motor_controller.stop();
        }
    }
}