📄 main.c
字号:
#include "reg52.h"
#include <absacc.h>
typedef char int8; /* 1 : -128 - 127 */
typedef unsigned char uint8; /* 1 : 0 - 255 */
typedef signed short int16; /* 2 : 0 - 65,536 */
typedef unsigned short uint16; /* 2 : -32,768 - 32,767 */
typedef signed long int32; /* 4 : -2,147,483,648 - 2,147,483,648 */
typedef unsigned long uint32; /* 4 : 0 - 4,294,967,295 */
#define _BIT8(n) (((uint8)(1)) << (n))
#define OK 0
#define FAILD 1
#define DISABLE 0
#define ENABLE 1
#define FALSE 0
#define TRUE 1
#define STATE_INIT 0
#define STATE_KEYBOARD_1 1
#define STATE_KEYBOARD_2 2
#define STATE_KB_SEND 3
#define STATE_MOUSE_1 4
#define STATE_MOUSE_2 5
#define STATE_MS_SEND 6
#define KB_HEADER_BYTE 0xaa
#define MS_HEADER_BYTE 0x55
void Delay( uint16 us10 );
void KBInit( void );
uint8 KBSendByte( uint8 dat );
void MSInit( void );
uint8 MSSendByte( uint8 dat );
void UartInit( void );
uint8 UartSend( uint8 dat );
uint8 UartGet( uint8 *dat );
#define RX_BUF_LEN 16
#define TX_BUF_LEN 32
#define RX_BUF_SUB (RX_BUF_LEN - 1)
#define TX_BUF_SUB (TX_BUF_LEN - 1)
uint8 RxBuf[RX_BUF_LEN], TxBuf[TX_BUF_LEN];
uint8 volatile RxIn;
uint8 volatile RxOut;
uint8 volatile TxIn;
uint8 volatile TxOut;
uint8 volatile TxEmpty;
#define LED_PIN P37
#define LED_ON 0
#define LED_OFF 1
uint8 i;
/****************************** MAIN ********************************/
void main( void )
{
uint8 dat, state;
uint8 s;
UartInit();
s = 0;
for( i=0; i<6; i++ ) //send MAC addr
{
TI = 0;
SBUF = CBYTE[2042+i];
s += CBYTE[2042+i];
while( !TI );
}
TI = 0;
SBUF = s;
while( !TI );
KBInit();
MSInit();
EA = ENABLE; //enable interrupt
LED_PIN = LED_ON;
for(i=0; i<255; i++)
{
Delay( 255 );
}
EA = DISABLE;
for( i=0; i<10; i++ )
{
if( !( MSSendByte( 0xf4 ) ) )
{
EA = ENABLE;
break;
}
}
EA = ENABLE;
LED_PIN = LED_OFF;
state = STATE_INIT;
while(1)
{
for( i=0; i<254; i++ )
{
s = UartGet( &dat );
if( s == OK )
{
break;
}
Delay( 100 );
}
if( s != OK )
{
state = STATE_INIT;
}
else
{
switch (state)
{
case STATE_INIT:
if( dat == KB_HEADER_BYTE )
{
state = STATE_KEYBOARD_1;
}
else if( dat == MS_HEADER_BYTE )
{
state = STATE_MOUSE_1;
}
break;
case STATE_KEYBOARD_1:
if( dat == KB_HEADER_BYTE )
{
state = STATE_KB_SEND;
}
else
{
state = STATE_INIT;
}
break;
case STATE_KB_SEND:
EA = DISABLE;
for( i=0; i<3; i++ )
{
if( !( KBSendByte( dat ) ) )
{
EA = ENABLE;
state = STATE_INIT;
break;
}
}
EA = ENABLE;
state = STATE_INIT;
break;
case STATE_MOUSE_1:
if( dat == MS_HEADER_BYTE )
{
state = STATE_MS_SEND;
}
else
{
state = STATE_INIT;
}
break;
case STATE_MS_SEND:
EA = DISABLE;
for( i=0; i<3; i++ )
{
if( !( MSSendByte( dat ) ) )
{
EA = ENABLE;
state = STATE_INIT;
break;
}
}
EA = ENABLE;
state = STATE_INIT;
break;
default:
state = STATE_INIT;
break;
}
}
}
}
void Delay( uint16 us10 ) //10us
{
uint8 i;
for( i=0; i<us10; i++ )
{
}
}
/****************************** KB ********************************/
#define KB_TIMEOUT_VAL 0xff00
#define KB_DATA_IS_HIGH { P35 = 1; }
#define KB_DATA_IS_LOW { P35 = 0; }
#define KB_DATA_IS_INPUT { P35 = 1; }
#define KB_DATA_IN P35
#define KB_CLK_IS_HIGH { P33 = 1; }
#define KB_CLK_IS_LOW { P33 = 0; }
#define KB_CLK_IS_INPUT { P33 = 1; }
#define KB_CLK_IN P33
void KBInit( void )
{
IT1 = 0; //low level triggered
EX1 = ENABLE;
KB_DATA_IS_INPUT;
KB_CLK_IS_INPUT;
}
uint8 KBSendByte( uint8 dat )
{
uint8 parityCnt = 0;
uint8 i;
uint16 timeCnt = 0;
if( KB_CLK_IN )
{
Delay( 100 );
if( KB_CLK_IN )
{
KB_CLK_IS_LOW;
Delay( 10 );
KB_DATA_IS_LOW;
KB_CLK_IS_INPUT;
for( i=0; i<8; i++ )
{
while( KB_CLK_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
if( dat & _BIT8(i) )
{
KB_DATA_IS_HIGH;
parityCnt++;
}
else
{
KB_DATA_IS_LOW;
}
while( !KB_CLK_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
}
while( KB_CLK_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
if( parityCnt & 0x01 )
{
KB_DATA_IS_LOW;
}
else
{
KB_DATA_IS_HIGH;
}
while( !KB_CLK_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
while( KB_CLK_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
KB_DATA_IS_INPUT;
while( KB_DATA_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
while( KB_CLK_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
while( !KB_DATA_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
while( !KB_CLK_IN && (timeCnt<KB_TIMEOUT_VAL) )
{
timeCnt++;
}
if( timeCnt >= KB_TIMEOUT_VAL )
{
return 1;
}
else
{
return OK;
}
}
else
{
return 2;
}
}
else
{
return 3;
}
}
uint8 KBparityCnt = 0;
uint8 KBtmp = 0;
uint8 KBi;
uint16 KBtimeCnt = 0;
void EX1Isr( void ) interrupt 2
{
RD = 1;
RD = 0;
EA = DISABLE;
if( KB_DATA_IN ) //start bit
{
KBparityCnt = 0;
KBtmp = 0;
KBtimeCnt = 0;
EA = ENABLE;
return;
}
while( !KB_CLK_IN && (KBtimeCnt<KB_TIMEOUT_VAL) )
{
KBtimeCnt++;
}
for( KBi=0; KBi<8; KBi++ )
{
while( KB_CLK_IN && (KBtimeCnt<KB_TIMEOUT_VAL) ) //bit i
{
KBtimeCnt++;
}
if( KB_DATA_IN )
{
KBtmp |= _BIT8(KBi);
KBparityCnt++;
}
while( !KB_CLK_IN && (KBtimeCnt<KB_TIMEOUT_VAL) )
{
KBtimeCnt++;
}
}
while( KB_CLK_IN && (KBtimeCnt<KB_TIMEOUT_VAL) ) //parity bit
{
KBtimeCnt++;
}
if( KB_DATA_IN )
{
KBparityCnt++;
}
if( !(KBparityCnt & 0x01) )
{ //parity bit error
KBparityCnt = 0;
KBtmp = 0;
KBtimeCnt = 0;
EA = ENABLE;
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -