Help with programming rc522 to pic16F628A

Status
Not open for further replies.

SwordCircuit

Newbie
Joined
Jul 31, 2020
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
41
Hi,
I'm new to microcontroller programming, but have some basic knowledge. Can turn on/blink LED and control it with bluetooth.
Now I want to turn the LED on with a RC522 RFID, but I don't know how to program it. I searched the net, but couldn't get any clear info out off it.

Can anyone help me with programming the my pic? I'm using a pic16F628A.
I'm not asking to be spoon fed and that people will give me directly their code. Just some links, advice or tips will all be appreciated.

Thanks in advance
 

I'll try to help but my own RFID systems are based on HTRC110 devices which are slightly different.
Are you planning to use the SPI interface mode? It is probably the easiest to implement.
What language do you want to program in?

Brian.
 

I'll try to help but my own RFID systems are based on HTRC110 devices which are slightly different.
Are you planning to use the SPI interface mode? It is probably the easiest to implement.
What language do you want to program in?

Brian.
Thanks for the help. I'm programming in c. If you say that SPI interface is the easiest, I will you trust you on that and implement it.
I've also found some libararies online:

Wouter.
 

Attachments

  • MFRC522(.h).txt
    8 KB · Views: 270

That's a header file, not a library but it may be useful.
I usually bit-bang SPI because the protocols are so simple. All you have to do is loop the code to send SCK and at each transition set the data bit to be sent and capture the data coming back. Basically arrange all the bits you want to send before sending them and shift them out one by one, at the same time shift in the received bits to construct the data read back from the RFID interface.

You can either send bits by repeatedly shifting the data and checking the LSB or by using a mask and logic ANDing the data with the mask to see if the result is zero or not, at each pass of the loop you shift a single '1' to the left in the mask.

Brian.
 

Hello,

i used this library, easy to understand,
with a PIC18F ,
verry usefull and **broken link removed**
https://microcontrolandos.blogspot.com/2014/02/pic-rfid-mfrc522.html


Principales fonctions :
MFRC522_Init() - Initialisation du module RC522
MFRC522_Reset() - Reset du module MFRC522.
MFRC522_Halt() - Desactivation du module = Hibernation.
MFRC522_SelectTag( char *serNum ) - Selection type de carte, param servant à l'authentification. serNum = Numero de série. 5 bytes.
MFRC522_Auth ( char authMode, char BlockAddr, char *Sectorkey, char *serNum )
- Authentification et determination du bloc actif en Lecture et Ecriture
authMode: type d'autentification A ( PICC_AUTHENT1A ) ou B ( PICC_AUTHENT1B ).
BlockAddr - Numéro du block. 4 blocs de secteur.
SectorKey - Senha d'autentification 6 bytes. Generalement definit comme 0xFFFFFFFFFFFF.
serNum - Numero de série de la carte sur 5 bytes.
MFRC522_Write( char blockAddr, char *writeData ) Ecriture 16 bytes dans le numero de bloc destinataire, sert de logo d'autentification.
blockAddr - Numero do bloco. Exceto o quarto bloco do setor, que é usado para autenticar.
writeData - 16 bytes en ecriture.
MFRC522_Read( char blockAddr, char *recvData ) Lecture des 16 bytes du numéro de bloc passé, logo d' authentification.
blockAddr - Numero do bloc.
recvData - Retourne les 16 bytes lus dans le tabelau recvData.
MFRC522_isCard( char *TagType ) - Verification presence d'une carte devant le lecteur.
TagType - Retourne un type de carte.
MFRC522_ReadCardSerial( char *str ) Lecture du numero de serie de la carte
str - Retourne un pointeur sur le numero de serie : 5 bytes ( 4 bytes + checksum ).
 
Reactions: FvM

Hi Brian,

Thanks for the tips. I sort of understand what you mean, but not fullly. I don't really know how to get a bit-bang SPI(again, I'm a beginner).
Looked it up, found some code, but it gave my program errors. SSPSTAT, SSPCON, SSPBUF didn't work with my pic(https://circuitdigest.com/microcontroller-projects/pic16f877a-spi-communication-tutorial this site told me to use them, but it could be outdated or unfit for my pic)

