⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code1.txt

📁 单片机与电脑一体机数据传送的接收部分.请多多支持
💻 TXT
字号:
void KeyBoardReceive(void)      
{
    	BYTE i;    
    	BYTE ucPARITY;		
       BYTEDataType ucReceiveBuf;
    	//EX1=0;                     //disable interrupt
	mcuClrIntEn();
	
    	MCU_Delayxus(40); //delay 24 us
	//when CLK rising edge and Data is low ,start receive
	    i = 10;
	    while (i--)
	    {
	        if (KBDATA)
	        {
	        	 continue;
	        }
		else
		{
			break;
		}
	        MCU_Delayxus(4); //delay 24 us
	    } // while
	//    putstr("\r\n--KB0--");
	//when CLK rising edge and Data is low ,start receive

    	if(KBDATA)    		//if keyboard data is low,recieve start,else exit and send 0xFE to host
    	{ 
//	    putstr("\r\n--KB0--");
    		KeyBaordSend(0xfe);
	    	//EX1=1;                     //enable interrupt
	    	mcuSetIntEn();
       	return;
    	}
		MCU_Delayxus(12);	//delay 40 us    
		MCU_Delayxus(12);	//delay 40 us    
		MCU_Delayxus(12);	//delay 40 us    

//-----------------------------------------------------------------
#if 0
    ES = 0; // disable uart interrupt
    SBUF =49; // transfer to uart
    // wait transfer completing
    while(1) { if (TI) break; } // check flag
    TI = 0; // clear flag
    ES = 1; // release uart interrupt
#endif
//-----------------------------------------------------------------	
//	putstr("\r\n--KB0--");		
    	//start generate keyboard clk	
//------------------detect if  there are other keyboard connect-------------------------
//when power up delay to detect extern keyboard
//if  there are other keyboard,exit comunicat	//delay 40us*60 = 2400us
	if (GET_KB_INT)
	{
//		putstr("\r\n--dect extern keyboard--");	
//		MCU_Delayxus(12);	//delay 40 us    
//		MCU_Delayxus(12);	//delay 40 us    
//		MCU_Delayxus(12);	//delay 40 us    
	    i = 100;
	    while (i--)
	    {
	        if (KBCLK)
	        {
	        	 continue;
	        }
		else
		{

#if 0
				putstr("\r\n--extern keyboard--");	
#endif
				CLAER_COMMAND_FLAG();
				EXTERN_KEYBOARD();
	    			//EX1=1;                     //enable interrupt
			    	mcuSetIntEn();
				MCU_INTENA1A &= 0xEF;		//disable P0_6 interrupt and exit
		    		return;
		}
	        MCU_Delayxus(4); //delay 24 us
	    } // while		
	}
//----------------------------------------------------------------------------	
    	KBCLK=0;     			//generate the keyboard clk
	//receive one bit at rising edging
    	for(i=0;i<8;i++)	//receive data from host
    	{		
		MCU_Delayxus(12);//delay 40 us
        	KBCLK=1;        
		MCU_Delayxus(6);//delay 16 us	
	    	if(!KBCLK) 		//receive the stop bit
	    	{
		    	//EX1=1;                     //enable interrupt
		    	mcuSetIntEn();
	    		return;
	    	}			
		ucReceiveBuf.ucByte.bit7 = KBDATA; 	//data is LSB
		 if(i<7)
		 {
	           	ucReceiveBuf.ucData = ucReceiveBuf.ucData << 1;			        		
		 }		
		MCU_Delayxus(12);//delay 16 us	
        	KBCLK=0;
      }
       MCU_Delayxus(12);	//delay 24 us
	//receive parity bit	
    	KBCLK=1;	
	MCU_Delayxus(6);	//delay 24 us	
	//if  clk was pull down,exit!
    	if(!KBCLK) 		//receive the stop bit
    	{
	    	//EX1=1;                     //enable interrupt
	    	mcuSetIntEn();
    		return;
    	}		
//-----------------------------------------------------------------		
#if 0
    ES = 0; // disable uart interrupt
    SBUF =50; // transfer to uart
    // wait transfer completing
    while(1) { if (TI) break; } // check flag
    TI = 0; // clear flag
    ES = 1; // release uart interrupt
#endif
//-----------------------------------------------------------------
    	ucPARITY=KBDATA;  //reciver parity bit	
	MCU_Delayxus(6);			//delay 24 us	
	//receive stop bit,data was pull high
    	KBCLK=0;    
	MCU_Delayxus(12);	//delay 24 us			
    	KBCLK=1;	
	MCU_Delayxus(6);	//delay 24 us		
//	putstr("\r\n--KB0--");	
    	if(!KBDATA) 		//receive the stop bit
    	{
    		KeyBaordSend(0xfe);
	    	//EX1=1;                     //enable interrupt
	    	mcuSetIntEn();
    		return;
    	}		
//-----------------------------------------------------------------	
#if 0
    ES = 0; // disable uart interrupt
    SBUF =51; // transfer to uart
    // wait transfer completing
    while(1) { if (TI) break; } // check flag
    TI = 0; // clear flag
    ES = 1; // release uart interrupt
#endif
//-----------------------------------------------------------------
	
	//when receive the stop bit,keyboard data should be high,if not,send 0xFE to host
//	putstr("\r\n--KB1--");		
	//send ack to host
//	MCU_Delayxus(12);//delay 24 us		
    	KBDATA=0;				//send ack to host
	MCU_Delayxus(6);//delay 24 us	
    	KBCLK=0;
    	MCU_Delayxus(12);//delay 40 us
    	KBCLK=1; 
    	MCU_Delayxus(6);//delay 40 us		
    	KBDATA=1;				//set keyboard clk and data high
    	MCU_Delayxus(12);//delay 16 us
	//judge the pairy bit
//	putstr("\r\n--KB3--");

	ucPARITY = ucPARITY&0x01;
//-----------------------------------------------------------------
#if 0
    ES = 0; // disable uart interrupt
    SBUF =(ucPARITY+48); // transfer to uart
    // wait transfer completing
    while(1) { if (TI) break; } // check flag
    TI = 0; // clear flag
    ES = 1; // release uart interrupt

    ES = 0; // disable uart interrupt
    SBUF =(Parity(ucReceiveBuf.ucData )+48); // transfer to uart
    // wait transfer completing
    while(1) { if (TI) break; } // check flag
    TI = 0; // clear flag
    ES = 1; // release uart interrupt
#endif
//----------------------------------------------------------------- 
    	if(ucPARITY !=Parity(ucReceiveBuf.ucData ))//judge the parity bit,if ok return the data else send 0xFE to host and exit
    	{
    		KeyBaordSend(0xfe);
	    	//EX1=1;                     //enable interrupt
	    	mcuSetIntEn();
    		return;
    	}		

//-----------------------------------------------------------------
#if 0
    ES = 0; // disable uart interrupt
    SBUF =52; // transfer to uart
    // wait transfer completing
    while(1) { if (TI) break; } // check flag
    TI = 0; // clear flag
    ES = 1; // release uart interrupt
#endif
//----------------------------------------------------------------- 
//	putstr("\r\n--KB4--");		
	MCU_Delayxus(12);//delay 40 us	
	MCU_Delayxus(12);//delay 40 us	
	MCU_Delayxus(12);//delay 40 us	
 //   	putstr("\r\n--KB1--");	
//	putstr("\r\n--KB2--");				
//	MCU_Delayxus(12);//delay 40 us		
	//send the ack command to computer
//	KeyBaordSend(0xfa);           //send the ack
	sKeyboardCommand.ucCommand =ucReceiveBuf.ucData;// ucReceiveBuf.ucData;
	sKeyboardCommand.ucFlag          = TRUE;       
//	putstr("\r\n--KB--");
/*
	if (sKeyboardCommand.ucFlag == FALSE)     //no command
	{
//		putstr("\r\n--KB ok--");	
		sKeyboardCommand.ucCommand = ucReceiveBuf.ucData;
		sKeyboardCommand.ucFlag          = TRUE;                //command OK			
	}
	else
	{
		if ((sKeyboardCommand.ucCommand == 0xF0)||(sKeyboardCommand.ucCommand == 0xF3))  //command need parameter
		{
			sKeyboardCommand.ucParameter= ucReceiveBuf.ucData; //command parameter receive
		}
		sKeyboardCommand.ucFlag = FALSE;
	}
*/

	receive_process();
    	//EX1=1;  				//enable interrupt	
    	mcuSetIntEn();
//	putstr("\r\n--KB1--");		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -