CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

RC522 error when connect PIC16F877a

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ntrungtruc2003



Joined: 16 Feb 2011
Posts: 42

View user's profile Send private message

RC522 error when connect PIC16F877a
PostPosted: Thu Sep 24, 2015 5:11 am     Reply with quote

Dear Sir.

I use MFRC522 connect to PIC16f877a using SPI sofware.
MF522_NSS --->PIN_E2
MF522_SCK --->PIN_C3
MF522_SI --->PIN_C4
MF522_SO --->PIN_C5
MF522_RST --->PIN_E0

I found this code from here
http://laptrinhpic.info/ung-dung-rfid-trong-dieu-khien-voi-pic16f877a-ccs-c/

The problem is PIC16F877A can not communicate with rc522.
The value returned by MFRC522_Request() function is equal to 2(error)

How can I fix it? Please help.
I have also try with PIC18LF4620 (3.3V)

Thank you very much.

mrfc522.c


Code:
#include "mfrc522.h"
#define MAXRLEN    18


                                   
uint8_t MFRC522_Request(uint8_t req_code,uint8_t *pTagType)
{
   uint8_t status = MI_ERR;
   uint16_t Length;
   uint8_t Buffer[MAXRLEN];
   ClearBitMask (STATUS2_REGISTER, 0x08) ;
   MFRC522_WriteRegister (BIT_FRAMING_REGISTER, 0x07) ;
   SetBitMask (TX_CONTROL_REGISTER, 0x03) ;
   
   
   Buffer[0] = req_code;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 1, Buffer,&Length);

   IF ((status == MI_OK) && (Length == 0x10))
   {
      * pTagType = Buffer[0];
      * (pTagType + 1) = Buffer[1];
   }

   ELSE
   {
      status = MI_ERR;
   }

   RETURN status;
}


uint8_t MFRC522_Anticoll(uint8_t *pSnr)
{
   uint8_t status;
   uint8_t i, snr_check = 0;
   uint16_t Length;
   uint8_t Buffer[MAXRLEN];
   
   ClearBitMask (STATUS2_REGISTER, 0x08) ;
   MFRC522_WriteRegister (BIT_FRAMING_REGISTER, 0x00) ;
   ClearBitMask (COLLISION_REGISTER, 0x80) ;
   Buffer[0] = PICC_ANTICOLL1;
   Buffer[1] = 0x20;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 2, Buffer,&Length);

   IF (status == MI_OK)
   {
      FOR (i = 0; i < 4; i++)
      {
         * (pSnr + i) = Buffer[i];
         snr_check ^= Buffer[i];
      }

      IF (snr_check != Buffer[i])
      { status = MI_ERR; }
   }

   
   SetBitMask (COLLISION_REGISTER, 0x80) ;
   RETURN status;
}


uint8_t MFRC522_Select(uint8_t *pSnr)
{
   uint8_t status;
   uint8_t i;
   uint16_t Length;
   uint8_t Buffer[MAXRLEN];
   
   Buffer[0] = PICC_ANTICOLL1;
   Buffer[1] = 0x70;
   Buffer[6] = 0;
   FOR (i = 0; i < 4; i++)
   {
      Buffer[i + 2] = * (pSnr + i);
      Buffer[6] ^= *(pSnr + i) ;
   }

   CalulateCRC (Buffer, 7,&Buffer[7]) ;
   
   ClearBitMask (STATUS2_REGISTER, 0x08) ;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 9, Buffer,&Length);
   
   IF ((status == MI_OK) && (Length == 0x18))
   { status = MI_OK; }
   ELSE
   { status = MI_ERR; }
   RETURN status;
}

 
uint8_t MFRC522_AuthState(uint8_t auth_mode,uint8_t addr,uint8_t *pKey,uint8_t *pSnr)
{
   uint8_t status;
   uint16_t Length;
   uint8_t i, Buffer[MAXRLEN];
   Buffer[0] = auth_mode;
   Buffer[1] = addr;
   FOR (i = 0; i < 6; i++)
   {
      Buffer[i + 2] = * (pKey + i);
   }

   FOR (i = 0; i < 4; i++)
   { Buffer[i + 8] = * (pSnr + i); }
   status = MFRC522_ComMF522 (MFRC522_AUTHENT, Buffer, 12, Buffer,&Length);

   if ((status != MI_OK) || (! (MFRC522_ReadRegister (STATUS2_REGISTER)&0x08)))
   { status = MI_ERR; }
   
   RETURN status;
}


