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:
Thu Feb 02 05:23:12 2012 +0000
Revision:
1:b3d94e04adfa
Parent:
0:77dbce152406
Child:
2:2d07f1c46eb3
comment added

Who changed what in which revision?

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