rfid reader group 1

Dependencies:   mbed

Revision:
1:cedf6a3c299e
Parent:
0:672cd82db097
--- a/main.cpp	Fri Feb 28 15:20:53 2020 +0000
+++ b/main.cpp	Fri Jun 19 09:57:08 2020 +0000
@@ -1,26 +1,33 @@
 #include "mbed.h"
 
-Serial pc(USBTX, USBRX);
+Serial pc(PA_2, PA_3);
 Serial reader(PB_6, PB_7);
 InterruptIn Green(PB_0);
 DigitalIn CTS(PB_1);
 int TXBsize = 1;
-char TXB[16];
+char TXB[16];    
+//for init RFID
 uint8_t READRFID = 0b01010010;
 uint8_t WRITEEEPROM = 0b01010000;
 uint8_t POLLING = 0x1;
 uint8_t POLLINGADDRESS = 0;
 uint8_t RFIDTYPEADDRESS = 0x3;
 uint8_t RFIDTYPE = 0x0;
-uint8_t ADDRESS = 0;
+uint8_t ADDRESS = 2;
+uint8_t ADDRESSMAX = 40;  
 uint8_t KEY = 0;
 bool FINDINGUID = 0;
-bool UIDFOUND = 0;
+bool UIDFOUND = 0;  
 bool SENDSUCCESS = 0;
 bool UIDTRIED = 0;
+bool inword = 0;   //avoid sending dummy bits, flag. etc..
+bool spoken = 0;
+char speak [256];
+uint8_t speaklength=0;  //length of words to be send
 uint8_t UID[8];
-void readerRX() {
-   if(FINDINGUID == 1)
+ bool nextread = 1;   //read next address or stop
+void readerRX() {  //will enter this through interrupt, recieving data from rfid mdule
+   if(FINDINGUID == 1)   //only for multiple tag read, not used in this project
    {UID[0]=reader.getc();
     UID[1]=reader.getc();
     UID[2]=reader.getc();
@@ -35,52 +42,46 @@
     FINDINGUID = 0;
    }
    else
-   {pc.putc(reader.getc());}
+   {
+    char tmp = reader.getc();
+    if(tmp == '@')
+     {inword = 1;}
+    else if(tmp == ';')
+     {inword = 0;}
+    if(inword == 1 && tmp != '@' && tmp != ';' && tmp < 0b10000000) //to avoid non char character to be sent
+       {spoken = 0;
+        speak[speaklength]=tmp;
+        speaklength++;
+       }
+   }
    
 }
 
-void GreenIRQ() {
-     bool sent = 0;
-    Timer timer;
-    timer.start();
-     while(!UIDTRIED)
-   {sent = 0;
-   while (sent == 0)
-    {if(CTS == 0)
-       { FINDINGUID = 1;
-       reader.putc(0b01010101);
-       UIDTRIED = 1;
-       sent = 1;}
-     }
-   }
-  if(UIDTRIED == 1 && UIDFOUND == 0)
-  { FINDINGUID = 0;}
-    TXBsize = 3;
-    TXB[0]=READRFID;
+void GreenIRQ() { //enter through interrupt, send command and read address to the RFID chips, send words to central MCU.
+     if(Green == 0 && nextread == 1)
+     {bool sent = 0;
+       uint8_t TXBused = 0; 
+     TXB[0]=READRFID;
       TXB[1]=KEY;
       TXB[2]=ADDRESS;
-      //TXB[2]=0xE0;
-      TXB[3]=0x04;
-      TXB[4]=0x01;
-      TXB[5]=0x50;
-      TXB[6]=0xD4;
-      TXB[7]=0xF2;
-      TXB[8]=0x23;
-      TXB[9]=0x87;
-    uint8_t TXBused = 0;
-            
+      TXBsize = 3;
    
-    if(SENDSUCCESS == 1 && TXB[0] == READRFID)
-      {ADDRESS++;
+    if(SENDSUCCESS == 1 && TXB[0] == READRFID) //synthesize the command
+      {ADDRESS=ADDRESS+4;  
       SENDSUCCESS = 0;
-      if(ADDRESS == 0)
+      TXBsize = 3;
+      if(ADDRESS > ADDRESSMAX)
+      {ADDRESS = 2;
+      nextread = 0;
+       }
+    /*  if(ADDRESS == 0)
        {KEY++;
          if(KEY == 0b00100000)
          {KEY = 0b10000000;}
          else if(KEY == 0b10100000)
          {KEY = 0b00000000;}
          pc.printf("%d",KEY);
-        }
+        } */
       TXB[1]=ADDRESS;
       TXB[2]=KEY;
       }
@@ -89,21 +90,46 @@
         // Time keeping
     while (TXBused<TXBsize){
         if (CTS == 0 && sent == 0){
-            reader.putc(TXB[TXBused]);
-          //  pc.printf("%d",TXB[TXBused]);
+            reader.putc(TXB[TXBused]);  //send the command to RFID chip when CTS=0
             TXBused++;
            // sent = 1;
       }
       else if(CTS == 1)
       {sent = 0;
        }
-    }
      SENDSUCCESS = 1;
-     
+      }
+      while(CTS == 0);
+      
+     }
+    else if(Green == 1)
+     {nextread = 1;
+      ADDRESS = 2;
+      if(spoken == 0)
+      {uint8_t tmpsize = speaklength+1;
+       if(speaklength == 255)
+       {tmpsize=255;}
+       speak[tmpsize]='!';  //Tell the Central MCU thst the word are finished.
+       for(int i=0; i<=tmpsize; i++)  // sent the composed mesaage to the central MCU
+       {pc.putc(speak[i]);}      
+       speaklength = 0;
+       spoken = 1;
+      }
+     }
+    if(spoken == 0 && nextread == 0)
+      {uint8_t tmpsize = speaklength+1;
+       if(speaklength == 255)
+       {tmpsize=255;}
+       speak[tmpsize]='!';
+       for(int i=0; i<=tmpsize; i++)
+       {pc.putc(speak[i]);}
+       speaklength = 0;
+       spoken = 1;
+      }
 }
 
 
-void initRFID()
+void initRFID()  //program the EEPROM of RFID chip to set it to the correct mode
 {   bool sent = 0;
 
 while (sent ==0 )
@@ -127,7 +153,7 @@
        sent = 1;
       }
      }
-
+   
   }
 
 
@@ -136,10 +162,12 @@
     pc.baud(9600);
     reader.baud(9600);
    initRFID();
-    reader.attach(&readerRX,Serial::RxIrq);
+   reader.attach(&readerRX,Serial::RxIrq);
     Green.fall(&GreenIRQ);  // attach the address of the flip function to the rising edge
     while(true)
-    {
-    }
+    {if(Green == 0)
+     { wait(0.05);
+      GreenIRQ();}
+   }
     
 }