Why It is stuck in loop? RX pin is not receiving data from Virtual Terminal?

191 Views Asked by At

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';
    }
}
0

There are 0 best solutions below