基于51單片機(jī)372的USB通信代碼
#include
#include "INTRINS.h"
#define OP_CMD1// 命令操作碼
#define OP_DATA0// 數(shù)據(jù)操作碼
#define DELAY_TIMES8// 延時(shí)2微秒
#define DELAY_CNT_NUMS2*DELAY_TIMES// 延時(shí)2微秒所需的時(shí)鐘周期數(shù)(對(duì)于22.1184MHz晶振,每個(gè)微秒包含22.1184個(gè)時(shí)鐘周期)
//#define MY_USB_VENDOR_ID0x4348// USB設(shè)備制造商標(biāo)識(shí)
//#define MY_USB_PRODUCT_ID0x5537// USB設(shè)備產(chǎn)品標(biāo)識(shí)
//sbitUSB_CS=P1^3;//cs接地內(nèi)部晶振24.5M 倍頻49M
sbitUSB_INT=P0^0;
sbitUSB_A0=P1^4;
sbitUSB_WR=P1^6;
sbitUSB_RD=P1^5;
//sfrUSB_DATA=0x84;
#define get_usb P2
#define set_usb(dat1) P2=dat1
//#define set_usb(dat1) P2|=(dat1>>1);P17=((dat1 & 0x01)==1)
#define delay_ns _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
constUINT8CMyDevDescr[] = { 0x12, 0x01, 0x10, 0x01,
0xFF, 0x80, 0x37, 0x08,
0x48, 0x43, 0x37, 0x55,// 廠商ID和產(chǎn)品ID
0x00, 0x01, 0x01, 0x02,
0x00, 0x01 };
// 配置描述符
constUINT8CMyCfgDescr[] = { 0x09, 0x02, 0x27, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32,
0x09, 0x04, 0x00, 0x00, 0x03, 0xFF, 0x80, 0x37, 0x00,
0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x00 };
// 語(yǔ)言描述符
constUINT8CMyLangDescr[] = { 0x04, 0x03, 0x09, 0x04 };
// 廠家信息
constUINT8CMyManuInfo[] = { 0x0E, 0x03, 'w', 0, 'c', 0, 'h', 0, '.', 0, 'c', 0, 'n', 0 };
// 產(chǎn)品信息
constUINT8CMyProdInfo[] = { 0x0C, 0x03, 'C', 0, 'H', 0, '3', 0, '7', 0, '4', 0 };
UINT8UsbConfig = 0;
void Delay2us( )
{
INT8U i;
i = (INT8U)DELAY_CNT_NUMS;
while (--i);
}
voidmDelaymS( INT8U ms )
{
INT8U j;
while ( ms -- ) {
j=250;
do
{
Delay2us();
Delay2us();
//Delay2us();
//Delay2us();
}
while ( j -- );
}
}
void CH375_WR_CMD_PORT(INT8U dat)
{
USB_WR = 1;
USB_RD = 1;
set_usb(dat);
delay_ns;
USB_A0 = OP_CMD;
// 準(zhǔn)備寫命令
_nop_();
//USB_CS = 0;
USB_WR = 0;
USB_WR = 0;
USB_WR = 0;
USB_WR = 0;
USB_WR = 1;
//USB_CS = 1;// 結(jié)束寫命令(USB_WR信號(hào)的有效時(shí)間不能超過(guò)10us)
USB_A0 = 0;
P2=0xff;
//P2|=0x7f;
//P1|=0x80;
}
void CH375_WR_DAT_PORT(INT8U dat)
{
USB_WR = 1;
USB_RD = 1;
set_usb(dat);
delay_ns;
USB_A0 = OP_DATA;// 準(zhǔn)備寫數(shù)據(jù)
_nop_();
//USB_CS = 0;
USB_WR = 0;
USB_WR = 0;// 開始寫數(shù)據(jù)
USB_WR = 0;
USB_WR = 0;
USB_WR = 1;// 結(jié)束寫命令(USB_WR信號(hào)的有效時(shí)間不能超過(guò)10us)
//USB_CS = 1;
USB_A0 = 0;
P2=0xff;
//P2|=0x7f;
//P1|=0x80;
}
INT8U CH375_RD_DAT_PORT(void)
{
INT8U nData;
USB_WR= 1;
USB_RD= 1;
delay_ns;
//delay_ns;// 將USB_DATA(P4口)配置為數(shù)字輸入
USB_A0 = OP_DATA;
//USB_CS = 0;// 準(zhǔn)備讀數(shù)據(jù)
_nop_();
USB_RD = 0;
USB_RD = 0;
USB_RD = 0;
nData = get_usb;
USB_RD = 1;// 結(jié)束讀命令(USB_RD信號(hào)的有效時(shí)間不能超過(guò)10us)
//USB_CS = 1;
USB_A0 = 0;
return nData;// 返回讀取的數(shù)據(jù)
}
INT8URead374Byte(INT8U mAddr )
{
CH375_WR_CMD_PORT( mAddr );
return( CH375_RD_DAT_PORT());
}
voidWrite374Byte( INT8U mAddr, INT8U mData )
{
CH375_WR_CMD_PORT( mAddr );
CH375_WR_DAT_PORT( mData );
}
voidRead374Block( INT8U mAddr, INT8U mLen, INT8U * mBuf )
{
CH375_WR_CMD_PORT( mAddr );
while ( mLen -- ) *mBuf++ = CH375_RD_DAT_PORT( );
}
voidWrite374Block( INT8U mAddr, INT8U mLen, INT8U * mBuf )
{
INT8U dat,k;
k=0;
CH375_WR_CMD_PORT( mAddr );
while ( mLen -- )
{
dat=*mBuf++;
//dat= up[k++];
CH375_WR_DAT_PORT(dat);
}
}