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:
- 7:199f109eb0c6
- Parent:
- 6:138665018069
diff -r 138665018069 -r 199f109eb0c6 PCA9629.cpp --- a/PCA9629.cpp Wed Jul 18 07:42:48 2012 +0000 +++ b/PCA9629.cpp Mon Jul 23 04:57:38 2012 +0000 @@ -1,13 +1,21 @@ -/** A sample code for PCA9629 +/** PCA9629 library * * @author Akifumi (Tedd) OKANO, NXP Semiconductors - * @version 1.0 - * @date 24-Apr-2011 + * @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: http://mbed.org/license/mit * * An operation sample of PCU9629 stepper motor controller. * The mbed accesses the PCU9629 registers through I2C. + * + * About PCA9629: + * http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_bus_controller_and_bridge_ics/PCA9629PW.html */ #include "mbed.h" @@ -28,13 +36,22 @@ PCA9629::PCA9629( PinName I2C_sda, PinName I2C_scl, + short steps_per_rotation, char I2C_address ) : i2c( I2C_sda, I2C_scl ), i2c_addr( I2C_address ) { i2c.frequency( 400 * 1000 ); + software_reset(); init_registers(); } +void PCA9629::software_reset( void ) { + char v = 0x06; + + i2c.write( 0x00, &v, 1 ); + wait_ms( 1 ); +} + 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 @@ -71,7 +88,7 @@ error( "PCA9629 writing failed\r\n" ); } -void PCA9629::write( Register16bits register_name, short value ) { +void PCA9629::write( RegisterNameFor16bitAccess register_name, short value ) { int error_code; char cmd[ 3 ]; @@ -105,7 +122,7 @@ return ( data ); } -short PCA9629::read( Register16bits register_name ) { +short PCA9629::read( RegisterNameFor16bitAccess register_name ) { int error_code; char cmd; char data[ 2 ]; @@ -150,7 +167,7 @@ step_pulse_width |= (prescaler << 13); - write( (dir == CW) ? CWPW_ : CCWPW_, step_pulse_width ); + write( (dir == CW) ? CW__STEP_WIDTH : CCW_STEP_WIDTH, step_pulse_width ); return ( step_pulse_width ); } @@ -172,14 +189,18 @@ return ( pps( dir, (PrescalerRange)p, (int)pulse_per_second ) ); } -void PCA9629::rotations( Direction dir, int rotations ) { - write( (dir == CW) ? CWRCOUNT_ : CCWRCOUNT_, rotations ); +void PCA9629::rotations( Direction dir, int rotation_count ) { + write( (dir == CW) ? CW__ROTATION_COUNT : CCW_ROTATION_COUNT, rotation_count ); } -void PCA9629::steps( Direction dir, int steps ) { - write( (dir == CW) ? CWSCOUNT_ : CCWSCOUNT_, steps ); +void PCA9629::steps( Direction dir, int step_count ) { + write( (dir == CW) ? CW__STEP_COUNT : CCW_STEP_COUNT, step_count ); } +void PCA9629::rotations_and_steps( Direction dir, int rotation_count, int step_count ) { + rotations( dir, rotation_count ); + steps( dir, step_count ); +} void PCA9629::register_dump( void ) { char data[ 0x27 ];