uint8_t MFRC522_Read(uint8_t addr,uint8_t *pData)
{
   uint8_t status;
   uint16_t Length;
   uint8_t i, Buffer[MAXRLEN];
   Buffer[0] = PICC_READ;
   Buffer[1] = addr;
   CalulateCRC (Buffer, 2,&Buffer[2]) ;
   
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 4, Buffer,&Length);

   IF ((status == MI_OK) && (Length == 0x90))
   {
      FOR (i = 0; i < 16; i++)
      { * (pData + i) = Buffer[i]; }
   }

   ELSE
   { status = MI_ERR; }
   
   RETURN status;
}

uint8_t MFRC522_Write(uint8_t addr,uint8_t *pData)
{
   uint8_t status;
   uint16_t Length;
   uint8_t i, Buffer[MAXRLEN];
   
   Buffer[0] = PICC_WRITE;
   Buffer[1] = addr;
   CalulateCRC (Buffer, 2,&Buffer[2]) ;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 4, Buffer,&Length);

   IF ((status != MI_OK) || (Length != 4) || ((Buffer[0]&0x0F) != 0x0A) )
   { status = MI_ERR; }
   
   IF (status == MI_OK)
   {
      FOR (i = 0; i < 16; i++)
      { Buffer[i] = * (pData + i) ; }
      CalulateCRC (Buffer, 16,&Buffer[16]) ;
      status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 18, Buffer,&Length);

      IF ((status != MI_OK) || (Length != 4) || ((Buffer[0]&0x0F) != 0x0A) )
      { status = MI_ERR; }
   }

   
   RETURN status;
}

uint8_t MFRC522_Value(uint8_t dd_mode,uint8_t addr,uint8_t *pValue)
{
   uint8_t status;
   uint16_t Length;
   uint8_t i, Buffer[MAXRLEN];
   
   Buffer[0] = dd_mode;
   Buffer[1] = addr;
   CalulateCRC (Buffer, 2,&Buffer[2]) ;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 4, Buffer,&Length);

   IF ((status != MI_OK) || (Length != 4) || ((Buffer[0]&0x0F) != 0x0A) )
   { status = MI_ERR; }
   
   IF (status == MI_OK)
   {
      FOR (i = 0; i < 16; i++)
      { Buffer[i] = * (pValue + i) ; }
      CalulateCRC (Buffer, 4,&Buffer[4]) ;
      Length = 0;
      status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 6, Buffer,&Length);

      IF (status != MI_ERR)
      { status = MI_OK; }
   }

   
   IF (status == MI_OK)
   {
      Buffer[0] = PICC_TRANSFER;
      Buffer[1] = addr;
      CalulateCRC (Buffer, 2,&Buffer[2]);
     
      status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 4, Buffer,&Length);

      IF ((status != MI_OK) || (Length != 4) || ((Buffer[0]&0x0F) != 0x0A) )
      { status = MI_ERR; }
   }

   RETURN status;
}

uint8_t MFRC522_BakValue(uint8_t sourceaddr, uint8_t goaladdr)
{
   uint8_t status;
   uint16_t Length;
   uint8_t Buffer[MAXRLEN];
   Buffer[0] = PICC_RESTORE;
   Buffer[1] = sourceaddr;
   CalulateCRC (Buffer, 2,&Buffer[2]) ;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 4, Buffer,&Length);

   IF ((status != MI_OK) || (Length != 4) || ((Buffer[0]&0x0F) != 0x0A) )
   { status = MI_ERR; }
   
   IF (status == MI_OK)
   {
      Buffer[0] = 0;
      Buffer[1] = 0;
      Buffer[2] = 0;
      Buffer[3] = 0;
      CalulateCRC (Buffer, 4,&Buffer[4]) ;
      status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 6, Buffer,&Length);

      IF (status != MI_ERR)
      { status = MI_OK; }
   }

   
   IF (status != MI_OK)
   { RETURN MI_ERR; }
   
   Buffer[0] = PICC_TRANSFER;
   Buffer[1] = goaladdr;
   CalulateCRC (Buffer, 2,&Buffer[2]) ;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 4, Buffer,&Length);

   IF ((status != MI_OK) || (Length != 4) || ((Buffer[0]&0x0F) != 0x0A) )
   { status = MI_ERR; }
   RETURN status;
}

uint8_t MFRC522_Halt(VOID)
{
   uint8_t status;
   uint16_t Length;
   uint8_t Buffer[MAXRLEN];
   Buffer[0] = PICC_HALT;
   Buffer[1] = 0;
   CalulateCRC (Buffer, 2,&Buffer[2]) ;
   status = MFRC522_ComMF522 (MFRC522_TRANSCEIVE, Buffer, 4, Buffer,&Length);
   RETURN MI_OK;
}

void CalulateCRC(uint8_t *pIndata,uint8_t len,uint8_t *pOutData)
{
   uint8_t i, n;
   ClearBitMask (DIV_IRQ_REGISTER, 0x04) ;
   MFRC522_WriteRegister (COMMAND_REGISTER, MFRC522_IDLE) ;
   SetBitMask (FIFO_LEVEL_REGISTER, 0x80) ;
   FOR (i = 0; i < len; i++)
   { MFRC522_WriteRegister (FIFO_DATA_REGISTER, * (pIndata + i)); }
   MFRC522_WriteRegister (COMMAND_REGISTER, MFRC522_CALCCRC);
   i = 0xFF;

   DO
   {
      n = MFRC522_ReadRegister (DIV_IRQ_REGISTER);
      i--;
   }

   WHILE ((i != 0)&& ! (n&0x04));
   pOutData[0] = MFRC522_ReadRegister (CRC_RESULT_L_REGISTER);
   pOutData[1] = MFRC522_ReadRegister (CRC_RESULT_M_REGISTER);
}

uint8_t MFRC522_Reset(VOID)
{
   output_bit (MF522_RST, 1) ;
   delay_us (1);
   output_bit (MF522_RST, 0) ;
   delay_us (1);
   output_bit (MF522_RST, 1) ;
   delay_us (1);
   MFRC522_WriteRegister (COMMAND_REGISTER, MFRC522_RESETPHASE) ;
   delay_us (1);
   
   MFRC522_WriteRegister (MODE_REGISTER, 0x3D);
   MFRC522_WriteRegister (TIMER_RELOAD_L_REGISTER, 30);
   MFRC522_WriteRegister (TIMER_RELOAD_H_REGISTER, 0) ;
   MFRC522_WriteRegister (TMODE_REGISTER, 0x8D) ;
   MFRC522_WriteRegister (TIMER_PRESCALER_REGISTER, 0x3E) ;
   MFRC522_WriteRegister (TX_ASK_REGISTER, 0x40) ;
   
   RETURN MI_OK;
}

uint8_t MFRC522_ReadRegister(uint8_t Address)
{
   uint8_t i, ucAddr;
   uint8_t ucResult = 0;
   output_bit (MF522_SCK, 0);
   output_bit (MF522_NSS, 0);
   ucAddr = ( (Address<<1)&0x7E)|0x80;
   //Write spi
   FOR (i = 8; i > 0; i--)
   {
      output_bit (MF522_SI, ((ucAddr&0x80) == 0x80));
      output_bit (MF522_SCK, 1);
      ucAddr <<= 1;
      output_bit (MF522_SCK, 0);
   }
//SPI read
   FOR (i = 8; i > 0; i--)
   {
      output_bit (MF522_SCK, 1);
      ucResult <<= 1;
      ucResult|= (INT1) input (MF522_SO);
      output_bit (MF522_SCK, 0);
   }

   
   output_bit (MF522_NSS, 1);
   output_bit (MF522_SCK, 1);
   RETURN ucResult;
}

void MFRC522_WriteRegister(uint8_t Address, uint8_t value)
{
   
   uint8_t i, ucAddr;
   output_bit (MF522_SCK, 0);
   output_bit (MF522_NSS, 0);
   ucAddr = ( (Address<<1)&0x7E);
   FOR (i = 8; i > 0; i--)
   {
      output_bit (MF522_SI, ( (ucAddr&0x80) == 0x80));
      output_bit (MF522_SCK, 1);
      ucAddr <<= 1;
      output_bit (MF522_SCK, 0);
   }

   
   FOR (i = 8; i > 0; i--)
   {
      output_bit (MF522_SI, ( (value&0x80) == 0x80));
      output_bit (MF522_SCK, 1);
      value <<= 1;
      output_bit (MF522_SCK, 0);
   }

   output_bit (MF522_NSS, 1);
   output_bit (MF522_SCK, 1);
}

void SetBitMask(uint8_t reg,uint8_t mask) 
{
   uint8_t tmp = 0x0;
   tmp = MFRC522_ReadRegister (reg);
   MFRC522_WriteRegister (reg, tmp|mask); // set bit mask
}

void ClearBitMask(uint8_t reg,uint8_t mask) 
{
   uint8_t tmp = 0x0;
   tmp = MFRC522_ReadRegister (reg);
   MFRC522_WriteRegister (reg, tmp&~mask); // clear bit mask
}

uint8_t MFRC522_ComMF522(uint8_t  Command,
                         uint8_t * pInData,
                         uint8_t InLenByte,
                         uint8_t * pOutData,
                         uint16_t * pOutLenBit)
{
                            uint8_t status = MI_ERR;
                            uint8_t irqEn = 0x00;
                            uint8_t waitFor = 0x00;
                            uint8_t lastBits;
                            uint8_t n;
                            uint16_t i;

                            SWITCH (Command)
                            {
                               CASE MFRC522_AUTHENT:
                               irqEn = 0x12;
                               waitFor = 0x10;
                               BREAK;

                               CASE MFRC522_TRANSCEIVE:
                               irqEn = 0x77;
                               waitFor = 0x30;
                               BREAK;

                               DEFAULT:
                               BREAK;
                            }

                           
                            MFRC522_WriteRegister (IE_REGISTER, irqEn|0x80) ;
                            ClearBitMask (IRQ_REGISTER, 0x80) ;
                            MFRC522_WriteRegister (COMMAND_REGISTER, MFRC522_IDLE) ;
                            SetBitMask (FIFO_LEVEL_REGISTER, 0x80) ;
                           
                            FOR (i = 0; i < InLenByte; i++)
                            {
                               MFRC522_WriteRegister (FIFO_DATA_REGISTER, pInData[i]);
                            }

                            MFRC522_WriteRegister (COMMAND_REGISTER, Command);

                            IF (Command == MFRC522_TRANSCEIVE)
                            { SetBitMask (BIT_FRAMING_REGISTER, 0x80); }
                           
                            i = 600; //25ms
                            DO
                            {
                               n = MFRC522_ReadRegister (IRQ_REGISTER);
                               i--;
                            }

                            WHILE ((i != 0)&& ! (n&0x01) && ! (n&waitFor));
                            ClearBitMask (BIT_FRAMING_REGISTER, 0x80) ;
                           
                            IF (i != 0)
                            {
                               if ( ! (MFRC522_ReadRegister (ERROR_REGISTER)&0x1B))
                               {
                                  status = MI_OK;

                                  IF (n&irqEn&0x01)
                                  {
                                     status = MI_NOTAGERR;
                                  }

                                  IF (Command == MFRC522_TRANSCEIVE)
                                  {
                                     n = MFRC522_ReadRegister (FIFO_LEVEL_REGISTER);
                                     lastBits = MFRC522_ReadRegister (CONTROL_REGISTER)&0x07;

                                     IF (lastBits)
                                     {
                                        * pOutLenBit = (n - 1) * 8 + lastBits;
                                     }

                                     ELSE
                                     {
                                        * pOutLenBit = n*8;
                                     }

                                     IF (n == 0)
                                     {
                                        n = 1;
                                     }

                                     IF (n > MAXRLEN)
                                     {
                                        n = MAXRLEN;
                                     }

                                     FOR (i = 0; i < n; i++)
                                     {
                                        pOutData[i] = MFRC522_ReadRegister (FIFO_DATA_REGISTER);
                                     }
                                  }
                               }

                               ELSE
                               {
                                  status = MI_ERR;
                               }
                            }

                            SetBitMask (CONTROL_REGISTER, 0x80); // stop timer now
                            MFRC522_WriteRegister (COMMAND_REGISTER, MFRC522_IDLE);
                            RETURN status;
}

void MFRC522_AntennaOn ()
                         {
                            uint8_t i;
                            i = MFRC522_ReadRegister (TX_CONTROL_REGISTER);

                            IF (! (i&0x03))
                            {
                               SetBitMask (TX_CONTROL_REGISTER, 0x03);
                            }
                         }

                         VOID MFRC522_AntennaOff ()
                         {
                            ClearBitMask (TX_CONTROL_REGISTER, 0x03);
                         }


mfrc522.h

Code:
#ifndef __MFRC522_H
#define __MFRC522_H

#include "mfrc522.h"

uint8_t MFRC522_Reset(void);
void MFRC522_AntennaOn(void);
void MFRC522_AntennaOff(void);
uint8_t MFRC522_Request(uint8_t req_code,uint8_t *pTagType);   
uint8_t MFRC522_Anticoll(uint8_t *pSnr);
uint8_t MFRC522_Select(uint8_t *pSnr);         
uint8_t MFRC522_AuthState(uint8_t auth_mode,uint8_t addr,uint8_t *pKey,uint8_t *pSnr);     
uint8_t MFRC522_Read(uint8_t addr,uint8_t *pData);     
uint8_t MFRC522_Write(uint8_t addr,uint8_t *pData);   
uint8_t MFRC522_Value(uint8_t dd_mode,uint8_t addr,uint8_t *pValue);   
uint8_t MFRC522_BakValue(uint8_t sourceaddr, uint8_t goaladdr);                                 
uint8_t MFRC522_Halt(void);
uint8_t MFRC522_ComMF522(uint8_t Command,
                 uint8_t *pInData,
                 uint8_t InLenByte,
                 uint8_t *pOutData,
                 uint16_t  *pOutLenBit);
void CalulateCRC(uint8_t *pIndata,uint8_t len,uint8_t *pOutData);
void MFRC522_WriteRegister(uint8_t Address,uint8_t value);
uint8_t MFRC522_ReadRegister(uint8_t Address);
void SetBitMask(uint8_t reg,uint8_t mask);
void ClearBitMask(uint8_t reg,uint8_t mask);
/*********************************************************************************
*********************************************************************************/
//MF522 Command word
#define     MFRC522_IDLE              0x00
#define     MFRC522_AUTHENT           0x0E               
#define     MFRC522_RECEIVE           0x08               
#define     MFRC522_TRANSMIT          0x04               
#define     MFRC522_TRANSCEIVE        0x0C               
#define     MFRC522_RESETPHASE        0x0F               
#define     MFRC522_CALCCRC           0x03               

 // Mifare_One card command word 
#define     PICC_REQIDL           0x26               
#define     PICC_REQALL           0x52               
#define     PICC_ANTICOLL1        0x93               
#define     PICC_ANTICOLL2        0x95               
#define     PICC_AUTHENT1A        0x60               
#define     PICC_AUTHENT1B        0x61               
#define     PICC_READ             0x30               
#define     PICC_WRITE            0xA0               
#define     PICC_DECREMENT        0xC0               
#define     PICC_INCREMENT        0xC1               
#define     PICC_RESTORE          0xC2               
#define     PICC_TRANSFER         0xB0               
#define     PICC_HALT             0x50               


#define     DEF_FIFO_LENGTH          64                 //FIFO size=64byte

 //------------------MFRC522 Register---------------
