⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.lst

📁 MP3播放器和U盘功能的源代码。采用集成了MP3解码芯片和USB接口芯片的AT89C51SND1C单片机
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V7.50   USB                                                                   02/08/2007 11:18:10 PAGE 1   


C51 COMPILER V7.50, COMPILATION OF MODULE USB
OBJECT MODULE PLACED IN USB.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE USB.C LARGE BROWSE DEBUG OBJECTEXTEND

line level    source

   1          //
   2          #include <stdio.h>
   3          #include <stdlib.h>
   4          #include <string.h>
   5          #include "REGSND1.H"
   6          #include "USBDEF.H"
   7          #include "UART.h"
   8          
   9          //时钟参数宏
  10          #define         Rdiv            11
  11          //USB寄存器设置相关的宏定义
  12          #define         PLLRES          (0x1<<3)        //PLLCON
  13          #define         PLLEN           (0x1<<1)
  14          #define         USBE            (0x1<<7)        //USBCON
  15          #define         EEOFINT         (0x1<<4)
  16          #define         FEN                     (0x1<<7)
  17          #define         FADDEN          (0x1)
  18          #define         CONFG           (0x1<<1)
  19          #define         DIR                     (0x1<<7)                //UEPSTAX
  20          #define         RXOUTB1         (0x1<<6)
  21          #define         STALLRQ         (0x1<<5)
  22          #define         TXRDY           (0x1<<4)
  23          #define         STLCRC          (0x1<<3)
  24          #define         RXSETUP         (0x1<<2)
  25          #define         RXOUTB0         (0x1<<1)
  26          #define         TXCMP           (0x1<<0)
  27          
  28          //USB 请求的bRequert的含意宏
  29          #define get_status                      0x00            //取得状态
  30          #define clear_feature           0x01            //清除特性
  31          #define reserved                        0x02            //保留
  32          #define set_feature                     0x03            //设置特性
  33          
  34          #define set_address                     0x05            //设置地址
  35          #define get_descriptor          0x06            //取得描述符
  36          
  37          #define get_configuration       0x08            //取得配置
  38          #define set_configuration       0x09            //设置配置
  39          #define get_interface           0x0a            //取得接口
  40          #define set_interface           0x0b            //设置接口
  41          
  42          /**---------------------------------------------------------------------------------
  43          
  44          ----------------------------------------------------------------------------------**/
  45          
  46          void AtmelUSBInit()             //用于USB的PLL时钟20M晶振时如下设置
  47          {
  48   1              int data i;     
  49   1              PLLNDIV =       0x04;                     //设置PLL时钟
  50   1      /*
  51   1              PLLCON |=       (0x3&Rdiv)<<6;
  52   1              PLLRDIV =       (0x3ff&Rdiv)>>2;
  53   1              USBCLK=0;
  54   1              PLLCON&=(~PLLRES);                    //使能PLL
  55   1              PLLCON|=PLLEN;                                     */  
C51 COMPILER V7.50   USB                                                                   02/08/2007 11:18:10 PAGE 2   

  56   1      
  57   1          PLLCON |=   (0x3&Rdiv)<<6;
  58   1              PLLRDIV =       (0x3ff&Rdiv)>>2;
  59   1              USBCLK=0;
  60   1              PLLCON&=(~PLLRES);                    //使能PLL
  61   1              PLLCON|=PLLEN;                              
  62   1      
  63   1        
  64   1              USBCON&=(~USBE);                      //关闭USB并复位USB
  65   1              for(i=0;i<3000;i++);                  //等待PLL工作稳定
  66   1              USBCON|=USBE;                         //开启USB控制器
  67   1      
  68   1      }       
  69          
  70          void EpEnable(void)                                       //初始化USB端点
  71          {
  72   1              UEPNUM=0x00;    UEPCONX=0x80;
  73   1              UEPNUM=0x01;    UEPCONX=0x86;
  74   1              UEPNUM=0x02;    UEPCONX=0x82;
  75   1              UEPRST=0x07;    UEPRST= 0x00;
  76   1              UEPIEN=0x07;    USBIEN|=EEOFINT;
  77   1              USBADDR=FEN;
  78   1      }
  79          
  80          unsigned char ReadEp(unsigned char EpNum,unsigned char *Data)   //读取端口数据
  81          {
  82   1              unsigned char data i=0;
  83   1              UEPNUM=EpNum;                                     //指端口号
  84   1              while(i<UBYCTLX)                          //读数据到缓冲//UBYCTX得到数据长度
  85   1              {
  86   2                      Data[i++]=UEPDATX;
  87   2              }       
  88   1              UEPSTAX&=~(RXOUTB0|RXOUTB1|RXSETUP);                                        //清除相关位
  89   1              return(i);
  90   1      }
  91          
  92          void WriteEp(unsigned char EpNum,unsigned char nLength,unsigned char *Data)     //写端口数据
  93          {
  94   1              unsigned char data i=0;
  95   1              UEPNUM=EpNum;                                                                                        //选择端口
  96   1              UEPSTAX|=DIR;                                                                                        //控制方向
  97   1              while(nLength--) UEPDATX=Data[i++];                              //写入FIFO
  98   1              UEPSTAX|=TXRDY;                                                                                      //开始发送
  99   1              while(!(UEPSTAX&TXCMP)) ;
 100   1              UEPSTAX&=(~(TXCMP));                                                                     //清除TXCMP
 101   1      }
 102          
 103          //-------------以下用于对端点进行处理
 104          
 105          void Set_Address(unsigned char EpNum)                            //设置USB地址1-127
 106          {
 107   1              WriteEp(0,0,0);                                                                                      //在Status阶段过后才能改变设备地址
 108   1              USBADDR|=EpNum;
 109   1              USBADDR|=FEN;                                                                                        //设置地址
 110   1              USBCON|=FADDEN;                                                                                      //地址使能
 111   1      }
 112          
 113          //设备描述符
 114          code unsigned char Device_Descriptor[18] = {
 115                                                                                   0x12,                     //0x12
 116                                                                                           0x01,             //DEVICE descriptor
 117                                                                                           0x10, 0x01,       //spec rev level (BCD) 1.0
C51 COMPILER V7.50   USB                                                                   02/08/2007 11:18:10 PAGE 3   

 118                                                                                           0x0,              //device class
 119                                                                                           0x0,              //device subclass
 120                                                                                           0x0,              //device protocol
 121                                                                                           0x20,             //max packet size
 122                                                                                           0x05, 0x82,       //National's vendor ID
 123                                                                                           0x00, 0x11,       //National's product ID  
 124                                                                                           0x00, 0x00,       //National's revision ID  
 125                                                                                           0,                //index of manuf. string   
 126                                                                                           0,                //index of prod.  string  
 127                                                                                           0,                //index of ser. # string   
 128                                                                                           0x01              //number of configs.         
 129                                                                                  };
 130          //配置描述符
 131          code unsigned char Configuration_Descriptor_All[32] = {
 132          
 133                                                                                      9,                 //Size of Descriptor in Bytes
 134                                                                                      2,                 //Configuration Descriptor (0x02)
 135                                                                                      0x20,              //Total length in bytes of data returned  LSB
 136                                                                                          0x00,              //MSB
 137                                                                                          1,                 //Number of Interfaces
 138                                                                                          1,                 //Value to use as an argument to select this configuration
 139                                                                                      0,                 //Index of String Descriptor describing this configuration
 140                                                                                          0x80,
 141                                                                                          0xfa,              //Maximum Power Consumption in 2mA units 
 142                                                                                  
 143                                                                                      9,
 144                                                                                      4,
 145                                                                                      0,                 //the index of the interface descriptor Number of Interface
 146                                                                                      0,                 //Value used to select alternative setting
 147                                                                                          2,                 //EndPoint Number Used in this Descriptor
 148                                                                                          8,                 //Class Code (Assigned by USB Org)
 149                                                                                          6,                     //interface subclass1=RBC,2=SFF,3=QIC,4=UFI,5=SFF,6=SCSI
 150                                                                                          0x50,              //bulk 0nly Transport
 151                                                                                          0,                 //Index of String Descriptor Describing this interface
 152          
 153                                                                                                             //Bulk-in Endpoint
 154                                                                                           0x07,             //length of this desc.   
 155                                                                                           0x05,             //ENDPOINT descriptor TYPE
 156                                                                                           0x81,             //address (IN) Endpoint 4 84
 157                                                                                           0x02,             //attributes  (BULK)    
 158                                                                                           0x40, 0x00,       //max packet size (64)  
 159                                                                                           0x0,              //Does not apply to Bulk endpoints
 160                                                                  
 161                                                                                                             //Bulk-out Endpoint
 162                                                                                           0x07,             //length of this desc.   
 163                                                                                           0x05,             //ENDPOINT descriptor TYPE
 164                                                                                           0x02,             //address (OUT) Endpoint 5 05
 165                                                                                           0x02,             //attributes  (BULK)
 166                                                                                           0x40, 0x00,       //max packet size (64)
 167                                                                                           0x0               //Does not apply to Bulk endpoints

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -