Firmware for MC33926 evaluation on KL25Z-based EVB. This works with a Graphical User Interface (GUI) available on NXP.com to control a brushed DC motor using FRDM-33926ESEVM or FRDM-33926PNBEVM. The code enables control of the PWM frequency, duty cycle, enable/disable controls, invert, slew rate control, real-time current monitoring, and includes status flag pin monitoring for undervoltage, short circuit and over-temperature events.
Fork of Brushed_DC_Motor_Control_MC33926 by
Diff: main.cpp
- Revision:
- 3:d7fd752817e1
- Parent:
- 2:d98eb31a4b69
diff -r d98eb31a4b69 -r d7fd752817e1 main.cpp --- a/main.cpp Mon Jul 09 17:53:48 2018 +0000 +++ b/main.cpp Mon Jul 09 19:23:33 2018 +0000 @@ -8,7 +8,7 @@ // Serial Number: 0x0001 USBHID hid(64, 64, 0x15A2, 0x0138, 0x0001, true); -//Setup Digital Outputs for the LEDs on the FRDM +// Setup Digital Outputs for the LEDs on the FRDM //PwmOut red_led(LED1); //DigitalOut green_led(LED2); //DigitalOut blue_led(LED3); @@ -25,13 +25,13 @@ AnalogIn CFB(PTB0); // Pin FB output from MC33926 to FRDM-KL25Z //DigitalOut READY(PTC7); // Pin READY input to Motor Control Board (FRDM PIN Name) -//Variables +// Variables int pwm_freq_lo; int pwm_freq_hi; int frequencyHz = 500; int runstop = 0; int direction = 1; -int braking; // needs to be initialized? +int braking; // does not require initialization int dutycycle = 75; int newDataFlag = 0; int status = 0; @@ -41,7 +41,7 @@ uint16_t CFBAvg; uint16_t CFBtemp; -//storage for send and receive data +// Storage for send and receive data HID_REPORT send_report; HID_REPORT recv_report; @@ -50,13 +50,13 @@ // USB COMMANDS // These are sent from the PC #define WRITE_LED 0x20 -#define WRITE_GEN_EN 0x40 // what is this? What should the GUI kickoff here? +#define WRITE_GEN_EN 0x40 #define WRITE_DUTY_CYCLE 0x50 #define WRITE_PWM_FREQ 0x60 #define WRITE_RUN_STOP 0x70 #define WRITE_DIRECTION 0x71 #define WRITE_BRAKING 0x90 -#define WRITE_RESET 0xA0 // what is this? What should the GUI kickoff here? +#define WRITE_RESET 0xA0 #define WRITE_D1 0xB1 #define WRITE_EN 0xC1 #define WRITE_D2B 0xD5 @@ -108,38 +108,38 @@ newDataFlag = 1; if(recv_report.data[1] != 0) { - runstop = 1; + runstop = 1; // outputs on, run motor } else { - runstop = 0; + runstop = 0; // outputs off, brake motor } break; //////// case WRITE_DIRECTION: newDataFlag = 1; - if(recv_report.data[1] == 1) // used to be != 0 + if(recv_report.data[1] == 1) { direction = 1; // corrected allocation for FWD - //direction = 0; // corrected allocation for REV + //direction = 0; } else { direction = 0; // corrected allocation for REV - //direction = 1; // corrected allocation for FWD + //direction = 1; } break; //////// case WRITE_BRAKING: newDataFlag = 1; - if(recv_report.data[1] != 0) // used to be == 1 + if(recv_report.data[1] != 0) { - braking = 1; // this is HS recirc + braking = 1; // this is HS recirc } else { - braking = 0; // this is LS recirc + braking = 0; // this is LS recirc } break; //////// @@ -147,11 +147,11 @@ newDataFlag = 1; if(recv_report.data[1] == 1) { - DIS1 = 1; // logic hi will disable the part + DIS1 = 1; // logic hi disables the part } else { - DIS1 = 0; // logic lo will enable the part + DIS1 = 0; // logic lo enables the part } break; //////// @@ -159,13 +159,13 @@ newDataFlag = 1; if(recv_report.data[1] == 1) { - //EN = 0; // this is enable case - EN = 1; // align with the signal being sent from GUI + //EN = 0; + EN = 1; // logic hi enables the part } else { - //EN = 1; // this is disable case - EN = 0; // align with signal being sent from GUI + //EN = 1; + EN = 0; // logic lo disable the part } break; //////// @@ -173,11 +173,11 @@ newDataFlag = 1; if(recv_report.data[1] == 1) { - D2B = 1; + D2B = 1; // logic hi enables the part } else { - D2B = 0; + D2B = 0; // logic lo disables the part } break; @@ -186,11 +186,11 @@ newDataFlag = 1; if(recv_report.data[1] == 1) { - SLEW = 1; + SLEW = 1; // logic hi prompts FAST slew } else { - SLEW = 0; + SLEW = 0; // logic lo prompts SLOW slew } break; @@ -199,12 +199,12 @@ newDataFlag = 1; if(recv_report.data[1] == 1) { - INV = 1; + INV = 1; // logic hi inverts operation //INV = 0; } else { - INV = 0; + INV = 0; // logic lo does not invert operation //INV = 1; } break; @@ -262,15 +262,15 @@ if(braking == 1) //dynamic { IN1.period(1/(float)frequencyHz); - //IN1 = (float)dutycycle/100.0; // this is REV + HS, inverted d - IN1 = 1.0-((float)dutycycle/100.0); // this is REV + HS + //IN1 = (float)dutycycle/100.0; // REV + HS, inverted d + IN1 = 1.0-((float)dutycycle/100.0); // REV + HS IN2 = 1; } else //coast { - IN1 = 0; // this is REV + LS + IN1 = 0; IN2.period(1/(float)frequencyHz); - IN2 = (float)dutycycle/100.0; + IN2 = (float)dutycycle/100.0; // REV + LS } } else //forward @@ -279,8 +279,8 @@ { IN1 = 1; IN2.period(1/(float)frequencyHz); - //IN2 = (float)dutycycle/100.0; // this is FWD + HS, inverted d - IN2 = 1.0-((float)dutycycle/100.0); // this is FWD + HS + //IN2 = (float)dutycycle/100.0; // FWD + HS, inverted d + IN2 = 1.0-((float)dutycycle/100.0); // FWD + HS } else //coast { @@ -290,16 +290,16 @@ } } } - else //Stopped + else //stopped { - if(braking == 1) //braking + if(braking == 1) // braking, HS recirc { IN1.period(1); IN2.period(1); IN1.write(1); IN2.write(1); } - else //coasting + else // coasting, LS recirc { IN1.period(1); IN2.period(1); @@ -311,4 +311,3 @@ } } } -