This program is about limit switches11_18version.

Dependencies:   mbed

Revision:
3:7bbcd64e7127
Parent:
2:f64f79a6b5e0
diff -r f64f79a6b5e0 -r 7bbcd64e7127 main.cpp
--- a/main.cpp	Thu Sep 05 09:42:23 2019 +0000
+++ b/main.cpp	Mon Nov 18 06:20:56 2019 +0000
@@ -18,9 +18,8 @@
 DigitalOut green(D12);//電源確認
 DigitalOut red(D11);//動作確認
 
-
 //データ格納用変数
-char data;
+char data=0x00;
 
 
 
@@ -28,160 +27,91 @@
 void send_data(char send);
 
 //main()関数
-int main(){
-    
-    
-    limit0.mode(PullUp);
+int main()
+{
     
+    master.baud(115200);
+    pc.baud(115200);
+
+    limit0.mode(PullUp);
+
     limit1.mode(PullUp);
-    
+
     limit2.mode(PullUp);
-    
+
     limit3.mode(PullUp);
-    
+
     limit4.mode(PullUp);
-    
+
     limit5.mode(PullUp);
-    
+
     limit6.mode(PullUp);
-    
+
     limit7.mode(PullUp);
-    
-    while(true){
+
+    while(true) {
         
-        if(!limit0&&!limit1){
-                
-                red=1;
-                
-                green=1;    
-                
-                data=0x20;
-                
-                send_data(data);
-                
-            }else if(!limit0&&limit1){
-                
-                red=0;
-                
-                green=1;
-                
-                    data=0x22;
-                    
-                
-                send_data(data);
-                
-            }else if(limit0&&!limit1){
-                
-                red=1;
-                
-                green=0;
-                
-                data=0x24;
-                
-                send_data(data);
-                
-            }
-            
-            if(!limit2&&!limit3){
-                
-                green=1;
-            
-                red=1;
+        red=1;
 
-                data=0x26;
-                
-                send_data(data);
-                
-            }else if(!limit2&&limit3){
-                
-                green=1;
-                
-                red=0;
-                
-                data=0x28;
-                
-                send_data(data);
-                
-            }else if(limit2&&!limit3){
-                
-                green=0;
-                
-                red=1;
-                
-                    data=0x30;
-                    
-                
-                send_data(data);
-                 
-            }
-            
-             if(!limit4){
-                
-                red=1;
-                
-                green=0;
-                
-                data=0x32;
-                
-                send_data(data);
-                
-            }
-            
-             if(!limit5){
-                
-                red=0;
-                
-                green=1;
-                
-                data=0x34;
-                
-                send_data(data);
-
-            }
-             if(!limit6){
-                
-                red=1;
-                
-                green=0;
-                
-                data=0x36;
-                
-                send_data(data);
-                
-            }
-            
-            
-             if(!limit7&&data==0x40){
-                
-                red=0;
-                
-                green=1;
-                
-                data=0x38;
-                
-                send_data(data);
-                
-            }
-            
-            if(limit0&&limit1&&limit2&&limit3&&limit4&&limit5&&limit6&&limit7){
-                
-                red=0;
-                
-                green=0;
-                
-                data=0x40;
-                
-                send_data(data);
-                
-            }
-         pc.printf("%c\n",data);
-            
+        if(!limit0) {
+            data = data | 0x01;
+        } else {
+            data = data & 0b11111110;
+        }
+        if(!limit1) {
+            data = data | 0x02;
+        } else {
+            data = data & 0b11111101;
+        }
+        if(!limit2) {
+            data = data | 0x04;
+        } else {
+            data = data & 0b11111011;
+        }
+        if(!limit3) {
+            data = data | 0x08;
+        } else {
+            data = data & 0b11110111;
+        }
+        if(!limit4) {
+            data = data | 0x10;
+        } else {
+            data = data & 0b11101111;
+        }
+        if(!limit5) {
+            data = data | 0x20;
+            green=1;
+        } else {
+            data = data & 0b11011111;
+            green=0;
+        }
+        if(!limit6) {
+            data = data | 0x40;
+        } else {
+            data = data & 0b10111111;
+        }
+        if(!limit7) {
+            data = data | 0x80;
+        } else {
+            data = data & 0b01111111;
         }
         
+        
+
+
+        send_data(data);
+        
+        red=0;
+        
+        pc.printf("0x%x\n",data);
+
     }
 
+}
+
 //rs232通信
-void send_data(char send){
+void send_data(char send)
+{
     master.putc(send);
-    wait(0.005);
-    }
\ No newline at end of file
+    wait(0.02);
+}
\ No newline at end of file