'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

MAXREFDES89# Component Page

Committer:
j3
Date:
Fri Sep 18 18:38:54 2015 +0000
Revision:
0:1db98cbf6150
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:1db98cbf6150 1 /**********************************************************************
j3 0:1db98cbf6150 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:1db98cbf6150 3 *
j3 0:1db98cbf6150 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:1db98cbf6150 5 * copy of this software and associated documentation files (the "Software"),
j3 0:1db98cbf6150 6 * to deal in the Software without restriction, including without limitation
j3 0:1db98cbf6150 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:1db98cbf6150 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:1db98cbf6150 9 * Software is furnished to do so, subject to the following conditions:
j3 0:1db98cbf6150 10 *
j3 0:1db98cbf6150 11 * The above copyright notice and this permission notice shall be included
j3 0:1db98cbf6150 12 * in all copies or substantial portions of the Software.
j3 0:1db98cbf6150 13 *
j3 0:1db98cbf6150 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:1db98cbf6150 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:1db98cbf6150 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:1db98cbf6150 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:1db98cbf6150 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:1db98cbf6150 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:1db98cbf6150 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:1db98cbf6150 21 *
j3 0:1db98cbf6150 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:1db98cbf6150 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:1db98cbf6150 24 * Products, Inc. Branding Policy.
j3 0:1db98cbf6150 25 *
j3 0:1db98cbf6150 26 * The mere transfer of this software does not imply any licenses
j3 0:1db98cbf6150 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:1db98cbf6150 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:1db98cbf6150 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:1db98cbf6150 30 * ownership rights.
j3 0:1db98cbf6150 31 **********************************************************************/
j3 0:1db98cbf6150 32
j3 0:1db98cbf6150 33
j3 0:1db98cbf6150 34 #include "mbed.h"
j3 0:1db98cbf6150 35 #include "max14871_shield.h"
j3 0:1db98cbf6150 36
j3 0:1db98cbf6150 37
j3 0:1db98cbf6150 38 //helper functions for demo
j3 0:1db98cbf6150 39 uint32_t menu_select_motor_driver(void);
j3 0:1db98cbf6150 40
j3 0:1db98cbf6150 41 void menu_set_duty_cycle_(uint8_t md, Max14871_Shield *shld);
j3 0:1db98cbf6150 42
j3 0:1db98cbf6150 43 void menu_set_op_mode(uint8_t md, Max14871_Shield *shld);
j3 0:1db98cbf6150 44
j3 0:1db98cbf6150 45 void menu_set_current_reg(uint8_t md, Max14871_Shield *shld);
j3 0:1db98cbf6150 46
j3 0:1db98cbf6150 47 void menu_print_motor_data(uint8_t md, Max14871_Shield *shld);
j3 0:1db98cbf6150 48
j3 0:1db98cbf6150 49 void menu_print_maxim_banner(void);
j3 0:1db98cbf6150 50
j3 0:1db98cbf6150 51 void clear_screen(void);
j3 0:1db98cbf6150 52
j3 0:1db98cbf6150 53
j3 0:1db98cbf6150 54 int main(void)
j3 0:1db98cbf6150 55 {
j3 0:1db98cbf6150 56 uint8_t idx = 0;
j3 0:1db98cbf6150 57 uint32_t user_entry = 0;
j3 0:1db98cbf6150 58 const float PWM_PERIOD = 40e-6f; //~25KHz
j3 0:1db98cbf6150 59
j3 0:1db98cbf6150 60 I2C i2c_bus(D14, D15);
j3 0:1db98cbf6150 61
j3 0:1db98cbf6150 62 Max14871_Shield shld(&i2c_bus, true);
j3 0:1db98cbf6150 63
j3 0:1db98cbf6150 64 //set pwm switching frequency
j3 0:1db98cbf6150 65 for(idx = 0; idx < 4; idx++)
j3 0:1db98cbf6150 66 {
j3 0:1db98cbf6150 67 shld.set_pwm_period((Max14871_Shield::max14871_motor_driver_t) (idx + 1), PWM_PERIOD);
j3 0:1db98cbf6150 68 }
j3 0:1db98cbf6150 69
j3 0:1db98cbf6150 70 idx = 1;
j3 0:1db98cbf6150 71
j3 0:1db98cbf6150 72 //main menu
j3 0:1db98cbf6150 73 while(user_entry != 5)
j3 0:1db98cbf6150 74 {
j3 0:1db98cbf6150 75 menu_print_maxim_banner();
j3 0:1db98cbf6150 76
j3 0:1db98cbf6150 77 //print currently selected motor's data
j3 0:1db98cbf6150 78 menu_print_motor_data(idx, &shld);
j3 0:1db98cbf6150 79
j3 0:1db98cbf6150 80 printf("Please select an option below:\n");
j3 0:1db98cbf6150 81 printf("%t1. Select Active Motor Driver\n");
j3 0:1db98cbf6150 82 printf("%t2. Set PWM Duty Cycle\n");
j3 0:1db98cbf6150 83 printf("%t3. Set Operating Mode\n");
j3 0:1db98cbf6150 84 printf("%t4. Set Current Regulation Mode\n");
j3 0:1db98cbf6150 85 printf("%t5. Quit\n");
j3 0:1db98cbf6150 86
j3 0:1db98cbf6150 87 scanf("%d", &user_entry);
j3 0:1db98cbf6150 88
j3 0:1db98cbf6150 89 switch(user_entry)
j3 0:1db98cbf6150 90 {
j3 0:1db98cbf6150 91 case 1:
j3 0:1db98cbf6150 92 idx = menu_select_motor_driver();
j3 0:1db98cbf6150 93 clear_screen();
j3 0:1db98cbf6150 94 break;
j3 0:1db98cbf6150 95
j3 0:1db98cbf6150 96 case 2:
j3 0:1db98cbf6150 97 menu_set_duty_cycle_(idx, &shld);
j3 0:1db98cbf6150 98 clear_screen();
j3 0:1db98cbf6150 99 break;
j3 0:1db98cbf6150 100
j3 0:1db98cbf6150 101 case 3:
j3 0:1db98cbf6150 102 menu_set_op_mode(idx, &shld);
j3 0:1db98cbf6150 103 clear_screen();
j3 0:1db98cbf6150 104 break;
j3 0:1db98cbf6150 105
j3 0:1db98cbf6150 106 case 4:
j3 0:1db98cbf6150 107 menu_set_current_reg(idx, &shld);
j3 0:1db98cbf6150 108 clear_screen();
j3 0:1db98cbf6150 109 break;
j3 0:1db98cbf6150 110
j3 0:1db98cbf6150 111 case 5:
j3 0:1db98cbf6150 112 printf("Ending Program\n");
j3 0:1db98cbf6150 113 break;
j3 0:1db98cbf6150 114
j3 0:1db98cbf6150 115 default:
j3 0:1db98cbf6150 116 printf("Invalid entry, please try again\n");
j3 0:1db98cbf6150 117 break;
j3 0:1db98cbf6150 118 }
j3 0:1db98cbf6150 119 }
j3 0:1db98cbf6150 120
j3 0:1db98cbf6150 121 return 0;
j3 0:1db98cbf6150 122 }
j3 0:1db98cbf6150 123
j3 0:1db98cbf6150 124
j3 0:1db98cbf6150 125 uint32_t menu_select_motor_driver(void)
j3 0:1db98cbf6150 126 {
j3 0:1db98cbf6150 127 uint32_t md;
j3 0:1db98cbf6150 128
j3 0:1db98cbf6150 129 do
j3 0:1db98cbf6150 130 {
j3 0:1db98cbf6150 131 printf("\nPlease enter motor driver number, 1-4\n");
j3 0:1db98cbf6150 132 scanf("%d", &md);
j3 0:1db98cbf6150 133
j3 0:1db98cbf6150 134 if((md < 1) || (md > 4))
j3 0:1db98cbf6150 135 {
j3 0:1db98cbf6150 136 printf("Value out of range\n");
j3 0:1db98cbf6150 137 }
j3 0:1db98cbf6150 138 }
j3 0:1db98cbf6150 139 while((md < 1) || (md > 4));
j3 0:1db98cbf6150 140
j3 0:1db98cbf6150 141 return md;
j3 0:1db98cbf6150 142 }
j3 0:1db98cbf6150 143
j3 0:1db98cbf6150 144
j3 0:1db98cbf6150 145 void menu_set_duty_cycle_(uint8_t md, Max14871_Shield *shld)
j3 0:1db98cbf6150 146 {
j3 0:1db98cbf6150 147 float dc = 0.0f;
j3 0:1db98cbf6150 148
j3 0:1db98cbf6150 149 do
j3 0:1db98cbf6150 150 {
j3 0:1db98cbf6150 151 printf("\nPlease enter new duty cycle, 0.0 - 1.0\n");
j3 0:1db98cbf6150 152 scanf("%f", &dc);
j3 0:1db98cbf6150 153
j3 0:1db98cbf6150 154 if((dc < 0.0f) || (dc > 1.0f))
j3 0:1db98cbf6150 155 {
j3 0:1db98cbf6150 156 printf("Value out of range\n");
j3 0:1db98cbf6150 157 }
j3 0:1db98cbf6150 158 }
j3 0:1db98cbf6150 159 while((dc < 0.0f) || (dc > 1.0f));
j3 0:1db98cbf6150 160
j3 0:1db98cbf6150 161 shld->set_pwm_duty_cycle((Max14871_Shield::max14871_motor_driver_t) md, dc);
j3 0:1db98cbf6150 162 }
j3 0:1db98cbf6150 163
j3 0:1db98cbf6150 164
j3 0:1db98cbf6150 165 void menu_set_op_mode(uint8_t md, Max14871_Shield *shld)
j3 0:1db98cbf6150 166 {
j3 0:1db98cbf6150 167 uint32_t mode;
j3 0:1db98cbf6150 168
j3 0:1db98cbf6150 169 do
j3 0:1db98cbf6150 170 {
j3 0:1db98cbf6150 171 printf("\nPlease enter one of the following modes:\n");
j3 0:1db98cbf6150 172 printf("%t1. COAST\n");
j3 0:1db98cbf6150 173 printf("%t2. BRAKE\n");
j3 0:1db98cbf6150 174 printf("%t3. REVERSE\n");
j3 0:1db98cbf6150 175 printf("%t4. FORWARD\n");
j3 0:1db98cbf6150 176
j3 0:1db98cbf6150 177 scanf("%d", &mode);
j3 0:1db98cbf6150 178
j3 0:1db98cbf6150 179 if((mode < 1) || (mode > 4))
j3 0:1db98cbf6150 180 {
j3 0:1db98cbf6150 181 printf("Value out of range\n");
j3 0:1db98cbf6150 182 }
j3 0:1db98cbf6150 183 }
j3 0:1db98cbf6150 184 while((mode < 1) || (mode > 4));
j3 0:1db98cbf6150 185
j3 0:1db98cbf6150 186 shld->set_operating_mode((Max14871_Shield::max14871_motor_driver_t) md,(Max14871_Shield::max14871_operating_mode_t) mode);
j3 0:1db98cbf6150 187 }
j3 0:1db98cbf6150 188
j3 0:1db98cbf6150 189
j3 0:1db98cbf6150 190 void menu_set_current_reg(uint8_t md, Max14871_Shield *shld)
j3 0:1db98cbf6150 191 {
j3 0:1db98cbf6150 192 uint32_t mode;
j3 0:1db98cbf6150 193 float v_ref = 0.0f;
j3 0:1db98cbf6150 194
j3 0:1db98cbf6150 195 do
j3 0:1db98cbf6150 196 {
j3 0:1db98cbf6150 197 printf("\nPlease enter one of the following modes:\n");
j3 0:1db98cbf6150 198 printf("%t1. 25%% ripple, internal reference\n");
j3 0:1db98cbf6150 199 printf("%t2. 25%% ripple, external reference\n");
j3 0:1db98cbf6150 200 printf("%t3. TCOFF fast decay, internall reference\n");
j3 0:1db98cbf6150 201 printf("%t4. TCOFF slow decay, internall reference\n");
j3 0:1db98cbf6150 202 printf("%t5. TCOFF fast decay, external reference\n");
j3 0:1db98cbf6150 203 printf("%t6. TCOFF slow decay, external reference\n");
j3 0:1db98cbf6150 204
j3 0:1db98cbf6150 205 scanf("%d", &mode);
j3 0:1db98cbf6150 206
j3 0:1db98cbf6150 207 if((mode < 1) || (mode > 6))
j3 0:1db98cbf6150 208 {
j3 0:1db98cbf6150 209 printf("Value out of range\n");
j3 0:1db98cbf6150 210 }
j3 0:1db98cbf6150 211 }
j3 0:1db98cbf6150 212 while((mode < 1) || (mode > 6));
j3 0:1db98cbf6150 213
j3 0:1db98cbf6150 214 if((mode == 2) || (mode >= 5))
j3 0:1db98cbf6150 215 {
j3 0:1db98cbf6150 216 do
j3 0:1db98cbf6150 217 {
j3 0:1db98cbf6150 218 printf("Please enter external Vref, 0.0V - 2.0V\n");
j3 0:1db98cbf6150 219 scanf("%f", &v_ref);
j3 0:1db98cbf6150 220
j3 0:1db98cbf6150 221 if((v_ref < 0.0f) || (v_ref > 2.0f))
j3 0:1db98cbf6150 222 {
j3 0:1db98cbf6150 223 printf("Value out of range\n");
j3 0:1db98cbf6150 224 }
j3 0:1db98cbf6150 225 }
j3 0:1db98cbf6150 226 while((v_ref < 0.0f) || (v_ref > 2.0f));
j3 0:1db98cbf6150 227 }
j3 0:1db98cbf6150 228
j3 0:1db98cbf6150 229 shld->set_current_regulation_mode((Max14871_Shield::max14871_motor_driver_t) md,(Max14871_Shield::max14871_current_regulation_mode_t) mode, v_ref);
j3 0:1db98cbf6150 230 }
j3 0:1db98cbf6150 231
j3 0:1db98cbf6150 232
j3 0:1db98cbf6150 233 void menu_print_motor_data(uint8_t md, Max14871_Shield *shld)
j3 0:1db98cbf6150 234 {
j3 0:1db98cbf6150 235 Max14871_Shield::max14871_operating_mode_t op_state;
j3 0:1db98cbf6150 236 Max14871_Shield::max14871_current_regulation_mode_t reg_state;
j3 0:1db98cbf6150 237 float dc;
j3 0:1db98cbf6150 238 float v_ref;
j3 0:1db98cbf6150 239
j3 0:1db98cbf6150 240 op_state = shld->get_operating_mode((Max14871_Shield::max14871_motor_driver_t) md);
j3 0:1db98cbf6150 241
j3 0:1db98cbf6150 242 reg_state = shld->get_current_regulation_mode((Max14871_Shield::max14871_motor_driver_t) md);
j3 0:1db98cbf6150 243
j3 0:1db98cbf6150 244 dc = shld->get_pwm_duty_cycle((Max14871_Shield::max14871_motor_driver_t) md);
j3 0:1db98cbf6150 245
j3 0:1db98cbf6150 246 v_ref = shld->get_external_voltage_ref((Max14871_Shield::max14871_motor_driver_t) md);
j3 0:1db98cbf6150 247
j3 0:1db98cbf6150 248 printf("Active Motor Driver = %d\n", md);
j3 0:1db98cbf6150 249
j3 0:1db98cbf6150 250 switch(op_state)
j3 0:1db98cbf6150 251 {
j3 0:1db98cbf6150 252 case 1:
j3 0:1db98cbf6150 253 printf("Operating Mode = COAST\n");
j3 0:1db98cbf6150 254 break;
j3 0:1db98cbf6150 255
j3 0:1db98cbf6150 256 case 2:
j3 0:1db98cbf6150 257 printf("Operating Mode = BRAKE\n");
j3 0:1db98cbf6150 258 break;
j3 0:1db98cbf6150 259
j3 0:1db98cbf6150 260 case 3:
j3 0:1db98cbf6150 261 printf("Operating Mode = REVERSE\n");
j3 0:1db98cbf6150 262 break;
j3 0:1db98cbf6150 263
j3 0:1db98cbf6150 264 case 4:
j3 0:1db98cbf6150 265 printf("Operating Mode = FORWARD\n");
j3 0:1db98cbf6150 266 break;
j3 0:1db98cbf6150 267
j3 0:1db98cbf6150 268 default:
j3 0:1db98cbf6150 269 printf("Operating Mode = UNKNOWN\n");
j3 0:1db98cbf6150 270 break;
j3 0:1db98cbf6150 271 }
j3 0:1db98cbf6150 272
j3 0:1db98cbf6150 273
j3 0:1db98cbf6150 274 switch(reg_state)
j3 0:1db98cbf6150 275 {
j3 0:1db98cbf6150 276 case 1:
j3 0:1db98cbf6150 277 printf("Current Regulation Mode = 25%% ripple, internal reference\n");
j3 0:1db98cbf6150 278 break;
j3 0:1db98cbf6150 279
j3 0:1db98cbf6150 280 case 2:
j3 0:1db98cbf6150 281 printf("Current Regulation Mode = 25%% ripple, external reference\n");
j3 0:1db98cbf6150 282 break;
j3 0:1db98cbf6150 283
j3 0:1db98cbf6150 284 case 3:
j3 0:1db98cbf6150 285 printf("Current Regulation Mode = TCOFF fast decay, internall reference\n");
j3 0:1db98cbf6150 286 break;
j3 0:1db98cbf6150 287
j3 0:1db98cbf6150 288 case 4:
j3 0:1db98cbf6150 289 printf("Current Regulation Mode = TCOFF slow decay, internall reference\n");
j3 0:1db98cbf6150 290 break;
j3 0:1db98cbf6150 291
j3 0:1db98cbf6150 292 case 5:
j3 0:1db98cbf6150 293 printf("Current Regulation Mode = TCOFF fast decay, external reference\n");
j3 0:1db98cbf6150 294 break;
j3 0:1db98cbf6150 295
j3 0:1db98cbf6150 296 case 6:
j3 0:1db98cbf6150 297 printf("Current Regulation Mode = TCOFF slow decay, external reference\n");
j3 0:1db98cbf6150 298 break;
j3 0:1db98cbf6150 299
j3 0:1db98cbf6150 300 default:
j3 0:1db98cbf6150 301 printf("Current Regulation Mode = UNKNOWN\n");
j3 0:1db98cbf6150 302 break;
j3 0:1db98cbf6150 303 }
j3 0:1db98cbf6150 304
j3 0:1db98cbf6150 305 printf("Duty Cycle = %0.2f\n", dc);
j3 0:1db98cbf6150 306 printf("Voltage Reference = %0.2f\n", v_ref);
j3 0:1db98cbf6150 307 }
j3 0:1db98cbf6150 308
j3 0:1db98cbf6150 309
j3 0:1db98cbf6150 310 void menu_print_maxim_banner(void)
j3 0:1db98cbf6150 311 {
j3 0:1db98cbf6150 312 printf( "\r\n\r\n");
j3 0:1db98cbf6150 313 printf( "///////////////////////////////////////////////////////////////////\r\n");
j3 0:1db98cbf6150 314 printf( "// _____ _____ //\r\n");
j3 0:1db98cbf6150 315 printf( "// / __ \\ / __ \\ //\r\n");
j3 0:1db98cbf6150 316 printf( "// | | \\/ / / | | //\r\n");
j3 0:1db98cbf6150 317 printf( "// | | / / | | //\r\n");
j3 0:1db98cbf6150 318 printf( "// | | / \\ | | //\r\n");
j3 0:1db98cbf6150 319 printf( "// | | / /\\ \\ | | //\r\n");
j3 0:1db98cbf6150 320 printf( "// |_| /_/ \\_\\ |_| //\r\n");
j3 0:1db98cbf6150 321 printf( "// //\r\n");
j3 0:1db98cbf6150 322 printf( "///////////////////////////////////////////////////////////////////\r\n");
j3 0:1db98cbf6150 323 printf("\r\n");
j3 0:1db98cbf6150 324 }
j3 0:1db98cbf6150 325
j3 0:1db98cbf6150 326
j3 0:1db98cbf6150 327 void clear_screen(void)
j3 0:1db98cbf6150 328 {
j3 0:1db98cbf6150 329 printf("%c[2J", 0x1B); //clear screen
j3 0:1db98cbf6150 330 printf("%c[H", 0x1B); //move cursor to Home
j3 0:1db98cbf6150 331 }
j3 0:1db98cbf6150 332