URGENT HELP required

Discussion in 'Embedded Systems and Microcontrollers' started by aamna, May 8, 2008.

  1. aamna

    Thread Starter New Member

    May 4, 2008
    4
    0
    hey,
    im using the microcontroller AT89C51.i have to interface the microcontroller with the PC,ie when u send data may it be character or integers thru hyperterminal from a PC via a serial port ie RS-232 it has to be shown on any one of the ports of the microcontroller maybe by putting LED's infront of an entire port and the blinking of the LED can show that whether it has recieved a 1 or a 0 on that pin.. data is coming serially n i have to show it in parallel on any port of the microcontroller.The problem that im facing is that the data is coming from the serial port and reaching the microcontroller but it is not showing on the port..any help regarding the code wil be appreciated!!
    regards.
     
  2. aamna

    Thread Starter New Member

    May 4, 2008
    4
    0
    this is the code that i had been using..:

    #include<reg51.h>
    void main(void)
    {
    unsigned char mybyte;
    TMOD=0x20;
    TH1=0xFD;
    SCON=0x50;
    TR1=1;
    while(1)
    {
    while(RI==0);
    mybyte=SBUF;
    P1=mybyte;
    RI=0;
    }
    }
     
    Last edited: May 9, 2008
  3. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Ahhhh....you need to check for RI==1 in order for the content of SBUF to be meaningful in the sense of being the ASCII value of some character. You also need to clear RI, (set it back to zero after reading SBUF) or else you will never see another character.
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I have reformatted your code to use indentation to make it more readable.
     
    Last edited: May 9, 2008
  5. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    The inner while loop looks like an infinite loop to me. I have no idea what RI, SBUF, P1, or R1 represent. No doubt they are defined in the header file you're using. So based on a limited understanding of this code, here's what I see:
    The outer loop is an infinite loop--I'm sure about that.
    What I don't know is the initial value of R0.

    If RI equals 0, the inner while loop executes forever, which means that the following statements never execute.
    If RI is some value other than 0, the inner while loop doesn't execute, which causes the next three statements to execute.

    The names of RI and R1 are similar enough that I wonder if one of them is a typo.

    I also wonder about the reason for the inner loop. Since there aren't any comments, I have no idea what its purpose is.

    Mark
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    RI is the UART Receive Interrupt Flag. The 8051 hardware sets it to a 1 when there is a character in SBUF. The code as written will IGNORE all input characters. Please read my previous post.

    Mark44:
    If you're going to reply to posts like this I really think you should track down the datasheet and know what the symbols stand for so your response has some credibility. Otherwise it just adds to the overall noise level.
     
  7. aamna

    Thread Starter New Member

    May 4, 2008
    4
    0
    sorry the last statement was RI=0 not R1=0..my typing mistake..but that still doesnt solve the problem!!
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Did you change the while condition to "RI==1"? It has no chance of working until you do.
    EDIT: missed the ; for the null while loop. If you do change to looking for RI==1 then the code fillowing the null while should go in curly braces {}
    Code ( (Unknown Language)):
    1.  
    2. while(1)
    3. {
    4.   if(RI==1)
    5.   {
    6.     mbyte = SBUF ;
    7.     P1 = mbyte ;
    8.     RI = 0 ;
    9.   }
    10.   else if(...)
    11.   {
    12.     /* Do other tasks - if any */
    13.   }
    14.   else
    15.   {
    16.     /* Catch all for no conditions matched in the if else if string */
    17.   }
    18. }  /* End while(FOREVER) */
    19.  
    This structure allows you to do other things when there is no character present.

    You've selected an 8-bit UART and enabled the receiver (SCON = 0x50)? Is this correct?
    TMOD=0x20 and TH1 = 0xFD with no mention of SMOD being 0 or 1 suggests that you are trying for 9600 BAUD and using an 11.0592 MHz crystal. Is this correct?

    Your're probably OK on this but since Timer 2 and the Internal BRG can also be used you might want to check the processors RESET conditions to see if one of those other methods is selected.
     
    Last edited: May 9, 2008
  9. aamna

    Thread Starter New Member

    May 4, 2008
    4
    0
    #include <AT89C51.H>

    unsigned char RecCh;
    void InitSerial()
    {
    SCON = 0x50; //mode 1, 10 bit
    TMOD = 0x20; /* timer 1 mode 2: 8-Bit reload */
    TH1 = 0xfd; //9600 baud rate over 11.0592MHz crystal TH1 = 256 - ((Crystal/384)/Baud Rate)
    TR1 = 1; //Turn timer1 On
    EA = 1; //Enable All Interrupts
    ES = 1; //Enable Serial Port Interrupt
    }//end void InitSerial()
    //***************************************************************************
    serial() interrupt 4
    {
    if(RI)
    {
    RecCh = SBUF;
    RI = 0;
    P2 = RecCh;
    }
    }
    void main()
    {
    InitSerial();
    while(1)//endless loop
    {

    }
    }

    i changed my code but still no use..
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    I'm thinking you need to put an oscilliscope on the serial input P3.1(??) to see if the input signal you expect is there. It should work either with or without using the interrupt. Personally I'd get it working without the interrupt first.
     
Loading...