19E042PIM DC motor

Dependencies:   mbed

Committer:
tzwell
Date:
Tue Jan 04 14:36:43 2022 +0000
Revision:
0:9e09213d299d
First publish, first commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tzwell 0:9e09213d299d 1 /*
tzwell 0:9e09213d299d 2 * An example of DC motor control, using NUCLEO-L476RG.
tzwell 0:9e09213d299d 3 *
tzwell 0:9e09213d299d 4 * Katedra za Elektroniku i digitalne sisteme
tzwell 0:9e09213d299d 5 * Elektrotehnicki fakultet
tzwell 0:9e09213d299d 6 * Beograd
tzwell 0:9e09213d299d 7 *
tzwell 0:9e09213d299d 8 * Decembar 2021.
tzwell 0:9e09213d299d 9 *
tzwell 0:9e09213d299d 10 */
tzwell 0:9e09213d299d 11
tzwell 0:9e09213d299d 12 /*
tzwell 0:9e09213d299d 13 * Biblioteke za uvoz:
tzwell 0:9e09213d299d 14 */
tzwell 0:9e09213d299d 15 #include "mbed.h"
tzwell 0:9e09213d299d 16 #include "mb_pins.h"
tzwell 0:9e09213d299d 17
tzwell 0:9e09213d299d 18 /*
tzwell 0:9e09213d299d 19 * Definisanje makroa:
tzwell 0:9e09213d299d 20 */
tzwell 0:9e09213d299d 21 #define DEBOUNCE_DELAY_MS 50
tzwell 0:9e09213d299d 22 #define MOTOR_PERIOD_MS 100
tzwell 0:9e09213d299d 23
tzwell 0:9e09213d299d 24 /*
tzwell 0:9e09213d299d 25 * Globalne promenljive:
tzwell 0:9e09213d299d 26 */
tzwell 0:9e09213d299d 27 InterruptIn motor_ctrl (MB_SW1);
tzwell 0:9e09213d299d 28 AnalogOut motor_out(PA_5); //SCK/D13
tzwell 0:9e09213d299d 29 AnalogIn pot(MB_POT1);
tzwell 0:9e09213d299d 30 // spin = 1 motor spins, spin = 0 motor stops:
tzwell 0:9e09213d299d 31 char spin = 0;
tzwell 0:9e09213d299d 32
tzwell 0:9e09213d299d 33 /*
tzwell 0:9e09213d299d 34 * Deklaracija funkcija:
tzwell 0:9e09213d299d 35 */
tzwell 0:9e09213d299d 36 void ISR_ctrl(void);
tzwell 0:9e09213d299d 37
tzwell 0:9e09213d299d 38
tzwell 0:9e09213d299d 39 /*
tzwell 0:9e09213d299d 40 * Glavna funkcija:
tzwell 0:9e09213d299d 41 */
tzwell 0:9e09213d299d 42 int main()
tzwell 0:9e09213d299d 43 {
tzwell 0:9e09213d299d 44 motor_ctrl.fall(&ISR_ctrl);
tzwell 0:9e09213d299d 45 while(1)
tzwell 0:9e09213d299d 46 {
tzwell 0:9e09213d299d 47 if (spin)
tzwell 0:9e09213d299d 48 {
tzwell 0:9e09213d299d 49 motor_out.write(pot.read());
tzwell 0:9e09213d299d 50 }
tzwell 0:9e09213d299d 51 else
tzwell 0:9e09213d299d 52 {
tzwell 0:9e09213d299d 53 motor_out.write(0);
tzwell 0:9e09213d299d 54 }
tzwell 0:9e09213d299d 55 wait_ms(MOTOR_PERIOD_MS);
tzwell 0:9e09213d299d 56 }
tzwell 0:9e09213d299d 57 }
tzwell 0:9e09213d299d 58 /*
tzwell 0:9e09213d299d 59 * Definicija funkcija:
tzwell 0:9e09213d299d 60 */
tzwell 0:9e09213d299d 61 void ISR_ctrl(void){
tzwell 0:9e09213d299d 62 if(!motor_ctrl.read())
tzwell 0:9e09213d299d 63 {
tzwell 0:9e09213d299d 64 wait_ms(DEBOUNCE_DELAY_MS);
tzwell 0:9e09213d299d 65 if (!motor_ctrl.read()) spin = !spin;
tzwell 0:9e09213d299d 66 }
tzwell 0:9e09213d299d 67 }