main.c

来自「单片机USB接口模拟键盘,该程序可以控制PC机的PS/2键盘的NUM LOCK和」· C语言 代码 · 共 242 行

C
242
字号
// Switch S1 is connected to P0.2 (Down=Start Movement/Up=Stop Movement)
// Switch S2 is connected to P0.3 (Down=Left Button Down/Up=Left Button Up)
// Switch S3 is connected to P0.4 (Not used)
// 
// LED D1 is connected to P0.5 (Tracks S1: On=Down/Off=Up)
// LED D2 is connected to P0.6 (Tracks S2: On=Down/Off=Up)
// LED D3 is connected to P1.3 (Not used)
// 
//----------------------------------------------------------------------------

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules

// Constant for button handling
#define GO        1
#define STOP      0
#define UP        0
#define DOWN      1
#define NULL      0
#define FULL      1
// Constants for the mouse reports
#define STEP_SIZE 1
#define X_SIGN    1
#define Y_SIGN    1
#define Cap_L_UP	1
#define Cap_L_DOWN	0
#define Shift_UP	1
#define Shift_DOWN	0
// Movement Vectors for the letters U, S, and B

/*#define STEP(ctl,NULL,char1,char2,char3,char4,char5,char6)	( ctl ), \
        (NULL),(char1),(char2),(char3),(char4),(char5),(char6),0

const BYTE a_logo_vectors[] =
{
  STEP(NULL, NULL,0x00,0x00,0x00,0x00,0x00,0x97),  //	;1
  STEP(Shift_DOWN,NULL,0x00,0x00,0x00,0x00,0x00,0x97),  //	;1
};

void delay(unsigned char ch)
{
unsigned char couter; 
while(ch)
	{
	couter=0xff;
	while(couter)
		couter--;
	ch--;
	}
	return;
}	
		*/