// PAGE 0         
#define     RFU00                       0x00   
#define     COMMAND_REGISTER            0x01   
#define     IE_REGISTER                 0x02   
#define     DIV_IE_REGISTER             0x03   
#define     IRQ_REGISTER                0x04   
#define     DIV_IRQ_REGISTER            0x05
#define     ERROR_REGISTER              0x06   
#define     STATUS1_REGISTER            0x07   
#define     STATUS2_REGISTER            0x08   
#define     FIFO_DATA_REGISTER          0x09
#define     FIFO_LEVEL_REGISTER         0x0A
#define     WATER_LEVEL_REGISTER        0x0B
#define     CONTROL_REGISTER            0x0C
#define     BIT_FRAMING_REGISTER        0x0D
#define     COLLISION_REGISTER          0x0E
#define     RFU0F                       0x0F
// PAGE 1     
#define     RFU10                       0x10
#define     MODE_REGISTER               0x11
#define     TX_MODE_REGISTER            0x12
#define     RX_MODE_REGISTER            0x13
#define     TX_CONTROL_REGISTER         0x14
#define     TX_ASK_REGISTER             0x15
#define     TX_SEL_REGISTER             0x16
#define     RX_SEL_REGISTER             0x17
#define     RX_THRESHOLD_REGISTER       0x18
#define     DEMOD_REGISTER              0x19
#define     RFU1A                       0x1A
#define     RFU1B                       0x1B
#define     MIFARE_REGISTER             0x1C
#define     RFU1D                       0x1D
#define     RFU1E                       0x1E
#define     SERIAL_SPEED_REGISTER       0x1F
// PAGE 2   
#define     RFU20                       0x20 
#define     CRC_RESULT_M_REGISTER       0x21
#define     CRC_RESULT_L_REGISTER       0x22
#define     RFU23                       0x23
#define     MOD_WIDTH_REGISTER          0x24
#define     RFU25                       0x25
#define     RF_CONFIG_REGISTER          0x26
#define     GSN_REGISTER                0x27
#define     CWF_CONFIG_REGISTER         0x28
#define     MODGS_CONFIG_REGISTER       0x29
#define     TMODE_REGISTER              0x2A
#define     TIMER_PRESCALER_REGISTER    0x2B
#define     TIMER_RELOAD_H_REGISTER     0x2C
#define     TIMER_RELOAD_L_REGISTER     0x2D
#define     TIMER_VALUE_H_REGISTER      0x2E
#define     TIMER_VALUE_L_REGISTER      0x2F
// PAGE 3     
#define     RFU30                       0x30
#define     TEST_SEL_1_REGISTER         0x31
#define     TEST_SEL_2_REGISTER         0x32
#define     TEST_PIN_EN_REGISTER        0x33
#define     TEST_PIN_VALUE_REGISTER     0x34
#define     TEST_BUS_REGISTER           0x35
#define     AUTO_TEST_REGISTER          0x36
#define     VERSION_REGISTER            0x37
#define     ANALOG_TEST_REGISTER        0x38
#define     TEST_ADC1_REGISTER          0x39 
#define     TEST_ADC2_REGISTER          0x3A   
#define     TEST_ADC_REGISTER           0x3B   
#define     RFU3C                       0x3C   
#define     RFU3D                       0x3D   
#define     RFU3E                       0x3E   
#define     RFU3F                          0x3F
//And MF522 The error code is returned when communication
#define     MI_OK                       0
#define     MI_NOTAGERR                 1
#define     MI_ERR                      2

#endif


main.c

Code:
#include <16F877A.h>
#device ADC=10
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used FOR I/O
#use delay(crystal=12MHz)
#define DELAY 10   
#define RC PIN_E0
#define LCD_ENABLE_PIN PIN_D2             
#define LCD_RS_PIN PIN_D0                 
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D4   
#define LCD_DATA5 PIN_D5                                   
#define LCD_DATA6 PIN_D6                             
#define LCD_DATA7 PIN_D7
/*
SPI SS      RFID-RC522(SDA) - SlaveSelect (SS)
SPI SDI     RFID-RC522(MOSI)
SPI SDO     RFID-RC522(MISO)
SPI SCK     RFID-RC522(SCK)           
 */
#define MF522_NSS PIN_E2
#define MF522_SCK PIN_C3
#define MF522_SI  PIN_C4
#define MF522_SO  PIN_C5             
#define MF522_RST PIN_E0               
#include <LCD_TM.c>
//#include <Built_in.h>

typedef   SIGNED char int8_t;

