Conversion of Arduino program to Flowcode..

Hi guys,

I have an arduino program for a RC522 RFID Module. I am attempting to convert this program to a flowcode program as I have little knowledge of actual programming.

I am however pretty good at using flowcode.

Due to my lack of real knowledge on C++ I am struggling in parts to understand what the arduino program is doing so as to convert this to something flowcode can process.

I am working my way through slowly but have got stuck on a particular point, I am hoping you can tell me in basic terms what is happening.

Heres the code.

    if (i != 0)
    {    
        if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
        {
            status = MI_OK;
            if (n & irqEn & 0x01)
            {   
    status = MI_NOTAGERR;   //??   
   }

Thanks in advance :slight_smile:

    if (i != 0)
    {

If the variable "i" is anything but a 0 then do everything between this bracket and its balancing bracket.

        if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
        {

If the results of the function Read_MRFC522(ErrorReg), when ANDed with the literal value 0x1B (in HEX) are equal to 0 (0 is false, anything else is true, so !0 will be true) then do everything between this bracket and its balancing bracket.

            status = MI_OK;

Set the status to some predefine value

            if (n & irqEn & 0x01)
            {   
    status = MI_NOTAGERR;   //??   
   }

If the variable "n" when ANDed with the value in variable "irqEn" and also ANDed with the literal value 0x01 (in HEX) is anything but a 0 then set the status to some other prefefined value
There should be more } at this point to close the previous IF statements.

more about AND ing see -Arduino Playground - BitMath -

Thank you for the detailed explanation. I think i should be able to work that out now :slight_smile:

One other thing i am stuck with is a variable that looks like this

Uchar Backlength

But when called in a function it is *Backlength

Does the * mean something??

I would post the actual code but im not on my PC.

I'm not familiar with Uchar, other than as a town in Kyrgyzstan. My guess is it's possibly some kind of unsigned char or something.

A * signifies a pointer, or the contents of a pointer, depending on context.

Pointers are a whole other tricky ball game :wink:

Its the 'dereference' operator. It treats the value stored in the variable as being an address of an object in the RAM, and says, ok, I'll load the value from this address and use it.

A daft example, that doesn't compile, but tries to demonstrate my point. (You can make it compile by doing: (byte)hi, instead of just *hi)

byte hi = 4;
if (hi == 4) {
  //this would always execute
}
if (*hi == 4) {
  //This only executes if the value stored in the ram at memory address 4 is equal to 4.
}

You will need to post actual code for us to be more specific.

/*
 * Function?MFRC522_Request
 * Description?Searching card, read card type
 * Input parameter?reqMode--search methods?
 *    TagType--return card types
 *     0x4400 = Mifare_UltraLight
 *    0x0400 = Mifare_One(S50)
 *    0x0200 = Mifare_One(S70)
 *    0x0800 = Mifare_Pro(X)
 *    0x4403 = Mifare_DESFire
 * return?return MI_OK if successed
 */
uchar MFRC522_Request(uchar reqMode, uchar *TagType)
{
 uchar status;  
 uint backBits;   //the data bits that received

  Write_MFRC522(BitFramingReg, 0x07);  //TxLastBists = BitFramingReg[2..0] ???
 
 TagType[0] = reqMode;
 status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);

  if ((status != MI_OK) || (backBits != 0x10))
 {    
  status = MI_ERR;
 }
   
 return status;
}

 
 /*
 * Function?MFRC522_ToCard
 * Description?communicate between RC522 and ISO14443
 * Input parameter?command--MF522 command bits
 *    sendData--send data to card via rc522
 *    sendLen--send data length   
 *    backData--the return data from card
 *    backLen--the length of return data
 * return?return MI_OK if successed
 */
uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)
{
    uchar status = MI_ERR;
    uchar irqEn = 0x00;
    uchar waitIRq = 0x00;
    uchar lastBits;
    uchar n;
    uint i;

     switch (command)
    {
        case PCD_AUTHENT:  //verify card password
  {
   irqEn = 0x12;
   waitIRq = 0x10;
   break;
  }
  case PCD_TRANSCEIVE: //send data in the FIFO
  {
   irqEn = 0x77;
   waitIRq = 0x30;
   break;
  }
  default:
   break;
    }
   
    Write_MFRC522(CommIEnReg, irqEn|0x80); //Allow interruption
    ClearBitMask(CommIrqReg, 0x80);   //Clear all the interrupt bits
    SetBitMask(FIFOLevelReg, 0x80);   //FlushBuffer=1, FIFO initilizate
    
 Write_MFRC522(CommandReg, PCD_IDLE); //NO action;cancel current command ???

  //write data into FIFO
    for (i=0; i<sendLen; i++)
    {   
  Write_MFRC522(FIFODataReg, sendData[i]);    
 }

  //procceed it
 Write_MFRC522(CommandReg, command);
    if (command == PCD_TRANSCEIVE)
    {    
  SetBitMask(BitFramingReg, 0x80);  //StartSend=1,transmission of data starts  
 }   
    
 //waite receive data is finished
 i = 2000; //i should adjust according the clock, the maxium the waiting time should be 25 ms???
    do 
    {
  //CommIrqReg[7..0]
  //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
        n = Read_MFRC522(CommIrqReg);
        i--;
    }
    while ((i!=0) && !(n&0x01) && !(n&waitIRq));

     ClearBitMask(BitFramingReg, 0x80);   //StartSend=0
 
    if (i != 0)
    {    
        if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
        {
            status = MI_OK;
            if (n & irqEn & 0x01)
            {   
    status = MI_NOTAGERR;   //??   
   }

             if (command == PCD_TRANSCEIVE)
            {
                n = Read_MFRC522(FIFOLevelReg);
               lastBits = Read_MFRC522(ControlReg) & 0x07;
                if (lastBits)
                {   
     *backLen = (n-1)*8 + lastBits;   
    }
                else
                {   
     *backLen = n*8;   
    }

                 if (n == 0)
                {   
     n = 1;    
    }
                if (n > MAX_LEN)
                {   
     n = MAX_LEN;   
    }
    
    //read the data from FIFO
                for (i=0; i<n; i++)
                {   
     backData[i] = Read_MFRC522(FIFODataReg);    
    }
            }
        }
        else
        {   
   status = MI_ERR;  
  }
        
    }
 
    //SetBitMask(ControlReg,0x80);           //timer stops
    //Write_MFRC522(CommandReg, PCD_IDLE); 

     return status;
}

Hi, This is the code im working with, There are 2 of the functions required. The tocard function is called from within the request function.

Im beginning to think this wasn't as easy as I first thought lol.

Ok, uchar in this case is "unsigned char" which is exactly the same as "byte".

A "uchar" us a single byte.

A "uchar *" is an array of bytes, or maybe a string, depending on context.

(as an aside, I'd love to know who decided that a "char" should be signed. I mean, what is a "negative" character? What is -Q?)

majenko, do you use Skype. I would like to ask a few questions if I can to get my head round this.

as an aside, I'd love to know who decided that a "char" should be signed.

Well, ASCII characters are seven bits, so that's quite a good fit really.

NorthEastDiver:
majenko, do you use Skype. I would like to ask a few questions if I can to get my head round this.

It took me 6 years to get my head around pointers properly :wink:

For this you don't need to worry about it that much. If you see a variable defined as just "uchar" think "a number between 0-255" or maybe "a single letter". If it is defined as "uchar *" then think "An array of numbers between 0-255", such as "23, 48, 119, 227" or maybe "A string of characters" such as "this is a string".

For example, the function

uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)

It returns a single number, between 0 and 255.
It takes 5 parameters:

uchar command - a command code between 0 and 255
uchar *sendData - an array of bytes to send
uchar sendLen - the number of bytes to send (the size of the array)
uchar *backData - an array to store incoming data in
uint *backLen - somewhere to store the number of incoming bytes received.

The latter is a little more tricky - it's basically a way of returning a second value from a function, which can normally only return one value. Instead of passing the contents of a variable, as you normally do, you're effectively passing the variable itself, so the function can modify the contents, which you can then examine later outside the function.

And no offense, but I don't hand my skype ID out to just anyone on a forum :stuck_out_tongue:

No offense taken, :slight_smile:

This is the entire program, I have worked my way backwards from the tocard function and it goes back to a string called "str", I am struggling to see tho where STR is being given a value its not loaded with any value during creation. It is used in the request function tho and still doesn't appear to have a value???

This is the line im looking at.

status = MFRC522_Request(PICC_REQIDL, str);

what are you trying to do with the rc522 rfid module? The source code from the web is partly wrong, e.g. read card storage volume.
I wrote about the rc522 in my german blog: 5Euro RFID/NFC Modul RC522 zum Auslesen von Mifare Tags | Regnerischer Nachmittag
or the source code
myarduino/RC522DumpMifare.ino at master · rena2019/myarduino · GitHub

What I would like to do with the module is this.

Register Keyfob, Scan the keyfob and read its UID and store it as a saved fob.
Unlock System, Scan the keyfob if it matches the stored UID then access will be allowed to a menu system.

At this point this is all that needs to be done.

NorthEastDiver:
No offense taken, :slight_smile:

ASSIST ARDUINO: RFID Lecture RC522

This is the entire program, I have worked my way backwards from the tocard function and it goes back to a string called "str", I am struggling to see tho where STR is being given a value its not loaded with any value during creation. It is used in the request function tho and still doesn't appear to have a value???

This is the line im looking at.

status = MFRC522_Request(PICC_REQIDL, str);

That function is saying "Get me the ID (REQuest IDL) and store it in str", so that is where str is getting its contents.

Thanks for your help guys, im going to try and use all the info you have given to get this working, hopefully it will be straight forward now for me :slight_smile:

Hi i seem to be stuck again understanding whats happening here.

  read the data from FIFO               
 for (i=0; i<n; i++)              
  {   backData[i] = Read_MFRC522(FIFODataReg);   
 }           

 }       
 }        

else       

 {   status = MI_ERR;  }          

  }

I can see it is running a loop but its the part at the bottom. The part else.... how would this look in a flowchart?

The else is performed if the previous if is false. You haven't shown that if, so who knows when it would happen.

if (test is true) {
  do this
} else {
  do that
}

In a flowchart it's the "other" option for a decision or condition node - the "false" line in this image:

for (i=0; i<sendLen; i++)
    {   
  Write_MFRC522(FIFODataReg, sendData[i]);    
 }

  //procceed it
 Write_MFRC522(CommandReg, command);
    if (command == PCD_TRANSCEIVE)
    {    
  SetBitMask(BitFramingReg, 0x80);  //StartSend=1,transmission of data starts  
 }   
    
 //waite receive data is finished
 i = 2000; //i should adjust according the clock, the maxium the waiting time should be 25 ms???
    do 
    {
  //CommIrqReg[7..0]
  //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
        n = Read_MFRC522(CommIrqReg);
        i--;
    }
    while ((i!=0) && !(n&0x01) && !(n&waitIRq));

     ClearBitMask(BitFramingReg, 0x80);   //StartSend=0
 
    if (i != 0)
    {    
        if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
        {
            status = MI_OK;
            if (n & irqEn & 0x01)
            {   
    status = MI_NOTAGERR;   //??   
   }

             if (command == PCD_TRANSCEIVE)
            {
                n = Read_MFRC522(FIFOLevelReg);
               lastBits = Read_MFRC522(ControlReg) & 0x07;
                if (lastBits)
                {   
     *backLen = (n-1)*8 + lastBits;   
    }
                else
                {   
     *backLen = n*8;   
    }

                 if (n == 0)
                {   
     n = 1;    
    }
                if (n > MAX_LEN)
                {   
     n = MAX_LEN;   
    }
    
    //read the data from FIFO
                for (i=0; i<n; i++)
                {   
     backData[i] = Read_MFRC522(FIFODataReg);    
    }
            }
        }
        else
        {   
   status = MI_ERR;  
  }
        
    }
 
    //SetBitMask(ControlReg,0x80);           //timer stops
    //Write_MFRC522(CommandReg, PCD_IDLE); 

     return status;

