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/

Files at this revision

API Documentation at this revision

Comitter:
nxp_ip
Date:
Wed Jul 18 07:42:48 2012 +0000
Parent:
5:aff87a1c8bd6
Child:
7:199f109eb0c6
Commit message:
auto prescaler setting

Changed in this revision

PCA9629.cpp Show annotated file Show diff for this revision Revisions of this file
PCA9629.h Show annotated file Show diff for this revision Revisions of this file
--- a/PCA9629.cpp	Tue Apr 24 08:05:03 2012 +0000
+++ b/PCA9629.cpp	Wed Jul 18 07:42:48 2012 +0000
@@ -6,8 +6,8 @@
  *
  *  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. 
+ *  An operation sample of PCU9629 stepper motor controller.
+ *  The mbed accesses the PCU9629 registers through I2C.
  */
 
 #include    "mbed.h"
@@ -31,26 +31,25 @@
     char    I2C_address
 ) : i2c( I2C_sda, I2C_scl ), i2c_addr( I2C_address ) {
 
-    i2c.frequency( 400 * 1000 );    
+    i2c.frequency( 400 * 1000 );
     init_registers();
 }
 
 void PCA9629::init_registers( void ) {
     char    init_array[] = { 0x80,                                                //  register access start address (0x00) with incremental access flag (MSB)
-                            0x30, 0xE2, 0xE4, 0xE6, 0xE0, 0x02, 0x10,             //  for registers MODE - WDCNTL (0x00 - 0x06
-                            0x00, 0x00,                                           //  for registers IP and INTSTAT (0x07, 0x08)
-                            0x07, 0x0F, 0x00, 0x0F, 0x0F, 0x00, 0x03, 0x02, 0x01, //  for registers OP - INT_AUTO_CLR (0x09 - 0x11)
-                            0x00, 0x00, 0x30, 0x00, 0x82, 0x66, 0x82, 0x06,       //  for registers SETMODE - CCWPWH (0x12 - 0x19)
-                            0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,       //  for registers CWSCOUNTL - CCWRCOUNTH (0x1A - 0x21)
-                            0x00, 0x00,                                           //  for registers EXTRASTEPS0 and EXTRASTEPS1 (0x22, 0x23)
-                            0x00, 0x00, 0x00                                      //  for registers RMPCNTL - MCNTL (0x24 - 0x26)
+                             0x30, 0xE2, 0xE4, 0xE6, 0xE0, 0x02, 0x10,             //  for registers MODE - WDCNTL (0x00 - 0x06
+                             0x00, 0x00,                                           //  for registers IP and INTSTAT (0x07, 0x08)
+                             0x07, 0x0F, 0x00, 0x0F, 0x0F, 0x00, 0x03, 0x02, 0x01, //  for registers OP - INT_AUTO_CLR (0x09 - 0x11)
+                             0x00, 0x00, 0x30, 0x00, 0x82, 0x66, 0x82, 0x06,       //  for registers SETMODE - CCWPWH (0x12 - 0x19)
+                             0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,       //  for registers CWSCOUNTL - CCWRCOUNTH (0x1A - 0x21)
+                             0x00, 0x00,                                           //  for registers EXTRASTEPS0 and EXTRASTEPS1 (0x22, 0x23)
+                             0x00, 0x00, 0x00                                      //  for registers RMPCNTL - MCNTL (0x24 - 0x26)
                            };
 
     set_all_registers( init_array, sizeof( init_array ) );
 }
 
-void PCA9629::set_all_registers( char *a, char size )
-{
+void PCA9629::set_all_registers( char *a, char size ) {
     int     error_code;
 
     error_code  = i2c.write( i2c_addr, a, size );
@@ -134,6 +133,7 @@
     write( MCNTL, 0x00 );
 }
 
+
 short PCA9629::pps( Direction dir, PrescalerRange prescaler, int pps ) {
     int     step_pulse_width;
 
@@ -155,6 +155,23 @@
     return ( step_pulse_width );
 }
 
+short PCA9629::pps( Direction dir, float pulse_per_second ) {
+    char    p       = 0;
+    char    ratio;
+
+    ratio   = (char)(40.6901 / pulse_per_second);
+
+    p   = (ratio & 0x01) ? 1 : p;
+    p   = (ratio & 0x02) ? 2 : p;
+    p   = (ratio & 0x04) ? 3 : p;
+    p   = (ratio & 0x08) ? 4 : p;
+    p   = (ratio & 0x10) ? 5 : p;
+    p   = (ratio & 0x20) ? 6 : p;
+    p   = (ratio & 0x40) ? 7 : p;
+
+    return ( pps( dir, (PrescalerRange)p, (int)pulse_per_second ) );
+}
+
 void PCA9629::rotations( Direction dir, int rotations ) {
     write( (dir == CW) ? CWRCOUNT_ : CCWRCOUNT_, rotations );
 }
--- a/PCA9629.h	Tue Apr 24 08:05:03 2012 +0000
+++ b/PCA9629.h	Wed Jul 18 07:42:48 2012 +0000
@@ -17,51 +17,10 @@
 
 #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;
-
-
 /** PCA9629 class
  *
  */
- 
+
 class PCA9629 {
 public:
 
@@ -167,7 +126,7 @@
      *  The initializing values are defined in the function
      */
     void set_all_registers( char *a, char size );
-    
+
     /** Write 1 byte data into a register
      *
      *  Setting 8 bits data into a register
@@ -208,12 +167,12 @@
      *
      *  Start command
      *  This function starts motor operation with hard-stop flag and rotation+step enabled, no repeat will be performed
-     *  If custom start is required, use "write( PCA9629::MCNTL, 0xXX  )" to control each bits. 
+     *  If custom start is required, use "write( PCA9629::MCNTL, 0xXX  )" to control each bits.
      *
      *  @param dir rotate direction ("CW" or "CCW")
      */
     void start( Direction dir );
-    
+
     /** Motor stop
      *
      *  Stop command
@@ -228,10 +187,10 @@
      *
      *  @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
+     *  @param pulse_per_second 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 );
+    short pps( Direction dir, PrescalerRange prescaler, int pulse_per_second );
 
     /** Set PPS
      *
@@ -239,10 +198,10 @@
      *  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
+     *  @param pulse_per_second 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, float pulse_per_second );
 
     /** Set rotation count
      *
@@ -280,6 +239,7 @@
     void speed_change( unsigned short pw );
 
 private:
+
     I2C     i2c;
     char    i2c_addr;
 };