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; //??
}
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.
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.
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
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".
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
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???
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.
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.
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