Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RFID with Pic16f877A

Status
Not open for further replies.

abdo.medo1000

New Member
Hi dear all,

I have been working on my school project for couple of weeks and I'm trying to make RFID Door Lock by using Pic16f877A and RDM6300 RFID reader , I made a code works on PROTUS but I interfaced the cards tags as decimal number like(123456) and after that I noticed that all RF cards have HEX value like (B5:CF:79:23)
now I want to store my cards tag in the code and if i put the card on the RFID antenna it should be works if the card its correct
may you guide me to store my card tags on the program (B5:CF:79:23)

my code
Code:
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
void main()
    {
     unsigned char  i, rfid[13];
     //char String[8]= "B5:CF:79:23";
     int abdo =54321;
     int islam =12345;
     int y;
     Lcd_Init();
     trisc.RC3=0;
     portc.RC3=0;
     portc.RC4=0;
     trisc.RC4=0;
     portc.RC5=0;
     trisc.RC5=0;                     // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);              // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);         // Cursor off
     Lcd_Out(1,1,"RFID Tag Reader");   // Write text in first row
     UART1_Init(9600);                 // Initialize UART, 9600 baud rate
     rfid[12] = '\0';                  // String Terminating Character
       while(1)                        // Infinite Loop
      {
        if (UART1_Data_Ready())        // If UART Data Ready
        {

          for(i=0;i<5;)                // To Read 5 characters
          {
            if (UART1_Data_Ready())
            {
                  rfid[i] = UART1_Read();
                  i++;
            }
          }
          Lcd_Cmd(_LCD_CLEAR);              // Clear display
          Lcd_Cmd(_LCD_CURSOR_OFF);         // Cursor off
          Lcd_Out(1,1,"  << Welcome >>");   // Write text in first row
          Lcd_Cmd(_LCD_SECOND_ROW);
          Lcd_Out_cp("  Card ID:");
          for(i=0;i<5;)
          {
            Lcd_Chr_Cp(rfid[i]);
            UART1_Write(rfid[i]);
            i++;
            delay_us(1000);
          }
            y = atoi(&rfid);
            if( islam == y )
            {
              Lcd_Out(1,1,"  Welcome ISlAM  ");
              portc.RC5 =0;
              portc.RC3 =1;
               if(portc.RC3 =1)
              {
                portc.RC4 =1;
                delay_ms(200);
                portc.RC4 =0;
              }
            }
            else if( abdo == y )
            {
              Lcd_Out(1,1,"  Welcome ABDO  ");
              portc.RC5 =0;
              portc.RC3 =1;
              if(portc.RC3 =1)
              {
                portc.RC4 =1;
                delay_ms(200);
                portc.RC4 =0;
              }
            }
            else
             {
                portc.RC3 =0;
                portc.RC4 =0;
                portc.RC5 =1;
                if(portc.RC5 =1)
                {
                  portc.RC4 =1;
                  delay_ms(600);
                  portc.RC4 =0;

                  delay_ms(200);
                }
                Lcd_Out(1,1,"Check Your Card");
             }
              delay_ms(2000);
        }
        else
        {
          portc.RC3 =0;
          portc.RC4 =0;
          portc.RC5 =1;
          Lcd_Out(1,1,"  << Welcome >>  ");
          Lcd_Cmd(_LCD_SECOND_ROW);
          Lcd_Out(2,2,"Press Your Card  ");
         }
     }
   }
 

Attachments

  • 1234.jpg
    1234.jpg
    335.5 KB · Views: 595
This is my ASCII to hex routine. It takes one ASCII character and returns the four bit binary for the hex digit.

Call it twice to convert each two characters in the string and combine them to a byte, shifting the result for the high half four bits left before ORing the two results.

You can use either pointer typecasting or a union to put each converted byte in to one location of an unsigned long (32 bit), so assembling all the parts to make a single number from eight hex digits.

C:
int8 ahex(int8 a) {
    int8 b;
    
    if(a >= 0x30 && a <= 0x39) {
        return a - 0x30;
    }
    else {
        a &= 0xDF;
        
        if (a >= 'A' && a <= 'F') {
            return a - 0x37;
        }
    }
}
 
This is my ASCII to hex routine. It takes one ASCII character and returns the four bit binary for the hex digit.

Call it twice to convert each two characters in the string and combine them to a byte, shifting the result for the high half four bits left before ORing the two results.

You can use either pointer typecasting or a union to put each converted byte in to one location of an unsigned long (32 bit), so assembling all the parts to make a single number from eight hex digits.

C:
int8 ahex(int8 a) {
    int8 b;
   
    if(a >= 0x30 && a <= 0x39) {
        return a - 0x30;
    }
    else {
        a &= 0xDF;
       
        if (a >= 'A' && a <= 'F') {
            return a - 0x37;
        }
    }
}
thank you for answering I tried to add it to my code but it did not work ,may you add it to my code with the tag number (B5:CF:79:23) or any tag number
 
This is what I use to convert an ascii string into a 32 bit integer,
Code:
uint32_t ascii2int32(char * buff){
  uint32_t retVal;  
  for(uint8_t i=0;i<8;i++){
    retVal<<=4;
    uint8_t chr=buff[i];
    if(chr>='0' && chr<='9'){
      chr-='0';
    }else if(chr>'A' && chr<='F'){
      chr-=('A'-10);
    }else if(chr>'a' && chr<='f'){
      chr-=('a'-10);
    }
    retVal|=chr;
  }
  return retVal;
}
Note, it expects the string to contain 8 ascii characters.
You call it by doing,
Code:
char rfID[9]="B5CF7923";

