PCA9629 a stepper motor controller class library
Class library for PCA9629.
A sample program available on http://mbed.org/users/nxp_ip/code/PCA9629_Hello/
Diff: PCA9629.cpp
- Revision:
- 6:138665018069
- Parent:
- 5:aff87a1c8bd6
- Child:
- 7:199f109eb0c6
diff -r aff87a1c8bd6 -r 138665018069 PCA9629.cpp --- 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 ); }