Sample code for PCA9629 operation

Dependencies:   mbed PCA9629

What is "PCA9629" ?


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

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.

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.

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.

TSSOP16 package

Datasheet is available at next URL.


A sample schematic diagram is shown in next picture.


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.


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.



File content as of revision 2:2e75cedf6565:

/** A sample code for PCA9629
 *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
 *  @version 1.1
 *  @date    23-Jul-2012
 *  revision history
 *      version 1.0 (24-Apr-2011) : Initial version
 *      version 1.1 (23-Jul-2012) : API modification
 *                                  Correction for comments
 *  Released under the MIT License:
 *  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
 *  About PCA9629:

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

BusOut      leds( LED4, LED3, LED2, LED1 );
PCA9629     motor_controller( p28, p27, 48 );  //  SDA, SCL, Steps/rotation, I2C_address (option)

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();   //  demo 0

        leds    = 0x3;
        motor_action_1();   //  demo 1

        leds    = 0x7;
        motor_action_2();   //  demo 2

        leds    = 0xF;
        motor_action_3();   //  demo 3

void motor_action_0( void ) {

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


    motor_controller.pps( PCA9629::CW, 200 );
    motor_controller.rotations_and_steps( PCA9629::CW, 0, 96 );

    motor_controller.pps( PCA9629::CCW, 100 );
    motor_controller.rotations_and_steps( PCA9629::CCW, 0, 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


    //  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


    //  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 );

    wait( 0.5 );


void motor_action_3( void ) {

    //  speed change sample while the motor is running


    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, 55 * s );
            motor_controller.start( PCA9629::CCW );
            wait( 1.0 );

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

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

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

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