Contains code to drive a small brushed DC motor with a Freescale FRDM-17510 MPC17510 H-bridge driver evaluation board, and a Freedom FRDM-KL25Z. Configures the KL25Z as USB HID device and requres a GUI on the PC.

Dependencies:   USBDevice mbed

Fork of LVHB DC Motor Drive by Freescale

This program uses a Freescale FRDM-KL25 and FRDM-17510 Motor control board. It configures the KL25 as a USB HID device and is intended to work with a GUI which is downloaded from Freescale. (where is the link to this GUI?)

There is a list of Analog Tools that mention mbed software support here: http://www.freescale.com/webapp/sps/site/overview.jsp?code=ANALOGTOOLBOX&tid=vanAnalogTools

Committer:
bdbohn
Date:
Wed Nov 12 19:25:24 2014 +0000
Revision:
0:41c74fead7a9
First Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bdbohn 0:41c74fead7a9 1 #include "mbed.h"
bdbohn 0:41c74fead7a9 2 #include "USBHID.h"
bdbohn 0:41c74fead7a9 3
bdbohn 0:41c74fead7a9 4 // We declare a USBHID device.
bdbohn 0:41c74fead7a9 5 // HID In/Out Reports are 64 Bytes long
bdbohn 0:41c74fead7a9 6 // Vendor ID (VID): 0x15A2
bdbohn 0:41c74fead7a9 7 // Product ID (PID): 0x0138
bdbohn 0:41c74fead7a9 8 // Serial Number: 0x0001
bdbohn 0:41c74fead7a9 9 USBHID hid(64, 64, 0x15A2, 0x0138, 0x0001, true);
bdbohn 0:41c74fead7a9 10
bdbohn 0:41c74fead7a9 11 //Setup Digital Outputs for the LEDs on the FRDM
bdbohn 0:41c74fead7a9 12 DigitalOut red_led(LED1);
bdbohn 0:41c74fead7a9 13 DigitalOut green_led(LED2);
bdbohn 0:41c74fead7a9 14 DigitalOut blue_led(LED3);
bdbohn 0:41c74fead7a9 15
bdbohn 0:41c74fead7a9 16 //Setup PWM and Digital Outputs from FRDM-KL25Z to FRDM-17510
bdbohn 0:41c74fead7a9 17 PwmOut IN1(PTD4); // Pin IN1 input to MPC17510 (FRDM PIN Name)
bdbohn 0:41c74fead7a9 18 PwmOut IN2(PTA12); // Pin IN2 input to MPC17510 (FRDM PIN Name)
bdbohn 0:41c74fead7a9 19 DigitalOut EN(PTC7); // Pin EN input to MPC17510 (FRDM PIN Name)
bdbohn 0:41c74fead7a9 20 DigitalOut GINB(PTC0); // Pin GIN_B input to MPC17510 (FRDM PIN Name)
bdbohn 0:41c74fead7a9 21 DigitalOut READY(PTC5); // Pin READY input to Motor Control Board (FRDM PIN Name)
bdbohn 0:41c74fead7a9 22
bdbohn 0:41c74fead7a9 23 //Variables
bdbohn 0:41c74fead7a9 24 int pwm_freq_lo;
bdbohn 0:41c74fead7a9 25 int pwm_freq_hi;
bdbohn 0:41c74fead7a9 26 int frequencyHz;
bdbohn 0:41c74fead7a9 27 int storedFreq;
bdbohn 0:41c74fead7a9 28 int storedDuty;
bdbohn 0:41c74fead7a9 29 int runstop = 0;
bdbohn 0:41c74fead7a9 30 int runStopChange;
bdbohn 0:41c74fead7a9 31 int direction = 1;
bdbohn 0:41c74fead7a9 32 int directionChange;
bdbohn 0:41c74fead7a9 33 int braking;
bdbohn 0:41c74fead7a9 34 int brakingChange;
bdbohn 0:41c74fead7a9 35 int dutycycle;
bdbohn 0:41c74fead7a9 36 int motorState = 0;
bdbohn 0:41c74fead7a9 37 int ramptime = 0;
bdbohn 0:41c74fead7a9 38
bdbohn 0:41c74fead7a9 39 //storage for send and receive data
bdbohn 0:41c74fead7a9 40 HID_REPORT send_report;
bdbohn 0:41c74fead7a9 41 HID_REPORT recv_report;
bdbohn 0:41c74fead7a9 42
bdbohn 0:41c74fead7a9 43 bool initflag = true;
bdbohn 0:41c74fead7a9 44
bdbohn 0:41c74fead7a9 45 // USB COMMANDS
bdbohn 0:41c74fead7a9 46 // These are sent from the PC
bdbohn 0:41c74fead7a9 47 #define WRITE_LED 0x20
bdbohn 0:41c74fead7a9 48 #define WRITE_GEN_EN 0x40
bdbohn 0:41c74fead7a9 49 #define WRITE_DUTY_CYCLE 0x50
bdbohn 0:41c74fead7a9 50 #define WRITE_PWM_FREQ 0x60
bdbohn 0:41c74fead7a9 51 #define WRITE_RUN_STOP 0x70
bdbohn 0:41c74fead7a9 52 #define WRITE_DIRECTION 0x71
bdbohn 0:41c74fead7a9 53 #define WRITE_BRAKING 0x90
bdbohn 0:41c74fead7a9 54 #define WRITE_RESET 0xA0
bdbohn 0:41c74fead7a9 55
bdbohn 0:41c74fead7a9 56
bdbohn 0:41c74fead7a9 57 // MOTOR STATES
bdbohn 0:41c74fead7a9 58 #define STOP 0x00
bdbohn 0:41c74fead7a9 59 #define RUN 0x02
bdbohn 0:41c74fead7a9 60 #define RESET 0x05
bdbohn 0:41c74fead7a9 61
bdbohn 0:41c74fead7a9 62 // LED CONSTANTS
bdbohn 0:41c74fead7a9 63 #define LEDS_OFF 0x00
bdbohn 0:41c74fead7a9 64 #define RED 0x01
bdbohn 0:41c74fead7a9 65 #define GREEN 0x02
bdbohn 0:41c74fead7a9 66 #define BLUE 0x03
bdbohn 0:41c74fead7a9 67 #define READY_LED 0x04
bdbohn 0:41c74fead7a9 68
bdbohn 0:41c74fead7a9 69 // LOGICAL CONSTANTS
bdbohn 0:41c74fead7a9 70 #define OFF 0x00
bdbohn 0:41c74fead7a9 71 #define ON 0x01
bdbohn 0:41c74fead7a9 72
bdbohn 0:41c74fead7a9 73 //Functions
bdbohn 0:41c74fead7a9 74 void set_PWM_Freq(int freq);
bdbohn 0:41c74fead7a9 75 void set_Duty_Cycle(int dutycycle);
bdbohn 0:41c74fead7a9 76
bdbohn 0:41c74fead7a9 77
bdbohn 0:41c74fead7a9 78 int main()
bdbohn 0:41c74fead7a9 79 {
bdbohn 0:41c74fead7a9 80 send_report.length = 64;
bdbohn 0:41c74fead7a9 81 recv_report.length = 64;
bdbohn 0:41c74fead7a9 82
bdbohn 0:41c74fead7a9 83 red_led = 1; // Red LED = OFF
bdbohn 0:41c74fead7a9 84 green_led = 1; // Green LED = OFF
bdbohn 0:41c74fead7a9 85 blue_led = 0; // Blue LED = ON
bdbohn 0:41c74fead7a9 86
bdbohn 0:41c74fead7a9 87 motorState = RESET;
bdbohn 0:41c74fead7a9 88 initflag = true;
bdbohn 0:41c74fead7a9 89
bdbohn 0:41c74fead7a9 90 IN1.period(1); //initially set period to 1 second
bdbohn 0:41c74fead7a9 91 IN2.period(1);
bdbohn 0:41c74fead7a9 92
bdbohn 0:41c74fead7a9 93 IN1.write(0); //set PWM percentage to 0
bdbohn 0:41c74fead7a9 94 IN2.write(0);
bdbohn 0:41c74fead7a9 95
bdbohn 0:41c74fead7a9 96
bdbohn 0:41c74fead7a9 97 while(1)
bdbohn 0:41c74fead7a9 98 {
bdbohn 0:41c74fead7a9 99 //try to read a msg
bdbohn 0:41c74fead7a9 100 if(hid.readNB(&recv_report))
bdbohn 0:41c74fead7a9 101 {
bdbohn 0:41c74fead7a9 102 if(initflag == true)
bdbohn 0:41c74fead7a9 103 {
bdbohn 0:41c74fead7a9 104 initflag = false;
bdbohn 0:41c74fead7a9 105 blue_led = 1; //turn off blue LED
bdbohn 0:41c74fead7a9 106 }
bdbohn 0:41c74fead7a9 107 switch(recv_report.data[0]) //byte 0 of recv_report.data is command
bdbohn 0:41c74fead7a9 108 {
bdbohn 0:41c74fead7a9 109 //-----------------------------------------------------------------------------------------------------------------
bdbohn 0:41c74fead7a9 110 // COMMAND PARSER
bdbohn 0:41c74fead7a9 111 //-----------------------------------------------------------------------------------------------------------------
bdbohn 0:41c74fead7a9 112 ////////
bdbohn 0:41c74fead7a9 113 case WRITE_LED:
bdbohn 0:41c74fead7a9 114 switch(recv_report.data[1])
bdbohn 0:41c74fead7a9 115 {
bdbohn 0:41c74fead7a9 116 case LEDS_OFF:
bdbohn 0:41c74fead7a9 117 red_led = 1;
bdbohn 0:41c74fead7a9 118 green_led = 1;
bdbohn 0:41c74fead7a9 119 blue_led = 1;
bdbohn 0:41c74fead7a9 120 break;
bdbohn 0:41c74fead7a9 121 case RED:
bdbohn 0:41c74fead7a9 122 if(recv_report.data[2] == 1){red_led = 0;} else {red_led = 1;}
bdbohn 0:41c74fead7a9 123 break;
bdbohn 0:41c74fead7a9 124 case GREEN:
bdbohn 0:41c74fead7a9 125 if(recv_report.data[2] == 1){green_led = 0;} else {green_led = 1;}
bdbohn 0:41c74fead7a9 126 break;
bdbohn 0:41c74fead7a9 127 case BLUE:
bdbohn 0:41c74fead7a9 128 if(recv_report.data[2] == 1){blue_led = 0;} else {blue_led = 1;}
bdbohn 0:41c74fead7a9 129 break;
bdbohn 0:41c74fead7a9 130 default:
bdbohn 0:41c74fead7a9 131 break;
bdbohn 0:41c74fead7a9 132
bdbohn 0:41c74fead7a9 133
bdbohn 0:41c74fead7a9 134 }// End recv report data[1]
bdbohn 0:41c74fead7a9 135 break;
bdbohn 0:41c74fead7a9 136 ////////
bdbohn 0:41c74fead7a9 137
bdbohn 0:41c74fead7a9 138 ////////
bdbohn 0:41c74fead7a9 139 case WRITE_DUTY_CYCLE:
bdbohn 0:41c74fead7a9 140 dutycycle = recv_report.data[1];
bdbohn 0:41c74fead7a9 141 set_Duty_Cycle(dutycycle);
bdbohn 0:41c74fead7a9 142 break;
bdbohn 0:41c74fead7a9 143 ////////
bdbohn 0:41c74fead7a9 144 case WRITE_PWM_FREQ: //PWM frequency can be larger than 1 byte
bdbohn 0:41c74fead7a9 145 pwm_freq_lo = recv_report.data[1]; //so we have to re-assemble the number
bdbohn 0:41c74fead7a9 146 pwm_freq_hi = recv_report.data[2] * 100;
bdbohn 0:41c74fead7a9 147 frequencyHz = pwm_freq_lo + pwm_freq_hi;
bdbohn 0:41c74fead7a9 148 set_PWM_Freq(frequencyHz);
bdbohn 0:41c74fead7a9 149 break;
bdbohn 0:41c74fead7a9 150 ////////
bdbohn 0:41c74fead7a9 151 case WRITE_RUN_STOP:
bdbohn 0:41c74fead7a9 152 if(recv_report.data[1] == 1)
bdbohn 0:41c74fead7a9 153 {
bdbohn 0:41c74fead7a9 154 if(runstop != 1)
bdbohn 0:41c74fead7a9 155 {
bdbohn 0:41c74fead7a9 156 red_led = 1;
bdbohn 0:41c74fead7a9 157 blue_led = 1;
bdbohn 0:41c74fead7a9 158 green_led = 0;
bdbohn 0:41c74fead7a9 159 READY = 1;
bdbohn 0:41c74fead7a9 160 runstop = 1;
bdbohn 0:41c74fead7a9 161 runStopChange = 1;
bdbohn 0:41c74fead7a9 162 motorState = RUN;
bdbohn 0:41c74fead7a9 163 frequencyHz = storedFreq;
bdbohn 0:41c74fead7a9 164 set_PWM_Freq(frequencyHz);
bdbohn 0:41c74fead7a9 165 dutycycle = storedDuty;
bdbohn 0:41c74fead7a9 166 set_Duty_Cycle(dutycycle);
bdbohn 0:41c74fead7a9 167 }
bdbohn 0:41c74fead7a9 168
bdbohn 0:41c74fead7a9 169 }
bdbohn 0:41c74fead7a9 170 else
bdbohn 0:41c74fead7a9 171 {
bdbohn 0:41c74fead7a9 172 if(runstop != 0)
bdbohn 0:41c74fead7a9 173 {
bdbohn 0:41c74fead7a9 174 runstop = 0;
bdbohn 0:41c74fead7a9 175 runStopChange = 1;
bdbohn 0:41c74fead7a9 176 motorState = STOP;
bdbohn 0:41c74fead7a9 177 red_led = 0;
bdbohn 0:41c74fead7a9 178 green_led = 1;
bdbohn 0:41c74fead7a9 179 blue_led = 1;
bdbohn 0:41c74fead7a9 180 storedFreq = frequencyHz;
bdbohn 0:41c74fead7a9 181 storedDuty = dutycycle;
bdbohn 0:41c74fead7a9 182 READY = 0;
bdbohn 0:41c74fead7a9 183 }
bdbohn 0:41c74fead7a9 184 }
bdbohn 0:41c74fead7a9 185 break;
bdbohn 0:41c74fead7a9 186 ////////
bdbohn 0:41c74fead7a9 187 case WRITE_DIRECTION:
bdbohn 0:41c74fead7a9 188
bdbohn 0:41c74fead7a9 189
bdbohn 0:41c74fead7a9 190 if(recv_report.data[1] == 1)
bdbohn 0:41c74fead7a9 191 {
bdbohn 0:41c74fead7a9 192 if(direction != 1)
bdbohn 0:41c74fead7a9 193 {
bdbohn 0:41c74fead7a9 194 direction = 1;
bdbohn 0:41c74fead7a9 195 directionChange = 1;
bdbohn 0:41c74fead7a9 196 }
bdbohn 0:41c74fead7a9 197 }
bdbohn 0:41c74fead7a9 198 else
bdbohn 0:41c74fead7a9 199 {
bdbohn 0:41c74fead7a9 200 if(direction != 0)
bdbohn 0:41c74fead7a9 201 {
bdbohn 0:41c74fead7a9 202 direction = 0;
bdbohn 0:41c74fead7a9 203 directionChange = 1;
bdbohn 0:41c74fead7a9 204 }
bdbohn 0:41c74fead7a9 205 }
bdbohn 0:41c74fead7a9 206 break;
bdbohn 0:41c74fead7a9 207 ////////
bdbohn 0:41c74fead7a9 208 case WRITE_BRAKING:
bdbohn 0:41c74fead7a9 209 if(recv_report.data[1] == 1)
bdbohn 0:41c74fead7a9 210 {
bdbohn 0:41c74fead7a9 211 braking = 1;
bdbohn 0:41c74fead7a9 212 }
bdbohn 0:41c74fead7a9 213 else
bdbohn 0:41c74fead7a9 214 {
bdbohn 0:41c74fead7a9 215 braking = 0;
bdbohn 0:41c74fead7a9 216 }
bdbohn 0:41c74fead7a9 217 break;
bdbohn 0:41c74fead7a9 218 ////////
bdbohn 0:41c74fead7a9 219 default:
bdbohn 0:41c74fead7a9 220 break;
bdbohn 0:41c74fead7a9 221 }// End Switch recv report data[0]
bdbohn 0:41c74fead7a9 222
bdbohn 0:41c74fead7a9 223 //-----------------------------------------------------------------------------------------------------------------
bdbohn 0:41c74fead7a9 224 // end command parser
bdbohn 0:41c74fead7a9 225 //-----------------------------------------------------------------------------------------------------------------
bdbohn 0:41c74fead7a9 226
bdbohn 0:41c74fead7a9 227 send_report.data[0] = recv_report.data[0]; // Echo Command
bdbohn 0:41c74fead7a9 228 send_report.data[1] = recv_report.data[1]; // Echo Subcommand 1
bdbohn 0:41c74fead7a9 229 send_report.data[2] = recv_report.data[2]; // Echo Subcommand 2
bdbohn 0:41c74fead7a9 230 send_report.data[3] = 0x00;
bdbohn 0:41c74fead7a9 231 send_report.data[4] = 0x00;
bdbohn 0:41c74fead7a9 232 send_report.data[5] = 0x00;
bdbohn 0:41c74fead7a9 233 send_report.data[6] = 0x00;
bdbohn 0:41c74fead7a9 234 send_report.data[7] = 0x00;
bdbohn 0:41c74fead7a9 235
bdbohn 0:41c74fead7a9 236 //Send the report
bdbohn 0:41c74fead7a9 237 hid.send(&send_report);
bdbohn 0:41c74fead7a9 238 }// End If(hid.readNB(&recv_report))
bdbohn 0:41c74fead7a9 239 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bdbohn 0:41c74fead7a9 240 //End of USB message handling
bdbohn 0:41c74fead7a9 241 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bdbohn 0:41c74fead7a9 242
bdbohn 0:41c74fead7a9 243 //************************************************************************************************************************
bdbohn 0:41c74fead7a9 244 // This is handling of PWM, braking, direction
bdbohn 0:41c74fead7a9 245 //***********************************************************************************************************************/
bdbohn 0:41c74fead7a9 246
bdbohn 0:41c74fead7a9 247 switch (motorState)
bdbohn 0:41c74fead7a9 248 {
bdbohn 0:41c74fead7a9 249 case STOP:
bdbohn 0:41c74fead7a9 250 READY = 0;
bdbohn 0:41c74fead7a9 251
bdbohn 0:41c74fead7a9 252 if(braking == 1)
bdbohn 0:41c74fead7a9 253 {
bdbohn 0:41c74fead7a9 254 EN = 0;
bdbohn 0:41c74fead7a9 255 IN1.period(1);
bdbohn 0:41c74fead7a9 256 IN2.period(1);
bdbohn 0:41c74fead7a9 257 IN1.write(0);
bdbohn 0:41c74fead7a9 258 IN2.write(0);
bdbohn 0:41c74fead7a9 259 }
bdbohn 0:41c74fead7a9 260 else
bdbohn 0:41c74fead7a9 261 {
bdbohn 0:41c74fead7a9 262 EN = 0;
bdbohn 0:41c74fead7a9 263 IN1.period(1);
bdbohn 0:41c74fead7a9 264 IN2.period(1);
bdbohn 0:41c74fead7a9 265 IN1.write(0);
bdbohn 0:41c74fead7a9 266 IN2.write(0);
bdbohn 0:41c74fead7a9 267 EN = 1;
bdbohn 0:41c74fead7a9 268 }
bdbohn 0:41c74fead7a9 269 break;
bdbohn 0:41c74fead7a9 270
bdbohn 0:41c74fead7a9 271 case RUN:
bdbohn 0:41c74fead7a9 272 if(runStopChange != 0)
bdbohn 0:41c74fead7a9 273 {
bdbohn 0:41c74fead7a9 274 READY = 1;
bdbohn 0:41c74fead7a9 275 EN = 1;
bdbohn 0:41c74fead7a9 276 directionChange = 0;
bdbohn 0:41c74fead7a9 277 runStopChange = 0;
bdbohn 0:41c74fead7a9 278 set_PWM_Freq(frequencyHz);
bdbohn 0:41c74fead7a9 279 set_Duty_Cycle(dutycycle);
bdbohn 0:41c74fead7a9 280 }
bdbohn 0:41c74fead7a9 281 break;
bdbohn 0:41c74fead7a9 282
bdbohn 0:41c74fead7a9 283 case RESET:
bdbohn 0:41c74fead7a9 284 IN1.write(0.0);
bdbohn 0:41c74fead7a9 285 IN2.write(0.0);
bdbohn 0:41c74fead7a9 286 IN1.period(0.0);
bdbohn 0:41c74fead7a9 287 IN2.period(0.0);
bdbohn 0:41c74fead7a9 288 runStopChange = false;
bdbohn 0:41c74fead7a9 289 motorState = STOP;
bdbohn 0:41c74fead7a9 290 break;
bdbohn 0:41c74fead7a9 291
bdbohn 0:41c74fead7a9 292 }// end switch motorState
bdbohn 0:41c74fead7a9 293 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bdbohn 0:41c74fead7a9 294 }// End While Loop
bdbohn 0:41c74fead7a9 295 }// End Main
bdbohn 0:41c74fead7a9 296
bdbohn 0:41c74fead7a9 297
bdbohn 0:41c74fead7a9 298 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bdbohn 0:41c74fead7a9 299 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bdbohn 0:41c74fead7a9 300
bdbohn 0:41c74fead7a9 301 void set_PWM_Freq(int freq)
bdbohn 0:41c74fead7a9 302 {
bdbohn 0:41c74fead7a9 303 storedFreq = freq;
bdbohn 0:41c74fead7a9 304 float period = 1.0/freq;
bdbohn 0:41c74fead7a9 305
bdbohn 0:41c74fead7a9 306 if(direction == 1)
bdbohn 0:41c74fead7a9 307 {
bdbohn 0:41c74fead7a9 308 IN1.period(period);
bdbohn 0:41c74fead7a9 309 IN2.period(0.0);
bdbohn 0:41c74fead7a9 310 }
bdbohn 0:41c74fead7a9 311 else
bdbohn 0:41c74fead7a9 312 {
bdbohn 0:41c74fead7a9 313 IN1.period(0.0);
bdbohn 0:41c74fead7a9 314 IN2.period(period);
bdbohn 0:41c74fead7a9 315 }
bdbohn 0:41c74fead7a9 316 }
bdbohn 0:41c74fead7a9 317
bdbohn 0:41c74fead7a9 318 void set_Duty_Cycle(int dc)
bdbohn 0:41c74fead7a9 319 {
bdbohn 0:41c74fead7a9 320 storedDuty = dc;
bdbohn 0:41c74fead7a9 321 if(direction == 1)
bdbohn 0:41c74fead7a9 322 {
bdbohn 0:41c74fead7a9 323 red_led = 0;
bdbohn 0:41c74fead7a9 324 green_led = 1;
bdbohn 0:41c74fead7a9 325 IN1.write(float(dc/100.0));
bdbohn 0:41c74fead7a9 326 IN2.write(0);
bdbohn 0:41c74fead7a9 327 }
bdbohn 0:41c74fead7a9 328 else
bdbohn 0:41c74fead7a9 329 {
bdbohn 0:41c74fead7a9 330 red_led = 1;
bdbohn 0:41c74fead7a9 331 green_led = 0;
bdbohn 0:41c74fead7a9 332 IN2.write(float(dc/100.00));
bdbohn 0:41c74fead7a9 333 IN1.write(0);
bdbohn 0:41c74fead7a9 334 }
bdbohn 0:41c74fead7a9 335 }