void send(uchar x) /* function for send a char data*/
{
uchar i,temp,char_temp;
bit flag_check =1;
INHIBIT =1; //disable standard keyboard
delay_ms(3);
temp = x;
for( i=0; i<8; i++) //find the number of 1 in this uchar x is odd or not
{
char_temp = temp & 0x01;
if(char_temp == 0x01)
{
flag_check =!flag_check;
}
temp = temp >>1;
}
CLK =1; //send 1 to P1 then read P1
while(!CLK) //if CLK is low wait
{
;
}
CLK =1;
DATA =1; //send 1 to P1 then read P1
if(CLK ==1)
{
delay_us(30);
}
if(CLK==1 && DATA==1) //send data
{
DATA =0; //start bit 0
delay_us(10);
CLK =0;
delay_us(5);
temp =x;
for(i=0;i<8;i++) //send 8 bits LSB first
{
CLK =1;
delay_us(5);
char_temp = temp & 0x01;
if ( char_temp == 0x01)
{
DATA =1;
}
else
{
DATA =0;
}
//DATA=(bit)(temp&0x01);
//LSB
delay_us(10);
CLK = 0;
delay_us(5);
temp = temp>>1;
}
CLK = 1; //send check bit
delay_us(5);
DATA = flag_check;
delay_us(10);
CLK = 0;
delay_us(5);
CLK =1; //send stop bit
delay_us(5);
DATA =1;
dalay_us(10);
CLK = 0;
delay_us(5);
CLK =1;
delay_us(30);
CLK =1;
DATA =1 ; //send 1 to P1 then read P1
if(CLK ==1 && DATA == 0)
{
return ; //pc is sending data to mcu,goto
//receiving function
}
INHIBIT = 0; //enable standard keyboard
}