typedef   SIGNED int int16_t;
typedef   SIGNED long int32_t;
typedef   UNSIGNED char uint8_t;
typedef   UNSIGNED int  uint16_t;                                 
typedef   UNSIGNED long uint32_t;
typedef   float FLOAT32_t; 
#include "mfrc522.c"
uint8_t  data2[4]  = {0x12,0,0,0};
uint8_t  DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t g_ucTempbuf[20];
                                                     
                                                     
void main()
{
   
   uint8_t status;
   uint8_t TempStr[30];
   uint8_t Str[30];
   lcd_init ();
   //printf (LCD_PUTC, "\f MuaLinhKien.Vn \nPIC 16 / 18 Basic Kit");
   status = MFRC522_Write (1, Str);
   printf (LCD_PUTC, "\n %4x", status);
   status = MFRC522_BakValue (1, 2);
   status = MFRC522_Value (PICC_DECREMENT, 1, data2);
   
   MFRC522_Reset () ;
   MFRC522_AntennaOff ();
   MFRC522_AntennaOn ();
   delay_ms (2000);
  // printf (LCD_PUTC, "%2X", status);
   WHILE (true)
   {
      //REQUEST
      status = MFRC522_Request (PICC_REQALL, g_ucTempbuf);
      printf (LCD_PUTC, "\n %4x", status);
      IF (status != MI_OK)
      {
         //LED_GREEN = 1;   
         
         CONTINUE;
      }

      //LED_GREEN = 0;
      status = MFRC522_Anticoll (g_ucTempbuf);

      IF (status != MI_OK)
      {
      CONTINUE;
      }
    //   printf (LCD_PUTC, "\n %4x", status);
    printf (LCD_PUTC, "\fID:%2X:%2X:%2X:%2X", (uint16_t)g_ucTempbuf[0], (uint16_t) g_ucTempbuf[1], (uint16_t) g_ucTempbuf[2], (uint16_t) g_ucTempbuf[3]);
     
      status = MFRC522_Select (g_ucTempbuf);

      //LUA CHON CARD
      IF (status != MI_OK)
      { CONTINUE; }
      status = MFRC522_AuthState (PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);

      IF (status != MI_OK)
      { CONTINUE; }
      status = MFRC522_Read (1, g_ucTempbuf);

      IF (status != MI_OK)
      { CONTINUE; }
     
                                         
      g_ucTempbuf[15] = 0;
     
      printf (LCD_PUTC, "\n %s", g_ucTempbuf);
      MFRC522_Halt () ;   //Ngu dong
   }
}                                     

temtronic



Joined: 01 Jul 2010
Posts: 9118
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Sep 24, 2015 5:55 am     Reply with quote

hmm.. most modules these days are 3 volt devices so connecting them to a 5 volt PIC is bad! Without seeing your hardware schematic it is very possible you've destroyed the RFID module. If the driver was written for the 877A and wired correctly then contact the creator of the driver as he is the best person to ask why it doesn't work.

If it really is a 3 volt device then use a 3 volt PIC. Most(all) PICs these days do have an 'L' version.

All this is assuming you've gotten the 1Hz LED program to run properly.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19234

View user's profile Send private message

PostPosted: Wed Oct 07, 2015 2:34 am     Reply with quote

Start with you need to be using the LF4620, not the 877A.

You are over-driving the inputs of the MFRC522, and the module will not produce the output needed to drive the PIC reliably.
I see Temtronic has just made the same comment. You may well have damaged either the PIC or the device. :(

You cannot connect 3.3v devices directly to 5v devices and expect them to work (there are a few exceptions, some 3.3v devices have '5v tolerant' inputs, and some PIC inputs have a low enough threshold to be driven by 3v devices - but _not_ SPI_). It's like expecting a Diesel car to run on petrol....

So you either need to add buffering, or use the 3.3v PIC.

Unfortunately, the way the driver is written, the error code doesn't actually tell you anything, except that it didn't work. So once you have connected the 3.3v device (remember that on SPI, the SDO line on the PIC goes to the data in line on the device, and the SDI line on the PIC is fed from the data out line on the device), you need to add some diagnostics, and actually display what value you are getting back from each read. You then have a hope of starting to do some diagnostics, assuming the device is not damaged...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group