void TD_init()
{
	M8C_EnableGInt;
	
//	USB_INTERFACE_0_OUT_RPT_DATA[0] = 0;
	
//	OSC_CR0 &= ~OSC_CR0_CPU;
//	OSC_CR0 |=  OSC_CR0_CPU_24MHz;
	
	USB_Start(0);
	while(USB_bGetConfiguration() == 0);
	
//	while(USB_INTERFACE_0_OUT_RPT_DATA[0] == 0);
}		
// 
BYTE left_button;
BYTE move,right_button;
BYTE pre_move,pre_LB_down,pre_RB_down;
BYTE a_ram[8]={0x00, 0x00,0x53,0x00,0x00,0x00,0x00,0x00};
BYTE b_ram[8]={0x00, 0x00,0x47,0x00,0x00,0x00,0x00,0x00};
BYTE c_ram[8]={0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00};
BYTE d_ram[8]={0x00, 0x00,0x39,0x00,0x00,0x00,0x00,0x00};
//unsigned char *p_data;
void main()
{
    TD_init();
    
    //p_data = &a_logo_vectors[0]; // Point to the first of the logo vectors

    move = STOP;
    left_button = UP;
    right_button= UP;
    pre_move=UP;
    pre_LB_down=UP;
    pre_RB_down=UP;
    
    P0DATA |= (0x20);		// P0.5 low (on)
    P0DATA |= (0x40);     	// P0.6 low (on)
    P1DATA |= (0x08);		// P1.3 low (on)
    
    while (1)
    {
    	move         = ((P0DATA & 0x04) ? STOP : GO);  // P0.2 Up or Down
	    left_button  = ((P0DATA & 0x08) ? UP : DOWN);  // P0.3 Up or Down
	    right_button = ((P0DATA & 0x10) ? UP : DOWN);  // P0.4 Up or Down
	    if (move == STOP)
	 	{
			P0DATA |= 0x20;        // P0.5 high (off)
			pre_move=UP;
		//	while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
		//	USB_LoadEP(1, &c_ram[0], 8);			
		}
		if (move == GO && pre_move ==UP)
		{
			P0DATA &= ~(0x20);     // P0.5 low (on)	
			pre_move=DOWN;
			// Check to see if the endpoint is empty
			while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
			USB_LoadEP(1, &a_ram[0], 8);
		}
		///////////////////
		if (left_button == UP)
		{
		    P0DATA |= 0x40;        // P0.6 high (off)
		    pre_LB_down =UP;
		//	while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
		//	USB_LoadEP(1, &c_ram[0], 8);			
		}
		if (left_button == DOWN && pre_LB_down ==UP)
		{
		  	P0DATA &= ~(0x40);     // P0.6 low (on)	
		   	pre_LB_down=DOWN;
		   	switch (USB_bGetEPState(1))
    		{
    			case 0:	P0DATA &= ~(0x20);break;		// P0.5 low (on)
    			case 1:	P0DATA &= ~(0x40);break;     	// P0.6 low (on)
    			case 2:	P1DATA &= ~(0x08);break;		// P1.3 low (on)
    			default: break;
    		}
		    // Check to see if the endpoint is empty
//			while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
			USB_LoadEP(1, &b_ram[0], 8);
		}
				///////////////////
		if (right_button == UP)
		{
		    P1DATA |= 0x08;        // P1.3 high (off)
		    pre_RB_down =UP;
		//	while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
		//	USB_LoadEP(1, &c_ram[0], 8);			
		}
		if (right_button == DOWN && pre_RB_down ==UP)
		{
		  	P1DATA &= ~(0x08);     // P0.6 low (on)	
		   	pre_RB_down=DOWN;
//		   	USB_EnableEP(1);
		   	switch (USB_bGetEPState(1))
    		{
    			case 0:	P0DATA &= ~(0x20);break;		// P0.5 low (on)
    			case 1:	P0DATA &= ~(0x40);break;     	// P0.6 low (on)
    			case 2:	P1DATA &= ~(0x08);break;		// P1.3 low (on)
    			default: break;
    		}
		    // Check to see if the endpoint is empty
//			while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
			USB_LoadEP(1, &d_ram[0], 8);
		}
		/*
		        
	    switch (USB_bGetEPState(1))
    	{
    		case 0:	P0DATA &= ~(0x20);break;		// P0.5 low (on)
    		case 1:	P0DATA &= ~(0x40);break;     	// P0.6 low (on)
    		case 2:	P1DATA &= ~(0x08);break;		// P1.3 low (on)
    		default: break;
    	}
    	while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
    	USB_LoadEP(1, &b_ram[0], 8);
    	USB_LoadEP(1, &c_ram[0], 8);
    
    	USB_EnableEP(1);
    
    	switch (USB_bGetEPState(1))
    	{
    		case 0:	P0DATA &= ~(0x20);break;		// P0.5 low (on)
    		case 1:	P0DATA &= ~(0x40);break;     	// P0.6 low (on)
    		case 2:	P1DATA &= ~(0x08);break;		// P1.3 low (on)
    		default: break;
    	}
    
    	USB_LoadEP(1, &a_ram[0], 8);
    
    	USB_DisableEP(1);
    	switch (USB_bGetEPState(1))
    	{
    		case 0:	P0DATA &= ~(0x20);break;		// P0.5 low (on)
    		case 1:	P0DATA &= ~(0x40);break;     	// P0.6 low (on)
    		case 2:	P1DATA &= ~(0x08);break;		// P1.3 low (on)
    		default: break;
    	}*/
	}
	
    while (0)
    {
    	//USB接收保存PC数据
  //  	while (USB_bGetEPState(1) == OUT_BUFFER_EMPTY)
    	{
   // 		USB_EnableEP(1);

		    //USB按键发送键盘数据
	        move        = ((P0DATA & 0x04) ? STOP : GO);  // P0.2 Up or Down
	        left_button = ((P0DATA & 0x08) ? UP : DOWN);  // P0.3 Up or Down
	        ///////////////////
	        if(move== GO || left_button==DOWN)
	        {
	        	if (move == STOP)
	 	       	{
		            P0DATA |= 0x20;        // P0.5 high (off)
		            pre_move=UP;
		            while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
		            USB_LoadEP(1, &c_ram[0], 8);			
		        }
		        if (move == GO && pre_move ==UP)
		        {
		        	P0DATA &= ~(0x20);     // P0.5 low (on)	
		        	pre_move=DOWN;
		            // Check to see if the endpoint is empty
		            while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
		            USB_LoadEP(1, &a_ram[0], 8);
		        }
		        ///////////////////
		        if (left_button == UP)
		        {
		            P0DATA |= 0x40;        // P0.6 high (off)
		            pre_LB_down =UP;
		            while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
		            USB_LoadEP(1, &c_ram[0], 8);			
		        }
		        if (left_button == DOWN && pre_LB_down ==UP)
		        {
		        	P0DATA &= ~(0x40);     // P0.6 low (on)	
		        	pre_LB_down=DOWN;
		            // Check to see if the endpoint is empty
		            while(USB_bGetEPState(1) != IN_BUFFER_EMPTY);
		            USB_LoadEP(1, &b_ram[0], 8);
		        }
		    }
	    }
    }
}

⌨️ 快捷键说明

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