Outher...Miura Souta

Dependencies:   mbed MCP23017

Files at this revision

API Documentation at this revision

Comitter:
M_souta
Date:
Tue Jan 21 11:10:33 2020 +0000
Parent:
0:db8d4af513c0
Child:
2:32d2cd7d744b
Commit message:
lll

Changed in this revision

MCP/MCP.cpp Show annotated file Show diff for this revision Revisions of this file
MCP/MCP.h Show annotated file Show diff for this revision Revisions of this file
Motor/Motor.cpp Show annotated file Show diff for this revision Revisions of this file
Motor/Motor.h 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
--- a/MCP/MCP.cpp	Mon Jan 20 08:46:24 2020 +0000
+++ b/MCP/MCP.cpp	Tue Jan 21 11:10:33 2020 +0000
@@ -1,14 +1,50 @@
 #include "MCP.h"
 #include "mbed.h"
+#include "MCP23017.h"
 
 MCP::MCP(PinName sda, PinName scl, uint8_t device_address)
-    :i2c(sda, scl)
+    :i2c(sda, scl), mcp(i2c, device_address) 
 {
-    _write_opcode = device_address & 0xFE; // low order bit = 0 for write
-    _read_opcode  = device_address | 0x01; // low order bit = 1 for read;
+    _iodir_data.all = 0xffff;
+    _pull_data.all  = 0x0000;
+    _read_data.all  = 0x0000;
+    _write_data.all = 0x0000;
+}
+
+void MCP::PinMode(uint8_t pin, pin_mode mode)
+{
+    if(mode == OUTPUT) {
+        _iodir_data.all &= ~(0x0001 << pin);
+    } else if(mode == INPUT) {
+        _iodir_data.all |= (0x0001 << pin);
+    } else if(mode == INPUT_PULLUP) {
+        _iodir_data.all |= (0x0001 << pin);
+        _pull_data.all |= (0x0001 << pin);
+    }
 }
 
-void MCP::PinMode(uint8_t pin, uint8_t mode)
+void MCP::Write(uint8_t pin, bool signal)
+{
+    if(signal == 1) {
+        _write_data.all |= (0x0001 << pin);
+    } else {
+        _write_data.all &= ~(0x0001 << pin);
+    }
+}
+
+bool MCP::Read(uint8_t pin)
 {
-    //_pull_data
-}
\ No newline at end of file
+    return (_read_data.all >> pin) & 0x01;
+}
+        
+void MCP::Update(void)
+{
+    mcp.direction(PORT_A, _iodir_data.port.port_A);
+    mcp.direction(PORT_B, _iodir_data.port.port_B);
+    mcp.configurePullUps(PORT_A, _pull_data.port.port_A);
+    mcp.configurePullUps(PORT_B, _pull_data.port.port_B);
+    _read_data.port.port_A = mcp.read(PORT_A);
+    _read_data.port.port_B = mcp.read(PORT_B);
+   // mcp.write(PORT_A, _write_data.port_A);
+   // mcp.write(PORT_B, _write_data.port_B);
+}
--- a/MCP/MCP.h	Mon Jan 20 08:46:24 2020 +0000
+++ b/MCP/MCP.h	Tue Jan 21 11:10:33 2020 +0000
@@ -2,35 +2,34 @@
 #define  MCP_H_
 
 #include "mbed.h"
+#include "MCP23017.h"
 
 #define SDA PB_7
 #define SCL PB_6
 
 #define MCP_ADDRESS 0x40
 
-// MCP register address
-#define IODIRA   0x00
-#define IODIRB   0x01
-#define IPOLA    0x02
-#define IPOLB    0x03
-#define GPINTENA 0x04
-#define GPINTENB 0x05
-#define DEFVALA  0x06
-#define DEFVALB  0x07
-#define INTCONA  0x08
-#define INTCONB  0x09
-#define IOCONA   0x0A
-#define IOCONB   0x0B
-#define GPPUA    0x0C
-#define GPPUB    0x0D
-#define INTFA    0x0E
-#define INTFB    0x0F
-#define INTCAPA  0x10
-#define INTCAPB  0x11
-#define GPIOA    0x12
-#define GPIOB    0x13
-#define OLATA    0x14
-#define OLATB    0x15
+
+
+typedef enum {
+    a0 = 0,
+    a1 = 1,
+    a2 = 2,
+    a3 = 3,
+    a4 = 4,
+    a5 = 5,
+    a6 = 6,
+    a7 = 7,
+    b0 = 8,
+    b1 = 9,
+    b2 = 10,
+    b3 = 11,
+    b4 = 12,
+    b5 = 13,
+    b6 = 14,
+    b7 = 15,
+}pin_name;
+
 
 typedef enum {
     OUTPUT,
@@ -38,10 +37,15 @@
     INPUT_PULLUP,
 }pin_mode;
 
