0

This code is to simulate RFID reader working using AT89C51 in Proteus Software. Instead of RFID reader i am using Virtual Terminal for sending or scanning RFID, here in my case I am Typing ID in Virtual Terminal, string 'a' stores input and compares with existing string and gives Output. Problem here is the ID I am typing in virtual terminal is not copied to string 'a'. It is stuck in while loop. RI is a interrupt flag that tells if any serial data received in RX pin.

//CODE of lcd interfacing with 8051 microcontroller
#include<reg51.h>
#include<string.h>

void delay( unsigned int i );
void lcd_cmd( unsigned char a );
void lcd_data( unsigned char b );
void lcd_init( void );
void lcd_str( unsigned char* str );
void sendser_char( unsigned char b );
void sendser_str( unsigned char* str );
sbit rs = P2 ^ 0;
sbit en = P2 ^ 1;
unsigned int i;
unsigned char a[60], b[10];
sfr ldata = 0x90;//port1 
void clear( void );

void main()
{
    TMOD = 0x20;//timer1 mode2 -auto reload mode
    TH1 = 0xfd;//9600 baud rate
    SCON = 0x50;//8bit data ,1start bit,1stop bit
    TR1 = 1;
    REN = 1;
    lcd_init();
    lcd_str( "   WELCOME TO   " );
    lcd_cmd( 0xc0 );
    lcd_str( "   MY PROJECT   " );

    delay( 65000 );
    while( 1 )
    {
        lcd_cmd( 0x01 );
        lcd_cmd( 0x80 );
        lcd_str( "   Waiting For    " );
        delay( 65000 );
        lcd_cmd( 0xc0 );
        lcd_str( "   RFID Tag   " );
        delay( 65000 );

        for( i = 0; i < 10; i++ )
        {
            while( RI == 0 );   //code is stuck here, RI value shall be changed 
                            //when input given in virtual terminal.but it is never changing.
            a[i] = SBUF;
            RI = 0;
        }
        a[i] = '\0';
        delay( 65000 );
        if( 0 == strcmp( "0123456789", a ) )
        {
            lcd_cmd( 0x01 );
            lcd_cmd( 0x80 );
            lcd_str( " WELCOME USER 1" );
            lcd_cmd( 0xc0 );
            lcd_str( a );
            delay( 65000 );
            clear();
        }
        else if( 0 == strcmp( "10003B0CAE", a ) )
        {
            lcd_cmd( 0x01 );
            lcd_cmd( 0x80 );
            lcd_str( " WELCOME USER 2" );
            lcd_cmd( 0xc0 );
            lcd_str( a );
            delay( 65000 );
            clear();
        }
        else
        {
            lcd_cmd( 0x01 );
            lcd_cmd( 0x80 );
            lcd_str( " INVALID TAG" );
            lcd_cmd( 0xc0 );
            lcd_str( a );
            delay( 65000 );
            clear();
        }
    }
}

void lcd_init()
{
    lcd_cmd( 0x38 );
    lcd_cmd( 0x0c );
    lcd_cmd( 0x01 );
    lcd_cmd( 0x80 );
}

void delay( unsigned int i )
{
    unsigned int j;
    for( j = 0; j < i; j++ );
}

void lcd_cmd( unsigned char a )
{
    rs = 0;//cmd 
    ldata = a;
    en = 1;
    delay( 5 );
    en = 0;
    delay( 5 );
}

void lcd_data( unsigned char b )
{
    rs = 1;//data 
    ldata = b;
    en = 1;
    delay( 5 );
    en = 0;
    delay( 5 );
}

void lcd_str( unsigned char* str )
{
    while( *str )
    {
        lcd_data( *str++ );
    }
}

void sendser_char( unsigned char b )
{
    SBUF = b;
    while( TI == 0 );
    TI = 0;
}

void sendser_str( unsigned char* str )
{
    while( *str )
    {
        sendser_char( *str++ );
    }
}

void clear( void )
{
    unsigned int i;
    for( i = 0; i < 60; i++ )
    {
        a[i] = '\0';
    }
}
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Stumbled over the following (old) question: https://community.arm.com/support-forums/f/keil-forum/18194/can-t-get-ti-flag-to-be-set-when-writing-into-sbuf . In the initialization they enable interrupts with `ES=1;EA=1`, maybe this is a start. – izlin Jan 28 '22 at 13:31
  • So much 1980s nostalgia: 8051... Yoda conditions... On a serious note please edit your post and fix the code formatting - it's all over the place. – Lundin Jan 28 '22 at 13:50
  • `REN` is a bit 4 in `SCON` so `SCON=0x50;` sets `REN`, so no need to do that separately. `TH1=0xfd;//9600 baud rate` is correct for an 11.059Hz crystal - is that what you have? `if (RI==1)` after the while loop serves no purpose - you have already determined that by exiting the loop. `i` in `clear()` shadows the rather [ill-advised _global_](https://www.embedded.com/a-pox-on-globals/) `i`. – Clifford Jan 29 '22 at 19:42
  • @izlin The RI flag is polled here, no interrupt handler is provided, so enabling interrupts is unlikely the answer I think. – Clifford Jan 29 '22 at 19:48
  • The TH1 value is good for an 11.059MHz XTAL - is that what you are using? I guess that is the default in Proteus? – Clifford Jan 29 '22 at 19:50
  • You call `strcmp()` on `a` without terminating the string. Add (for example) `a[i + 1] = 0 ;` after reading SBUF or `a[i] = 0 ;` after the for loop. – Clifford Jan 29 '22 at 19:56
  • @Clifford yeah, i am using 11.0592 MHz frequency, by default in proteus it is 12Mhz but I changed it to 11.0592 MHz. – Vedaraj Jan 30 '22 at 17:49
  • 1
    RI flag interrupt is handled by proteus software, may be it is problem with the software I guess, – Vedaraj Jan 30 '22 at 17:51

0 Answers0