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

serial output problem

hi,
i'm using a- 80c31 microcontroller to send the status
of port bits: p3.3,p3.4,p3.5 using serial comm.
my program works fine only in simulation and i cant
get an idea where is the problem.
i checked the TX pin(11) with a scope and it seems that no data are being sent.

please help...

#include <Reg51.h>
sbit Input1 = 0xB3;     /* P3.3  */
sbit Input2 = 0xB4;     /* P3.4  */
sbit Input3 = 0xB5;     /* P3.5  */

unsigned int Tmr1 = 0;
unsigned int Tmr2 = 0;
unsigned int Tmr3 = 0;

void SendStatus() {
if (Input1 == 0){
        P2 = P2 | 0x03;
        Tmr1 = 0;
        }
if (Input2 == 0){
        P2 = P2 | 0x18;
        Tmr2 = 0;
        }
if (Input3 == 0){
        P2 = P2 | 0xC0;
        Tmr3 = 0;
        }
TI = 0;
SBUF = P2;
while (TI == 0);        /* Wait For Transmit */
if (TF0 == 1) {
        Tmr1 = Tmr1 + 1;
        Tmr2 = Tmr2 + 1;
        Tmr3 = Tmr3 + 1;
        if ((Tmr1>912)&&(Tmr2>912)&&(Tmr3>912)) {
                Tmr1 = 0;
                Tmr2 = 0;
                Tmr3 = 0;
                TR0 = 0;
                TF0 = 0;
                P2 = 0x00;
                }
        else if (Tmr1 == 912) {/* 60 Sec Alarm Hold*/
                P2 = P2 & 0xFC;
                }
        else if (Tmr2 == 912) {/* 60 Sec Alarm Hold*/
                P2 = P2 & 0xE7;
                }
        else if (Tmr3 == 912) {/* 60 Sec Alarm Hold*/
                P2 = P2 & 0x3F;
                }
        else {
                TH0 = 0xFF;
                TL0 = 0xFF;
                TF0 = 0;
                }
        }
}