uint32_t rfID32 = ascii2int32(rfID);

The above could be changed to more error tolerant.

Mike.
 
uint32_t ascii2int32 is not working with me
this is my clear code
Code:
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

unsigned char byte;

void main()
{
 char i, rfid[13];
 int y;
 int islam=12345678;
 Lcd_Init();                       // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR);              // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Out(1,1,"RFID Tag Reader");   // Write text in first row

 UART1_Init(9600);                 // Initialize UART, 9600 baud rate

 rfid[12] = '\0';                  // String Terminating Character

 while(1)                          // Infinite Loop
 {
   if(UART1_Data_Ready())          // If UART Data Ready
   {
     for(i=0;i<12;)                // To Read 12 characters
     {
       if(UART1_Data_Ready())
       {
         rfid[i] = UART1_Read();
         i++;
       }
     }
     lcd_cmd(_LCD_SECOND_ROW);
     lcd_out_cp("Tag:");
     for(i=3;i<12;)
     {
     lcd_chr_cp(rfid[i]);
     UART1_Write(rfid[i]);
     i++;
     }
     delay_ms(1000);
   }

 }
}
 
The module you are using sends 14 bytes of data starting with 0x02. You're only reading 12 - I think.

Mike.
 
hi Mr Brian.
i did a simple code that shows the cards tag on lcd
tthis is the code :
Code:
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

unsigned char byte;

void main()
{
 char i, rfid[13];
 int y;
 Lcd_Init();                       // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR);              // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Out(1,1,"RFID Tag Reader");   // Write text in first row

 UART1_Init(9600);                 // Initialize UART, 9600 baud rate

 rfid[12] = '\0';                  // String Terminating Character

 while(1)                          // Infinite Loop
 {
   if(UART1_Data_Ready())          // If UART Data Ready
   {
     for(i=0;i<12;)                // To Read 12 characters
     {
       if(UART1_Data_Ready())
       {
         rfid[i] = UART1_Read();
         i++;
       }
     }
     lcd_cmd(_LCD_SECOND_ROW);
     lcd_out_cp("Tag:");
     for(i=3;i<12;)
     {
     lcd_chr_cp(rfid[i]);
     UART1_Write(rfid[i]);
     i++;
     }
     delay_ms(1000);
   }
 
 }
}

and it read without any problem and print the tags on LCD as shows below ,
but i want to save the tags before in the code to compare the coming tags with the ones I have stored on the code but i could not do it
is there is any libirary that helps to do that ,if that how to install it on MikroC because I found this project but the library it is not working
i took this message <169 380 Arguments cannot have explicit memory specificator mfrc522.h>

what should i do how can i compare the tags
* i have RC522 RFID reader and RDM6300 RFID reader
 

Attachments

  • WhatsApp Image 2021-05-27 at 17.38.47 (1).jpeg
    WhatsApp Image 2021-05-27 at 17.38.47 (1).jpeg
    53.2 KB · Views: 436
  • WhatsApp Image 2021-05-27 at 17.38.47.jpeg
    WhatsApp Image 2021-05-27 at 17.38.47.jpeg
    47.1 KB · Views: 400
  • WhatsApp Image 2021-05-27 at 17.38.48 (2).jpeg
    WhatsApp Image 2021-05-27 at 17.38.48 (2).jpeg
    41.7 KB · Views: 392
  • WhatsApp Image 2021-05-27 at 17.38.48 (1).jpeg
    WhatsApp Image 2021-05-27 at 17.38.48 (1).jpeg
    59.2 KB · Views: 384
  • WhatsApp Image 2021-05-27 at 17.38.48.jpeg
    WhatsApp Image 2021-05-27 at 17.38.48.jpeg
    51.7 KB · Views: 390
Thank you for your patience Mr Mike,

I wrote Mr Brian by mistake sorry for that ,
Where did you posted the the code and its explanation?
 
Where did you posted the the code and its explanation?
Above. I posted example code and an explanation of what's needed (read 14 bytes). You don't seem to be able to follow simple code so not sure how I could help you.

Mike.
 
If the tag is read OK as just text, then store them as text strings (character array for the received data) and use strcmp() to compare the received string with each stored string.
You only really need to convert to a number if you are doing other processing or storing vast quantities, where space matters.

Don't forget to add a null (zero) after the last received character, to terminate the string.


Or strcmpi() may be preferable, as hex characters should not be case-sensitive:
 
Above. I posted example code and an explanation of what's needed (read 14 bytes). You don't seem to be able to follow simple code so not sure how I could help you.

Mike.
I'm new with Microcontroller for that some things I do not get it easily.
I was actually saying that this functions is not working in MikroC program with me
Code:
uint32_t ascii2int32
i will try to use
Code:
StrToInt
to convert the coming data to integer and i will convert the tag number into decimal and save it in "int"
i hope it working .
Thank you Mr Mike
 
If the tag is read OK as just text, then store them as text strings (character array for the received data) and use strcmp() to compare the received string with each stored string.
You only really need to convert to a number if you are doing other processing or storing vast quantities, where space matters.

Don't forget to add a null (zero) after the last received character, to terminate the string.


Or strcmpi() may be preferable, as hex characters should not be case-sensitive:

thank you , will try to do that
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top