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

📄 fw.c

📁 在KEILC下进行编译的程序,单片机可以进行AD,DA数据采集
💻 C
📖 第 1 页 / 共 2 页
字号:
   // disconnected and need to connect.  If we just renumerated this
   // is not necessary but doesn't hurt anything
   USBCS &=~bmDISCON;
//////////////////////////////////////////
 CPUCS |=0x02;//保证时钟输出48M
CPUCS &=0xE7;//时钟分频12M
//CPUCS &=0xEF;//时钟分频12M
//CPUCS &=0xF7;//时钟分频48M
// CPUCS |=0x1A;//时钟分频48M




///////////////////////////////////////////	

   CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch to 0 (after renumeration)

   // clear the Sleep flag.
   Sleep = FALSE;
   memset(DispBuf,16,4);//无显示数据
   // Task Dispatcher
   while(TRUE)               // Main Loop
   {
//////////////////////////////////////////////////////////
	//OEB=0x00;

/*	OEA=0xFF;
	OEC=0xFF;
	PORTACFG=0x00;
	IOA=0xFF;
	IOC=0xFF;
*/
///////////////////////////////////////////////////////////	


      if(GotSUD)            // Wait for SUDAV
      {
         SetupCommand();          // Implement setup command
           GotSUD = FALSE;            // Clear SUDAV flag
      }
      //如果GotSUD为一表示SETUP数据到了,程序调用SetupCommand()。
      // Poll User Device
      // NOTE: Idle mode stops the processor clock.  There are only two
      // ways out of idle mode, the WAKEUP pin, and detection of the USB
      // resume state on the USB bus.  The timers will stop and the
      // processor will not wake up on any other interrupts.
      if (Sleep)
          {
          if(TD_Suspend())
              { 
              Sleep = FALSE;            // Clear the "go to sleep" flag.  Do it here to prevent any race condition between wakeup and the next sleep.
              do
                  {
                    EZUSB_Susp();         // Place processor in idle mode.
                  }
                while(!Rwuen && EZUSB_EXTWAKEUP());
                // Must continue to go back into suspend if the host has disabled remote wakeup
                // *and* the wakeup was caused by the external wakeup pin.
                
             // 8051 activity will resume here due to USB bus or Wakeup# pin activity.
             EZUSB_Resume();   // If source is the Wakeup# pin, signal the host to Resume.      
             TD_Resume();
              }   
          }
      TD_Poll();//设备停滞时调用
	  Led_Display();//LED的显示不停调用
	  temp_data = OKey_RP_Scan();//输出按钮的值
	  //----------------------------------------
	  ad0809();
//*************************************************************************//

//	   AD0809= ad0809();


//*************************************************************************//
	  //----------------------------------------
	  if(temp_data != 0xFF)
	  {
	  	OKey_Value = temp_data;
	  }
   }
}

