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
#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;
}
This is a forum for ISP not general 8051 questions. You will probably get a better response at the forum on 8052.com.