Bug program

Stefox

Newbie
Joined
Sep 22, 2024
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
85
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!



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);
}

 

Attachments

  • main.zip
    2 KB · Views: 9

Since your MFRC522_Request() function is always returning 0, you need to dig into it, now debugging each command.
If no UART is available, this task will be painful; use an LED to get the value of each command inside, comparing the actual one with the expected one.

Anyway, before doing anything, try coding a simple application to blink the LED, to at least know if firmware is indeed being writed:


Code C - [expand]
1
2
3
4
5
6
7
while (1) 
    {
            LED = 0;
        __delay_ms(500);
            LED = 1;
        __delay_ms(500);
    }

 

I'll try to see what I can do with this function and an LED tomorrow..

regarding the led, yes it works well, because if I change it and turn it on before the condition present in the while, it lights up and if I change it and turn it off, it turns off

the condition of the function you are talking about does not actually work, hence my question, that is my problem, I do not understand where it is going wrong
 

the condition of the function you are talking about does not actually work, hence my question, that is my problem, I do not understand where it is going wrong

Reread what I wrote above in the first 2 lines.
 

status = MFRC522_Request(0x26, TagType); //We check if a card is presented
I canΒ΄t see how 0x26 refers to "TagType".

Did you read the MFRC522 datasheet?
I can not find the phrase "tag type" in the datsheet, nor does "0x26 refer" to anything similar.

Klaus
 

Similar threads

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