Naruto
Naruto

Reputation: 1810

communication of 8051 micro controllers C

I have written a code for communication between 2 micro controllers. Controller 1 sends a number to Controller 2 and goes in 8 bit mode of transmission , if the number is matched, Controller 2 also goes in to 8 bit mode. The code is compiling without errors or warnings but still I am not getting any output on Proteus. I am posting the code:

void resetTimer16();


void main()
{
unsigned char i;

i=0;

SCON=0x80;     // 9bit data mode 

TMOD=0x10; // 16bit timer mode

resetTimer16();

TR1=1;

while(1)
{
    if(i==0)
        SBUF=1; // send slave 1 id
    else
        {
            TMOD=0x20;      // move into 8 bit mode

            TH1=-12; // considering a baud rate of 2400 to achieve

            SBUF='U';

            TR1=1;
        }

    while(TI==0)
    {
        if(TF1==1)
        {
            if(i==0)
                resetTimer16();

            TF1=0;
        }
    }

    TI=0;

    i++;        

    if(i==2)
        break;
}

while(1);
 }


void resetTimer16()
{
TH1=0xff;
TL1=0xf4;
} 

Controller 2 (receiver controller)

sbit rs=P2^4;
sbit en=P2^5;

void resetTimer16();
void sendDataLCD(unsigned char dataa);
void sendCommandLCD(unsigned char );
void delay();

void main()
{
unsigned char i,dataa;

i=0;
dataa=0;

SCON=0x90; // enabled receiving and 9bit mode

TMOD=0x20;

            sendCommandLCD(0x38);
        sendCommandLCD(0x0E);
        sendCommandLCD(0x01);
        sendCommandLCD(0x02);

resetTimer16();

while(1)
{
    while(RI==0)
    {
        if(TF1==1)
        {
            if(i==0)
                resetTimer16();

            dataa=1;
            TF1=0;
        }
    }
    RI=0;

    dataa=SBUF;

    if(dataa==1)
    {
        i=1;

        TMOD=0x10; // move into 8bit mode

        TH1=-12;

        TR1=1;
    }

    while(RI==0);

    RI=0;

    dataa=SBUF;


    if(dataa>0) // for testing purpose controler 1 is not sending data =0
    {   
        sendDataLCD(dataa);
    }

}
}

void resetTimer16()
{
TH1=0xff;
TL1=0xf4;
}

 void sendDataLCD(unsigned char dataa)
{
P1=dataa;

rs=1;

en=1;
delay();
en=0;
 }

void sendCommandLCD(unsigned char cmd)
 {
P1=cmd;

rs=0;

en=1;
delay();
en=0;
 }

void delay()
{
unsigned char i,j;

for(i=0;i<255;i++)
    for(j=0;j<255;j++)
    {}
}

I have tried to figure out the problem but I failed, so I am seeking help from experts here. Your help is greatly appreciated.

Regards

Upvotes: 2

Views: 891

Answers (1)

Michael Dorgan
Michael Dorgan

Reputation: 12515

In your 2nd controller, you are setting TMOD to 0x20 - 8-bit auto-reload, but according to that link, 8-bit mode uses one register for the counter and the other for the destination - meaning that as you count up, you are only giving it 11 - tics before interrupting. I am not familiar with the timings of this chip, but that seems to be a small value. Especially considering you are calling it a "resetTimer16", which applies better to the timer mode than the first chip is using. Is this screwing up your BAUD rate causing bytes not to be received perhaps?

If not, I'll keep digging in a bit...

More digging - is EA, ET1, and ES all enabled - are interrupts, timer1, and serial communications interrupts allowed?

Upvotes: 1

Related Questions