Also I couldn't find anything about "mask and logic" on the net. Could you explain that to me, or give me a link.

Sorry for all these noob questions
 
Last edited:

You must not necessarily use bit-bang SPI. Presuming you have a recent C compiler, e.g. XC8, it comes with complete IO libraries including SPI.
 

You must not necessarily use bit-bang SPI. Presuming you have a recent C compiler, e.g. XC8, it comes with complete IO libraries including SPI.
Could you perhaps tell me where to find those libraries? I can't find them anywhere
 

I think FvM is referring to MCC which generates a lot of the basic code for you. MCC is a free add-on to the MPLABx IDE.
If you want to, SPI is a very simple module to control yourself and there are lots of examples here and also elsewhere on the Internet.
Susan
 

The 16F628A is quite an old device and only has 2K of program memory and 224 bytes of RAM, it can't run code for 18F devices because the memory banking is different and some instructions are not implemented. General purpose libraries tend to include routines that are 'generic' rather than concentrating only your specific requirements so unless your compiler is clever enough to pick out only the bits it needs you tend to waste a lot of code space. Note that the 16F628A does not have an internal SPI interface module so you HAVE to implement it in software anyway.

Of all the protocols, SPI is probably easiest to implement though. It is a simple 'one data bit per one clock pulse' system. All you have to do is assign pins for the data input (from the RFID), data output (to the RFID) and clock (to the RFID) then set or read the data bits as you drive the clock line high and low. There should also be an output to the RFID (possibly the 'SS' pin on the card reader) to tell it the first bit is on its way and then afterwards to say 'the last bit has been sent'. The exact sequence of bits is in the MFRC522 data sheet. You probably only want to send a few of the many commands, most likely the one to read the tag serial number.

Brian.
 

Thanks for the tip. Still new at this though. I don't fully understand how to implement that in the code. Looked online, but found nothing. Could you perhaps give me a link, or an example of code where it's explained step by step.

I know it's probably too much to ask.
 

This is a snippet of MUCH larger code for writing and reading bytes from an SPI memory but the device isn't important, all SPI transfers are basically the same. "DataTo RAM" is the pin transferring data from the MCU to the device and "DataFromRAM" is the equivalent input pin reading data back from it. "ClkToRAM" is the clock output from the MCU to the device. Normally you have an additional signal to enable the device, you make it active (usually low) at the start of transferring data then make it inactive at the end. This might be the 'SS' pin on your reader but check its data sheet to be sure.

Code:
//*******************************************************************************
// sends one unsigned char from the SDO output pin.
void SendSPIByte(unsigned char SPIData)
{
    unsigned char Clocks = 8;

    while(Clocks)
    {
        DataToRAM = SPIData & 0x80;
        SPIData = SPIData << 1;
        delay_us(1);
        ClkToRAM = 1;
        ClkToRAM = 0;
        Clocks--;
    }
}

//*******************************************************************************
// reads one unsigned char from the SDI input pin
unsigned char GetSPIByte()
{
    unsigned char SPIData =0;
    unsigned char Clocks = 8;
   
    DataToRAM = 0;
    while(Clocks)
    {
        ClkToRAM = 1;
        delay_us(1);
        if(DataFromRAM) SPIData |= (1 << (Clocks - 1));
        ClkToRAM = 0;
        Clocks--;
        delay_us(1);
    }
    return SPIData;
}

Use #define to associate the real pin with its name, for example you might use:
#define DataFromRam PORTA,0
#define DataToRAM PORTA,1

In my application it is used to read configuration from a 1Mb EEPROM and store logging information back into it at a different address. To access the different registers in your device you typically have to send the address register followed by data or some command, in other words use "SendSPIByte()" more than once before deactivating the enable signal.

Brian.
 


Hello, I'm using this library, but I'm having some problems. Can you help me? I did a post with the doubt:
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…