Maverickmax
Advanced Member level 1
at89c51 uart
Hi
I use serial communication to transmitt simple 8-bit data with start and stop bits.
In the transmitter circuit, you see two buttons which allow you to press one of them. When one of them has been pressed, I was expecting to see one LED to swtich on in the receiver circuit. But my receiver circuit sometime switch the LEDs on without me pressing the buttons.
So I had to identify the problem by disconnecting the power in the transmitter circuit in order to see if something affect my receiver circuit and I could not see any fault in my receiver circuit because both LEDs didn't turn on while the TX was disabled.
Then I connected the power to my transmitter and the LEDs started to switch on with out the button being pressed. I realised there was a problem in my transmitter circuit.
So far I could not identify any problem in my TX circuit and it has to be my codes:
Transmitter code
Receiver code
[/code]
Hi
I use serial communication to transmitt simple 8-bit data with start and stop bits.
In the transmitter circuit, you see two buttons which allow you to press one of them. When one of them has been pressed, I was expecting to see one LED to swtich on in the receiver circuit. But my receiver circuit sometime switch the LEDs on without me pressing the buttons.
So I had to identify the problem by disconnecting the power in the transmitter circuit in order to see if something affect my receiver circuit and I could not see any fault in my receiver circuit because both LEDs didn't turn on while the TX was disabled.
Then I connected the power to my transmitter and the LEDs started to switch on with out the button being pressed. I realised there was a problem in my transmitter circuit.
So far I could not identify any problem in my TX circuit and it has to be my codes:
Transmitter code
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=0xF3; //Setup for 2400 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();
//send_serial('1');
while(1)
{
// SECOND_LED=0;
// FIRST_LED=0;
if (FIRST_BUTTON == PRESSED)
{
send_serial('1');
while(1)
{
if (FIRST_BUTTON == NOT_PRESSED)
{
FIRST_BUTTON = NOT_PRESSED;
break;
}
}
}
if (SECOND_BUTTON == PRESSED)
{
send_serial('2');
while(1)
{
if (SECOND_BUTTON == NOT_PRESSED)
{
SECOND_BUTTON = NOT_PRESSED;
break;
}
}
}
Delay_Loop(5);
}
}
Receiver code
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=0xF3; //Setup for 2400 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)
{
received_flag_G=0;
serial_init();
FIRST_LED=OFF;
SECOND_LED=OFF;
for(;;)
{
while(received_flag_G==0)
{
}
received_flag_G=0;
if (received_data_G==1)
{
FIRST_LED=ON;
// SECOND_LED=ON;
Delay_Loop(50);
FIRST_LED=OFF;
// SECOND_LED=OFF;
}
if (received_data_G==2)
{
// FIRST_LED=ON;
SECOND_LED=ON;
Delay_Loop(50);
// FIRST_LED=OFF;
SECOND_LED=OFF;
}
}
}