Sample code for PCA9629 operation
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /** A sample code for PCA9629 00002 * 00003 * @author Akifumi (Tedd) OKANO, NXP Semiconductors 00004 * @version 1.1 00005 * @date 23-Jul-2012 00006 * 00007 * revision history 00008 * version 1.0 (24-Apr-2011) : Initial version 00009 * version 1.1 (23-Jul-2012) : API modification 00010 * Correction for comments 00011 * 00012 * Released under the MIT License: http://mbed.org/license/mit 00013 * 00014 * An operation sample of PCU9629 stepper motor controller. 00015 * The mbed accesses the PCU9629 registers through I2C. 00016 * 00017 * This sample code demonstrates 4 type of PCA9629 operation 00018 * 00019 * About PCA9629: 00020 * http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_bus_controller_and_bridge_ics/PCA9629PW.html 00021 */ 00022 00023 #include "mbed.h" 00024 #include "PCA9629.h" 00025 00026 BusOut leds( LED4, LED3, LED2, LED1 ); 00027 PCA9629 motor_controller( p28, p27, 48 ); // SDA, SCL, Steps/rotation, I2C_address (option) 00028 00029 void motor_action_0( void ); 00030 void motor_action_1( void ); 00031 void motor_action_2( void ); 00032 void motor_action_3( void ); 00033 void motor_action_CDE( void ); 00034 00035 int main() { 00036 printf( "PCA9629 simple sample program\r\n" ); 00037 00038 while ( 1 ) { 00039 leds = 0x1; 00040 motor_action_0(); // demo 0 00041 00042 leds = 0x3; 00043 motor_action_1(); // demo 1 00044 00045 leds = 0x7; 00046 motor_action_2(); // demo 2 00047 00048 leds = 0xF; 00049 motor_action_3(); // demo 3 00050 } 00051 } 00052 00053 void motor_action_0( void ) { 00054 00055 // simple motor operation 00056 // the motor spins 2 times (200pps) for clockwise (CW), 1 time (100pps) for counter-clockwise. 00057 00058 motor_controller.stop(); 00059 motor_controller.init_registers(); 00060 00061 motor_controller.pps( PCA9629::CW, 200 ); 00062 motor_controller.rotations_and_steps( PCA9629::CW, 0, 96 ); 00063 00064 motor_controller.pps( PCA9629::CCW, 100 ); 00065 motor_controller.rotations_and_steps( PCA9629::CCW, 0, 48 ); 00066 00067 for ( int i = 0; i < 2; i++ ) { 00068 motor_controller.start( PCA9629::CW ); 00069 wait( 2.0 ); 00070 00071 motor_controller.start( PCA9629::CCW ); 00072 wait( 2.0 ); 00073 } 00074 } 00075 00076 void motor_action_1( void ) { 00077 00078 // ramp control demo 00079 00080 motor_controller.stop(); 00081 motor_controller.init_registers(); 00082 00083 // auto generated example code for PCA9629 on mbed 00084 // setting: total operation time = 2, initial speed = 40.69, rotations = 5, steps = 0, ramp-up enable = enable, ramp-down enable = enable, rotate direction = CW 00085 00086 // motor_controller.write( PCA9629::STEPS_PER_ROATION, 48 ); 00087 motor_controller.write( PCA9629::CW__STEP_WIDTH, 1175 ); 00088 motor_controller.write( PCA9629::CW__STEP_COUNT, 35 ); 00089 motor_controller.write( PCA9629::CW__ROTATION_COUNT, 2 ); 00090 motor_controller.write( PCA9629::RAMPCNTL, 0x37 ); 00091 00092 for ( int i = 0; i < 2; i++ ) { 00093 motor_controller.write( PCA9629::MCNTL, 0xA8 ); // start 00094 wait( 2.5 ); 00095 } 00096 } 00097 00098 void motor_action_2( void ) { 00099 00100 // interrupt operation demo 00101 00102 motor_controller.stop(); 00103 00104 // this array is a sample for PCA9629 local interrupt operation 00105 // the P0 and P1 inputs enabled for interrupt in. 00106 // motor rotation will be reversed by interrupt signals. 00107 // the motor direction will be changed 12 steps after when the interrupt happened 00108 // 00109 // CW = 40.69pps, CCW = 81.38pps 00110 00111 char init_array2[] = { 0x80, 00112 0x20, 0xE2, 0xE4, 0xE6, 0xE0, 0xFF, 0x10, // for registers MODE - WDCNTL (0x00 - 0x06 00113 0x00, 0x00, // for registers IP and INTSTAT (0x07, 0x08) 00114 0x0F, 0x03, 0x0C, 0x0F, 0x03, 0x01, 0x03, 0x03, 0x01, // for registers OP - INT_AUTO_CLR (0x09 - 0x11) 00115 0x00, 0x00, 0x30, 0x00, 0xFF, 0x1F, 0xFF, 0x0F, // for registers SETMODE - CCWPWH (0x12 - 0x19) 00116 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x00, 0x0A, 0x00, // for registers CWSCOUNTL - CCWRCOUNTH (0x1A - 0x21) 00117 0x0C, 0x0C, // for registers EXTRASTEPS0 and EXTRASTEPS1 (0x22, 0x23) 00118 0x00, 0x00, 0xA8 // for registers RMPCNTL - MCNTL (0x24 - 0x26) 00119 }; 00120 00121 motor_controller.set_all_registers( init_array2, sizeof( init_array2 )); 00122 wait( 5.0 ); 00123 00124 motor_controller.stop(); 00125 wait( 0.5 ); 00126 00127 } 00128 00129 void motor_action_3( void ) { 00130 00131 // speed change sample while the motor is running 00132 00133 motor_controller.stop(); 00134 motor_controller.init_registers(); 00135 00136 motor_controller.rotations( PCA9629::CCW, 0xFFFF ); 00137 00138 for ( int i = 0; i < 2; i++ ) { 00139 for ( int s = 1; s <= 5; s++ ) { 00140 motor_controller.pps( PCA9629::CCW, 55 * s ); 00141 motor_controller.start( PCA9629::CCW ); 00142 wait( 1.0 ); 00143 motor_controller.stop(); 00144 } 00145 } 00146 } 00147 00148 void motor_action_CDE( void ) { 00149 char scale[ 8 ] = { 4, 6, 8, 9, 11, 13, 15, 16 }; // CDEFGABC (A=0) 00150 float freq[ 8 ]; 00151 00152 for ( int i = 0; i < 8; i++ ) 00153 freq[ i ] = (440.0 / 8.0) * pow( 2.0, (float)(scale[ i ]) / 12.0 ); 00154 00155 motor_controller.stop(); 00156 motor_controller.init_registers(); 00157 motor_controller.rotations( PCA9629::CCW, 0xFFFF ); 00158 motor_controller.write( PCA9629::PHCNTL, 0x1 ); 00159 00160 for ( int i = 0; i < 2; i++ ) { 00161 motor_controller.stop(); 00162 for ( int s = 0; s < 8; s++ ) { 00163 motor_controller.pps( PCA9629::CCW, freq[ s ] ); 00164 motor_controller.start( PCA9629::CCW ); 00165 wait( 1 ); 00166 00167 motor_controller.stop(); 00168 } 00169 } 00170 } 00171
Generated on Wed Jul 13 2022 21:18:06 by 1.7.2