19E042PIM Servomotor SG90

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
tzwell
Date:
Tue Jan 04 14:34:40 2022 +0000
Commit message:
First publish, first commit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mb_pins.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jan 04 14:34:40 2022 +0000
@@ -0,0 +1,107 @@
+/*
+ * An example of motor control, using step SG90 and NUCLEO-L476RG.
+ *
+ * Katedra za Elektroniku i digitalne sisteme
+ * Elektrotehnicki fakultet
+ * Beograd 
+ *
+ * Decembar 2021.
+ *
+ */
+
+/*
+ * Biblioteke za uvoz:
+ */
+#include "mbed.h"
+#include "mb_pins.h"
+
+/*
+ * Definisanje makroa:
+ */
+#define DEBOUNCE_DELAY_MS 50
+#define MOTOR_PERIOD_MS 20
+#define MOTOR_PULSE_INIT_MS 1
+#define MOTOR_PULSE_INC_US 50   // 4.5 degrees increment
+#define MAX_PULSE_US 3000
+#define MIN_PULSE_US 500
+
+ 
+/*
+ * Globalne promenljive:
+ */
+PwmOut motor_out(PB_0);  // orange -- A3 pin
+InterruptIn clkwise (MB_SW1);
+InterruptIn cntclkwise (MB_SW2);
+
+int clkwise_dir = 0;
+int cntclkwise_dir = 0;
+  
+/*
+ * Deklaracija funkcija:
+ */
+void ISR_clkwise(void);
+void ISR_cntclkwise(void);
+
+/*
+ * Glavna funkcija:
+ */
+
+int main()
+{
+    clkwise.fall(&ISR_clkwise);
+    cntclkwise.fall(&ISR_cntclkwise);
+    
+    motor_out.period_ms(MOTOR_PERIOD_MS);
+    motor_out.pulsewidth_ms(MOTOR_PULSE_INIT_MS);
+    
+    int pulsewidth = 0;
+    while(1)
+    {
+        if (clkwise_dir)
+        {
+            pulsewidth = motor_out.read()*MOTOR_PERIOD_MS*1000;
+            printf("Pulsewidth when SW1 pressed: %d us\r\n", pulsewidth);
+            if (pulsewidth < MAX_PULSE_US - MOTOR_PULSE_INC_US)
+            {
+                pulsewidth = pulsewidth + MOTOR_PULSE_INC_US;
+                printf("Pulsewidth after SW1 increment: %d us\r\n", pulsewidth);
+                motor_out.pulsewidth_us(pulsewidth);
+            }
+            clkwise_dir = 0;
+            //wait_ms(DEBOUNCE_DELAY_MS);
+        }
+        else if(cntclkwise_dir)
+        {
+            pulsewidth = motor_out.read()*MOTOR_PERIOD_MS*1000;
+            printf("Pulsewidth when SW2 pressed: %d us\r\n", pulsewidth);
+            if (pulsewidth > MIN_PULSE_US + MOTOR_PULSE_INC_US)
+            {
+                pulsewidth = pulsewidth - MOTOR_PULSE_INC_US;
+                printf("Pulsewidth after SW1 increment: %d us\r\n", pulsewidth);
+                motor_out.pulsewidth_us(pulsewidth);
+            }
+            cntclkwise_dir = 0;
+            //wait_ms(DEBOUNCE_DELAY_MS);
+        }
+    }
+}
+
+
+/*
+ * Definicija funkcija:
+ */
+void ISR_clkwise(void){
+    if(!clkwise.read())
+    {
+        wait_ms(DEBOUNCE_DELAY_MS);
+        if (!clkwise.read()) clkwise_dir = 1;
+    }
+}
+
+void ISR_cntclkwise(void){
+    if(!cntclkwise.read())
+    {
+        wait_ms(DEBOUNCE_DELAY_MS);
+        if (!cntclkwise.read()) cntclkwise_dir = 1;
+    }     
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mb_pins.h	Tue Jan 04 14:34:40 2022 +0000
@@ -0,0 +1,59 @@
+/*
+ * This file contains all the pin names neccessary
+ * for easy interaction with the EDS-MB for NUCLEOL476RG.
+ *
+ * University of Belgrade - School of Electrical Engineering
+ * Department of Electronics
+ * Bulevar Kralja Aleksandra 73, 11120 Belgrade, Serbia
+ *
+ * November 2021.
+ *
+ */
+
+// LEDs, 560R in series, cathode to GND; left - red LED1;  right - blue LED2:
+#define MB_LED1                                                            PA_11
+#define MB_LED2                                                            PB_15
+// Switches, DPST, pulled up with 10k resistor; left SW1, right SW2:
+// SW_EXT connector, from left to right:
+#define MB_SW1                                                             PC_9
+#define MB_SW2                                                             PC_8
+// Potentiometers; left POT1, right POT2:
+#define MB_POT1                                                            PA_0
+#define MB_POT2                                                            PA_1
+// Common anode, two digit seven-segment display. All signals active at '0':
+#define MB_SEL1                                                            PB_6
+#define MB_SEL2                                                            PC_7
+#define MB_A                                                               PA_10
+#define MB_B                                                               PA_9
+#define MB_C                                                               PA_8
+#define MB_D                                                               PB_10
+#define MB_E                                                               PB_5
+#define MB_F                                                               PB_4
+#define MB_G                                                               PB_3
+// Piezoelectric buzzer, 10k in parallel; Active at '0':
+#define MB_BUZZ                                                            PA_11
+// SDD1306-based OLED display; Uses I2C_2 to communicate with NUCLEO:
+#define MB_OLED_SDA                                                        PB_14
+#define MB_OLED_SCL                                                        PB_13
+// ESP-WROOM-02 WiFi microcontroller; Uses UART_3 to communicate with NUCLEO:
+#define MB_ESP_RST                                                         PA_12
+#define MB_ESP_TX                                                          PC_4
+#define MB_ESP_RX                                                          PC_5
+#define MB_ESP_EN                                                          RESET
+// mikroBUS pinout, upper leftmost pin is no. 1:
+#define MB_MIKRO_AN                                                        PB_1
+#define MB_MIKRO_RST                                                       PB_12
+#define MB_MIKRO_CS                                                        PD_2
+#define MB_MIKRO_SCK                                                       PC_10
+#define MB_MIKRO_MISO                                                      PC_11
+#define MB_MIKRO_MOSI                                                      PC_12
+#define MB_MIKRO_SDA                                                       PB_14
+#define MB_MIKRO_SCL                                                       PB_13
+#define MB_MIKRO_TX                                                        PB_11
+#define MB_MIKRO_RX                                                        PB_7
+#define MB_MIKRO_INT                                                       PB_2
+#define MB_MIKRO_PWM                                                       PC_6
+// ANALOG_EXT connector, from left to right:
+#define MB_AI1                                                             PC_3
+#define MB_AI2                                                             PC_2
+#define MB_A0                                                              PA_4
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jan 04 14:34:40 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file