Here was my plan to realize the communication between PC and AT89C52, Both of them are set to 2400 (baud rate), N, 8, 1. 1. I have shortened pin 2 and pin 3 of RS-232 attached to PC. When my pc was sending out DATA, at the same time it can receive this DATA. After doing this, I found my PC can correctly receive the DATA just sent out. 2. The following is my C51 code. After receiving the incoming data, I want to show that data by LED connected to P1 port. I thought my C51 code should be wrong, but I really have no idea……Please help me…. Sincere Thanks in advance.
unsigned char ISRIN; unsigned char ISROUT; unsigned char SERIALISRIN; void DELAY (); void INI_SERIAL(); unsigned char RECE_SERIAL (); void main () { unsigned char DATD; P1 = 0xff; INI_SERIAL(); While(1) { if (ISRIN ) { ISRIN = 0; DATD = RECE_SERIAL (); } } } void SERIAL_ISR ( ) interrupt 4 using 1 ( if ( TI ) { TI = 0 ; ISROUT = 1; } if ( RI ) { RI = 0; ISRIN = 1; } } void INI_SERIAL() { EA = 0; T2CON = 0x34; RCAP2L = 0x64; RCAP2H = 0xff; TL2 = 0x64; TH2 = 0xff; SCON= 0x50; TI = 0; RI = 0; TR2 = 1; ES= 1; EA =1; } unsigned char RECE_SERIAL () { unsigned char DAT; P1 = SBUF; DAT = P1; DELAY (); P1 = 0xff; return DAT; }
What does your DELAY() function do? As an aside, note that ALL-UPPERCASE is conventionally reserved for #defined symbols in 'C'.
What DELAY () does is to help me to check the received data by showing them on P1 port. I have done following steps to check my MCU and PC: 1. Shorten Pin 11 and Pin 12 of MAX232, I am sure PC can receive the sent data correctly. 2. Shorten P3.0 and Pin 3.1 of AT89C52, I am sure AT89C52 can receive the sent data correctly. Based on above tests, I think the setting of baud rate for AT89C52 is wrong, but according to the baud rate calculator posted on this website, my setting for 2400 bits using timer2 should be right. Only one thing I am confused is how to select the Serial Clock Divisor of AT89C52 Timer 2 (should be 32 or…?).