Hi My project involves serial transmission and serial reception. I have dumped the suitable code for the micro-controller(AT9S52) at the transmitter and the receiver. I have simulated both the codes successfully in keil u vision3. The micro-controller is transmitting the serial data without any problem. But the mc at receiver is not receiving any data. I'm not able to understand what the problem is...
My code at the receiver is as follows. The job of the receiver is to just receive any serial data coming. Please help me if there's any amendment I've to make...
#include"reg51.h" void initialize() { SCON=0x50; PCON=0X80; TMOD=0x20; TL1=0x00; TH1=0x5D; TR1=1; RI=0; } void main() { int rdata; while(1) { initialize(); while(RI==0); rdata=SBUF; RI=0; P2=rdata; } }
Where is the indentation?
Where are the comments?
Why are you initializing the serial port inside the loop?
hi i've put the comments. do you think initializing inside the loop could have caused the problem? i'll change it and dump the hex into the mc again. please check my code again and help me out.
#include"reg51.h" void initialize() { SCON=0x50; //set the uart in mode 1(variable baud rate) and receiver is enabled PCON=0X80; // doubled the baud rate set in TH1 TMOD=0x20; // set the timer1 in auto reload mode TL1=0x00; TH1=0x5D; // set required baud rate TR1=1; // start the timer1 RI=0; // just in case } void main() { int rdata; // to store the serially received data in to this variable initialize(); //start the timer and make the receiver active while(1) // to receive data continuously { while(RI==0); //waits till sbuf is filled rdata=SBUF; // stores in to a variable RI=0; //resets the flag P2=rdata; display the received data on port2 to make sure the data is received } }
What about the indentation ??
Iniialization of the serial port inside the loop is most definitely a big no-no. Do you really think the UART can keep itself in a working state and handle incomming data when you reinitialize it?
But why should we - again - check your code, when you haven't even thought it valuable to test your code before coming back and request more help? Don't you think that _you_ should be the most involved individual in solving the problems?
Other things to think about: - whenever you design a program with UART communication, you should try an initial transmission of characters just to allow you to verify the correct baudrate with an oscilloscope. Than you can remove the transmission and know that the UART will also have correct baudrate for receiving data. - you haven't mentioned any testing at all. But when UART communication doesn't work, you really should always check with an oscilloscope that you do get serial data with correct baudrate to the receive pin of the processor, so there are any data for the UART to pick up. It would be bad to spend too much time fighting the source code in case the signals are accidentally routed wrong...
P2 = rdata; display the received data on port2 to make sure the data is received
How will this show whether you receive a byte just once, or the same byte multiple times in sequence...?