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失步。
0.png (44.57 KB, 下載次數: 90)
下載附件
2017-8-7 22:43 上傳
所有資料51hei提供下載:
PDIUSBD12-keyboard.rar
(112.77 KB, 下載次數: 48)
2017-8-7 19:02 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
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黑下載附件…………
復制代碼
|