RX node

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
MadhuraT
Date:
Thu Jun 13 16:46:07 2019 +0000
Parent:
2:1ab320a11a3e
Commit message:
rx node code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 1ab320a11a3e -r 106ec6e30558 main.cpp
--- a/main.cpp	Fri Jul 07 06:39:56 2017 +0000
+++ b/main.cpp	Thu Jun 13 16:46:07 2019 +0000
@@ -1,13 +1,247 @@
+
+
+//Receiver
+
 #include "mbed.h"
 
-DigitalOut led(LED1);
+#define SERIAL_BAUD 9600
+#define CAN_BAUD 500000
+
+
+CAN can1(PB_8,PB_9);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalIn inp(PC_13);
+DigitalIn inp2(PC_2);
+DigitalIn inp1(PC_3);
+DigitalOut Sync(PA_4);
+Serial serial1(USBTX,USBRX);
+DigitalOut sync(PA_6);
+InterruptIn userButton(PC_13);
+Ticker aliveTicker;
+Timer t;
+int i,SW=0,Datarecd =0,Dataxmit=0; 
+CANMessage msgOut;
+void can_msgout();
+void sendCanMessage(int data);
+int s=0;
+int flag=0;
+char counter=6;
+
+void serialOut(char *message)
+{
+    int i = -1;
+    while (message[++i] != '\0') serial1.putc(message[i]);
+}
+
+void isrButton()
+{
+  /*  if(SW==0)
+    {
+        SW=1;
+        //sync=1;
+        sync=1;
+        wait_ms(1);
+        //sendCanMessage();
+        
+        }
+     else
+     {
+         SW=0;
+        //sync=0;
+        sync=0;
+         
+         }   */
+}
+
+void alive()
+{
+    //ledGreen = !ledGreen;
+    char *message = (char *)"Alive\r\n\0";
+    serialOut(message);
+}
+
+void isrCanRx()
+{
+   
+    /*CANMessage canMessage;
+    can1.read(canMessage); //Clears interrupt flag
+    Datarecd=canMessage.data[0];
+    if(canMessage.id==0x10)
+    {
+        if(Datarecd==0x0A)
+        {
+            led1=1;
+            }
+        else
+        {
+            led1=0;
+            }
+        }
+        if(canMessage.id==0x20)
+    {
+        if(Datarecd==0x0A)
+        {
+            led2=1;
+            }
+        else
+        {
+            led2=0;
+            }
+        }*/
+    
+}
+
+void isrCanTx()
+{
+         /*if(SW==1 && s<100) 
+                  {
+         sendCanMessage();
+         s++;
+         }*/
+        
+}
+
+void isrCanError()
+{
+    char *message = (char *)"CAN WERR IRQ\r\n\0";
+    serialOut(message);
+}
+
+void isrCanDataOverrun()
+{
+    char *message = (char *)"CAN DAO IRQ\r\n\0";
+    serialOut(message);
+}
+
+void isrCanWakeup()
+{
+    char *message = (char *)"CAN WAKEUP IRQ\r\n\0";
+    serialOut(message);
+}
+
+void isrCanErrorPassive()
+{
+    char *message = (char *)"CAN EP IRQ\r\n\0";
+    serialOut(message);
+}
+
+void isrCanArbitrationLost()
+{
+    char *message = (char *)"CAN ARB IRQ\r\n\0";
+    serialOut(message);
+}
+
+void isrCanBusError()
+{
+    char *message = (char *)"CAN BUSERR IRQ\r\n\0";
+    serialOut(message);
+}
+
+void isrCanReady()
+{
+    char *message = (char *)"CAN READY IRQ\r\n\0";
+    serialOut(message);
+}
+
+void sendCanMessage(int data)
+{
+    int ret = 0;
+    data= 0x01;
+    msgOut.format = CANStandard;
+    msgOut.type = CANData;
+    msgOut.len = 2;
+    msgOut.id = 0xffe;
+    //msgOut.id = data;
+    msgOut.data[0] = data;
+    msgOut.data[1] = data;
+    ret = can1.write(msgOut);
+    serial1.printf("can.write() returned %x", data);   
+}
+void can_msgout()
+{
+msgOut.data[0] = Dataxmit;//0xA;
+//sendCanMessage();
+}
+
+void setup()
+{
+    //Serial
+    serial1.baud(SERIAL_BAUD);
+    serial1.printf("START\r\n");
+
+    //CAN
+    can1.frequency(CAN_BAUD);
+
+    //Interrupts
+    //userButton.rise(&isrButton);
+    //can1.attach(&isrCanRx, CAN::RxIrq);
+    //can1.attach(&isrCanTx, CAN::TxIrq);
+    can1.attach(&isrCanError, CAN::EwIrq);
+    can1.attach(&isrCanDataOverrun, CAN::DoIrq);
+    can1.attach(&isrCanWakeup, CAN::WuIrq);
+  // can1.attach(&isrCanErrorPassive, CAN::EpIrq);
+    can1.attach(&isrCanArbitrationLost, CAN::AlIrq);
+    can1.attach(&isrCanBusError, CAN::BeIrq);
+    can1.attach(&isrCanReady, CAN::IdIrq);
+
+    aliveTicker.attach(&alive, 5.0);
+}
+
 
 int main()
 {
-    while(1) {
-        led = 1; // LED is ON
-        wait(0.2); // 200 ms
-        led = 0; // LED is OFF
-        wait(0.8); // 800 ms
+    
+    int data1;
+    int data2;
+    int ret;
+    int counter=0;
+    int counter1=0;
+    int counter3=0;
+    CANMessage msg;
+    sync=0;
+    setup();
+
+    
+    
+    while (true)
+    {
+       msg.len=2;
+       ret= can1.read(msg);
+      // printf("return value %d",ret);
+      //printf ("hi");
+       if(msg.id == 0x2aa)
+       {
+       // printf("\n Node1 Message received123: %d \r\n", msg.data[0]);
+        counter=1;
+       // printf( "first byte = %x " , msg.data[0]);
+        //printf( "second byte = %x " , msg.data[1]);
+        data1=   ((msg.data[1] << 8 )| msg.data[0]);
+       }
+        if(msg.id == 0x2ff)
+       {
+        //printf("\n Node1 Message received456: %d  \r\n", msg.data[0]);
+         data2=   ((msg.data[1] << 8 )| msg.data[0]);
+         //printf( "first byte = %x " , msg.data[0]);
+        //printf( "second byte = %x " , msg.data[1]);
+         counter1=1;
+       }
+       if(counter== 1 && counter1 == 1 && counter3 == 0)
+       {
+       if(data1 > data2)
+       {
+           printf (" Node2 won: Node1 Time=%d ms: Node 2 Time= %d ms \r\n ", data1,data2);
+           //printf (" Node 1 Time= %d", data1);
+           counter3=1;
+           
+           }
+           else
+           {
+               printf (" Node1 won: Node1 Time=%d ms : Node 2 Time= %d ms \r\n ", data1,data2);
+              // printf (" Node 1 Time= %d", data2);
+               counter3=1;
+               }
+      
+        }
     }
 }