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/

Committer:
nxp_ip
Date:
Wed Jul 18 07:42:48 2012 +0000
Revision:
6:138665018069
Parent:
5:aff87a1c8bd6
Child:
7:199f109eb0c6
auto prescaler setting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxp_ip 4:9a80b6d63005 1 /** A sample code for PCA9629
nxp_ip 4:9a80b6d63005 2 *
nxp_ip 5:aff87a1c8bd6 3 * @author Akifumi (Tedd) OKANO, NXP Semiconductors
nxp_ip 4:9a80b6d63005 4 * @version 1.0
nxp_ip 5:aff87a1c8bd6 5 * @date 24-Apr-2011
nxp_ip 4:9a80b6d63005 6 *
nxp_ip 4:9a80b6d63005 7 * Released under the MIT License: http://mbed.org/license/mit
nxp_ip 4:9a80b6d63005 8 *
nxp_ip 4:9a80b6d63005 9 * An operation sample of PCU9629 stepper motor controller.
nxp_ip 4:9a80b6d63005 10 * The mbed accesses the PCU9629 registers through I2C.
nxp_ip 4:9a80b6d63005 11 */
nxp_ip 4:9a80b6d63005 12
nxp_ip 4:9a80b6d63005 13 #ifndef MBED_PCA9629
nxp_ip 4:9a80b6d63005 14 #define MBED_PCA9629
nxp_ip 4:9a80b6d63005 15
nxp_ip 4:9a80b6d63005 16 #define INTELIGENT_WRITE
nxp_ip 4:9a80b6d63005 17
nxp_ip 4:9a80b6d63005 18 #define PCA9629_DEFAULT_ADDR 0x42
nxp_ip 4:9a80b6d63005 19
nxp_ip 4:9a80b6d63005 20 /** PCA9629 class
nxp_ip 4:9a80b6d63005 21 *
nxp_ip 4:9a80b6d63005 22 */
nxp_ip 6:138665018069 23
nxp_ip 4:9a80b6d63005 24 class PCA9629 {
nxp_ip 4:9a80b6d63005 25 public:
nxp_ip 4:9a80b6d63005 26
nxp_ip 4:9a80b6d63005 27 /** name of registers */
nxp_ip 4:9a80b6d63005 28 typedef enum {
nxp_ip 4:9a80b6d63005 29 MODE, /**< Mode rgister */
nxp_ip 4:9a80b6d63005 30 SUBADR1, /**< I2C-bus subaddress 1 */
nxp_ip 4:9a80b6d63005 31 SUBADR2, /**< I2C-bus subaddress 2 */
nxp_ip 4:9a80b6d63005 32 SUBADR3, /**< I2C-bus subaddress 3 */
nxp_ip 4:9a80b6d63005 33 ALLCALLADR, /**< All call I2C-bus address */
nxp_ip 4:9a80b6d63005 34 WDTOI, /**< Watchdog time-out interval register */
nxp_ip 4:9a80b6d63005 35 WDTCNTL, /**< Watchdog control register */
nxp_ip 4:9a80b6d63005 36 IP, /**< Input port register */
nxp_ip 4:9a80b6d63005 37 INTSTAT, /**< Interrupt status register */
nxp_ip 4:9a80b6d63005 38 OP, /**< Output port register */
nxp_ip 4:9a80b6d63005 39 IOC, /**< I/O configuration register */
nxp_ip 4:9a80b6d63005 40 MSK, /**< Mask interrupt register */
nxp_ip 4:9a80b6d63005 41 CLRINT, /**< Clear Interrupts */
nxp_ip 4:9a80b6d63005 42 INTMODE, /**< Interrupt mode register */
nxp_ip 4:9a80b6d63005 43 INT_ACT_SETUP, /**< Interrupt action setup control register */
nxp_ip 4:9a80b6d63005 44 INT_MTR_SETUP, /**< Interrupt motor setup control register */
nxp_ip 4:9a80b6d63005 45 INT_ES_SETUP, /**< Interrupt extra steps setup control register */
nxp_ip 4:9a80b6d63005 46 INT_AUTO_CLR, /**< Interrupt auto clear control register */
nxp_ip 4:9a80b6d63005 47 SETMODE, /**< Output state on STOP */
nxp_ip 4:9a80b6d63005 48 PHCNTL, /**< Phase control register */
nxp_ip 4:9a80b6d63005 49 SROTNL, /**< Steps per rotation low byte */
nxp_ip 4:9a80b6d63005 50 SROTNH, /**< Steps per rotation high byte */
nxp_ip 4:9a80b6d63005 51 CWPWL, /**< Step pulse width for CW rotation low byte */
nxp_ip 4:9a80b6d63005 52 CWPWH, /**< Step pulse width for CW rotation high byte */
nxp_ip 4:9a80b6d63005 53 CCWPWL, /**< Step pulse width for CCW rotation low byte */
nxp_ip 4:9a80b6d63005 54 CCWPWH, /**< Step pulse width for CCW rotation high byte */
nxp_ip 4:9a80b6d63005 55 CWSCOUNTL, /**< Number of steps CW low byte */
nxp_ip 4:9a80b6d63005 56 CWSCOUNTH, /**< Number of steps CW high byte */
nxp_ip 4:9a80b6d63005 57 CCWSCOUNTL, /**< Number of steps CCW low byte */
nxp_ip 4:9a80b6d63005 58 CCWSCOUNTH, /**< Number of steps CCW high byte */
nxp_ip 4:9a80b6d63005 59 CWRCOUNTL, /**< Number of rotatations CW low byte */
nxp_ip 4:9a80b6d63005 60 CWRCOUNTH, /**< Number of rotatations CW high byte */
nxp_ip 4:9a80b6d63005 61 CCWRCOUNTL, /**< Number of rotatations CCW low byte */
nxp_ip 4:9a80b6d63005 62 CCWRCOUNTH, /**< Number of rotatations CCW high byte */
nxp_ip 4:9a80b6d63005 63 EXTRASTEPS0, /**< Count value for extra steps or rotations for INTP0 */
nxp_ip 4:9a80b6d63005 64 EXTRASTEPS1, /**< Count value for extra steps or rotations for INTP1 */
nxp_ip 4:9a80b6d63005 65 RAMPCNTL, /**< Ramp control register */
nxp_ip 4:9a80b6d63005 66 LOOPDLY, /**< Loopdelay time register */
nxp_ip 4:9a80b6d63005 67 MCNTL, /**< Control start/stop motor */
nxp_ip 4:9a80b6d63005 68 } RegisterName;
nxp_ip 4:9a80b6d63005 69
nxp_ip 4:9a80b6d63005 70 /** register names to make 2 bytes access */
nxp_ip 4:9a80b6d63005 71 typedef enum {
nxp_ip 4:9a80b6d63005 72 SROTN_ = SROTNL | 0x80, /**< Steps per rotation */
nxp_ip 4:9a80b6d63005 73 CWPW_ = CWPWL | 0x80, /**< Step pulse width for CW rotation */
nxp_ip 4:9a80b6d63005 74 CCWPW_ = CCWPWL | 0x80, /**< Step pulse width for CCW rotation */
nxp_ip 4:9a80b6d63005 75 CWSCOUNT_ = CWSCOUNTL | 0x80, /**< Number of steps CW */
nxp_ip 4:9a80b6d63005 76 CCWSCOUNT_ = CCWSCOUNTL | 0x80, /**< Number of steps CCW */
nxp_ip 4:9a80b6d63005 77 CWRCOUNT_ = CWRCOUNTL | 0x80, /**< Number of rotatations CW */
nxp_ip 4:9a80b6d63005 78 CCWRCOUNT_ = CCWRCOUNTL | 0x80, /**< Number of rotatations CCW */
nxp_ip 4:9a80b6d63005 79 STEPS_PER_ROATION = SROTN_,
nxp_ip 4:9a80b6d63005 80 CW__STEP_WIDTH = CWPW_,
nxp_ip 4:9a80b6d63005 81 CCW_STEP_WIDTH = CCWPW_,
nxp_ip 4:9a80b6d63005 82 CW__STEP_COUNT = CWSCOUNT_,
nxp_ip 4:9a80b6d63005 83 CCW_STEP_COUNT = CCWSCOUNT_,
nxp_ip 4:9a80b6d63005 84 CW__ROTATION_COUNT = CWRCOUNT_,
nxp_ip 4:9a80b6d63005 85 CCW_ROTATION_COUNT = CCWRCOUNT_
nxp_ip 4:9a80b6d63005 86 } Register16bits;
nxp_ip 4:9a80b6d63005 87
nxp_ip 4:9a80b6d63005 88 /** keyword to select direction of rotation */
nxp_ip 4:9a80b6d63005 89 typedef enum {
nxp_ip 4:9a80b6d63005 90 CW = 0, /**< Clockwise direction */
nxp_ip 4:9a80b6d63005 91 CCW /**< ConterClockwise direction */
nxp_ip 4:9a80b6d63005 92 } Direction;
nxp_ip 4:9a80b6d63005 93
nxp_ip 4:9a80b6d63005 94 /** plescaler range setting */
nxp_ip 4:9a80b6d63005 95 typedef enum {
nxp_ip 4:9a80b6d63005 96 PRESCALER_FROM_40_TO_333333, /**< Prescaler range from 3us(333333pps) to 24.576ms(40 pps) */
nxp_ip 4:9a80b6d63005 97 PRESCALER_FROM_20_TO_166667, /**< Prescaler range from 6us(166667pps) to 49.152ms(20 pps) */
nxp_ip 4:9a80b6d63005 98 PRESCALER_FROM_10_TO_83333, /**< Prescaler range from 12us( 83333pps) to 98.304ms(10 pps) */
nxp_ip 4:9a80b6d63005 99 PRESCALER_FROM_5_TO_41667, /**< Prescaler range from 24us( 41667pps) to 196.608ms( 5 pps) */
nxp_ip 4:9a80b6d63005 100 PRESCALER_FROM_2_5_TO_20833, /**< Prescaler range from 48us( 20833pps) to 393.216ms( 2.5 pps) */
nxp_ip 4:9a80b6d63005 101 PRESCALER_FROM_1_27_TO_10416, /**< Prescaler range from 96us( 10416pps) to 786.432ms( 1.27pps) */
nxp_ip 4:9a80b6d63005 102 PRESCALER_FROM_0_64_TO_5208, /**< Prescaler range from 192us( 5208pps) to 1572.864ms( 0.64pps) */
nxp_ip 4:9a80b6d63005 103 PRESCALER_FROM_0_32_TO_2604, /**< Prescaler range from 384us( 2604pps) to 3145.728ms( 0.32pps) */
nxp_ip 4:9a80b6d63005 104 } PrescalerRange;
nxp_ip 4:9a80b6d63005 105
nxp_ip 4:9a80b6d63005 106 /** Create a PCA9629 instance connected to specified I2C pins with specified address
nxp_ip 4:9a80b6d63005 107 *
nxp_ip 4:9a80b6d63005 108 * @param I2C_sda I2C-bus SDA pin
nxp_ip 4:9a80b6d63005 109 * @param I2C_scl I2C-bus SCL pin
nxp_ip 4:9a80b6d63005 110 * @param I2C_address I2C-bus address (default: 0x42)
nxp_ip 4:9a80b6d63005 111 */
nxp_ip 4:9a80b6d63005 112 PCA9629(
nxp_ip 4:9a80b6d63005 113 PinName I2C_sda,
nxp_ip 4:9a80b6d63005 114 PinName I2C_scl,
nxp_ip 4:9a80b6d63005 115 char I2C_address = PCA9629_DEFAULT_ADDR
nxp_ip 4:9a80b6d63005 116 );
nxp_ip 4:9a80b6d63005 117
nxp_ip 4:9a80b6d63005 118 /** Initialize all registers
nxp_ip 4:9a80b6d63005 119 *
nxp_ip 4:9a80b6d63005 120 * The initializing values are defined in the function
nxp_ip 4:9a80b6d63005 121 */
nxp_ip 4:9a80b6d63005 122 void init_registers( void );
nxp_ip 4:9a80b6d63005 123
nxp_ip 4:9a80b6d63005 124 /** Initialize all registers
nxp_ip 4:9a80b6d63005 125 *
nxp_ip 4:9a80b6d63005 126 * The initializing values are defined in the function
nxp_ip 4:9a80b6d63005 127 */
nxp_ip 4:9a80b6d63005 128 void set_all_registers( char *a, char size );
nxp_ip 6:138665018069 129
nxp_ip 4:9a80b6d63005 130 /** Write 1 byte data into a register
nxp_ip 4:9a80b6d63005 131 *
nxp_ip 4:9a80b6d63005 132 * Setting 8 bits data into a register
nxp_ip 4:9a80b6d63005 133 *
nxp_ip 4:9a80b6d63005 134 * @param register_name the register name: data writing into
nxp_ip 4:9a80b6d63005 135 * @param value 8 bits writing data
nxp_ip 4:9a80b6d63005 136 */
nxp_ip 4:9a80b6d63005 137 void write( RegisterName register_name, char value );
nxp_ip 4:9a80b6d63005 138
nxp_ip 4:9a80b6d63005 139 /** Write 2 bytes data into a register
nxp_ip 4:9a80b6d63005 140 *
nxp_ip 4:9a80b6d63005 141 * Setting 16 bits data into registers
nxp_ip 4:9a80b6d63005 142 *
nxp_ip 4:9a80b6d63005 143 * @param register_name the register name: data writing into (it can be "SROTN_", "CWPW_", "CCWPW_", "CWRCOUNT_", "CCWSCOUNT_", "CWRCOUNT_" or "CCWRCOUNT_" )
nxp_ip 4:9a80b6d63005 144 * @param value 16 bits writing data
nxp_ip 4:9a80b6d63005 145 */
nxp_ip 4:9a80b6d63005 146 void write( Register16bits register_name, short value );
nxp_ip 4:9a80b6d63005 147
nxp_ip 4:9a80b6d63005 148 /** Read 1 byte data from a register
nxp_ip 4:9a80b6d63005 149 *
nxp_ip 4:9a80b6d63005 150 * Setting data into a register
nxp_ip 4:9a80b6d63005 151 *
nxp_ip 4:9a80b6d63005 152 * @param register_name the register name: data reading from
nxp_ip 4:9a80b6d63005 153 * @return read 8 bits data from the register
nxp_ip 4:9a80b6d63005 154 */
nxp_ip 4:9a80b6d63005 155 char read( RegisterName register_name );
nxp_ip 4:9a80b6d63005 156
nxp_ip 4:9a80b6d63005 157 /** Read 2 byte data from registers
nxp_ip 4:9a80b6d63005 158 *
nxp_ip 4:9a80b6d63005 159 * Setting data into a register
nxp_ip 4:9a80b6d63005 160 *
nxp_ip 4:9a80b6d63005 161 * @param register_name the register name: data writing into (it can be "SROTN_", "CWPW_", "CCWPW_", "CWRCOUNT_", "CCWSCOUNT_", "CWRCOUNT_" or "CCWRCOUNT_" )
nxp_ip 4:9a80b6d63005 162 * @return read 16 bits data from the registers
nxp_ip 4:9a80b6d63005 163 */
nxp_ip 4:9a80b6d63005 164 short read( Register16bits register_name );
nxp_ip 4:9a80b6d63005 165
nxp_ip 4:9a80b6d63005 166 /** Motor start
nxp_ip 4:9a80b6d63005 167 *
nxp_ip 4:9a80b6d63005 168 * Start command
nxp_ip 4:9a80b6d63005 169 * This function starts motor operation with hard-stop flag and rotation+step enabled, no repeat will be performed
nxp_ip 6:138665018069 170 * If custom start is required, use "write( PCA9629::MCNTL, 0xXX )" to control each bits.
nxp_ip 4:9a80b6d63005 171 *
nxp_ip 4:9a80b6d63005 172 * @param dir rotate direction ("CW" or "CCW")
nxp_ip 4:9a80b6d63005 173 */
nxp_ip 4:9a80b6d63005 174 void start( Direction dir );
nxp_ip 6:138665018069 175
nxp_ip 4:9a80b6d63005 176 /** Motor stop
nxp_ip 4:9a80b6d63005 177 *
nxp_ip 4:9a80b6d63005 178 * Stop command
nxp_ip 4:9a80b6d63005 179 *
nxp_ip 4:9a80b6d63005 180 */
nxp_ip 4:9a80b6d63005 181 void stop( void );
nxp_ip 4:9a80b6d63005 182
nxp_ip 4:9a80b6d63005 183 /** Set PPS
nxp_ip 4:9a80b6d63005 184 *
nxp_ip 4:9a80b6d63005 185 * Setting PulsePerSecond
nxp_ip 4:9a80b6d63005 186 * This interface can be used to set CWPWx or CCWPWx registers
nxp_ip 4:9a80b6d63005 187 *
nxp_ip 4:9a80b6d63005 188 * @param dir rotate direction ("CW" or "CCW")
nxp_ip 4:9a80b6d63005 189 * @param prescaler prescaler setting (for 3 bits setting range from 0 to 0x7. See datasheet)
nxp_ip 6:138665018069 190 * @param pulse_per_second pps defineds pulse width for the motor. The pulse width will be 1/pps
nxp_ip 4:9a80b6d63005 191 * @return 16 bit data that what set to the CWPWx or CCWPWx registers
nxp_ip 4:9a80b6d63005 192 */
nxp_ip 6:138665018069 193 short pps( Direction dir, PrescalerRange prescaler, int pulse_per_second );
nxp_ip 4:9a80b6d63005 194
nxp_ip 4:9a80b6d63005 195 /** Set PPS
nxp_ip 4:9a80b6d63005 196 *
nxp_ip 4:9a80b6d63005 197 * Setting PulsePerSecond
nxp_ip 4:9a80b6d63005 198 * This interface can be used to set CWPWx or CCWPWx registers
nxp_ip 4:9a80b6d63005 199 *
nxp_ip 4:9a80b6d63005 200 * @param dir rotate direction ("CW" or "CCW")
nxp_ip 6:138665018069 201 * @param pulse_per_second pps defineds pulse width for the motor. The pulse width will be 1/pps
nxp_ip 4:9a80b6d63005 202 * @return 16 bit data that what set to the CWPWx or CCWPWx registers
nxp_ip 4:9a80b6d63005 203 */
nxp_ip 6:138665018069 204 short pps( Direction dir, float pulse_per_second );
nxp_ip 4:9a80b6d63005 205
nxp_ip 4:9a80b6d63005 206 /** Set rotation count
nxp_ip 4:9a80b6d63005 207 *
nxp_ip 4:9a80b6d63005 208 * Setting rotation count
nxp_ip 4:9a80b6d63005 209 * This interfaces CWRCOUNTx, CCWRCOUNTx registers
nxp_ip 4:9a80b6d63005 210 *
nxp_ip 4:9a80b6d63005 211 * @param dir rotate direction ("CW" or "CCW")
nxp_ip 4:9a80b6d63005 212 * @param rotations sets number of rotations with 16 bit value
nxp_ip 4:9a80b6d63005 213 */
nxp_ip 4:9a80b6d63005 214 void rotations( Direction dir, int rotations );
nxp_ip 4:9a80b6d63005 215
nxp_ip 4:9a80b6d63005 216 /** Set PPS
nxp_ip 4:9a80b6d63005 217 *
nxp_ip 4:9a80b6d63005 218 * Setting step count
nxp_ip 4:9a80b6d63005 219 * This interfaces CWSCOUNTx, CCWSCOUNTx registers
nxp_ip 4:9a80b6d63005 220 *
nxp_ip 4:9a80b6d63005 221 * @param dir rotate direction ("CW" or "CCW")
nxp_ip 4:9a80b6d63005 222 * @param steps sets number of steps with 16 bit value
nxp_ip 4:9a80b6d63005 223 */
nxp_ip 4:9a80b6d63005 224 void steps( Direction dir, int pps );
nxp_ip 4:9a80b6d63005 225
nxp_ip 4:9a80b6d63005 226
nxp_ip 4:9a80b6d63005 227 /** Register dump
nxp_ip 4:9a80b6d63005 228 *
nxp_ip 4:9a80b6d63005 229 * Dumping all register data to serial console
nxp_ip 4:9a80b6d63005 230 *
nxp_ip 4:9a80b6d63005 231 */
nxp_ip 4:9a80b6d63005 232 void register_dump( void );
nxp_ip 4:9a80b6d63005 233
nxp_ip 4:9a80b6d63005 234 /** Register dump
nxp_ip 4:9a80b6d63005 235 *
nxp_ip 4:9a80b6d63005 236 * Dumping all register data to serial console
nxp_ip 4:9a80b6d63005 237 *
nxp_ip 4:9a80b6d63005 238 */
nxp_ip 4:9a80b6d63005 239 void speed_change( unsigned short pw );
nxp_ip 4:9a80b6d63005 240
nxp_ip 4:9a80b6d63005 241 private:
nxp_ip 6:138665018069 242
nxp_ip 4:9a80b6d63005 243 I2C i2c;
nxp_ip 4:9a80b6d63005 244 char i2c_addr;
nxp_ip 4:9a80b6d63005 245 };
nxp_ip 4:9a80b6d63005 246
nxp_ip 4:9a80b6d63005 247 #endif // MBED_PCA9629
nxp_ip 4:9a80b6d63005 248