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

Why output data is wrong by serial?

hello,everyone!
I wrote a program about serial communication and when I run it on simulator,it is no problem, but when I upload it to the Phytec KitCon167's flash,something wrong occur.
The program is below:

unsigned char temp,temp1;

void main()
{
  serial_init ();

  temp = 0x45;
  while(1)
  {
    transmit_data(temp);
    while (! S0TBIR)
    {
     ;
    }
    S0TBIR = 0;
    temp1 = 0x23;
    transmit_data(temp1);
    while (! S0TBIR)
    {
     ;
    }
    S0TBIR = 0;

    temp1 ++;
    transmit_data(temp1);
    while (! S0TBIR)
    {
     ;
    }
    S0TBIR = 0;
}

In simulator,serial output is 45 23 24;
but in KC167,serial output is FF 23 FF.
Who can tell me,what happen to the program?

Parents
  • complete program is below:

    #include <C167CS.H>
    
    void serial_init ();
    
    unsigned char temp,temp1;
    
    void main()
    {
      serial_init ();
    
      temp = 0x45;
      while(1)
      {
        S0TBUF = temp;
        while (! S0TBIR)
        {
         ;
        }
        S0TBIR = 0;
        temp1 = 0x23;
        S0TBUF = temp1;
        while (! S0TBIR)
        {
         ;
        }
        S0TBIR = 0;
    
        temp1 ++;
        S0TBUF = temp1;
        while (! S0TBIR)
        {
         ;
        }
        S0TBIR = 0;
       }
    }
    
    /******************************************************************************/
    /*       serial_init: initialize serial interface                             */
    /******************************************************************************/
    void serial_init ()
    {
      P3  |= 0x0400;             /* set PORT 3.10 output latch (TXD)              */
      DP3 |= 0x0400;             /* set PORT 3.10 direction control (TXD output)  */
      DP3 &= 0xF7FF;             /* reset PORT 3.11 direction control (RXD input) */
      S0TIC = 0x05;              /* transmit interrupt enable; ILVL = 1; GLVL = 1 */
      S0RIC = 0x06;              /* receive  interrupt enable; ILVL = 1; GLVL = 2 */
      S0BG  = 0x0207;             /* set baudrate to 1200 BAUD                    */
      S0CON = 0x8011;          /* set serial mode,S0M = 001B                    */
    	IEN = 1;
    }
    

Reply
  • complete program is below:

    #include <C167CS.H>
    
    void serial_init ();
    
    unsigned char temp,temp1;
    
    void main()
    {
      serial_init ();
    
      temp = 0x45;
      while(1)
      {
        S0TBUF = temp;
        while (! S0TBIR)
        {
         ;
        }
        S0TBIR = 0;
        temp1 = 0x23;
        S0TBUF = temp1;
        while (! S0TBIR)
        {
         ;
        }
        S0TBIR = 0;
    
        temp1 ++;
        S0TBUF = temp1;
        while (! S0TBIR)
        {
         ;
        }
        S0TBIR = 0;
       }
    }
    
    /******************************************************************************/
    /*       serial_init: initialize serial interface                             */
    /******************************************************************************/
    void serial_init ()
    {
      P3  |= 0x0400;             /* set PORT 3.10 output latch (TXD)              */
      DP3 |= 0x0400;             /* set PORT 3.10 direction control (TXD output)  */
      DP3 &= 0xF7FF;             /* reset PORT 3.11 direction control (RXD input) */
      S0TIC = 0x05;              /* transmit interrupt enable; ILVL = 1; GLVL = 1 */
      S0RIC = 0x06;              /* receive  interrupt enable; ILVL = 1; GLVL = 2 */
      S0BG  = 0x0207;             /* set baudrate to 1200 BAUD                    */
      S0CON = 0x8011;          /* set serial mode,S0M = 001B                    */
    	IEN = 1;
    }
    

Children
  • Some random stuff to try:

    1] Instead of using S0TBIR, try S0TIR. Also set S0TIR to 0 before putting anything into S0TBUF for the first time. Also remove the space between temp1 and the ++ operator.

    2] Use

    S0CON=0x0011;
    at the beginning of serial_init. At the end of serial_init add the line:
    S0R = 1;

    3] If I were you, I would move up the priority of interrupt to level 5 or more. When you add the Keil RTX, you will miss characters if the serial interrupts aren't greater than the RTX interrupts.

    4] Take a look at where temp and temp1 are mapped in memory. Instead of setting SOTBUF to temp and temp1, set
    SOTBUF=0x43;
    , etc....