Stefox
Newbie
Good morning,
I have a small problem with my program, I've been completely stuck for several days and I need your ideas!
I am developing a small program to read an RFID module (MFRC522) in SPI. To control it and interpret the data, I use a PIC18F46K22. I develop on MPLAB 6.20 XC8 2.50.
Currently, the program must, if it sees an RFID chip, turn off an LED. Nothing more for the moment. However, I presented him with many types of RFID cards, including the one supplied with the MFRC522 and the LED did not go out at all.
I replaced the PIC18F46K22 without success... I tested the MFRC522 with an Arduino on a serial interface and it reacted well with all the cards I presented to it. I measured the voltages, I have 5 volts on the PIC18F and I have 3.3 volts via a voltage divider on my MFRC522.
I've tried a lot of things and I admit that I'm completely dry.
Could you enlighten me and tell me what's wrong?
Here is my program attached if anyone is kind enough to take a look! ^^
For wiring:
β’ MFRC522: SCK β RC3
MOSI β RC5
MISO β RC4
SS β RA5
RST β Not connected
IRQ β Not connected
β’ LED: RA0
I thank you in advance for your enlightenment,
Stefox!
I have a small problem with my program, I've been completely stuck for several days and I need your ideas!
I am developing a small program to read an RFID module (MFRC522) in SPI. To control it and interpret the data, I use a PIC18F46K22. I develop on MPLAB 6.20 XC8 2.50.
Currently, the program must, if it sees an RFID chip, turn off an LED. Nothing more for the moment. However, I presented him with many types of RFID cards, including the one supplied with the MFRC522 and the LED did not go out at all.
I replaced the PIC18F46K22 without success... I tested the MFRC522 with an Arduino on a serial interface and it reacted well with all the cards I presented to it. I measured the voltages, I have 5 volts on the PIC18F and I have 3.3 volts via a voltage divider on my MFRC522.
I've tried a lot of things and I admit that I'm completely dry.
Could you enlighten me and tell me what's wrong?
Here is my program attached if anyone is kind enough to take a look! ^^
For wiring:
β’ MFRC522: SCK β RC3
MOSI β RC5
MISO β RC4
SS β RA5
RST β Not connected
IRQ β Not connected
β’ LED: RA0
I thank you in advance for your enlightenment,
Stefox!
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 #include <xc.h> #define MFRC522_CS LATAbits.LATA5 // Chip Select for MFRC522 #define _XTAL_FREQ 16000000 // Internal oscillation frequency 16 MHz #define LED PORTAbits.RA0 #pragma config FOSC = INTIO67 //Fosc=1000 Internal oscillator selection, I/O on RA6 and RA7 #pragma config PLLCFG = ON //4xPLL #pragma config PRICLKEN = ON //primary clock #pragma config FCMEN = OFF //Fail-Safe Clock Monitor #pragma config LVP = OFF //In situ programming (ICSP) at low voltage #pragma config PWRTEN = OFF //Startup timer #pragma config WDTEN = OFF //Watchdog disabled #pragma config BOREN = OFF //Power fluctuation detection (Brown-out) #pragma config IESO = OFF //after PWRT and POR see datasheet pg42 // Function prototypes void SPI_Init(void); unsigned char SPI_Transmit(unsigned char data); void MFRC522_Write(unsigned char addr, unsigned char value); unsigned char MFRC522_Read(unsigned char addr); void MFRC522_Init(void); unsigned char MFRC522_Request(unsigned char reqMode, unsigned char *TagType); unsigned char MFRC522_ToCard(unsigned char command, unsigned char *sendData, unsigned char sendLen, unsigned char *backData, unsigned int *backLen); void MFRC522_Halt(void); void Oscillator_Init(void); void main(void) { unsigned char status; unsigned char TagType[2]; TRISAbits.RA0 = 0; TRISAbits.RA5 = 0; LED = 1; Oscillator_Init(); __delay_ms(50); SPI_Init(); __delay_ms(50); MFRC522_Init(); __delay_ms(50); while (1) { status = MFRC522_Request(0x26, TagType); //We check if a card is presented if (status == 1) { LED = 0; } __delay_ms(500); } } void Oscillator_Init(void) { OSCCONbits.IRCF = 0b111; // IRCF = 111 -> Internal frequency of 16 MHz OSCCONbits.SCS = 0b10; // Select the internal oscillator as the system clock source } void SPI_Init(void) { // Configure SPI registers TRISCbits.TRISC3 = 0; // SCK (Clock) out TRISCbits.TRISC5 = 0; // SDO (Data Out) out TRISCbits.TRISC4 = 1; // SDI (Data In) in TRISAbits.TRISA5 = 0; // CS (Chip Select) out SSPCON1bits.SSPM = 0b0010; // SPI mode Master, FOSC/64 SSPCON1bits.CKP = 0; // Clock idle state is low SSPSTATbits.CKE = 1; // Transmit on active to idle (rising edge) SSPCON1bits.SSPEN = 1; // Enable SPI } unsigned char SPI_Transmit(unsigned char data) { SSPBUF = data; // Load buffer while (!SSPSTATbits.BF); // Wait for the transmission to complete return SSPBUF; //Return the received byte } void MFRC522_Write(unsigned char addr, unsigned char value) { MFRC522_CS = 0; //Enable MFRC522 (CS low) SPI_Transmit((addr << 1) & 0x7E); //Send register address with "write" command bit SPI_Transmit(value); // Send the data MFRC522_CS = 1; // Disable MFRC522 (CS high) } unsigned char MFRC522_Read(unsigned char addr) { unsigned char value; MFRC522_CS = 0; // Enable MFRC522 (CS low) SPI_Transmit(((addr << 1) & 0x7E) | 0x80); // Send address with read bit value = SPI_Transmit(0x00); // Read the data MFRC522_CS = 1; // Disable MFRC522 (CS high) return value; } void MFRC522_Init(void) { MFRC522_Write(0x01, 0x0F); // Command soft reset // Configure the transmission frequency MFRC522_Write(0x2A, 0x8D); // TModeReg MFRC522_Write(0x2B, 0x3E); // TPrescalerReg MFRC522_Write(0x2D, 30); // TReloadRegH MFRC522_Write(0x2C, 0); // TReloadRegL MFRC522_Write(0x15, 0x40); // TxASKReg MFRC522_Write(0x11, 0x3D); // ModeReg // Activer l'antenne unsigned char temp = MFRC522_Read(0x14); MFRC522_Write(0x14, temp | 0x03); // Enable bits to turn on the antenna __delay_ms(50); } unsigned char MFRC522_Request(unsigned char reqMode, unsigned char *TagType) { unsigned char status; unsigned int backBits; // Number of bits received MFRC522_Write(0x0D, 0x07); // BitFramingReg: set bit to start sending TagType[0] = reqMode; status = MFRC522_ToCard(0x0C, TagType, 1, TagType, &backBits); // Anti-collision control if ((status != 0) || (backBits != 0x10)) { status = 0; // If no tag is detected } return status; } unsigned char MFRC522_ToCard(unsigned char command, unsigned char *sendData, unsigned char sendLen, unsigned char *backData, unsigned int *backLen) { unsigned char status = 0; unsigned char irqEn = 0x00; unsigned char waitIRq = 0x00; unsigned char lastBits; unsigned char n; unsigned int i; if (command == 0x0C) // Anti-collision control { irqEn = 0x77; waitIRq = 0x30; } MFRC522_Write(0x02, irqEn | 0x80); // Enable IRQ MFRC522_Write(0x04, 0x00); // Clear IRQs MFRC522_Write(0x01, 0x00); // Idle // Writing data to send for (i = 0; i < sendLen; i++) { MFRC522_Write(0x09, sendData[i]); } MFRC522_Write(0x01, command); //Start transmitting i = 2000; // Wait for the end of the transmission (timeout) do { n = MFRC522_Read(0x04); i--; } while ((i != 0) && !(n & 0x01) && !(n & waitIRq)); if (i != 0) { if (!(MFRC522_Read(0x06) & 0x1B)) { status = 1; if (n & irqEn & 0x01) status = 0; // No card } } return status; } void MFRC522_Halt(void) { unsigned char haltData[4] = { 0x50, 0x00, 0x00, 0x00 }; unsigned int unLen; MFRC522_ToCard(0x0C, haltData, 4, haltData, &unLen); }