原创 圈圈教你玩USB--3.4 读取从主机发送到端点0的数据(程序)

2011-5-2 20:34 3728 9 10 分类: MCU/ 嵌入式

#include<at89x52.h>

#define DEBUG0
#define DEBUG1

#define uint8 unsigned char

#define Fclk       22118400UL
#define BitRate       9600UL
#define D12SetCommandAddr()    P3_5=1
#define D12SetDataAddr()     P3_5=0
#define D12SetWr()       P3_6=1
#define D12ClrWr()      P3_6=0
#define D12SetRd()      P3_7=1
#define D12ClrRd()      P3_7=0
#define D12SetPortIn()     P0=0xFF
#define D12SetPortOut()
#define D12SetData(Value)    P0=Value
#define D12GetData()     P0

#define D12_READ_BUFFER     0xF0
#define D12_ACKNOWLEDGE_SETUP   0xF1
#define D12_CLEAR_BUFFER    0xF2
#define D12_SET_MODE     0xF3
#define READ_INTERRUPT_REGISTER   0xF4

uint8 Sending;
uint8 Buffer[16];

code uint8 HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

void D12Write(uint8 d){
 D12ClrWr();
 D12SetPortOut();
 D12SetData(d);
 D12SetWr();
 D12SetPortIn();
}

void D12WriteByte(uint8 Value){
 D12SetDataAddr();
 D12Write(Value);
}

void D12WriteCommand(uint8 Command){
 D12SetCommandAddr();
 D12Write(Command);
}

uint8 D12ReadByte(){
 uint8 temp;
 D12SetDataAddr();
 D12ClrRd();
 temp=D12GetData();
 D12SetRd();
 return temp;
}

void InitUART(){
 EA=0;
 TMOD&=0x0F;
 TMOD|=0x20;
 PCON=0x80;
 SCON=0x50;
 TH1=256-Fclk/(BitRate*12*16);
 TL1=256-Fclk/(BitRate*12*16);
 ES=1;
 EA=1;
 TR1=1;
}

void UartISR() interrupt 4{
 if(RI) RI=0;
 else{
  TI=0;
  Sending=0;
 }
}

void UartPutChar(uint8 d){
 SBUF=d;
 Sending=1;
 while(Sending);
}

void Prints(uint8 *pd){
 while(*pd!='\0')
  UartPutChar(*pd++);
}

void PrintHex(uint8 d){
 Prints("0x");
 UartPutChar(HexTable[d>>4]);
 UartPutChar(HexTable[d&0x0F]);
 UartPutChar(' ');
}

void UsbConnect(){
#ifdef DEBUG0
 Prints("连接USB。\r\n");
#endif
 D12WriteCommand(D12_SET_MODE);
 D12WriteByte(0x16);
 D12WriteByte(0x47);
}

uint8 D12ReadEndpointLastStatus(uint8 Endp){
 D12WriteCommand(0x40+Endp);
 return D12ReadByte();
}

uint8 D12ReadEndpointBuffer(uint8 Endp,uint8 Len,uint8 *Buf){
 uint8 i,j;
 D12WriteCommand(0x00+Endp);
 D12WriteCommand(D12_READ_BUFFER);
 D12ReadByte();
 j=D12ReadByte();
 if(j>Len)
  j=Len;
#ifdef DEBUG1
 Prints("读端点");
 UartPutChar('0'+Endp/2);
 Prints("缓冲区");
 PrintHex(j);
 Prints("字节。\r\n");
#endif
 for(i=0;i<j;i++){
  *(Buf+i)=D12ReadByte();
 #ifdef DEBUG1
  PrintHex(*(Buf+i));
 #endif
 }
#ifdef DEBUG1
 if(j%16!=0) Prints("\r\n");
#endif
 return j;
}

void D12AcknowledgeSetup(void){
 D12WriteCommand(0x00+1);
 D12WriteCommand(D12_ACKNOWLEDGE_SETUP);
 D12WriteCommand(0x00+0);
 D12WriteCommand(D12_ACKNOWLEDGE_SETUP);
}

void UsbEp0Out(){
#ifdef DEBUG0
 Prints("USB端点0输出中断。\r\n");
#endif
 if(D12ReadEndpointLastStatus(0)&0x20){
  D12ReadEndpointBuffer(0,16,Buffer);
  D12AcknowledgeSetup();
 }
 else
  D12ReadEndpointBuffer(0,16,Buffer);
 D12WriteCommand(D12_ACKNOWLEDGE_SETUP);
}

void main(void){
 uint8 InterruptSource;
 InitUART();
 UsbConnect();
 while(1){
  D12WriteCommand(READ_INTERRUPT_REGISTER);
  InterruptSource=D12ReadByte();
  if(InterruptSource&0x80) Prints("USB总线挂起。\r\n");
  if(InterruptSource&0x40) Prints("USB总线复位。\r\n");
  if(InterruptSource&0x01) UsbEp0Out();
 }
}

PARTNER CONTENT

文章评论1条评论)

登录后参与讨论

用户1622434 2012-2-18 22:21

请问包含了config.h后, #ifdef DEBUG0 是不是调试时一直为真啊?那还要这句干嘛啊?难道运行时会没有吗?我还是听迷惑的。希望圈圈大侠能指点下。谢谢。
相关推荐阅读
Crystal_oscillator 2012-06-06 11:39
我的一些与研究生生涯的相关资料
如题。...
Crystal_oscillator 2012-06-02 19:42
EDA--IPCore的使用
很清晰地讲解了如何在EDA中使用IPCore,推荐在这里,如附件: ...
Crystal_oscillator 2012-05-21 14:12
c#与MySql连接
如附件:...
Crystal_oscillator 2012-04-01 20:41
关于搜索的一点思考
  现在的搜索引擎,仍然面对的一个现实就是:它依然找不到客户所需要的东西。   这里面有两个原因:   (1)现有的东西没有被归类,而是分散在浩如烟海的网络中,各种博客,微博等等。  ...
Crystal_oscillator 2012-04-01 12:00
有趣的加1电路
  在上一篇博客,我介绍了并行加法。在这里,我将带大家学习并行加一电路,并且,我们会发现结 论相当简单有趣。     我们知道对于一位加法电路,假设A,B是参与运算的两个位变量,C_1...
Crystal_oscillator 2012-03-21 17:53
【博客大赛】VHDL实现并行8位加法
  要实现8位加法,先要从1位开始。假设A,B,C_1分别为两个输入值和进位输入,S是和值,C为进位输出值,于是便有以下表达式:   S=A xor B xor C_1;   C=AB+BC_...
我要评论
1
9
关闭 站长推荐上一条 /3 下一条