下面是運(yùn)用串口進(jìn)行模塊調(diào)試返回的數(shù)據(jù)。
Chek OK
USB work type:51
Set mode OK
INT = 0F
bus rest!
INT = 0F
bus rest!
INT = 0C
Length = 08
Quest = 0x80 0x86 0x80 0x81 0x80 0x80 0xC0 0x80?
SETUP = 86
調(diào)試?RESET_ALL 實(shí)現(xiàn)硬件復(fù)位??CHECK_EXIST 測試工作狀態(tài) OK
設(shè)置 USB 工作模式?51H CMD_RET_SUCCESS 操作成功
但是當(dāng)插上 USB 數(shù)據(jù)線后獲取上位機(jī)機(jī)的標(biāo)準(zhǔn)請求碼,數(shù)據(jù)不正常?
0x80 0x86 0x80 0x81 0x80 0x80 0xC0 0x80?
最高位為0x80 不知道,請教一下這是哪里出了問題。
下面是代碼:
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define haltif(cmp,msg) if(cmp){print(msg);halt_}
#define halt_ for(;;){}
sbit INT = P3^2;
sbit A0 = P2^1;
sbit CS = P2^0;
sbit Read = P3^7;
sbit Write = P3^6;
void Delay10ms()//@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void Wr_cmd( unsigned char cmd)?
{? /* 向CH375的命令端口寫入命令,周期不小于4uS,如果單片機(jī)較快則延時(shí) */
P0 = 0xFF;
_nop_();
_nop_();
CS = 0;
A0 = 1;?
Write = 0;
P0 = cmd;
_nop_();
_nop_();_nop_();
Write = 1;
A0 = 0;
_nop_();
P0 = 0xFF;?
_nop_();
}
void Wr_Data( unsigned char dat)?
{? /* 向CH375的數(shù)據(jù)端口寫入數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) */
P0 = 0xFF;
_nop_();_nop_();
_nop_();_nop_();
CS = 0;
Write = 0;
P0 = dat;
_nop_();_nop_();
_nop_();_nop_();
Write = 1;
CS = 1;
_nop_();
P0 = 0xFF;
}
void Rd_Data(uchar *dat)?
{? /* 從CH375的數(shù)據(jù)端口讀出數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) */
P0 = 0xFF;
_nop_();_nop_();
_nop_();_nop_();
CS = 0;
Read = 0;
_nop_();
_nop_();
_nop_();_nop_();
*dat = P0;
Read = 1;
CS = 1;
A0 = 1;
_nop_();_nop_();
_nop_();_nop_();
P0=0xFF;
}
void SendOneByte(unsigned char c)? ?
{
SBUF = c;? ? ? ? ? ?//把數(shù)據(jù)送往寄存器
while(!TI);? ? ? ? ?//等待發(fā)送完成TI=0時(shí)一直發(fā)送,直到TI=1,發(fā)送后硬件自動置1
TI = 0;? ? ? ? ? ? ?//發(fā)送完成標(biāo)志
}
void print(unsigned char *p)
{
while(*p!='\0')
{
SendOneByte(*p++);
}
}
void print_8x(uchar c)
{
uchar String1[2];
uchar Str1[16]={"0123456789ABCDEF"};
uchar cH,cL,i;
cH = (c>>4)&0x0f;
cL = c & 0x0f;
String1[0] = Str1[cH];
String1[1] = Str1[cL];
for(i=0;i<2;i++)
{
SendOneByte(String1[i]);
}
}
?void UART_Init(void)
?{
? ? ? ? ? ? ? ? //單片機(jī)初始化//通信設(shè)置,串行口中斷
TMOD = 0x20;
SCON = 0x50;
TL1 = 0xFD;//設(shè)置定時(shí)初始值
TH1 = 0xFD;//設(shè)置定時(shí)初始值
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;? ? //定時(shí)器中斷1
IT0 = 1;? ? //外部中斷0下降沿觸發(fā)方式
EX0 = 1;? ? //開外部中斷0?
?}
void CH375_Init()
{
Read = 1;
Write = 1;
A0? = 0;
}
uchar poll_interrput()//獲取中斷狀態(tài)
{
uchar i;
while(INT);
Wr_cmd(0x22);
Rd_Data(&i);
return i;
}
uchar bRequest = 0;
void main(void)
{
uchar buf[8];//用于接受標(biāo)準(zhǔn)請求
? ?uchar i,tem,j;
CH375_Init();//初始化Ch375
INT = 1;
UART_Init();
Delay10ms();
Wr_cmd(0x05); //執(zhí)行硬件復(fù)位
Delay10ms(); //延時(shí)40us
Delay10ms();
Delay10ms();
Delay10ms();
Wr_cmd(0x06);//檢測工作狀態(tài)
Wr_Data(0xA5);//寫入數(shù)據(jù)
Rd_Data(&i);
tem = ~0x0A5;
haltif(i != tem,"Chek exits error!\r\n");? //檢測數(shù)據(jù)是否取反
print("Chek OK\r\n");
Wr_cmd(0x15); //設(shè)置USB工作模式
Wr_Data(0x01);//設(shè)置外部固件模式
for(;;)
{
Rd_Data(&i);
print("USB work type:");
print_8x(i);
print("\r\n");
if(i == 0x51)
{
break;
}
}
print("Set mode OK\r\n");
while(1)
{
i =poll_interrput();//獲取中斷狀態(tài)?
print("INT = ");
print_8x(i);
print("\r\n");
switch(i)
{
case 0x0c:
Wr_cmd(0x28);
Rd_Data(&i);
print("Length = ");
print_8x(i);
print("\r\n");
if(i==8)
{
for(j=0;j<8;j++)
{
Rd_Data(buf + j);
}
bRequest = buf[1];
print("Quest = ");
for(j=0;j<8;j++)
{
print("0x");
print_8x(buf[j]);
print(" ");
}
print("\r\n");
print("SETUP = ");
print_8x(bRequest);
print("\r\n");
switch(bRequest)
{
case 0x00:
break;
case 0x06:
break;
}
}
break;
default:
if((i&0x03)==0x03)
{
print("bus rest!\r\n");
}
Wr_cmd(0x23);
Rd_Data(&i);
break;
}
}
}
void int0(void) interrupt 0? ?//外部0中斷服務(wù)
{
EX0 = 0;
INT = 1;
EX0 = 1;?
}
void UARTInterrupt(void) interrupt 4? //4號串行中斷
{
? RI = 0;
}?