+typedef struct byte{
+    uint8_t port_A;
+    uint8_t port_B;
+}byte;
+
 typedef union {
-    uint8_t port_A, port_B;
+    byte port;
     uint16_t all;
-}mcp_register;
+}mcp_data;
 
 class MCP {
     public:
@@ -50,11 +54,11 @@
         /                                              */
         MCP(PinName sda, PinName scl, uint8_t device_address);
         // MCP pin define * pin number is 0 ~ 15
-        void PinMode(uint8_t pin, uint8_t mode);
+        void PinMode(uint8_t pin, pin_mode mode);
         // MCP DigitalWrite * pin number is 0 ~ 15
         void Write(uint8_t pin, bool signal);
         // MCP DigitalRead * pin number is 0 ~ 15
-        bool Read(uint8_t pin, bool signal);
+        bool Read(uint8_t pin);
         // MCP initialize
         void Initialize(void);
         // MCP register update and read new data
@@ -64,15 +68,17 @@
         
     private:
         I2C i2c;
+        MCP23017 mcp;
         char _read_opcode;
         char _write_opcode;
         
-        uint16_t _pull_data;
-        uint16_t _read_data;
-        uint16_t _write_data;
+        mcp_data _iodir_data;
+        mcp_data _pull_data;
+        mcp_data _read_data;
+        mcp_data _write_data;
         
         void _Write(uint8_t address, uint8_t data);
-        void _Read(uint8_t address, uint8_t data);    
+        void _Read(uint8_t address, uint8_t data);
     
 };
 
--- a/Motor/Motor.cpp	Mon Jan 20 08:46:24 2020 +0000
+++ b/Motor/Motor.cpp	Tue Jan 21 11:10:33 2020 +0000
@@ -7,23 +7,24 @@
 {
     D1_.write(0);
     D2_.write(0);
+    pwm_.period_us(50);  // 20kHz
     pwm_.write(0);
 }
 
 void MOTOR::Dir(dire mode, uint8_t pwm)
 {
     D1_.write((char)mode & 0x01);
-    D1_.write((char)mode & 0x02);
-    pwm_.write((float)(pwm / 100.0));
+    D2_.write((char)mode & 0x02);
+    pwm_.write((float)(pwm / 255.0));
 }
 
 void MOTOR::Dir(dire mode)
 {
     D1_.write((char)mode & 0x01);
-    D1_.write((char)mode & 0x02);
+    D2_.write((char)mode & 0x02);
 }
 
 void MOTOR::PWM(uint8_t pwm)
 {
-    pwm_.write((float)(pwm / 100.0));
+    pwm_.write((float)(pwm / 255.0));
 }
\ No newline at end of file
--- a/Motor/Motor.h	Mon Jan 20 08:46:24 2020 +0000
+++ b/Motor/Motor.h	Tue Jan 21 11:10:33 2020 +0000
@@ -21,7 +21,7 @@
             BRAKE
         */
         /* pwm
-            pwm is 0 ~ 100(%)
+            pwm is 0 ~ 255(%)
         */
         // durection and pwm set
         void Dir(dire mode, uint8_t pwm);
--- a/main.cpp	Mon Jan 20 08:46:24 2020 +0000
+++ b/main.cpp	Tue Jan 21 11:10:33 2020 +0000
@@ -9,15 +9,25 @@
 
 MCP MCP(SDA, SCL, MCP_ADDRESS);
 
+Serial pc(USBTX, USBRX);
+
 XBEE::ControllerData *controller;
 
 int main() {
     
+    for(int i = 0; i < 16; i++) {
+        MCP.PinMode(i, INPUT_PULLUP);
+    }
+    
     while(1) {
         controller = XBEE::Controller::GetData();
+        MCP.Update();
         
         /* write ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ */
-        
-        
+        //for(int i = 0; i < 16; i++) {
+            int a = MCP.Read(7);
+            pc.printf("%d",a);
+        //}
+        pc.printf("\n\r");
     }
 }