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:10:19 2012 +0000
Revision:
0:77dbce152406
Child:
1:b3d94e04adfa
first release

Who changed what in which revision?

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