main.LST
上传用户:lyfy_2008
上传日期:2014-07-13
资源大小:3016k
文件大小:15k
源码类别:

USB编程

开发平台:

Visual C++

  1. C51 COMPILER V7.02b   MAIN                                                                 09/28/2006 22:04:34 PAGE 1   
  2. C51 COMPILER V7.02b, COMPILATION OF MODULE MAIN
  3. OBJECT MODULE PLACED IN main.OBJ
  4. COMPILER INVOKED BY: C:KeilC51BINC51.EXE main.c BROWSE DEBUG OBJECTEXTEND
  5. stmt level    source
  6.    1          //#include <stdio.h>
  7.    2          #include <string.h>
  8.    3          #include <reg51.h>                /* special function register declarations   */
  9.    4          
  10.    5          //#include "epphal.h"
  11.    6          #include "d12ci.h"
  12.    7          #include "mainloop.h"
  13.    8          #include "usb100.h"
  14.    9          #include "chap_9.h"
  15.   10          //#include "protodma.h"
  16.   11          #include"address.h"
  17.   12          //#include"2-16.h"
  18.   13          //#include"24C00.H"
  19.   14          
  20.   15          
  21.   16          /*
  22.   17          //*************************************************************************
  23.   18          // USB protocol function pointer arrays
  24.   19          //*************************************************************************
  25.   20          */
  26.   21          
  27.   22          #define uchar unsigned char 
  28.   23          unsigned char code Tab[]=
  29.   24          { //数码管数据0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
  30.   25          0x12,0x9F,0x31,0x15,0x9C,0x54,0x50,0x1F,
  31.   26          0x10,0x14,0x18,0xD0,0x72,0x91,0x70,0x78
  32.   27          };
  33.   28          void send(uchar buf1,uchar buf2); //数码管显示程序
  34.   29          void EndpointII();//处理  2*16LCD和IIC
  35.   30          //USB标准请求
  36.   31          code void (*StandardDeviceRequest[])(void) =
  37.   32          {
  38.   33                  get_status,
  39.   34                  clear_feature,
  40.   35                  reserved,
  41.   36                  set_feature,
  42.   37                  reserved,
  43.   38                  set_address,
  44.   39                  get_descriptor,
  45.   40                  reserved,
  46.   41                  get_configuration,
  47.   42                  set_configuration,
  48.   43                  get_interface,
  49.   44                  set_interface,
  50.   45                  reserved,
  51.   46                  reserved,
  52.   47                  reserved,
  53.   48                  reserved
  54.   49          };
  55.   50          
  56.   51          //用户厂商请求
  57.   52          code void (*VendorDeviceRequest[])(void) =
  58.   53          {
  59.   54                  reserved,
  60.   55                  reserved,
  61. C51 COMPILER V7.02b   MAIN                                                                 09/28/2006 22:04:34 PAGE 2   
  62.   56                  reserved,
  63.   57                  reserved,
  64.   58                  reserved,
  65.   59                  reserved,
  66.   60                  reserved,
  67.   61                  reserved,
  68.   62                  reserved,
  69.   63                  reserved,
  70.   64                  reserved,
  71.   65                  reserved,
  72.   66                  reserved,
  73.   67                  reserved,
  74.   68                  reserved,
  75.   69                  reserved
  76.   70          };
  77.   71          
  78.   72          /*
  79.   73          //*************************************************************************
  80.   74          //  Public static data
  81.   75          //*************************************************************************
  82.   76          */
  83.   77          
  84.   78          extern EPPFLAGS bEPPflags;
  85.   79          extern unsigned long ClockTicks;
  86.   80          extern unsigned char idata GenEpBuf[];
  87.   81          extern IO_REQUEST idata ioRequest;
  88.   82          
  89.   83          extern unsigned char ioSize, ioCount;
  90.   84          extern unsigned char idata EpBuf[];
  91.   85          extern unsigned char idata mainbuflen;
  92.   86          
  93.   87          CONTROL_XFER ControlData;
  94.   88          BOOL bNoRAM;
  95.   89          
  96.   90          //定时器0初始化程序
  97.   91          //定时器用来定时检测按键状态
  98.   92          void init_timer0(void)
  99.   93          {
  100.   94   1              TMOD &= 0XF0;                    /* clear Timer 0   */
  101.   95   1              TMOD  |= 0X1;
  102.   96   1              TL0 = 0X0;                   /* value set by user    */
  103.   97   1              TH0 = 0X0;                  /* value set by user  */
  104.   98   1              ET0 = 1;        /* IE.1*/
  105.   99   1              TR0 = 1;                /* TCON.4 start timer  */
  106.  100   1              PT0 = 1;
  107.  101   1      
  108.  102   1              EA = 1;
  109.  103   1      }
  110.  104          
  111.  105          //中断设置
  112.  106          void init_special_interrupts(void)
  113.  107          {
  114.  108   1              IT1 = 0;
  115.  109   1              EX1 = 1;
  116.  110   1              PX1 = 0;
  117.  111   1      }
  118.  112          
  119.  113          //I/O口初始化程序
  120.  114          void init_port()
  121.  115          {
  122.  116   1              P0 = 0xFF;
  123.  117   1              P1 = 0xFF;
  124. C51 COMPILER V7.02b   MAIN                                                                 09/28/2006 22:04:34 PAGE 3   
  125.  118   1              P2 = 0xFF;
  126.  119   1              P3 = 0xFF;
  127.  120   1      }
  128.  121          void init()
  129.  122          {
  130.  123   1       send(0xff,0xff);//数码管关
  131.  124   1       LEDDATA=0x00;//LED关
  132.  125   1      
  133.  126   1      }
  134.  127          void main(void)
  135.  128          {
  136.  129   1              init_port();//初始化I/O口
  137.  130   1      //      initlcd();
  138.  131   1      //      printscreen("Hello!!" ,"Wellcome to USB");
  139.  132   1              init_timer0();//初始化定时器0
  140.  133   1              init_special_interrupts();//设置中断
  141.  134   1              init();
  142.  135   1              bEPPflags.value = 0;
  143.  136   1              reconnect_USB();//重新连接USB
  144.  137   1              /* Main program loop */
  145.  138   1      
  146.  139   1              while( TRUE )
  147.  140   1              {
  148.  141   2      
  149.  142   2                      if (bEPPflags.bits.timer)
  150.  143   2                      {
  151.  144   3                              DISABLE;//定时器溢出,检测按键状态
  152.  145   3                              bEPPflags.bits.timer = 0;
  153.  146   3                              ENABLE;
  154.  147   3      
  155.  148   3                              if(bEPPflags.bits.configuration)//设备未配置返回
  156.  149   3                                      check_key_LED();                //检测键盘
  157.  150   3                      }
  158.  151   2      
  159.  152   2      
  160.  153   2                      if (bEPPflags.bits.bus_reset) 
  161.  154   2                      {//设备复位处理
  162.  155   3                              DISABLE;
  163.  156   3                              bEPPflags.bits.bus_reset = 0;
  164.  157   3                              ENABLE;
  165.  158   3                      
  166.  159   3                      } // if bus reset
  167.  160   2      
  168.  161   2                      if(bEPPflags.bits.ep1_rxdone)  //端点1输出
  169.  162   2                      {  //控制LED/数码管/
  170.  163   3                      /*
  171.  164   3                        GenEpBuf[0]    操作对象  
  172.  165   3                                1                        LED
  173.  166   3                                2                        数码管
  174.  167   3                                3                        锋鸣器
  175.  168   3                                0x55             读IIC
  176.  169   3                      */
  177.  170   3                       DISABLE;
  178.  171   3                       bEPPflags.bits.ep1_rxdone=0;
  179.  172   3                       ENABLE;
  180.  173   3                       if(GenEpBuf[0]==1) P1 = GenEpBuf[1]; //LEDDATA=GenEpBuf[1];
  181.  174   3                       if(GenEpBuf[0]==2)send(Tab[GenEpBuf[1]],Tab[GenEpBuf[2]]);
  182.  175   3                       if(GenEpBuf[0]==3)BEEB=GenEpBuf[1]&0x01;
  183.  176   3                       if(GenEpBuf[0]==0x55)
  184.  177   3                       {
  185.  178   4      //                Read24c02_nByte(GenEpBuf[1],10,EpBuf) ;
  186.  179   4                        D12_WriteEndpoint(5, 10, EpBuf);
  187. C51 COMPILER V7.02b   MAIN                                                                 09/28/2006 22:04:34 PAGE 4   
  188.  180   4                       }
  189.  181   3                       GenEpBuf[0]=0;
  190.  182   3      
  191.  183   3                      }
  192.  184   2              
  193.  185   2      
  194.  186   2                      if (bEPPflags.bits.setup_packet)
  195.  187   2                      {//Setup包处理
  196.  188   3                              DISABLE;
  197.  189   3                              bEPPflags.bits.setup_packet = 0;
  198.  190   3                              ENABLE;
  199.  191   3                              control_handler();//调用请求处理子程序
  200.  192   3                      } // if setup_packet
  201.  193   2      
  202.  194   2                      if(bEPPflags.bits.main_rxdone)                  //端点2输出
  203.  195   2                      {
  204.  196   3                              DISABLE;
  205.  197   3                              bEPPflags.bits.main_rxdone=0;
  206.  198   3                              ENABLE;
  207.  199   3                              EndpointII();//操作LCD和I2C
  208.  200   3                      } //if main_rxdone
  209.  201   2              } // Main Loop
  210.  202   1      }
  211.  203          
  212.  204          //返回stall应答
  213.  205          void stall_ep0(void)
  214.  206          {
  215.  207   1              D12_SetEndpointStatus(0, 1);
  216.  208   1              D12_SetEndpointStatus(1, 1);
  217.  209   1      }
  218.  210          
  219.  211          //断开USB总线
  220.  212          void disconnect_USB(void)
  221.  213          {
  222.  214   1              // Initialize D12 configuration
  223.  215   1              D12_SetMode(D12_NOLAZYCLOCK, D12_SETTOONE | D12_CLOCK_12M);
  224.  216   1      }
  225.  217          
  226.  218          //连接USB总线
  227.  219          void connect_USB(void)
  228.  220          {
  229.  221   1              // reset event flags
  230.  222   1              DISABLE;
  231.  223   1              bEPPflags.value = 0;//清除所有状态
  232.  224   1              ENABLE;
  233.  225   1      
  234.  226   1              // V2.1 enable normal+sof interrupt
  235.  227   1              D12_SetDMA(D12_ENDP4INTENABLE | D12_ENDP5INTENABLE);
  236.  228   1      
  237.  229   1              // Initialize D12 configuration
  238.  230   1              D12_SetMode(D12_NOLAZYCLOCK|D12_SOFTCONNECT, D12_SETTOONE | D12_CLOCK_12M);
  239.  231   1      }
  240.  232          
  241.  233          //重新连接到USB总线
  242.  234          void reconnect_USB(void)
  243.  235          {
  244.  236   1              unsigned long clk_cnt;
  245.  237   1              disconnect_USB();
  246.  238   1              clk_cnt = ClockTicks;
  247.  239   1              while(ClockTicks < clk_cnt + 20);
  248.  240   1              connect_USB();
  249.  241   1      }
  250. C51 COMPILER V7.02b   MAIN                                                                 09/28/2006 22:04:34 PAGE 5   
  251.  242          
  252.  243          //恢复到未配置状态
  253.  244          void init_unconfig(void)
  254.  245          {
  255.  246   1              D12_SetEndpointEnable(0);       /* Disable all endpoints but EPP0. */
  256.  247   1      }
  257.  248          
  258.  249          //设置配置状态
  259.  250          void init_config(void)
  260.  251          {
  261.  252   1              D12_SetEndpointEnable(1);       /* Enable  generic/iso endpoints. */
  262.  253   1      }
  263.  254          
  264.  255          //从端点号1发送数据
  265.  256          void single_transmit(unsigned char * buf, unsigned char len)
  266.  257          {
  267.  258   1              if( len <= EP0_PACKET_SIZE) {
  268.  259   2                      D12_WriteEndpoint(1, len, buf);
  269.  260   2              }
  270.  261   1      }
  271.  262          
  272.  263          //发送端点号1建立代码
  273.  264          void code_transmit(unsigned char code * pRomData, unsigned short len)
  274.  265          {
  275.  266   1              ControlData.wCount = 0;
  276.  267   1              if(ControlData.wLength > len)
  277.  268   1                      ControlData.wLength = len;
  278.  269   1      
  279.  270   1              ControlData.pData = pRomData;
  280.  271   1              if( ControlData.wLength >= EP0_PACKET_SIZE) {
  281.  272   2                      D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);//发送16字节数据
  282.  273   2                      ControlData.wCount += EP0_PACKET_SIZE;
  283.  274   2                      DISABLE;
  284.  275   2                      bEPPflags.bits.control_state = USB_TRANSMIT;
  285.  276   2                      ENABLE;
  286.  277   2              }
  287.  278   1              else {
  288.  279   2                      D12_WriteEndpoint(1, ControlData.wLength, pRomData);//发送16字节内数据
  289.  280   2                      ControlData.wCount += ControlData.wLength;
  290.  281   2                      DISABLE;
  291.  282   2                      bEPPflags.bits.control_state = USB_IDLE;
  292.  283   2                      ENABLE;
  293.  284   2              }
  294.  285   1      }
  295.  286          
  296.  287          //LED和按键处理子程序
  297.  288          void check_key_LED(void)
  298.  289          {
  299.  290   1              static unsigned char c ;
  300.  291   1              c =KEYDATA;
  301.  292   1              D12_WriteEndpoint(3,1,&c);         //输入键盘数据
  302.  293   1      
  303.  294   1      
  304.  295   1      }
  305.  296          //数码管显示程序,左buf1,右buf2
  306.  297          void send(uchar buf1,uchar buf2)
  307.  298          {
  308.  299   1       uchar i;
  309.  300   1       uchar temp=buf2;
  310.  301   1       for(i=0;i!=8;i++)
  311.  302   1       { 
  312.  303   2       if(temp&0x01)SPIDATA=1;
  313. C51 COMPILER V7.02b   MAIN                                                                 09/28/2006 22:04:34 PAGE 6   
  314.  304   2       else   SPIDATA=0;
  315.  305   2       SPICLK=0;SPICLK=1;
  316.  306   2       temp>>=1;
  317.  307   2       }
  318.  308   1        temp=buf1;
  319.  309   1       for(i=0;i!=8;i++)
  320.  310   1       { 
  321.  311   2       if(temp&0x01)SPIDATA=1;
  322.  312   2       else   SPIDATA=0;
  323.  313   2       SPICLK=0;SPICLK=1;
  324.  314   2       temp>>=1;
  325.  315   2       }
  326.  316   1       SPILatch=0;
  327.  317   1       SPILatch=1;
  328.  318   1      }
  329.  319          
  330.  320          void EndpointII()//2*16LCD/IIC
  331.  321          {
  332.  322   1      
  333.  323   1      // if(EpBuf[0]==1)  PrintUsb(EpBuf+2,EpBuf[1]); //LCD
  334.  324   1      // if(EpBuf[0]==2)  Write24c02_nByte(EpBuf[2],EpBuf[1],EpBuf+3);//IIC
  335.  325   1       EpBuf[0]=0;
  336.  326   1      }
  337.  327          
  338.  328          //请求处理子程序        处理Setup包
  339.  329          void control_handler()
  340.  330          {
  341.  331   1              unsigned char type, req;
  342.  332   1              type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
  343.  333   1              req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
  344.  334   1              if (type == USB_STANDARD_REQUEST)
  345.  335   1                      (*StandardDeviceRequest[req])();//调用标准请求
  346.  336   1              else if (type == USB_VENDOR_REQUEST)
  347.  337   1                      (*VendorDeviceRequest[req])();//调用厂商请求
  348.  338   1              else
  349.  339   1                      stall_ep0();
  350.  340   1      }
  351. MODULE INFORMATION:   STATIC OVERLAYABLE
  352.    CODE SIZE        =    630    ----
  353.    CONSTANT SIZE    =    112    ----
  354.    XDATA SIZE       =   ----    ----
  355.    PDATA SIZE       =   ----    ----
  356.    DATA SIZE        =     25       8
  357.    IDATA SIZE       =   ----    ----
  358.    BIT SIZE         =   ----    ----
  359. END OF MODULE INFORMATION.
  360. C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)