Author Topic: 89c51rd2BA  (Read 4789 times)

Amol

  • Guest
89c51rd2BA
« on: December 14, 2004, 02:21:39 am »
Hi,

I have written a code for serial communication between RD2 and PC, the code is as follows

Timer 2 is used to generate the baud rate for the serial port, timer 0 is used as interrupt to decrement the counter

putch function is polling based serial transmit function

void main(void)
{
......
while (1)
{
putch('a');     // Send data to PC

// wait till counter exhausts or sensor signal obtained
while((MX1Y==1) && (enm==1))
{
     MXCLK= MCLK;    // Give clock pulses
}

putch('b');

} //end of while(1)

}//end of main

void putch(unsigned char txdata)
{
 SBUF=txdata;
 while(TI==0)
 {
 }
 TI=0;
}


The problem is that after sending the first character 'a' to PC the counter starts decrementing in the timer 0 interrupt, but after counter is exhausted or the sensor signal is found the while loop breaks and send the 'b' character to PC but the character is not received on PC. And after that any serial communication is disabled!!!!.

Can anyone tell me the reason why serial communication is disabled after the while loop...

Regards,
Amol


Amol

  • Guest
Complete code
« Reply #1 on: December 14, 2004, 02:42:43 am »
#include
#include

sfr  mxadd2  = 0xA0;
sfr  mxadd1  = 0xB0;
sbit MX1Y = P3^7;
sbit MX2Y = P2^4;
sbit M5DIR = P1^4;
sbit M5CLK = P1^5;
#define OPH   0xD0

bit enm, MCLK;

int cnt;

long step;

void putch(unsigned char txdata);


void init_uart(void)
{                        //     baud  = 9.6 K  
      SCON = 0x50;
      T2MOD = 0x00;
      RCAP2H = 0xFF;
        RCAP2L = 0xB8;
      TH2 = 0xFF;
        TL2 = 0xB8;             /* */
      T2CON = 0x34;
}

void main(void)
{

   enm=MCLK=0;

   IE=0;

   TH1  = 0xFF;
   TL1  = 0x80;
   TMOD = 0x10;
   TCON = 0x00;

   init_uart();
   ES=0;
   ET1=1;

   TR1=0;
   EA=1;
   MX1Y=1;
   while (1)
   {
      putch('S');
      putch('N');

      M5DIR =1;
      step=7882;
      enm=1;

      mxadd1=OPH;
      ET1=1;
      TR1=1;

      // wait till sensor signal is low or enm=0(i.e steps are zero)
      while((enm==1) && (MX1Y==1))
      {
         M5CLK = MCLK;
      }
      ET1=0;
      TR1=0;
      TR2=1;
      putch('D');
   }
}


void serial_isr(void) interrupt 4
{
   EA=0;
   if(RI)
   {
      RI=0;
   }
   EA=1;
}

void timer0_ISR(void) interrupt 3
{
   EA=0;
   if(MCLK==0)
   {
      TH1  = 0xFF;
      TL1  = 0x80;
   }
   else
   {
      TH1  = 0xFE;//220 MICROSEC
      TL1  = 0x80;
   }

   MCLK=!MCLK;

   step--;
   if (step==0)
      enm=0;

   EA=1;
}

void putch(unsigned char txdata)
{
   SBUF=txdata;
   while(TI==0)
   {
   }
   TI=0;
}

Andy Ayre

  • ESAcademy Staff
  • Sr. Member
  • *****
  • Posts: 2121
    • View Profile
    • Embedded Systems Academy, Inc.
    • Email
Re: 89c51rd2BA
« Reply #2 on: December 14, 2004, 06:22:10 am »
This is a forum for ISP not general 8051 questions. You will probably get a better response at the forum on 8052.com.

Embedded Systems Academy, Inc.
support at esacademy dot com