void main() {

SCON = 0x40; /* 8-bit UART, BaudRate set by Timer-1 */
TMOD = 0x21; /* Timer1 mode 2, Timer-0 mode 1 */
TCON = 0x40; /* Start baud clock */
TR0 = 0;        /* Turn Timer-0 off */
TH0 = 0xFF;
TL0 = 0xFF;
TF0 = 0;        /* Clear rollover flag */
TR0 = 0;        /* Turn timer-0 on */
TH1 = 0xCC;     /* 600 baud 12MHz */
IE = 0x00;      /* Disable serial int */
IP = 0x80;      /* Interrupt priority stopped */
P2 = 0x00;
P3 = 0x00;
Input1 = 1;
Input2 = 1;
Input3 = 1;

while(1) {
  while ((Input1 == 1)&&(Input2 == 1)&&(Input3 == 1)) {
     TI = 0;
     SBUF = 0x00;
     while (TI == 0);/* Wait For Transmit */
     }
     TH0 = 0xFF;
     TL0 = 0xFF;
     TR0 = 1;            /* Turn timer-0 on */
     while (TR0 == 1) SendStatus();
     }
}

  • i'm using a- 80c31 microcontroller to send the status of port bits: p3.3,p3.4,p3.5 using serial comm.
    my program works fine only in simulation and i cant
    get an idea where is the problem.
    i checked the TX pin(11) with a scope and it seems that no data are being sent.

    I do not know what you "send the status of port bits" to, but guess some RS232 device like a PC.
    THEN i checked the TX pin seems to indicate that you do not have a MAX232(equivalent) in the chain, in which case P3.0 and P3.1 most likely are blown.

    80c31 why on earth? these days there are derivatives galore with internal flash at all kinds of prices with all kinds of features. Are you a nostalgia buff?

    Erik

  • hi, Erik

    first of all, i'm working with a pcb layout that implemented in other configuration,(for 10 years now) that is why i keep working with 80c31.

    i'm sending the status of the port bit because they are connected to 3 dry contacts of a relay which i
    monitor: open(1) or close(0).

    the TX pin is connected to a ADM233L which converts it to RS232 for the PC COM port.

    thanks

  • Check my 'whittling', try this and then answqer

    does this make The Txd pun toggle?

    #include <Reg51.h>
    void SendStatus()
    {
    TI = 0;
    SBUF = P2;
    while (TI == 0);        /* Wait For Transmit */
    }
    
    
    void main() {
    
    SCON = 0x40; /* 8-bit UART, BaudRate set by Timer-1 */
    TMOD = 0x21; /* Timer1 mode 2, Timer-0 mode 1 */
    TCON = 0x40; /* Start baud clock */
    TH1 = 0xCC;     /* 600 baud 12MHz */
    
    while(1)
    {
     SendStatus();
    
    }
    

    Erik

  • Are you using the aboninable tab character Erik? Your indents seem a bit short ;)

  • Hmm. Strange spelling for abominable...

  • Are you using the aboninable tab character Erik

    naah, cut and paste from the OP. OK maybe I should have cleaned his stuff up a bit better, but it was not "here is the code" but "do something like this"

    Erik

  • Yep,
    Erik your short code is work.

    but what's wrong with mine...

  • find it out

    gradually add the rest, when it stops working you'll know.

    Erik

  • i have encounter in a strange situation that must be solved and understand, with the help of experts or people smarter than me of course.

    All i want to do is to monitor 3 bits of P3:
    P3.3,P3.4,P3.5. the default state of the pins is '1'.(by pull up resistor)

    i monitor (with scope) the pin Txd (P3.1) while i'm simulating the bits aforementioned.
    when i set the P3.3 to 0 the output is OK=> SBUF=0x01.
    but when i set the P3.4 or P3.5 then the result
    of SBUF is inconstant:
    P3.4=0 => SBUF=0x02(OK) and changing to SBUF=0x80(BAD) alternately.
    and when i set P3.5=0 => SBUF=0x04(OK) and changing to SBUF=0x40(BAD),alternately.

    it seems like the value of sbuf is shifting
    according to the time that i set each bit to '0'.

    please help...

    #include <Reg51.h>
    
    sbit Input1 = 0xB3;// P3.3 (INT1) bit set, Input 1
    sbit Input2 = 0xB4;// P3.4 (T0)   bit set, Input 2
    sbit Input3 = 0xB5;// P3.5 (T1)   bit set, Input 3
    
    char TxOK = 0;  // Software transmission flag
    
    void serial_IT(void) interrupt 4
    {
       if (TI == 1) {       // if reception accur
          TI = 0;
          TxOK = 0; // Clear software transmission flag
          }
    }
    
    void main(){
    SCON = 0x40; // 8-bit UART, BaudRate set by Timer-1
    TMOD = 0x20;   // Timer1 mode 2, Timer-0 mode 1
    TR0 = 0;       // turn timer0 off
    TCON = 0x40;   // Start baud clock
    TH1 = 0xE6;    // 1200 baud 11.0592MHz
    TL1 = 0x00;
    ES = 1;       // Enable Serial Int
    EA = 1;       // Enable Globale Int
    TI = 1;        // send buffer first
    
    while(1){
       ACC = 0x00;
       if (Input1 == 0) {
            ACC = 0x01;
            }
       if (Input2 == 0) {
            ACC =ACC | 0x02;
            }
       if (Input3 == 0) {
            ACC =ACC | 0x04;
            }
       TxOK = 1;
       SBUF = ACC;
       while(TxOK);
       }
    }
    

  • Is "Nir Bohadana" the same person as "Nir boh" - who started this thread?

    Your code is almost certainly doomed to failure:

    while(1){
       ACC = 0x00;
       if (Input1 == 0) {
            ACC = 0x01;
            }
       if (Input2 == 0) {
            ACC =ACC | 0x02;
            }
       if (Input3 == 0) {
            ACC =ACC | 0x04;
            }
       TxOK = 1;
       SBUF = ACC ;
       while(TxOK);
       }
    }
    

    In a high-level language such as 'C', you simmply cannot rely upon any processor register such as ACC retaining its value across statements.

    So your first step must be to rewrite this properly in 'C' using a local variable to store your intermediate values...

  • yes i'm the same person.

    OK, i'll try this in a couple of minutes.

    thanks.

  • can you write the line for me that
    declare a local variable to store the bits before sending them to the SBUF.

    i'm confused with unsign char ,char ....

  • "i'm confused with unsign char ,char ...."

    What, specifically, is the confusion?

    Sounds like you need a 'C' textbook?

    http://www.keil.com/books/

  • that is waaaay too 'politically correct'

    Erik

  • i changed my program, and still i found some
    odd things..

    the result are as this

    send >expected output> alternate output
    1...........0x01..............0x40
    2...........0x02..............0x20
    3...........0x03..............0xA0
    4...........0x04..............0x10
    5...........0x05..............0x50
    6...........0x06..............0x90
    7...........0x07..............0xB0

    the alternate output accures when i
    trigger the input bit again.

    #include <Reg51.h>
    
    sbit Input1 = 0xB3;  /* P3.3 (INT1) bit set, Input 1 */
    sbit Input2 = 0xB4;  /* P3.4 (T0)   bit set, Input 2 */
    sbit Input3 = 0xB5;  /* P3.5 (T1)   bit set, Input 3 */
    
    unsigned char TxOK = 0;
    unsigned char xt = 0;
    
    void serial_IT(void) interrupt 4
    {
      if (TI == 1) {
        TI = 0;
        TxOK = 0;
        }
    }
    
    /* ~~~~~~~~~~ */
    /*   Main Sub */
    /* ~~~~~~~~~~ */
    void main(){
    SCON = 0x40;
    TMOD = 0x20;
    TR0 = 0;
    TCON = 0x40;
    TH1 = 0xCC;
    TL1 = 0x00;
    ES = 1;
    EA = 1;
    
    TxOK = 1;
    TI = 1;
    while(TxOK);
    
    while(1){
    if (Input1 == 0) {
            xt = 1;
            if (Input2 == 0) {
              xt = 3;
              if (Input3 == 0) {
                 xt = 6;
                 }
              }
            else if (Input3 == 0) {
              xt = 4;
              }
            }
    else if (Input2 == 0) {
            xt = 2;
            if (Input3 == 0) {
              xt = 5;
              }
            }
    else if (Input3 == 0) {
            xt = 7;
            }
    else xt = 0;
    
    TxOK = 1;
    SBUF = xt;
    while(TxOK);
    }
    }