'Hello World' program for the MAX14871_Shield Library. Simple terminal based program that allows the user to exercise the features of MAXREFDES89#.
Dependencies: MAX14871_Shield mbed
main.cpp
00001 /********************************************************************** 00002 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 **********************************************************************/ 00032 00033 00034 #include "mbed.h" 00035 #include "max14871_shield.h" 00036 00037 00038 //helper functions for demo 00039 uint32_t menu_select_motor_driver(void); 00040 00041 void menu_set_duty_cycle_(uint8_t md, Max14871_Shield *shld); 00042 00043 void menu_set_op_mode(uint8_t md, Max14871_Shield *shld); 00044 00045 void menu_set_current_reg(uint8_t md, Max14871_Shield *shld); 00046 00047 void menu_print_motor_data(uint8_t md, Max14871_Shield *shld); 00048 00049 void menu_print_maxim_banner(void); 00050 00051 void clear_screen(void); 00052 00053 00054 int main(void) 00055 { 00056 uint8_t idx = 0; 00057 uint32_t user_entry = 0; 00058 const float PWM_PERIOD = 40e-6f; //~25KHz 00059 00060 I2C i2c_bus(D14, D15); 00061 00062 Max14871_Shield shld(&i2c_bus, true); 00063 00064 //set pwm switching frequency 00065 for(idx = 0; idx < 4; idx++) 00066 { 00067 shld.set_pwm_period((Max14871_Shield::max14871_motor_driver_t) (idx + 1), PWM_PERIOD); 00068 } 00069 00070 idx = 1; 00071 00072 //main menu 00073 while(user_entry != 5) 00074 { 00075 menu_print_maxim_banner(); 00076 00077 //print currently selected motor's data 00078 menu_print_motor_data(idx, &shld); 00079 00080 printf("Please select an option below:\n"); 00081 printf("%t1. Select Active Motor Driver\n"); 00082 printf("%t2. Set PWM Duty Cycle\n"); 00083 printf("%t3. Set Operating Mode\n"); 00084 printf("%t4. Set Current Regulation Mode\n"); 00085 printf("%t5. Quit\n"); 00086 00087 scanf("%d", &user_entry); 00088 00089 switch(user_entry) 00090 { 00091 case 1: 00092 idx = menu_select_motor_driver(); 00093 clear_screen(); 00094 break; 00095 00096 case 2: 00097 menu_set_duty_cycle_(idx, &shld); 00098 clear_screen(); 00099 break; 00100 00101 case 3: 00102 menu_set_op_mode(idx, &shld); 00103 clear_screen(); 00104 break; 00105 00106 case 4: 00107 menu_set_current_reg(idx, &shld); 00108 clear_screen(); 00109 break; 00110 00111 case 5: 00112 printf("Ending Program\n"); 00113 break; 00114 00115 default: 00116 printf("Invalid entry, please try again\n"); 00117 break; 00118 } 00119 } 00120 00121 return 0; 00122 } 00123 00124 00125 uint32_t menu_select_motor_driver(void) 00126 { 00127 uint32_t md; 00128 00129 do 00130 { 00131 printf("\nPlease enter motor driver number, 1-4\n"); 00132 scanf("%d", &md); 00133 00134 if((md < 1) || (md > 4)) 00135 { 00136 printf("Value out of range\n"); 00137 } 00138 } 00139 while((md < 1) || (md > 4)); 00140 00141 return md; 00142 } 00143 00144 00145 void menu_set_duty_cycle_(uint8_t md, Max14871_Shield *shld) 00146 { 00147 float dc = 0.0f; 00148 00149 do 00150 { 00151 printf("\nPlease enter new duty cycle, 0.0 - 1.0\n"); 00152 scanf("%f", &dc); 00153 00154 if((dc < 0.0f) || (dc > 1.0f)) 00155 { 00156 printf("Value out of range\n"); 00157 } 00158 } 00159 while((dc < 0.0f) || (dc > 1.0f)); 00160 00161 shld->set_pwm_duty_cycle((Max14871_Shield::max14871_motor_driver_t) md, dc); 00162 } 00163 00164 00165 void menu_set_op_mode(uint8_t md, Max14871_Shield *shld) 00166 { 00167 uint32_t mode; 00168 00169 do 00170 { 00171 printf("\nPlease enter one of the following modes:\n"); 00172 printf("%t1. COAST\n"); 00173 printf("%t2. BRAKE\n"); 00174 printf("%t3. REVERSE\n"); 00175 printf("%t4. FORWARD\n"); 00176 00177 scanf("%d", &mode); 00178 00179 if((mode < 1) || (mode > 4)) 00180 { 00181 printf("Value out of range\n"); 00182 } 00183 } 00184 while((mode < 1) || (mode > 4)); 00185 00186 shld->set_operating_mode((Max14871_Shield::max14871_motor_driver_t) md,(Max14871_Shield::max14871_operating_mode_t) mode); 00187 } 00188 00189 00190 void menu_set_current_reg(uint8_t md, Max14871_Shield *shld) 00191 { 00192 uint32_t mode; 00193 float v_ref = 0.0f; 00194 00195 do 00196 { 00197 printf("\nPlease enter one of the following modes:\n"); 00198 printf("%t1. 25%% ripple, internal reference\n"); 00199 printf("%t2. 25%% ripple, external reference\n"); 00200 printf("%t3. TCOFF fast decay, internall reference\n"); 00201 printf("%t4. TCOFF slow decay, internall reference\n"); 00202 printf("%t5. TCOFF fast decay, external reference\n"); 00203 printf("%t6. TCOFF slow decay, external reference\n"); 00204 00205 scanf("%d", &mode); 00206 00207 if((mode < 1) || (mode > 6)) 00208 { 00209 printf("Value out of range\n"); 00210 } 00211 } 00212 while((mode < 1) || (mode > 6)); 00213 00214 if((mode == 2) || (mode >= 5)) 00215 { 00216 do 00217 { 00218 printf("Please enter external Vref, 0.0V - 2.0V\n"); 00219 scanf("%f", &v_ref); 00220 00221 if((v_ref < 0.0f) || (v_ref > 2.0f)) 00222 { 00223 printf("Value out of range\n"); 00224 } 00225 } 00226 while((v_ref < 0.0f) || (v_ref > 2.0f)); 00227 } 00228 00229 shld->set_current_regulation_mode((Max14871_Shield::max14871_motor_driver_t) md,(Max14871_Shield::max14871_current_regulation_mode_t) mode, v_ref); 00230 } 00231 00232 00233 void menu_print_motor_data(uint8_t md, Max14871_Shield *shld) 00234 { 00235 Max14871_Shield::max14871_operating_mode_t op_state; 00236 Max14871_Shield::max14871_current_regulation_mode_t reg_state; 00237 float dc; 00238 float v_ref; 00239 00240 op_state = shld->get_operating_mode((Max14871_Shield::max14871_motor_driver_t) md); 00241 00242 reg_state = shld->get_current_regulation_mode((Max14871_Shield::max14871_motor_driver_t) md); 00243 00244 dc = shld->get_pwm_duty_cycle((Max14871_Shield::max14871_motor_driver_t) md); 00245 00246 v_ref = shld->get_external_voltage_ref((Max14871_Shield::max14871_motor_driver_t) md); 00247 00248 printf("Active Motor Driver = %d\n", md); 00249 00250 switch(op_state) 00251 { 00252 case 1: 00253 printf("Operating Mode = COAST\n"); 00254 break; 00255 00256 case 2: 00257 printf("Operating Mode = BRAKE\n"); 00258 break; 00259 00260 case 3: 00261 printf("Operating Mode = REVERSE\n"); 00262 break; 00263 00264 case 4: 00265 printf("Operating Mode = FORWARD\n"); 00266 break; 00267 00268 default: 00269 printf("Operating Mode = UNKNOWN\n"); 00270 break; 00271 } 00272 00273 00274 switch(reg_state) 00275 { 00276 case 1: 00277 printf("Current Regulation Mode = 25%% ripple, internal reference\n"); 00278 break; 00279 00280 case 2: 00281 printf("Current Regulation Mode = 25%% ripple, external reference\n"); 00282 break; 00283 00284 case 3: 00285 printf("Current Regulation Mode = TCOFF fast decay, internall reference\n"); 00286 break; 00287 00288 case 4: 00289 printf("Current Regulation Mode = TCOFF slow decay, internall reference\n"); 00290 break; 00291 00292 case 5: 00293 printf("Current Regulation Mode = TCOFF fast decay, external reference\n"); 00294 break; 00295 00296 case 6: 00297 printf("Current Regulation Mode = TCOFF slow decay, external reference\n"); 00298 break; 00299 00300 default: 00301 printf("Current Regulation Mode = UNKNOWN\n"); 00302 break; 00303 } 00304 00305 printf("Duty Cycle = %0.2f\n", dc); 00306 printf("Voltage Reference = %0.2f\n", v_ref); 00307 } 00308 00309 00310 void menu_print_maxim_banner(void) 00311 { 00312 printf( "\r\n\r\n"); 00313 printf( "///////////////////////////////////////////////////////////////////\r\n"); 00314 printf( "// _____ _____ //\r\n"); 00315 printf( "// / __ \\ / __ \\ //\r\n"); 00316 printf( "// | | \\/ / / | | //\r\n"); 00317 printf( "// | | / / | | //\r\n"); 00318 printf( "// | | / \\ | | //\r\n"); 00319 printf( "// | | / /\\ \\ | | //\r\n"); 00320 printf( "// |_| /_/ \\_\\ |_| //\r\n"); 00321 printf( "// //\r\n"); 00322 printf( "///////////////////////////////////////////////////////////////////\r\n"); 00323 printf("\r\n"); 00324 } 00325 00326 00327 void clear_screen(void) 00328 { 00329 printf("%c[2J", 0x1B); //clear screen 00330 printf("%c[H", 0x1B); //move cursor to Home 00331 } 00332
Generated on Thu Jul 14 2022 04:09:40 by 1.7.2