Simulateur CAN 1.0

Dependencies:   mbed

Fork of CAN by Carlos Almeida

Files at this revision

API Documentation at this revision

Comitter:
protongamer
Date:
Tue Feb 06 07:26:55 2018 +0000
Parent:
0:cea062e60f85
Commit message:
Test

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r cea062e60f85 -r f961845c25eb main.cpp
--- a/main.cpp	Thu Mar 23 15:57:59 2017 +0000
+++ b/main.cpp	Tue Feb 06 07:26:55 2018 +0000
@@ -3,33 +3,463 @@
 Ticker ticker;
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-CAN can1(p9 , p10);
+
+Serial pc(D1, D0);
+CAN can1(p9, p10);
 CAN can2(p30, p29);
-char counter = 0;
- 
-void send() {
-    printf("send()\r\n");
-    if(can1.write(CANMessage(1100, &counter, 1))) {
-        printf("loop send()\r\n");
-        counter++;
-        printf("Message sent: %d\r\n", counter);
-        led1 = !led1;
-    }
+
+char value = 0;
+uint8_t state = 0;
+
+void send(char counter) {
+    //pc.printf("send()\n");
+    if(can2.write(CANMessage(1338, &counter, 8))) {
+        //pc.printf("wloop()\n");
+        //counter++;
+        pc.printf("Message sent: %d\r\n", counter);
+    } 
+    led1 = !led1;
 }
  
 int main() {
-    ticker.attach(&send, 2);
+    
+    //ticker.attach(&send, 1);
     CANMessage msg;
+    /*char c;
+    uint16_t instruction;
+    uint16_t instruction_2;
+    uint8_t bytes;
+    int8_t state_val;*/
     while(1) {
-        printf("loop wait()\r\n");
+       //c = pc.getc();
+       
+       if(can2.read(msg)) {
+            printf("Message received: %d\n", msg.data[0]);
+            led2 = !led2;
+            wait(1.0);
+            send(0x2A);
+        } 
+
+       printf("awaiting data \r\n");
+       wait(0.5);
+       }
+       
+       }
+    /*switch(state){
+       
+       case 0:
+       pc.printf("CAN BUS - TERMINAL \r\n");
+       pc.printf("Use 1 to set the value to be sent\r\n");
+       pc.printf("Use 2 to send the value \r\n");
+       state = 1;       
+       break;
+       
+       case 1:
+       
+       if(c == '1'){
+        instruction = 0;
+        state_val = 0;
+        pc.printf("1 \r\n\n");
+        wait(1.0);
+        state = 2;
+        }
+        
+        if(c == '2'){
+        pc.printf("2 \r\n\n");
+        wait(1.0);
+        switch(bytes){
+            
+            case 1:
+        send(instruction);
+            break;
+            
+            case 2:
+            send(instruction_2);
+            send(instruction);
+            break;
+            }
+        state = 0;
+        }
+        
+        
+       
+       break;
+       
+       case 2:
+       pc.printf("Set the value to be sent(hex value) : 0x");
+       state = 3;       
+       break;
+       
+       case 3:
+       
+       switch(c){
+           
+           
+                
+           case '0':
+           pc.printf("0");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0000;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b0000;
+               state_val++;
+               }
+            
+           break;
+           
+           case '1':
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0001;
+                }
+                
+           pc.printf("1");
+           if(state_val == 0){
+               instruction = 0b0001;
+               state_val++;
+               }
+            
+           break;
+           
+           case '2':
+           
+            if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0010;
+                }
+                
+           pc.printf("2");
+           if(state_val == 0){
+               instruction = 0b0010;
+               state_val++;
+               }
+           
+           break;
+           
+           case '3':
+           pc.printf("3");
+          
+          if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0011;
+                }
+          
+           if(state_val == 0){
+               instruction = 0b0011;
+               state_val++;
+               }
+            
+           break;
+           
+           case '4':
+           
+            if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0100;
+                }           
+           
+           pc.printf("4");
+           if(state_val == 0){
+               instruction = 0b0100;
+               state_val++;
+               }
+
+           break;
+           
+           case '5':
+           pc.printf("5");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0101;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b0101;
+               state_val++;
+               }
+            
+           break;
+           
+           case '6':
+           pc.printf("6");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0110;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b0110;
+               state_val++;
+               }
+            
+           break;
+           
+           case '7':
+           pc.printf("7");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b0111;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b0111;
+               state_val++;
+               }
+            
+           break;
+           
+           case '8':
+           pc.printf("8");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1000;
+                }
+           
+           
+           if(state_val == 0){
+               instruction = 0b1000;
+               state_val++;
+               }
+            
+           break;
+           
+           case '9':
+           pc.printf("9");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1001;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1001;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'a':
+           pc.printf("a");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1010;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1010;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'b':
+           pc.printf("b");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1011;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1011;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'c':
+           pc.printf("c");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1100;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1100;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'd':
+           pc.printf("d");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1101;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1101;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'e':
+           pc.printf("e");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1110;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1110;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'f':
+           pc.printf("f");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1111;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1111;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'A':
+           pc.printf("a");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1010;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1010;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'B':
+           pc.printf("b");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1011;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1011;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'C':
+           pc.printf("c");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1100;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1100;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'D':
+           pc.printf("d");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1101;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1101;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'E':
+           pc.printf("e");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1110;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1110;
+               state_val++;
+               }
+            
+           break;
+           
+           case 'F':
+           pc.printf("f");
+           
+           if(state_val > 0){
+                instruction = instruction << 4;
+                instruction |= 0b1111;
+                }
+           
+           if(state_val == 0){
+               instruction = 0b1111;
+               state_val++;
+               }
+            
+           break;
+           
+           case '\r':
+           pc.printf("\r\n0x%x \r\n", instruction);
+           wait(1.0);
+           state = 0;
+           if(instruction <= 255){
+               bytes = 1;
+               }
+               if(instruction > 255){
+               bytes = 2;
+               instruction_2 = instruction;
+               instruction_2 = instruction_2 >> 8;
+               instruction = instruction & 0b0000000011111111;
+               }
+           break;
+           
+           
+           }
+       
+       break;
+       
+       
+       
+       }   
+       
+       
+       
+        
+        //wait(0.2);
+    }
+}*/
+
+ /*printf("loop()\n");
         if(can2.read(msg)) {
-            printf("Message received: %d, from %d\r\n", msg.data[0], msg.id);
-            if(msg.id==1100)
-                led2 = !led2;
-            if(msg.id==1102)
-                led3 = !led3;     
-        }
-        wait(0.5);
-    }
-}
\ No newline at end of file
+            printf("Message received: %d\n", msg.data[0]);
+            led2 = !led2;
+        } */
\ No newline at end of file