spi read from an arduino program

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

spi read from an arduino program

Post by cobra1 »

Hi.

Could this be converted to a flowcode chart??

uchar Read_MFRC522(uchar addr)

{uchar val;digitalWrite(chipSelectPin, LOW);

//address format:1XXXXXX0
SPI.transfer(((addr<<1)&0x7E) | 0x80);
val =SPI.transfer(0x00);

digitalWrite(chipSelectPin, HIGH);

return val;

I dont understand where return val is pulled from????

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: spi read from an arduino program

Post by Benj »

Hello,

Yes this looks fairly simple to achieve. Which version of Flowcode are you wanting to use?

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: spi read from an arduino program

Post by cobra1 »

Im using version 3. :-)

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: spi read from an arduino program

Post by cobra1 »

Do you understand where the return val variable is being loaded from??

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: spi read from an arduino program

Post by cobra1 »

cobra1 wrote:Hi.

Could this be converted to a flowcode chart??

uchar Read_MFRC522(uchar addr)

{uchar val;digitalWrite(chipSelectPin, LOW);

//address format:1XXXXXX0
SPI.transfer(((addr<<1)&0x7E) | 0x80);
val =SPI.transfer(0x00);

digitalWrite(chipSelectPin, HIGH);

return val;

I dont understand where return val is pulled from????
Im not at a computer so cant test this yet but i wanted to see if this looks right.

CS = Low

addr = 0x14
Spidata = (((addr<<1)&0x7E) | 0x80);
SEND SPI BYTE (Spidata)

RECEIVE SPI BYTE (Val) not sure why the origional has 0x00
Return Val = Val

CS = High


Does this look like it will work??

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: spi read from an arduino program

Post by Benj »

Hello,

Yep that looks spot on.
RECEIVE SPI BYTE (Val) not sure why the origional has 0x00
A SPI transaction simultaneously transfers and receives a byte at the same time. Generally you only want to be able to read or write at any one time so the Flowcode macros mask off the surplus value to make things more straightforward. In your case the 0x00 is being sent when calling the receive macro but really it could be anything as it is the incoming value you are interested in.

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: spi read from an arduino program

Post by cobra1 »

Thanks for clearing that up ben. Will see if it works when i get home :-)

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: spi read from an arduino program

Post by cobra1 »

I have another question...


How would you deal with this?

WRITE_SPI (Register, Temp | Mask)

and also

WRITE_SPI (Register, Temp & (~Mask))


Mask = 0x80

Temp is a byte read from a read function.


I havnt done masking before so this is a bit new to me.

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 474 times
Been thanked: 458 times
Contact:

Re: spi read from an arduino program

Post by dazz »

Hi Cobra
Could you post the arduino code in a code box as it might make it easier to help

Regards
Dazz
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: spi read from an arduino program

Post by cobra1 »

Code: Select all

* Function:SetBitMask
 * Description:set RC522 register bit
 * Input parameter:reg--register address;mask--value
 * Return:null
 */
void SetBitMask(uchar reg, uchar mask)  
{
    uchar tmp;
    tmp = Read_MFRC522(reg);
    Write_MFRC522(reg, tmp | mask);  // set bit mask
}

 
 /*
 * Function:ClearBitMask
 * Description:clear RC522 register bit
 * Input parameter:reg--register address;mask--value
 * Return:null
 */
void ClearBitMask(uchar reg, uchar mask)  
{
    uchar tmp;
    tmp = Read_MFRC522(reg);
    Write_MFRC522(reg, tmp & (~mask));  // clear bit mask
} 


Post Reply