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

How to tansmit single character via RS232 using P89V51RD?

Hi,
I am using P89V51RD2 NXP micro controller. I tried this code and able to transmit the char 's' for infinite time. Now i want to print/transmit it once. After removing the while(1) it print infinite time.
What should i change in my program to transmit for one time?

// Program to test serial communication of controller with PC using hyper terminal
#include<reg51.h>

void ini()     // Initialize Timer 1 for serial communication
{
TMOD=0x20;  //Timer1, mode 2, baud rate 9600 bps
TH1=0XFD;
SCON=0x50;
TR1=1;
}

void transmit()  // Funtion to transmit serial data
{
SBUF='s';
while(TI==0);
TI=0;
}

void main()
{
  ini();
while(1)
{
  transmit();
}
}
/



  • If you remove the while loop - maybe you could tell this forum where you think your application will go after having completed the transmit() call?

    void main() {
        ini();
        transmit();
    }
    


    Do you think there is a catcher waiting with a pillow to stop any program that runs out of main()? Have you seen any command line prompt or any nice GUI that your microprocessor may reach after the program ends? Do you think your processor (who only knows how to process machine instructions) do know that your C program leaves main() and will then automatically stop processing machine instructions?

    By the way - you do have access to a debugger - have you tried to switch to assembly mode and continue to trace your application after it leaves main()? Maybe you should. It is normally considered "undefined behaviour" for an embedded program - but if you design a program explicitly assuming it ok to leave main(), it might be a good idea for you to check up what really do happen.

  • // Program to test serial communication of controller with PC using hyper terminal
    #include<reg51.h>
    
    void ini()     // Initialize Timer 1 for serial communication
    {
        TMOD=0x20;  //Timer1, mode 2, baud rate 9600 bps
        TH1=0XFD;
        SCON=0x50;
        TR1=1;
    }
    
    void transmit()  // Funtion to transmit serial data
    {
        SBUF='s';
        while(TI==0);
            TI=0;
    }
    
    void main()
    {
        ini();
        transmit();
    
        PCON = 0x02 ;         // Power Down Mode for P89V51R(Set PD bit in PCON)
    
    }
    

  • void main()
    { ini(); transmit(); while(1);
    }

    As Per points out, the program must return somewhere. Without the 'forever' loop, the code would proceed to la- la land. So, Keil placed a default jump to start code that is added by compiler just for the many beginners that leave out the forever loop. When you removed your while(1), Keil just added one for you. It may not restart and loop where your code should loop but it will loop.

    Bradford

  • OOPs, I haven't read Dan Henry lately.

    void main()
    {
      ini();
      transmit();
      while(1);
    }
    

    Bradford

  • Thank you, after the changes it work as desired. Now i am having problem with receiving a character.

    void receive()
    {
       char value;
       while(RI==0)
       {
       }
       value=SBUF;
    }
    


    Nothing is received to my controller.