This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

recive data from serial and use it for port

I need to write a program for a project that receive data from serial (RX pin) using HC06 module and then turn a led on according to the received information.
I'm using P89v51rd2 microcontroller.
Here is the code I write but something is wrong

#include <reg51.h>
#include <stdio.h>

sbit con1 = 0xa0;

void serial_init(void)             //Setup the serial port for 9600 baud at 11.0592MHz.
{
                SCON  = 0x50;         //* SCON: mode 1, 8-bit UART, enable rcvr
                TMOD  = 0x20;        //* TMOD: timer 1, mode 2, 8-bit reload
                TH1   = 0xFD;       //* TH1:  reload value for 9600 baud @ 11.0592MHz
                TL1   = 0xFD;
                TR1   = 1;         //* TR1:  timer 1 run
                TI    = 1;        //* TI:   set TI to send first char of UART
}


void receive()          //Function to receive serial data
{
                unsigned int digit;    //variable to hold the new character
                while(RI==0);         //Wait for Receive interrupt flag
                        {
                         digit=SBUF;
                         RI=0;                          //Clear eceive interrupt flag
                        }
                if (digit==1)
                        {
                         con1=0;
                        }
                else
                        {
                         con1=1;
                        }


void main()
{
                while(1)
                {
                serial_init();
                receive();
                }
}

where i'm wrong
please help me

Parents
  • "but something is wrong" is a quite weak error description - so weak that it isn't likely that you have spent any time actually trying to solve this issue.

    By the way - you want to get help from people but haven't even managed to document exactly what the program is expected to do. Is it really expected to react to the binary value 1 received on the UART? Or instead react to the character '1'?

    And why do you think it a good idea to initialize the UART between every received character? Did someone tell you that this is how programs normally make use of a serial port?

    TI    = 1;        //* TI:   set TI to send first char of UART
    


    What do you mean with the above comment? Is your program really interested in sending characters? If so: could you then explain to the forum exactly what data you intended to send - and where the send code is?

Reply
  • "but something is wrong" is a quite weak error description - so weak that it isn't likely that you have spent any time actually trying to solve this issue.

    By the way - you want to get help from people but haven't even managed to document exactly what the program is expected to do. Is it really expected to react to the binary value 1 received on the UART? Or instead react to the character '1'?

    And why do you think it a good idea to initialize the UART between every received character? Did someone tell you that this is how programs normally make use of a serial port?

    TI    = 1;        //* TI:   set TI to send first char of UART
    


    What do you mean with the above comment? Is your program really interested in sending characters? If so: could you then explain to the forum exactly what data you intended to send - and where the send code is?

Children