Hello program for PCA9629A. Demonstrates basic operations of PCA9629A component library.

Dependencies:   PCA9629A mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** A sample code for PCA9629A
00002  *
00003  *  @author  Akifumi (Tedd) OKANO, NXP Semiconductors
00004  *  @version 1.1
00005  *  @date    12-Sep-2012
00006  *
00007  *  revision history (PCA9629A)
00008  *      version 0.1 (05-Jun-2013) : PCA9629"A" initial version
00009  *      version 0.2 (05-Jun-2013) : register name fix, register description added
00010  *      version 1.0 (23-Apr-2014) : sample code for revision 1.0 library
00011  *      version 1.1 (12-Sep-2014) : sample code for revision 1.1 library
00012  *
00013  *  Released under the Apache 2 license License
00014  *
00015  *  An operation sample of PCU9629A stepper motor controller.
00016  *  The mbed accesses the PCU9629A registers through I2C.
00017  *
00018  *  This sample code demonstrates 4 type of PCA9629A operation
00019  *
00020  *  About PCA9629A:
00021  *    http://www.nxp.com/products/interface_and_connectivity/i2c/i2c_bus_controller_and_bridge_ics/PCA9629APW.html
00022  */
00023 
00024 #include "mbed.h"
00025 #include "PCA9629A.h"
00026 
00027 BusOut      leds( LED4, LED3, LED2, LED1 );
00028 
00029 PCA9629A    motor_controller( p28, p27, 0x42 );  //  SDA, SCL, Steps/rotation, I2C_address (option)
00030 
00031 void motor_action_0( void );
00032 void motor_action_1( void );
00033 void motor_action_2( void );
00034 void motor_action_3( void );
00035 void motor_action_CDE( void );
00036 
00037 int main()
00038 {
00039     printf( "PCA9629A simple sample program\r\n" );
00040 
00041     while ( 1 ) {
00042         leds    = 0x1;
00043         motor_action_0();   //  demo 0
00044 
00045         leds    = 0x3;
00046         motor_action_1();   //  demo 1
00047 
00048         leds    = 0x7;
00049         motor_action_2();   //  demo 2
00050 
00051         leds    = 0xF;
00052         motor_action_3();   //  demo 3
00053     }
00054 }
00055 
00056 void motor_action_0( void )
00057 {
00058     //  simple motor operation
00059     //  the motor spins 2 times (200pps) for clockwise (CW), 1 time (100pps) for counter-clockwise.
00060 
00061     motor_controller.stop();
00062     motor_controller.init_registers();
00063 
00064     motor_controller.pps( PCA9629A::CW, 200 );
00065     motor_controller.steps( PCA9629A::CW, 96 );
00066 
00067     motor_controller.pps( PCA9629A::CCW, 100 );
00068     motor_controller.steps( PCA9629A::CCW, 48 );
00069 
00070     for ( int i = 0; i < 2; i++ ) {
00071         motor_controller.start( PCA9629A::CW );
00072         wait( 2.0 );
00073 
00074         motor_controller.start( PCA9629A::CCW );
00075         wait( 2.0 );
00076     }
00077 }
00078 
00079 void motor_action_1( void )
00080 {
00081     //  ramp control demo
00082 
00083     motor_controller.stop();
00084     motor_controller.init_registers();
00085 
00086     motor_controller.write( PCA9629A::RUCNTL, 0x27  );
00087     motor_controller.write( PCA9629A::RDCNTL, 0x27  );
00088     motor_controller.write( PCA9629A::CW__STEP_WIDTH, 1175  );
00089     motor_controller.write( PCA9629A::CW__STEP_COUNT, 131  );
00090 
00091     for ( int i = 0; i < 2; i++ ) {
00092         motor_controller.start( PCA9629A::CW );  //  start
00093         wait( 2.5 );
00094     }
00095 }
00096 
00097 void motor_action_2( void )
00098 {
00099     //  interrupt operation demo
00100 
00101     motor_controller.stop();
00102 
00103     //  this array is a sample for PCA9629A local interrupt operation
00104     //  the P0 and P1 inputs enabled for interrupt in.
00105     //  motor rotation will be reversed by interrupt signals.
00106     //  the motor direction will be changed 12 steps after when the interrupt happened
00107     //
00108     //  CW = 40.69pps, CCW = 81.38pps
00109 
00110     char init_array2[] = {    0x80,
00111                               0x20, 0x0A, 0x00, 0x03, 0x13, 0x1C,             //  for registers MODE - MSK (0x00 - 0x07
00112                               0x00, 0x00, 0x69, 0x00, 0x00,                   //  for registers INTSTAT - EXTRASTEPS1 (0x06, 0xA)
00113                               0x10, 0x80,                                     //  for registers OP_CFG_PHS and OP_STAT_TO (0x0B - 0xC)
00114                               0x09, 0x09, 0x00, 0x00, 0x00,                   //  for registers RUCNTL - LOOPDLY_CCW (0xD- 0x10)
00115                               0x60, 0x00, 0x60, 0x00, 0x05, 0x0D, 0x05, 0x0D, //  for registers CWSCOUNTL - MCNTL (0x12 - 0x1A)
00116                               0x82,                                           //  for register MCNTL (0x1A)
00117                               0xE2, 0xE4, 0xE6, 0xE0                          //  for registers SUBADR1 - ALLCALLADR (0x1B - 0x1E)
00118                          };
00119 
00120     motor_controller.set_all_registers( init_array2, sizeof( init_array2 ));
00121     wait( 5.0 );
00122 
00123     motor_controller.stop();
00124     wait( 0.5 );
00125 }
00126 
00127 void motor_action_3( void )
00128 {
00129     //  speed change sample while the motor is running
00130 
00131     motor_controller.stop();
00132     motor_controller.init_registers();
00133 
00134     motor_controller.steps( PCA9629A::CCW, 0xFFFF );
00135 
00136     for ( int i = 0; i < 2; i++ ) {
00137         for ( int s = 1; s <= 5; s++ ) {
00138             motor_controller.pps( PCA9629A::CCW, 55 * s );
00139             motor_controller.start( PCA9629A::CCW );
00140             wait( 1.0 );
00141         }
00142     }
00143 }
00144 
00145 void motor_action_CDE( void )
00146 {
00147     char    scale[ 8 ]  = { 4, 6, 8, 9, 11, 13, 15, 16 };  //   CDEFGABC (A=0)
00148     float   freq[ 8 ];
00149 
00150     for ( int i = 0; i < 8; i++ )
00151         freq[ i ]   = (440.0 / 8.0) * pow( 2.0, (float)(scale[ i ]) / 12.0 );
00152 
00153     motor_controller.stop();
00154     motor_controller.init_registers();
00155     motor_controller.steps( PCA9629A::CCW, 0xFFFF );
00156 
00157     for ( int i = 0; i < 2; i++ ) {
00158         motor_controller.stop();
00159         for ( int s = 0; s < 8; s++ ) {
00160             motor_controller.pps( PCA9629A::CCW, freq[ s ] );
00161             motor_controller.start( PCA9629A::CCW );
00162             wait( 1 );
00163         }
00164     }
00165 }