This includes the if statements aswell, these curly brackets get a bit confusing.
Im trying to work out what goes with what.

for (i=0; i<sendLen; i++)
    {   
  Write_MFRC522(FIFODataReg, sendData[i]);    
 }

This part I see like this:

i=0

LOOP WHILE i<sendLen

Write_MFRC522(FIFODataReg, sendData*); *
i = i + 1
LOOP END
* *Write_MFRC522(CommandReg, command);* *
This is a write command after the loop.
```

  • if (command == PCD_TRANSCEIVE)
        {   
      SetBitMask(BitFramingReg, 0x80);  //StartSend=1,transmission of data starts 
    }*
    * *If (command == PCD_TRANSCEIVE)* *Then set the bit mask* *
    i = 2000; //i should adjust according the clock, the maxium the waiting time should be 25 ms???
        do
        {
      //CommIrqReg[7..0]
      //Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
            n = Read_MFRC522(CommIrqReg);
            i--;
        }

    * *This part im not sure on, i think its like this...* *i = 2000* *n = Read_MFRC522(CommIrqReg);* *i = i -1* *But then theres this line after it which makes me think that ^^^ needs to be inside the loop???* *
    while ((i!=0) && !(n&0x01) && !(n&waitIRq));
    ```
    From here im not sure what IF goes with what and what is supposed to branch from where.
    Could you give me any pointers on how its supposed to be read and interpreted???

these curly brackets get a bit confusing.
Im trying to work out what goes with what.

Do yourself a favour and use the Auto Format tool in the IDE (Ctrl/T) This neatly arranges the curly brackets so that pairs match vertically and warns if the number of lefts does not match the number if rights