PDIUSBD12芯片做的usb键盘的一个例子,51单片机主控.
EDN USB开发板上共有8个按键,功能分别为:
K1:左Ctrl键 K2:左Shift键 K3:左Alt键 K4:0键
K5:1键 K6:2键 K7:Caps Lock键 K8:Num Lock键
按键功能测试:
同时按下K1和K2可做输入法切换,这跟键盘上的两个键功能一样的。
K7可打开大写字母锁定,同时LED2亮起。K8是小数字键盘锁定键,
当数字键盘打开时,LED1亮起,这时可用K4,K5,K6输入数字。
请留意EDN板上的LED会跟你自己的键盘上的LED灯同步。不管按哪块
键盘切换都是如此。不过按住切换键不放,可能会导致两块键盘之间
的LED失步。
所有资料51hei提供下载:
PDIUSBD12-keyboard.rar
(112.77 KB, 下载次数: 48)
PDIUSBD12芯片USB键盘的单片机源程序如下:
- /******************************************************************
- 本程序只供学习使用,未经作者许可,不得用于其它任何用途
- USBKeyBoard.C file
- 作者:Computer-lov
- 版本:V1.1
- 版权所有,盗版必究。
- Copyright(C) Computer-lov 2007-2017
- All rights reserved
- *******************************************************************/
- #include "AT89x52.H"
- #include "USB.H"
- #include "PDIUSBD12.H"
- #include "My_type.h"
- #include "UART.H"
- #include "key.h"
- #include "keyboard.h"
- #include "led.h"
- #define DispLineInf() \
- { \
- Prints(" in file \""); \
- Prints(__FILE__); \
- Prints(", @ line "); \
- PrintLongInt(__LINE__-1); \
- Prints("\r\n"); \
- }
- //#define debug
- #define USB_COMMAND_ADD 1
- #define USB_DATA_ADD 0
- //USB芯片连接引脚
- #define USB_DATA P0
- #define USB_A0 P3_5
- #define USB_WR P3_6
- #define USB_RD P3_7
- #define USB_INT P3_2
- #define ENDPOINT_NUMBER 2
- #define MAX_CONTROL_DATA_SIZE 16
- //控制传输时保存数据
- CONTROL_DATA_BUFF Control_Data;
- //缓冲
- uint8 idata buffer[64];
- //交换数据时用的指针
- uint8 *pData;
- uint8 idle; //空闲状态
- uint8 protocol; //当前协议
- //高低字节交换
- #define SWAP16(x) ((((uint16)(x))<<8)|(((uint16)(x))>>8))
- //取一个整数的低字节
- #define LSB(x) ((uint8)(x))
- //取一个整数的高字节
- #define MSB(x) ((uint8)(((uint16)(x))>>8))
- //字符串描述符所用的语言种类
- code uint8 LANGUAGE_ID[4]={0x04,0x03,0x09,0x04};
- //设备序列号
- code uint8 device_serial_number[]=
- {22,STRING_DESCRIPTOR,'2',0,'0',0,'0',0,'7',0,'-',0,'1',0,'1',0,'-',0,'2',0,'9',0};
- //厂商字符串
- //汉字Unicode编码
- code uint8 ManufacturerString[80]=
- {80,STRING_DESCRIPTOR,0x35,0x75,0x11,0x81,0x08,0x57,0x08,0x57,0x84,0x76,'U',0x00,
- 'S',0x00,'B',0x00,0x13,0x4E,0x3A,0x53,0x20,0x00,'H',0,'t',0,'t',0,
- 'p',0,':',0,'/',0,'/',0,'g',0,'r',0,'o',0,'u',0,'p',0,'.',0,'e',0,'d',0,'n',0,
- 'c',0,'h',0,'i',0,'n',0,'a',0,'.',0,'c',0,'o',0,'m',0,'/',0,'9',0,'3',0};
- //产品字符串
- //汉字Unicode编码
- code uint8 ProducterString[96]=
- {96,STRING_DESCRIPTOR,0x35,0x75,0x11,0x81,0x08,0x57,0x08,0x57,0x84,0x76,'U',0x00,
- 'S',0x00,'B',0x00,0x13,0x4E,0x3A,0x53,0x3B,0x6D,0xA8,0x52,0x4B,0x4E,'U',0x00,
- 'S',0x00,'B',0x00,0x2E,0x95,0xD8,0x76,0x20,0x00,'H',0,'t',0,'t',0,
- 'p',0,':',0,'/',0,'/',0,'g',0,'r',0,'o',0,'u',0,'p',0,'.',0,'e',0,'d',0,'n',0,
- 'c',0,'h',0,'i',0,'n',0,'a',0,'.',0,'c',0,'o',0,'m',0,'/',0,'9',0,'3',0};
- code DEVICE_DESCRIPTOR_STRUCT device_descriptor= //设备描述符
- {
- sizeof(DEVICE_DESCRIPTOR_STRUCT), //设备描述符的字节数大小
- DEVICE_DESCRIPTOR, //设备描述符类型编号
- SWAP16(0x0110), //USB版本号,USB1.1
- 0x00, //USB分配的设备类代码
- 0x00, //USB分配的子类代码
- 0x00, //USB分配的设备协议代码
- 0x10, //端点0的最大包大小
- SWAP16(0x8888), //厂商编号VID,这里只是作为测试用,请使用自己的VID
- SWAP16(0x0001), //产品编号PID,这里只是作为测试用,请使用自己的PID
- SWAP16(0x0001), //设备出厂编号
- 0x01, //设备厂商字符串的索引
- 0x02, //描述产品字符串的索引
- 0x03, //描述设备序列号字符串的索引
- 0x01 //可能的配置数量
- };
- //定义配置,接口,端点等描述符结构体,因为它们是随配置描述符一起返回的
- typedef struct _CON_INT_ENDP_DESCRIPTOR_STRUCT
- {
- CONFIGURATION_DESCRIPTOR_STRUCT configuration_descriptor;
- INTERFACE_DESCRIPTOR_STRUCT interface_descriptor;
- HID_DESCRIPTOR_STRUCT hid_descriptor;
- ENDPOINT_DESCRIPTOR_STRUCT endpoint_descriptor[ENDPOINT_NUMBER];
- }CON_INT_ENDP_DESCRIPTOR_STRUCT;
- code CON_INT_ENDP_DESCRIPTOR_STRUCT con_int_endp_descriptor=
- {
- //configuration_descriptor //配置描述符
- {
- sizeof(CONFIGURATION_DESCRIPTOR_STRUCT), //配置描述符的字节数大小
- CONFIGURATION_DESCRIPTOR, //配置描述符类型编号
- SWAP16(sizeof(CON_INT_ENDP_DESCRIPTOR_STRUCT)),
- 0x01, //只包含一个接口
- 0x01, //该配置的编号
- 0x00, //iConfiguration字段
- 0xA0, //采用总线供电,支持远程唤醒
- 0xC8 //从总线获取最大电流400mA
- },
- //interface_descritor //接口描述符
- {
- sizeof(INTERFACE_DESCRIPTOR_STRUCT), //接口描述符的字节数大小
- INTERFACE_DESCRIPTOR, //接口描述符类型编号
- 0x00, //接口编号为0
- 0x00, //该接口描述符的编号
- ENDPOINT_NUMBER, //非0端点数量为2,只使用端点主端点输入和输出
- 0x03, //人机接口设备(HID)类
- 0x01, //使用的子类:支持BIOS根启动Boot的子类
- 0x01, //使用的协议:键盘
- 0x00 //接口描述符字符串索引
- },
- //hid_descriptor
- {
- sizeof(HID_DESCRIPTOR_STRUCT), //HID描述符的字节数大小
- HID_DESCRIPTOR, //HID描述符类型编号
- SWAP16(0x0110), //HID类协议版本号,为1.1
- 0x21, //固件的国家地区代号,0x21为美国
- NUM_SUB_DESCRIPTORS, //下级描述符的数量
- {
- {
- REPORT_DESCRIPTOR, //下级描述符为报告描述符
- SWAP16(sizeof(KeyBoardReportDescriptor)) //下级描述符的长度
- }
- },
- },
- //endpoint_descriptor[]
- {
- { //主端点输入描述
- sizeof(ENDPOINT_DESCRIPTOR_STRUCT), //端点描述符的字节数大小
- ENDPOINT_DESCRIPTOR, //端点描述符类型编号
- MAIN_POINT_IN, //端点号,主输入端点
- ENDPOINT_TYPE_INTERRUPT, //使用的传输类型:中断传输
- SWAP16(0x0040), //该端点支持的最大包尺寸,64字节
- 0x0A //中断扫描时间:设置为10ms
- },
-
- { //主端点输出描述
- sizeof(ENDPOINT_DESCRIPTOR_STRUCT), //端点描述符的字节数大小
- ENDPOINT_DESCRIPTOR, //端点描述符类型编号
- MAIN_POINT_OUT, //端点号,主输出端点
- ENDPOINT_TYPE_INTERRUPT, //使用的传输类型:中断传输
- SWAP16(0x0040), //该端点支持的最大包尺寸,64字节
- 0x0A //中断扫描时间:设置为10ms
- }
- }
- };
- union //程序标志位
- {
- uint16 Register;
- struct
- {
- uint8 bus_reset :1;
- uint8 suspend :1;
- uint8 remote_wakeup :1;
- uint8 int_isr :1;
- uint8 not_end :1;
- uint8 usb_idle :1;
- uint8 usb_busy :1;
- uint8 setup_packet_in :1;
- uint8 setup_packet_out :1;
- uint8 set_addr :1;
- uint8 usb_endp0_in :1;
- uint8 usb_endp2_in :1;
- uint8 usb_endp2_out :1;
- }flags;
- }usb_flags;
- union //中断寄存器
- {
- uint8 Register[2];
- struct
- {
- uint8 control_out_port :1;
- uint8 control_in_port :1;
- uint8 port_out_1 :1;
- uint8 port_in_1 :1;
- uint8 main_out_port :1;
- uint8 main_in_port :1;
- uint8 bus_reset :1;
- uint8 suspend_change :1;
- uint8 DMA_EOT :1;
- uint8 not_use :7;
- }Interrupt;
- }Interrupt_Register;
- union //端点最后处理状态
- {
- uint8 Register;
- struct
- {
- uint8 successful :1;
- uint8 error_code :4;
- uint8 setup_packet :1;
- uint8 data_1 :1;
- uint8 prestatus_not_read :1;
- }Status;
- }Last_Status;
- /***************** 延时x毫秒函数 ***********/
- void delay(uint16 x)
- {
- uint16 i;
- uint16 j;
- for(i=0;i<x;i++)
- for(j=0;j<230;j++);
- }
- /********************************************/
- /*******************************写USB命令******************************************/
- void write_usb_command(uint8 usb_command)
- {
- USB_A0=USB_COMMAND_ADD;
- USB_DATA=usb_command;
- USB_WR=0;
- USB_WR=1;
- USB_DATA=0xFF;
- }
- /******************************************************************************/
- /*********************写一字节USB数据*******************************************/
- void write_a_usb_data(uint8 usb_data)
- {
- USB_A0=USB_DATA_ADD;
- USB_DATA=usb_data;
- USB_WR=0;
- USB_WR=1;
- USB_DATA=0XFF;
- }
- /******************************************************************************/
- /****************************读一字节USB数据************************************/
- uint8 read_a_usb_data(void)
- {
- uint8 temp;
- USB_A0=USB_DATA_ADD;
- USB_RD=0;
- temp=USB_DATA;
- USB_RD=1;
- return temp;
- }
- /******************************************************************************/
- /************************读USB中断寄存器**************************************/
- void read_interrupt_register(void)
- {
- write_usb_command(Read_Interrupt_Register);
- Interrupt_Register.Register[0]=read_a_usb_data();
- Interrupt_Register.Register[1]=read_a_usb_data();
- }
- /******************************************************************************/
- /************************设置USB地址*******************************************/
- void set_usb_addr(uint8 addr)
- {
- write_usb_command(Set_Address);
- write_a_usb_data(0x80|addr);
- Prints(" 设置地址.\r\n");
- Prints(" 地址为: ");
- PrintLongInt(addr);
- Prints("\r\n");
- }
- /******************************************************************************/
- /*************************端点使能******************************************/
- void set_endpoint_enable(void)
- {
- write_usb_command(Set_Endpoint_Enable);
- write_a_usb_data(0x01);
- }
- /******************************************************************************/
- /****************************选择终端点*************************************/
- uint8 select_endpoint(uint8 endp)
- {
- write_usb_command(Select_EndPoint+endp);
- return read_a_usb_data();
- }
- /******************************************************************************/
- /****************************读端点最后状态**********************************/
- uint8 read_last_status(uint8 endp)
- {
- write_usb_command(Read_Last_Status+endp);
- return read_a_usb_data();
- }
- /******************************************************************************/
- /****************************设置端点状态**************************************/
- void set_endpoint_status(uint8 endp,uint8 status)
- {
- write_usb_command(0x40+endp);
- write_a_usb_data(!status);
- }
- /******************************************************************************/
- /*****************************读端点状态**************************************/
- uint8 read_endpoint_status(uint8 endp)
- {
- write_usb_command(0x80+endp);
- return read_a_usb_data();
- }
- /******************************************************************************/
- /************************清缓冲,在读取缓冲数据后调用**************************/
- void clear_buffer(void)
- {
- write_usb_command(Clear_Buffer);
- #ifdef debug
- Prints("Clear buffer.\r\n");
- #endif
- }
- /******************************************************************************/
- /***********************缓冲区数据有效,在写缓冲后调用**************************/
- void validate_buffer(void)
- {
- write_usb_command(Validate_Buffer);
- #ifdef debug
- Prints("Validate buffer.\r\n");
- #endif
- }
- /******************************************************************************/
- /***************************应答建立包************************************/
- void ack_setup(uint8 endp)
- {
- select_endpoint(endp);
- write_usb_command(Ack_Setup);
- #ifdef debug
- Prints("Ack setup ");
- PrintLongInt(endp);
- Prints(".\r\n");
- #endif
- }
- /******************************************************************************/
- /***********************出错处理******************************************/
- void error(uint8 number)
- {
- Prints("有错误发生!!!\r\n");
- number=0;
- }
- /******************************************************************************/
- /*************************读终端点缓冲****************************************/
- uint8 read_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
- {
- uint8 i,j;
- read_last_status(endp);
- if(!(select_endpoint(endp)&0x01)){error(0); return 0;}
- if((read_endpoint_status(endp)&0x60)!=0x60) //两个缓冲没有都满,才能清中断
- {
- read_last_status(endp); //清中断
- }
- write_usb_command(Read_Buffer);
- read_a_usb_data();
- j=read_a_usb_data();
- if(j>len)
- j=len;
- #ifdef debug
- Prints("Read endpoint");
- PrintLongInt(endp);
- Prints("\'s buffer ");
- PrintLongInt(j);
- Prints(" bytes.\r\n");
- #endif
- for(i=0;i<j;i++)
- {
- USB_RD=0;
- *(buff+i)=USB_DATA;
- USB_RD=1;
- #ifdef debug
- PrintHex(*(buff+i));
- #endif
- }
- #ifdef debug
- Prints("\r\n");
- #endif
- clear_buffer();
- return j;
- }
- /******************************************************************************/
- /*************************写终端点缓冲*****************************************/
- uint8 write_endpoint_buff(uint8 endp,uint8 len,uint8 * buff)
- {
- uint8 i;
- read_last_status(endp);
- select_endpoint(endp);
- write_usb_command(Write_Buffer);
- write_a_usb_data(0);
- write_a_usb_data(len);
- #ifdef debug
- Prints("Write endpoint");
- PrintLongInt(endp);
- Prints("\'s buffer ");
- PrintLongInt(len);
- Prints(" bytes.\r\n");
- #endif
- for(i=0;i<len;i++)
- {
- USB_DATA=*(buff+i);
- USB_WR=0;
- USB_WR=1;
- #ifdef debug
- PrintHex(*(buff+i));
- #endif
- }
- #ifdef debug
- Prints("\r\n");
- #endif
- USB_DATA=0xFF;
- validate_buffer();
- return len;
- }
- /******************************************************************************/
- /***************************断开USB连接****************************************/
- void disconnect_usb(void)
- {
- Prints("断开USB连接.\r\n");
- write_usb_command(0xf3);
- write_a_usb_data(0x0e);
- write_a_usb_data(0x47);
- delay(100);
- }
- /******************************************************************************/
- /*******************************连接USB**************************************/
- void connect_usb(void)
- {
- Prints("连接USB.\r\n");
- write_usb_command(0xf3); //初始化USBD12
- write_a_usb_data(0x1e); //连接USB
- write_a_usb_data(0x47); //设置频率
- }
- /******************************************************************************/
- /***************************初始化USB***************************************************/
- void init_usb(void)
- {
- Prints("USBD12芯片初始化\r\n");
- set_usb_addr(0);
- set_endpoint_enable();
- }
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
|