Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

AT89C51 to AT89C51 via wire

Status
Not open for further replies.

Maverickmax

Advanced Member level 1
Joined
Dec 6, 2004
Messages
404
Helped
8
Reputation
16
Reaction score
3
Trophy points
1,298
Activity points
3,689
Hello

I am trying to transmit a simple character across another device such as AT89C51 via wire in order to identify the character and switch red_led on for a short of time and switch it off.

What I get - NOTHING!

I am using 12MHz as an oscillator and the baud rate is 9600. So any help would be appreicated.

Here is two codes such as transmitter and receiver

Transmitter

Code:
// header files
#include "main.h"
#include "port.h"
#include "delay_loop.h"

//Function Prototypes

void serial_init();
void send_serial(unsigned char *s);


void serial_init()
{
	SCON=0x50;		//Setup for 8-bit data
	TMOD=0x20;		//Setup timer 1 for auto-reload
	TH1=0xF9;		//Setup for 9600 Baud
	TR1=1;			//Turn on timer 1
	TI=1;			//Indicate ready to transmit
}

void send_serial(unsigned char *s)
{
	while(*s !=0x00)
	{
		SBUF=*s;
		while(! TI)
		{

		}
		TI=0;

		s++;
	}
}


void main(void)
{

serial_init();
BLUE_LED=OFF;
Delay_Loop(100);
BLUE_LED=ON;
Delay_Loop(100);
BLUE_LED=OFF;

Delay_Loop(100);
BLUE_LED=ON;
Delay_Loop(50); 
send_serial('5');
BLUE_LED=OFF;
	
}


Receiver

Code:
// header files
#include "main.h"
#include "port.h"
#include "delay_loop.h"

//Global variable
tByte received_data_G;
int received_flag_G;

//Function Prototypes

void serial_init();	  

void serial_init()
{
	SCON=0x50;		//Setup for 8-bit data
	TMOD=0x20;		//Setup timer 1 for auto-reload
	TH1=0xF9;		//Setup for 9600 Baud
	TR1=1;			//Turn on timer 1
	TI=1;			//Indicate ready to transmit
	EA=1;			//Enable Interrupts
	ES=1;			//Enable serial port interrupt
}

serial() interrupt 4
{
	if(RI)
	{
		received_data_G=SBUF;
		RI=0;
		received_flag_G=1;		//Set received flag
	}
}


void main(void)
{
RED_LED=OFF;
Delay_Loop(100);
RED_LED=ON;
Delay_Loop(100);
RED_LED=OFF;



received_flag_G=0;
serial_init();

	for(;;)
	{

		while(received_flag_G==0)
		{

		}

		received_flag_G=0;

		if (received_data_G==5)
		{
			RED_LED=ON;
			Delay_Loop(50);
			RED_LED=OFF;
		}
	}
}

Is the baud rate configuration correct?

Maverickmax
 

Good idea is to test transmitter and receiver separately ..
You can use Windows Hyperterminal, or preferably other terminal software (there are plenty to choose from in EDA ) that allows you to see both ASCII and HEX, to check what is sent by the transmitter, and modify code, if required, unless you are satisfied that transmitter works correctly ..
The same can be done with the receiver; you can press keys from PC keyboard to simulate numbers; for example CTRL-A=01h, CTRL-B=02h and so on ..
At this stage I wouldn't use serial port interrupt at the receiver end .. try to follow the RI bit, and slow down the rate at which you ask the transmitter to send out bytes ..

For 9600bps and 12MHz crystal the re-load value should be FD ..
**broken link removed**

Regards,
IanP
 

Maverickmax said:
Thank you IanP. I have followed your suggestion.

I have found that my transmitter code works without any problem.

However the receiver code didn't work and I have been trying to pinpoint the problem for hours. Fortunately I have found the error in my receiver code.
I realised '5' should NOT be used because

ERROR - Receiver code

Code:
ERROR --> if (received_data_G==5)
		{
			RED_LED=ON;
			Delay_Loop(50);
			RED_LED=OFF;
		}

0x35 should be used instead of '5' because I did not realise that any letter must be changed into hexdecimal in accordance with ASCII codes. Therefore the receiver code should be like this:

Code:
		if (received_data_G==0x35)
		{
			RED_LED=ON;
			Delay_Loop(50);
			RED_LED=OFF;
		}


By the way, I have been using termv19b and I have not experience any problem with it as I could send and receive character.

However the hyperterminal from window would not allow me to enter character as I pressed one character and it didn't come on the screen but it accepts the data coming from my microcontroller chip. So can anyone explain why i experience that problem?

Maverick Max
 

Hyperterminal is not ideal tool; it has a lot of downsides comparing with other terminal packages ..
For Hyperterminal all your keybord is ASCII .. And if you want to send out hexadecimal characters, say 01h, you have to use CTRL+A, 02H=CTRL+B ... and so on ..
So, try something like this: h**p://www.rfinnovations.com.au/Uploads/Images/RFI-Interm%20Rev1.1(1).zip
It is a very nice and easy to use terminal software ..
Regards,
IanP
 

Thank you again IanP

I have finally solved my problem. The serial communication between two AT89C51 work very well and I am very pleased with it.

There is one thing that I need to be sure about....

Since I use 12MHz and 9600 baud rate therefore TH1 would be 0xF9, I found out that the error would be approximately 7 percent.

So that means the serial communication between two AT89C51s would likely get error.

Would I solve the problem if I implement 11.059MHz in order to get no error - zero percent?

If yes, so that means I need to adjust TH1 = 0xFD. Does it that sense?


Maverick Max
 

If both microcontrollers use the same crystals ad TH1 then the communication between them will go without errors ..
If, however, you would like to read ("spy") with, say PC, serial port what is sent the baud rate may be out of the standard 9600bps ..
In this case changing crystal to 11,095200 would be a good idea as this is the exact clock frequency for most of common baud rates ..
(FDh) ..
Rgards,
IanP
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top