看了一下USBDevice设备例子,改了一个USBHID的设备。实现了键盘鼠标手柄三合一例子。默认开始是手柄,可以通过手柄输出切换成键盘或者鼠标输出数据。用PB12,PB13,PB14,PB15做按键输入模拟。
main:
if(ctrl_data == 0x80)
{
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12))
{
pEP1_IN_DataBuf[6] |= 0x01;
}else
{
pEP1_IN_DataBuf[6] &= ~0x01;
}
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13))
{
pEP1_IN_DataBuf[6] |= 0x02;
}else
{
pEP1_IN_DataBuf[6] &= ~0x02;
}
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14))
{
pEP1_IN_DataBuf[6] |= 0x04;
}else
{
pEP1_IN_DataBuf[6] &= ~0x04;
}
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15))
{
pEP1_IN_DataBuf[6] |= 0x08;
}else
{
pEP1_IN_DataBuf[6] &= ~0x08;
}
DevEP1_IN_Deal( 7 );
}else if(ctrl_data == 0x20)
{
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12))
{
pEP2_IN_DataBuf[0] |= 0x01;
}else
{
pEP2_IN_DataBuf[0] &= ~0x01;
}
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13))
{
pEP2_IN_DataBuf[0] |= 0x02;
}else
{
pEP2_IN_DataBuf[0] &= ~0x02;
}
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14))
{
pEP2_IN_DataBuf[0] |= 0x04;
}else
{
pEP2_IN_DataBuf[0] &= ~0x04;
}
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15))
{
pEP2_IN_DataBuf[0] |= 0x08;
}else
{
pEP2_IN_DataBuf[0] &= ~0x08;
}
DevEP2_IN_Deal( 8 );
}else if(ctrl_data == 0x40)//
{
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12))
{
pEP3_IN_DataBuf[1] = 1;
}else if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13))
{
pEP3_IN_DataBuf[1] = 0xff;
}else
{
pEP3_IN_DataBuf[1] = 0;
}
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14))
{
pEP3_IN_DataBuf[2] = 1;
}else if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15))
{
pEP3_IN_DataBuf[2] = 0xff;
}else
{
pEP3_IN_DataBuf[2] = 0;
}
DevEP3_IN_Deal( 4 );
}
Delay_Ms(10);
}
具体输出控制在:
void DevEP1_OUT_Deal( UINT8 l )
{
ctrl_data = pEP1_OUT_DataBuf[0];
if(pEP1_OUT_DataBuf[0] & 0x01)
{
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
}else
{
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET );
}
if(pEP1_OUT_DataBuf[0] & 0x02)
{
GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_SET );
}else
{
GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_RESET );
}
}
下面是测试程序:
|