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