# Communication via RS232 how-to

Status
Not open for further replies.

#### pandaemonium

##### Member
I'm going to use a max232 I have laying around to interface to a uP (89C52), thats no problem and I'm a good C coder, what I want to know is the code procedure.

I assume I would first set the uPs serial buad rate and then loop in a parser? I havent found any clear C code examples and I don't know assembly.

Can someone point me to some good examples or give me an outline of the procedure I should use?

It looks like I can use a printf() function to sent data from the uP to my PC, but how do I send data to the uP from my PC? Some sort of buffer with a getc() or gets()?

#### bmcculla

##### New Member
Hyper terminal is the best way to send test data to the uC.

To receive characters you poll the character received interrupt flag (RI I think). This flag is set by the hardware when a character is received. When the flag is set just read the character from SBUF and set the flag to zero again. You can do the same thing with interrupts.

Code:
#pragma OT(0)
void UART0_ISR (void) interrupt 4
{

if (TI0)
{
TI0 = 0;
UART0Busy = 0;
}
if (RI0)
{
RI0 = 0;
}

}
Here's some interrupt code that receives the character and puts it into a circular buffer. The #pragma tuns off the compiler optimization; probably not nessesary but somtimes the compiler optimizes out some of the hardware flags (somethings not quite right with the SFR type).

Brent

#### TKS

##### New Member
join

i joined right now..!

#### pandaemonium

##### Member
OK, so if I want to parse a string from the PC to the uP all I have to do is watch this flag and keep adding the values to a variable while the flag is set? Is there a timing problem (reading the same character twice), how is this handled?

#### bmcculla

##### New Member
Each time a character is received RI0 is set to one. When you read that char from the buffer you set the flag back to zero. So you only read one character each time the flag goes high. This keeps you from reading the same character twice.

Brent

#### pandaemonium

##### Member
Are you saying that I must set the flag to zero, or that it is set back on its own?

Maybe I should write a 'good 'old fashioned zmodem protocol' for a 8052 when I figure this out!

BTW, thanks for your help. I really appreciate it!

#### bmcculla

##### New Member
Yeah you have to set the flag back to 0 in your software. If you are using interrupts it might be set back on its own (you have to check the datasheet to know for sure) but if you are polling you will always have to set it back to zero.