Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Communication via RS232 how-to

Status
Not open for further replies.
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()?
 
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;
		CircBufAddElement(&RxBuf, SBUF0);
	}

}

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
 
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?
 
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
 
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!
 
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.

I'm glad to help.

Brent
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top