PCA9629 a stepper motor controller class library

Dependents:   PCA9629_Hello

Class library for PCA9629.

A sample program available on http://mbed.org/users/nxp_ip/code/PCA9629_Hello/

Revision:
0:77dbce152406
Child:
1:b3d94e04adfa
diff -r 000000000000 -r 77dbce152406 PCA9629.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9629.h	Thu Feb 02 05:10:19 2012 +0000
@@ -0,0 +1,255 @@
+#ifndef        MBED_PCA9629
+#define        MBED_PCA9629
+
+#define     INTELIGENT_WRITE
+
+#define     PCA9629_DEFAULT_ADDR      0x42
+
+#define        INIT_VALUE__MODE          0x10;
+#define        INIT_VALUE__SUBADR1       0xE2;
+#define        INIT_VALUE__SUBADR2       0xE4;
+#define        INIT_VALUE__SUBADR3       0xE8;
+#define        INIT_VALUE__ALLCALLADR    0xE0;
+#define        INIT_VALUE__WDTOI         0xFF;
+#define        INIT_VALUE__WDTCNTL       0x00;
+#define        INIT_VALUE__IP            0x00;
+#define        INIT_VALUE__INTSTAT       0x00;
+#define        INIT_VALUE__OP            0x0F;
+#define        INIT_VALUE__IOC           0x03;
+#define        INIT_VALUE__MSK           0x0F;
+#define        INIT_VALUE__CLRINT        0x0C;
+#define        INIT_VALUE__INTMODE       0x00;
+#define        INIT_VALUE__INT_ACT_SETUP 0x00;
+#define        INIT_VALUE__INT_MTR_SETUP 0x00;
+#define        INIT_VALUE__INT_ES_SETUP  0x00;
+#define        INIT_VALUE__INT_AUTO_CLR  0x00;
+#define        INIT_VALUE__SETMODE       0x00;
+#define        INIT_VALUE__PHCNTL        0x00;
+#define        INIT_VALUE__SROTNL        0x30;
+#define        INIT_VALUE__SROTNH        0x00;
+#define        INIT_VALUE__CWPWL         0x00;
+#define        INIT_VALUE__CWPWH         0x00;
+#define        INIT_VALUE__CCWPWL        0x00;
+#define        INIT_VALUE__CCWPWH        0x00;
+#define        INIT_VALUE__CWSCOUNTL     0x00;
+#define        INIT_VALUE__CWSCOUNTH     0x00;
+#define        INIT_VALUE__CCWSCOUNTL    0x00;
+#define        INIT_VALUE__CCWSCOUNTH    0x00;
+#define        INIT_VALUE__CWRCOUNTL     0x00;
+#define        INIT_VALUE__CWRCOUNTH     0x00;
+#define        INIT_VALUE__CCWRCOUNTL    0x00;
+#define        INIT_VALUE__CCWRCOUNTH    0x00;
+#define        INIT_VALUE__EXTRASTEPS0   0x00;
+#define        INIT_VALUE__EXTRASTEPS1   0x00;
+#define        INIT_VALUE__RAMPCNTL      0x00;
+#define        INIT_VALUE__LOOPDLY       0x00;
+#define        INIT_VALUE__MCNTL         0x00;
+
+class PCA9629 {
+public:
+
+    /** name of registers */
+    typedef enum {
+        MODE,            /**< Mode rgister */
+        SUBADR1,         /**< I2C-bus subaddress 1 */
+        SUBADR2,         /**< I2C-bus subaddress 2 */
+        SUBADR3,         /**< I2C-bus subaddress 3 */
+        ALLCALLADR,      /**< All call I2C-bus address */
+        WDTOI,           /**< Watchdog time-out interval register */
+        WDTCNTL,         /**< Watchdog control register */
+        IP,              /**< Input port register */
+        INTSTAT,         /**< Interrupt status register */
+        OP,              /**< Output port register */
+        IOC,             /**< I/O configuration register */
+        MSK,             /**< Mask interrupt register */
+        CLRINT,          /**< Clear Interrupts */
+        INTMODE,         /**< Interrupt mode register */
+        INT_ACT_SETUP,   /**< Interrupt action setup control register */
+        INT_MTR_SETUP,   /**< Interrupt motor setup control register */
+        INT_ES_SETUP,    /**< Interrupt extra steps setup control register */
+        INT_AUTO_CLR,    /**< Interrupt auto clear control register */
+        SETMODE,         /**< Output state on STOP */
+        PHCNTL,          /**< Phase control register */
+        SROTNL,          /**< Steps per rotation low byte */
+        SROTNH,          /**< Steps per rotation high byte  */
+        CWPWL,           /**< Step pulse width for CW rotation low byte */
+        CWPWH,           /**< Step pulse width for CW rotation high byte */
+        CCWPWL,          /**< Step pulse width for CCW rotation low byte */
+        CCWPWH,          /**< Step pulse width for CCW rotation high byte */
+        CWSCOUNTL,       /**< Number of steps CW low byte */
+        CWSCOUNTH,       /**< Number of steps CW high byte */
+        CCWSCOUNTL,      /**< Number of steps CCW low byte */
+        CCWSCOUNTH,      /**< Number of steps CCW high byte */
+        CWRCOUNTL,       /**< Number of rotatations CW low byte */
+        CWRCOUNTH,       /**< Number of rotatations CW high byte */
+        CCWRCOUNTL,      /**< Number of rotatations CCW low byte */
+        CCWRCOUNTH,      /**< Number of rotatations CCW high byte */
+        EXTRASTEPS0,     /**< Count value for extra steps or rotations for INTP0 */
+        EXTRASTEPS1,     /**< Count value for extra steps or rotations for INTP1 */
+        RAMPCNTL,        /**< Ramp control register */
+        LOOPDLY,         /**< Loopdelay time register */
+        MCNTL,           /**< Control start/stop motor */
+    } RegisterName;
+
+    /** register names to make 2 bytes access */
+    typedef enum {
+        SROTN_      = SROTNL     | 0x80,    /**< Steps per rotation */
+        CWPW_       = CWPWL      | 0x80,    /**< Step pulse width for CW rotation */
+        CCWPW_      = CCWPWL     | 0x80,    /**< Step pulse width for CCW rotation */
+        CWSCOUNT_   = CWSCOUNTL  | 0x80,    /**< Number of steps CW */
+        CCWSCOUNT_  = CCWSCOUNTL | 0x80,    /**< Number of steps CCW */
+        CWRCOUNT_   = CWRCOUNTL  | 0x80,    /**< Number of rotatations CW */
+        CCWRCOUNT_  = CCWRCOUNTL | 0x80,    /**< Number of rotatations CCW */
+        STEPS_PER_ROATION   = SROTN_,
+        CW__STEP_WIDTH      = CWPW_,
+        CCW_STEP_WIDTH      = CCWPW_,
+        CW__STEP_COUNT      = CWSCOUNT_,
+        CCW_STEP_COUNT      = CCWSCOUNT_,
+        CW__ROTATION_COUNT  = CWRCOUNT_,
+        CCW_ROTATION_COUNT  = CCWRCOUNT_
+    } Register16bits;
+
+    /** keyword to select direction of rotation */
+    typedef enum {
+        CW      = 0,    /**< Clockwise direction */
+        CCW             /**< ConterClockwise direction */
+    } Direction;
+
+    /** plescaler range setting */
+    typedef enum {
+        PRESCALER_FROM_40_TO_333333,    /**< Prescaler range from   3us(333333pps) to   24.576ms(40   pps) */
+        PRESCALER_FROM_20_TO_166667,    /**< Prescaler range from   6us(166667pps) to   49.152ms(20   pps) */
+        PRESCALER_FROM_10_TO_83333,     /**< Prescaler range from  12us( 83333pps) to   98.304ms(10   pps) */
+        PRESCALER_FROM_5_TO_41667,      /**< Prescaler range from  24us( 41667pps) to  196.608ms( 5   pps) */
+        PRESCALER_FROM_2_5_TO_20833,    /**< Prescaler range from  48us( 20833pps) to  393.216ms( 2.5 pps) */
+        PRESCALER_FROM_1_27_TO_10416,   /**< Prescaler range from  96us( 10416pps) to  786.432ms( 1.27pps) */
+        PRESCALER_FROM_0_64_TO_5208,    /**< Prescaler range from 192us(  5208pps) to 1572.864ms( 0.64pps) */
+        PRESCALER_FROM_0_32_TO_2604,    /**< Prescaler range from 384us(  2604pps) to 3145.728ms( 0.32pps) */
+    } PrescalerRange;
+
+    /** Create a PCA9629 object connected to specified I2C pins with specified address
+     *
+     * @param I2C_sda I2C-bus SDA pin
+     * @param I2C_scl I2C-bus SCL pin
+     * @param I2C_address I2C-bus address (default: 0x42)
+     */
+    PCA9629(
+        PinName I2C_sda,
+        PinName I2C_scl,
+        char    I2C_address = PCA9629_DEFAULT_ADDR
+    );
+
+    /** 
+     *
+     * 
+     */
+    void init_registers( void );
+
+    /** Write 1 byte data into a register
+     *
+     *  Setting 8 bits data into a register
+     *
+     *  @param register_name the register name: data writing into
+     *  @param value 8 bits writing data
+     */
+    void write( RegisterName register_name, char value );
+
+    /** Write 2 bytes data into a register
+     *
+     *  Setting 16 bits data into registers
+     *
+     *  @param register_name the register name: data writing into (it can be "SROTN_", "CWPW_", "CCWPW_", "CWRCOUNT_", "CCWSCOUNT_", "CWRCOUNT_" or "CCWRCOUNT_" )
+     *  @param value 16 bits writing data
+     */
+    void write( Register16bits register_name, short value );
+
+    /** Read 1 byte data from a register
+     *
+     *  Setting data into a register
+     *
+     *  @param register_name the register name: data reading from
+     *  @return read 8 bits data from the register
+     */
+    char read( RegisterName register_name );
+
+    /** Read 2 byte data from registers
+     *
+     *  Setting data into a register
+     *
+     *  @param register_name the register name: data writing into (it can be "SROTN_", "CWPW_", "CCWPW_", "CWRCOUNT_", "CCWSCOUNT_", "CWRCOUNT_" or "CCWRCOUNT_" )
+     *  @return read 16 bits data from the registers
+     */
+    short read( Register16bits register_name );
+
+    /** Motor stop
+     *
+     *  Stop command
+     *
+     */
+    void stop( void );
+
+    /** Set PPS
+     *
+     *  Setting PulsePerSecond
+     *  This interface can be used to set CWPWx or CCWPWx registers
+     *
+     *  @param dir rotate direction ("CW" or "CCW")
+     *  @param prescaler prescaler setting (for 3 bits setting range from 0 to 0x7. See datasheet)
+     *  @param pps pps defineds pulse width for the motor. The pulse width will be 1/pps
+     *  @return 16 bit data that what set to the CWPWx or CCWPWx registers
+     */
+    short pps( Direction dir, PrescalerRange prescaler, int pps );
+
+    /** Set PPS
+     *
+     *  Setting PulsePerSecond
+     *  This interface can be used to set CWPWx or CCWPWx registers
+     *
+     *  @param dir rotate direction ("CW" or "CCW")
+     *  @param prescaler prescaler setting (for 3 bits setting range from 0 to 0x7. See datasheet)
+     *  @param pps pps defineds pulse width for the motor. The pulse width will be 1/pps
+     *  @return 16 bit data that what set to the CWPWx or CCWPWx registers
+     */
+
+    /** Set rotation count
+     *
+     *  Setting rotation count
+     *  This interfaces CWRCOUNTx, CCWRCOUNTx registers
+     *
+     *  @param dir rotate direction ("CW" or "CCW")
+     *  @param rotations sets number of rotations with 16 bit value
+     */
+    void rotations( Direction dir, int rotations );
+
+    /** Set PPS
+     *
+     *  Setting step count
+     *  This interfaces CWSCOUNTx, CCWSCOUNTx registers
+     *
+     *  @param dir rotate direction ("CW" or "CCW")
+     *  @param steps sets number of steps with 16 bit value
+     */
+    void steps( Direction dir, int pps );
+
+
+    /** Register dump
+     *
+     *  Dumping all register data to serial console
+     *
+     */
+    void register_dump( void );
+
+    /** Register dump
+     *
+     *  Dumping all register data to serial console
+     *
+     */
+    void speed_change( unsigned short pw );
+
+private:
+    I2C     i2c;
+    char    i2c_addr;
+};
+
+#endif  //  MBED_PCA9629
+