Keil Logo

C51: Using SIO0 and SIO1 with DALLAS 320


Information in this article applies to:

  • C51 Version 5.50a and Later

QUESTION

Can I use both of the serial ports on the Dallas 320 and Cypress devices?

ANSWER

Yes. However, the serial ports will probably need to be set up to use different baud rates with different timers.

The following example program shows how you can use both serial ports in your program. The putchar and _getkey functions have been rewritten to automatically select the serial port using the sio_port variable.

#include                /* special function register declarations   */

#include                 /* prototype declarations for I/O functions */


bit sio_port = 0;   /* SIO port to use (0 = SIO0) */

/*------------------------------------------------
------------------------------------------------*/
char putchar (char c)
{
if (sio_port == 0)
  {
  while (!TI);
  TI = 0;
  SBUF0 = c;
  }
else
  {
  while (!TI1);
  TI1 = 0;
  SBUF1 = c;
  }

return (c);
}

/*------------------------------------------------
------------------------------------------------*/
char _getkey (void)
{
char c;

if (sio_port == 0)
  {
  while (!RI);
  c = SBUF0;
  RI = 0;
  }
else
  {
  while (!RI1);
  c = SBUF1;
  RI1 = 0;
  }

return (c);
}

/*------------------------------------------------
------------------------------------------------*/
void main (void) {

/*------------------------------------------------
Setup the serial port 0 for 1200 baud at 12MHz
using timer 2.
------------------------------------------------*/

    SCON0  = 0x50;              /* SCON:  Mode 1, 8-bit UART, enable rcvr */
    T2MOD  = 0x00;              /* T2MOD: Count Up */
    T2CON  = 0x30;              /* T2CON: Use T2 for Baud Rate on SIO 0 */

    TL2    = 0xFF;
    TH2    = 0xFF;

    RCAP2L = 0xC8;              /* RCAP2: Reload value for 1200 @ 12 MHz */
    RCAP2H = 0xFE;              /* 12,000,000 / 2 / 1200 / 16 */

    TR2   = 1;                  /* TR2: T2 Run */
    TI    = 1;                  /* TI:  Set TI to send first char of SIO 0 */

/*------------------------------------------------
Setup the serial port 1 for 300 baud at 12MHz
using timer 1.
------------------------------------------------*/

    SCON1  = 0x50;              /* SCON1: Mode 1, 8-bit UART, enable rcvr */
    TMOD  |= 0x20;              /* TMOD:  Use T1 for Baud Rate on SIO 1 */
    WDCON |= 0x80;              /* SMOD_1: Set Double Baud Rate */

    TH1    = 0x30;              /* 300 baud at 12 MHz */
    TL1    = 0xFF;

    TR1   = 1;                  /* TR1: T1 Run */
    TI1   = 1;                  /* TI1: Set TI1 to send first char of SIO 1 */

/*------------------------------------------------
------------------------------------------------*/
  while (1) {
    char c;

    sio_port = !sio_port;       /* switch ports */
    printf ("Press a key: ");
    c = _getkey ();
    printf ("\r\nYou pressed %c\r\n", c);
  }
}

MORE INFORMATION

  • Refer to Basic I/O in the Cx51 User's Guide.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.