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

Using Printf statement for both serial ports

Hello All,

I am using MSC1212Y5 Microcontroller. It has two serial ports. I have directed printf to second serial port by changing putchar.c file in keil library. At the same time I am using first port for transmitting single characters only. i.e. 'T' ..'R'..etc. But whenever i try to use printf for both ports, of course by disabling one port for some time then it is not working..it sends data to only second serial port.

Parents
  • ohh sorry I will write the exact code here.

    //Program to test serial port of Texas Instruments MSC1212Y5 Controller
    // BaudRate: 9600
    //Important note: When using Printf disable INTR driven transfer

    #include "reg1212.h"
    #include "stdio.h"

    #define SET 1
    #define RESET 0

    unsigned char ch_0,ch_1,Disp_Flag; //character to rcv

    void init_port(); //Inits UART0 using timer2 at Baud rate of :
    void send_out(unsigned char);
    void init_port_timer1();
    void init_port1();
    void send_out1(unsigned char out1);

    void main()
    { init_port(); init_port1();

    while(1) {

    ES0=1;

    TI_0=1; printf("I am Port0"); TI_0=0; ES0=0;

    ES1=1; TI_1=1; printf("I am port1"); TI_1=0; ES1=0;

    }

    }

    void init_port()
    { P3DDRL|=0x07; //Define port3 for alternate fucntions P3.0=RXD and P3.1=TXD

    SCON=0x50; //Rcv enable mode2 1 start bit 8Data bits 1 Stop bit. RCAP2H=0xFF; //To generate baud rate of 9600 ..clk 25Mhz RCAP2L=0xAD; T2CON=0x34; //timer2 enabled for Baud generation of both Rx and Tx ES0=1; //Enable serial0 INTR
    // EA=1; TI_0=1; //To initiate first character Xmission

    }

    /*
    void init_port_timer1() //Timer1 to generate baud rate for UART0
    { SCON=0x50; TMOD=0x20; //Mode2 8bit autorld TH1=0xF9; //For 9600 with clk 25Mhz TR1=1; //Run timer ES0=1; EA=1; //TI_0=1;
    }*/

    void serial_Intr(void)interrupt 4
    { if(RI_0==1) { ch_0=SBUF0; RI_0=0; }

    /* else if(TI_0==1) { if(!Disp_Flag) goto down; TI_0=0; down: _nop_(); }*/
    }

    void send_out(unsigned char out)
    { SBUF0=out; while(TI_0==0); //Wait while char is Xmitted TI_0=0;
    }

    void init_port1()
    { SCON1=0x50; TH1=0XEC; //19200 Baud rate CKCON|=0x10; TMOD|=0X20; //Timer1 mode 2 TR1=1; ES1=1; TI_1=1; EA=1;

    }

    void send_out1(unsigned char out1)
    {

    SBUF1=out1; while(TI_1==0); //Wait while char is Xmitted TI_1=0;

    }

    void serial_Intr1(void)interrupt 6
    { if(RI_1==1) { ch_1=SBUF1; RI_1=0; }

    else if(TI_1==1) { //if(!Disp_Flag) goto down; TI_1=0; //down: _nop_(); }
    }

    *********************************************************************************
    And the Putchar.C file after modification is as follows.
    ********************************************************************************
    #include <reg51.h>

    #define XON 0x11
    #define XOFF 0x13

    sfr SBUF1 = 0xC1; /* SBUF for SIO 1 */
    sbit RI_1 = 0xC0; /* RI for SIO 1 */
    sbit TI_1 = 0xC1; /* TI for SIO 1 */
    sfr SCON1 = 0xC0; /* SCON for SIO 0 */

    sfr SBUF0 = 0x99;
    sbit RI_0 = 0x98;
    sbit TI_0 = 0x99;
    sfr SCON0 = 0x98;

    /* * putchar (full version): expands '\n' into CR LF and handles * XON/XOFF (Ctrl+S/Ctrl+Q) protocol */
    char putchar (char c) {

    if (c == '\n') { if (RI_1) { if (SBUF1 == XOFF) { do { RI_1 = 0; while (!RI); } while (SBUF1 != XON); RI_1 = 0; } } while (!TI_1); TI_1 = 0; SBUF1 = 0x0d; /* output CR */ } if (RI_1) { if (SBUF1 == XOFF) { do { RI_1 = 0; while (!RI); } while (SBUF1 != XON); RI_1 = 0; } } while (!TI_1); TI_1 = 0; return (SBUF1 = c);
    }

    #if 0 // comment out versions below

    /* * putchar (basic version): expands '\n' into CR LF */
    char putchar (char c) { if (c == '\n') { while (!TI_1); TI_1 = 0; SBUF1 = 0x0d; /* output CR */ } while (!TI_1); TI_1 = 0; return (SBUF1 = c);
    }

    /* * putchar (mini version): outputs charcter only */
    char putchar (char c) { while (!TI_1); TI_1 = 0; return (SBUF1 = c);
    }

    #endif

Reply
  • ohh sorry I will write the exact code here.

    //Program to test serial port of Texas Instruments MSC1212Y5 Controller
    // BaudRate: 9600
    //Important note: When using Printf disable INTR driven transfer

    #include "reg1212.h"
    #include "stdio.h"

    #define SET 1
    #define RESET 0

    unsigned char ch_0,ch_1,Disp_Flag; //character to rcv

    void init_port(); //Inits UART0 using timer2 at Baud rate of :
    void send_out(unsigned char);
    void init_port_timer1();
    void init_port1();
    void send_out1(unsigned char out1);

    void main()
    { init_port(); init_port1();

    while(1) {

    ES0=1;

    TI_0=1; printf("I am Port0"); TI_0=0; ES0=0;

    ES1=1; TI_1=1; printf("I am port1"); TI_1=0; ES1=0;

    }

    }

    void init_port()
    { P3DDRL|=0x07; //Define port3 for alternate fucntions P3.0=RXD and P3.1=TXD

    SCON=0x50; //Rcv enable mode2 1 start bit 8Data bits 1 Stop bit. RCAP2H=0xFF; //To generate baud rate of 9600 ..clk 25Mhz RCAP2L=0xAD; T2CON=0x34; //timer2 enabled for Baud generation of both Rx and Tx ES0=1; //Enable serial0 INTR
    // EA=1; TI_0=1; //To initiate first character Xmission

    }

    /*
    void init_port_timer1() //Timer1 to generate baud rate for UART0
    { SCON=0x50; TMOD=0x20; //Mode2 8bit autorld TH1=0xF9; //For 9600 with clk 25Mhz TR1=1; //Run timer ES0=1; EA=1; //TI_0=1;
    }*/

    void serial_Intr(void)interrupt 4
    { if(RI_0==1) { ch_0=SBUF0; RI_0=0; }

    /* else if(TI_0==1) { if(!Disp_Flag) goto down; TI_0=0; down: _nop_(); }*/
    }

    void send_out(unsigned char out)
    { SBUF0=out; while(TI_0==0); //Wait while char is Xmitted TI_0=0;
    }

    void init_port1()
    { SCON1=0x50; TH1=0XEC; //19200 Baud rate CKCON|=0x10; TMOD|=0X20; //Timer1 mode 2 TR1=1; ES1=1; TI_1=1; EA=1;

    }

    void send_out1(unsigned char out1)
    {

    SBUF1=out1; while(TI_1==0); //Wait while char is Xmitted TI_1=0;

    }

    void serial_Intr1(void)interrupt 6
    { if(RI_1==1) { ch_1=SBUF1; RI_1=0; }

    else if(TI_1==1) { //if(!Disp_Flag) goto down; TI_1=0; //down: _nop_(); }
    }

    *********************************************************************************
    And the Putchar.C file after modification is as follows.
    ********************************************************************************
    #include <reg51.h>

    #define XON 0x11
    #define XOFF 0x13

    sfr SBUF1 = 0xC1; /* SBUF for SIO 1 */
    sbit RI_1 = 0xC0; /* RI for SIO 1 */
    sbit TI_1 = 0xC1; /* TI for SIO 1 */
    sfr SCON1 = 0xC0; /* SCON for SIO 0 */

    sfr SBUF0 = 0x99;
    sbit RI_0 = 0x98;
    sbit TI_0 = 0x99;
    sfr SCON0 = 0x98;

    /* * putchar (full version): expands '\n' into CR LF and handles * XON/XOFF (Ctrl+S/Ctrl+Q) protocol */
    char putchar (char c) {

    if (c == '\n') { if (RI_1) { if (SBUF1 == XOFF) { do { RI_1 = 0; while (!RI); } while (SBUF1 != XON); RI_1 = 0; } } while (!TI_1); TI_1 = 0; SBUF1 = 0x0d; /* output CR */ } if (RI_1) { if (SBUF1 == XOFF) { do { RI_1 = 0; while (!RI); } while (SBUF1 != XON); RI_1 = 0; } } while (!TI_1); TI_1 = 0; return (SBUF1 = c);
    }

    #if 0 // comment out versions below

    /* * putchar (basic version): expands '\n' into CR LF */
    char putchar (char c) { if (c == '\n') { while (!TI_1); TI_1 = 0; SBUF1 = 0x0d; /* output CR */ } while (!TI_1); TI_1 = 0; return (SBUF1 = c);
    }

    /* * putchar (mini version): outputs charcter only */
    char putchar (char c) { while (!TI_1); TI_1 = 0; return (SBUF1 = c);
    }

    #endif

Children