// Device request parser
//实现控制传输的数据和状态阶段
void SetupCommand(void)
{
   void   *dscr_ptr;

   switch(SETUPDAT[1])      //判断,既是第一个字段,表示不同的请求!
   {
      case SC_GET_DESCRIPTOR:                  // *** Get Descriptor取描述符号//-------06
         if(DR_GetDescriptor())
            switch(SETUPDAT[3])                //看wValuel的值是多少        
            {
               case GD_DEVICE:            // Device如果是设备描述符号?01
                  SUDPTRH = MSB(pDeviceDscr);//SUDPTRH是什么意思?
                  SUDPTRL = LSB(pDeviceDscr);
                  break;
               case GD_DEVICE_QUALIFIER:            // Device Qualifier设备限定06
                  SUDPTRH = MSB(pDeviceQualDscr);
                  SUDPTRL = LSB(pDeviceQualDscr);
                  break;
               case GD_CONFIGURATION:         // Configuration配置02
                  SUDPTRH = MSB(pConfigDscr);
                  SUDPTRL = LSB(pConfigDscr);
                  break;
               case GD_OTHER_SPEED_CONFIGURATION:  // Other Speed Configuration其他速度配置07
                  SUDPTRH = MSB(pOtherConfigDscr);
                  SUDPTRL = LSB(pOtherConfigDscr);
                  break;
               case GD_STRING:            // String字符串03
                  if(dscr_ptr = (void *)EZUSB_GetStringDscr(SETUPDAT[2]))
                  {
                     SUDPTRH = MSB(dscr_ptr);
                     SUDPTRL = LSB(dscr_ptr);
                  }
                  else 
                     EZUSB_STALL_EP0();   // Stall End Point 0
                  break;
               default:            // Invalid request
                  EZUSB_STALL_EP0();      // Stall End Point 0
            }
         break;
	 //-------------------------------------------------以上程序获取描述符号
      case SC_GET_INTERFACE:                  // *** Get Interface-----0A
         DR_GetInterface();
         break;
	 //-------------------------------------------------获取接口---------
      case SC_SET_INTERFACE:                  // *** Set Interface
         DR_SetInterface();
         break;
	  //-------------------------------------------------设置接口---------
      case SC_SET_CONFIGURATION:               // *** Set Configuration
         DR_SetConfiguration();
         break;
	  //-------------------------------------------------------------------
      case SC_GET_CONFIGURATION:               // *** Get Configuration--08
         DR_GetConfiguration();
         break;
	  //-------------------------------------------------------------------
      case SC_GET_STATUS:                  // *** Get Status获取状态
         if(DR_GetStatus())
            switch(SETUPDAT[0])            //第一位
            {
               case GS_DEVICE:            // Device----80
                  EP0BUF[0] = ((BYTE)Rwuen << 1) | (BYTE)Selfpwr;
                  EP0BUF[1] = 0;
                  EP0BCH = 0;
                  EP0BCL = 2;
                  break;
               case GS_INTERFACE:         // Interface----81
                  EP0BUF[0] = 0;
                  EP0BUF[1] = 0;
                  EP0BCH = 0;
                  EP0BCL = 2;
                  break;
               case GS_ENDPOINT:         // End Point-----82
                  EP0BUF[0] = *(BYTE xdata *) epcs(SETUPDAT[4]) & bmEPSTALL;
                  EP0BUF[1] = 0;
                  EP0BCH = 0;
                  EP0BCL = 2;
                  break;
               default:            // Invalid Command
                  EZUSB_STALL_EP0();      // Stall End Point 0
            }
         break;
	  //--------------------------------------------------------------------------
      case SC_CLEAR_FEATURE:                  // *** Clear Feature清除特征
         if(DR_ClearFeature())
            switch(SETUPDAT[0])               //
            {
               case FT_DEVICE:            // Device-----清除设备特征
                  if(SETUPDAT[2] == 1)
                     Rwuen = FALSE;       // Disable Remote Wakeup
                  else
                     EZUSB_STALL_EP0();   // Stall End Point 0
                  break;
               case FT_ENDPOINT:         // End Point-----02清除端点特征
                  if(SETUPDAT[2] == 0)
                  {
                     *(BYTE xdata *) epcs(SETUPDAT[4]) &= ~bmEPSTALL;
                     EZUSB_RESET_DATA_TOGGLE( SETUPDAT[4] );
                  }
                  else
                     EZUSB_STALL_EP0();   // Stall End Point 0
                  break;
            }
         break;
	  //---------------------------------------------------------------------------
      case SC_SET_FEATURE:                  // *** Set Feature-----03
         if(DR_SetFeature())
            switch(SETUPDAT[0])
            {
               case FT_DEVICE:            // Device--00
                  if(SETUPDAT[2] == 1)
                     Rwuen = TRUE;      // Enable Remote Wakeup
                  else if(SETUPDAT[2] == 2)
                     // Set Feature Test Mode.  The core handles this request.  However, it is
                     // necessary for the firmware to complete the handshake phase of the
                     // control transfer before the chip will enter test mode.  It is also
                     // necessary for FX2 to be physically disconnected (D+ and D-)
                     // from the host before it will enter test mode.
                     break;
                  else
                     EZUSB_STALL_EP0();   // Stall End Point 0
                  break;
               case FT_ENDPOINT:         // End Point---02
                  *(BYTE xdata *) epcs(SETUPDAT[4]) |= bmEPSTALL;
                  break;
            }
         break;
      default:                     // *** Invalid Command
         if(DR_VendorCmnd())
            EZUSB_STALL_EP0();            // Stall End Point 0
   }

   // Acknowledge handshake phase of device request
   EP0CS |= bmHSNAK;

}

// Wake-up interrupt handler
void resume_isr(void) interrupt WKUP_VECT
{
   EZUSB_CLEAR_RSMIRQ();
}


⌨️ 快捷键说明

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