URGENT HELP required

Thread Starter

aamna

Joined May 4, 2008
4
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.
 

Thread Starter

aamna

Joined May 4, 2008
4
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:

Papabravo

Joined Feb 24, 2006
21,228
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.
 

hgmjr

Joined Jan 28, 2005
9,027
Rich (BB code):
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;   // Changed to reflect correction referred to in a later post in this thread....
   }
}
I have reformatted your code to use indentation to make it more readable.
 
Last edited:

Mark44

Joined Nov 26, 2007
628
Rich (BB code):
while(1)
{
    while(RI==0);
    mybyte=SBUF;
    P1=mybyte;
    R1=0;
}
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
 

Papabravo

Joined Feb 24, 2006
21,228
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.
 

Thread Starter

aamna

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

Papabravo

Joined Feb 24, 2006
21,228
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 {}
Rich (BB code):
while(1)
{
  if(RI==1)
  {
    mbyte = SBUF ;
    P1 = mbyte ;
    RI = 0 ;
  }
  else if(...)
  {
    /* Do other tasks - if any */
  }
  else
  {
    /* Catch all for no conditions matched in the if else if string */
  }
}  /* End while(FOREVER) */
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:

Thread Starter

aamna

Joined May 4, 2008
4
#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..
 

Papabravo

Joined Feb 24, 2006
21,228
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.
 
Top