this receive i2cmaster 's data(reg, int) it move Stepping Motor. for WRS2019

Dependencies:   mbed i2cslave

Files at this revision

API Documentation at this revision

Comitter:
sgrsn
Date:
Tue Dec 17 07:16:39 2019 +0000
Commit message:
First Commit

Changed in this revision

define.h Show annotated file Show diff for this revision Revisions of this file
i2cslave.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp 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
diff -r 000000000000 -r 831a6764c8cf define.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/define.h	Tue Dec 17 07:16:39 2019 +0000
@@ -0,0 +1,23 @@
+
+// Motor Position
+typedef enum
+{
+    RightForward    = 1,
+    LeftForward     = 2,
+    RightBack       = 3,
+    LeftBack        = 4,
+};
+
+// Motor
+typedef enum
+{
+    COAST   = 0,
+    BRAKE   = 1,    
+    CW      = 2,
+    CCW     = 3,
+};
+
+#define WHO_AM_I        0x00
+#define MY_IIC_ADDR     0x01
+#define MOTOR_DIR       0x04
+#define PWM_FREQUENCY   0x05
\ No newline at end of file
diff -r 000000000000 -r 831a6764c8cf i2cslave.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/i2cslave.lib	Tue Dec 17 07:16:39 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/sgrsn/code/i2cslave/#6e9ecf7b83e3
diff -r 000000000000 -r 831a6764c8cf main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Dec 17 07:16:39 2019 +0000
@@ -0,0 +1,76 @@
+#include "mbed.h"
+#include "i2cslave.h"
+#include "define.h"
+
+
+// define IIC_ADDR 1:0xB0, 2:0xC0, 3:0xD0 or 4:0xD0
+// define I_AM but it not used. 
+#define IIC_ADDR        0xB0
+#define I_AM            RightForward
+
+int32_t Register[0x20] = {};
+i2cslave i2c(dp5, dp27, Register);
+ 
+int main()
+{
+    Register[WHO_AM_I] = I_AM;
+    Register[MY_IIC_ADDR] = IIC_ADDR;
+    
+    Register[MOTOR_DIR] = COAST;
+    
+    PwmOut PWM_PIN(dp24);
+    DigitalOut PWM_ENABLE(dp14);
+    DigitalOut DIR_PIN(dp18);
+    DigitalOut CON_PIN(dp17);
+    
+    DIR_PIN = 0;
+    CON_PIN = 0;
+    
+    i2c.address(IIC_ADDR);
+    
+    float f = 1000;
+    float T = 0.05;
+    float T_last = T;
+    PWM_PIN = 0;
+    int dir = COAST;
+    int last_dir = 0;
+    
+    while(1)
+    {
+        T_last = T;
+        f = float(Register[PWM_FREQUENCY]);
+        if(f > 0 && f <= 1000000) T = 1.0 / f;
+        if(T_last != T) PWM_PIN.period(T);
+        
+        last_dir = dir;
+        dir = Register[MOTOR_DIR];
+        
+        if(dir != last_dir)
+        {
+            switch(dir)
+            {
+                case COAST:
+                    PWM_ENABLE = 0;
+                    CON_PIN = 0;
+                    break;
+                case BRAKE:
+                    CON_PIN = 1;
+                    PWM_ENABLE = 1;
+                    PWM_PIN = 0; 
+                    break;
+                case CW:
+                    CON_PIN = 1;
+                    DIR_PIN = 0;
+                    PWM_ENABLE = 1;
+                    PWM_PIN = 0.5;
+                    break;
+                case CCW:
+                    CON_PIN = 1;
+                    DIR_PIN = 1;
+                    PWM_ENABLE = 1;
+                    PWM_PIN = 0.5;
+                    break;
+            }
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 831a6764c8cf mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Dec 17 07:16:39 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file