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

📄 periph.c

📁 在KEILC下进行编译的程序,单片机可以进行AD,DA数据采集
💻 C
字号:
#pragma NOIV               // Do not generate interrupt vectors
//-----------------------------------------------------------------------------
//   File:      periph.c
//   Contents:   Hooks required to implement USB peripheral function.
//
//   Copyright (c) 1997 AnchorChips, Inc. All rights reserved
//-----------------------------------------------------------------------------
#include "fx2.h"
#include "fx2regs.h"
#include "io.h"
#include "key.h"
#include "led.h"
//#include "serial.h"
#include "eeprom.h"
#include "fx2sdly.h"     
extern BOOL   GotSUD;         // Received setup data flag
extern BOOL   Sleep;
extern BOOL   Rwuen;
extern BOOL   Selfpwr;

BYTE   Configuration;      // Current configuration
BYTE   AlternateSetting;   // Alternate settings
	char AD_X1;
//-----------------------------------------------------------------------------
// Task Dispatcher hooks
//   The following hooks are called by the task dispatcher.任务调度
//-----------------------------------------------------------------------------

void TD_Init(void)             // Called once at startup
{
	REVCTL = 0x03; // MUST set REVCTL.0 and REVCTL.1 to 1
    SYNCDELAY;
    BREAKPT &= ~bmBPEN;      // to see BKPT LED go out TGE
    Rwuen = TRUE;            // Enable remote-wakeup*/
}

void TD_Poll(void)             // Called repeatedly while the device is idle
{
}

BOOL TD_Suspend(void)          // Called before the device goes into suspend mode
{
   return(TRUE);
}

BOOL TD_Resume(void)          // Called after the device resumes
{
   return(TRUE);
}

//-----------------------------------------------------------------------------
// Device Request hooks设备请求钩子程序
//   The following hooks are called by the end point 0 device request parser.
//-----------------------------------------------------------------------------
//下面的钩子程序被端点0设备请求器调用
BOOL DR_GetDescriptor(void)
{
   return(TRUE);
}

BOOL DR_SetConfiguration(void)   // Called when a Set Configuration command is received
{
   Configuration = SETUPDAT[2];
   return(TRUE);            // Handled by user code
}

BOOL DR_GetConfiguration(void)   // Called when a Get Configuration command is received
{
   EP0BUF[0] = Configuration;//34页,端口0
   EP0BCH = 0;               //32页,端口0
   EP0BCL = 1;
   return(TRUE);            // Handled by user code
}

BOOL DR_SetInterface(void)       // Called when a Set Interface command is received
{
   AlternateSetting = SETUPDAT[2];//收到端口命令时
   return(TRUE);            // Handled by user code
}

BOOL DR_GetInterface(void)       // Called when a Set Interface command is received
{
   EP0BUF[0] = AlternateSetting;
   EP0BCH = 0;
   EP0BCL = 1;
   return(TRUE);            // Handled by user code
}

BOOL DR_GetStatus(void)
{
   return(TRUE);
}

BOOL DR_ClearFeature(void)
{
   return(TRUE);
}

BOOL DR_SetFeature(void)
{
   return(TRUE);
}


#define	VR_UPLOAD		0xc0     //1100
#define VR_DOWNLOAD		0x40     //0100

#define ZSUSB20DDB_VENDOR_REQUEST_LED_DISP                0xB0   //led显示
#define ZSUSB20DDB_VENDOR_REQUEST_KEY_READ                0xB1   //键盘读取
#define ZSUSB20DDB_VENDOR_REQUEST_EEPROM_ACCESS           0xB2   //EEPROM访问
#define ZSUSB20DDB_VENDOR_REQUEST_SRAM_ACCESS             0xB3   //SRAM访问
//#define ZSUSB20DDB_VENDOR_REQUEST_SERIAL_ACCESS           0xB4   //串口访问

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

#define ZSUSB20DDB_VENDOR_REQUEST_PA_ON_OFF               0xB6   //PA的亮/灭控制 
#define ZSUSB20DDB_VENDOR_REQUEST_AD                      0xB7   //AD 
///////////////////////////////////////////////////////////////////////////

BOOL DR_VendorCmnd(void)
{
  	WORD		addr, len, bc;                         //这几个数都是WORD型变量
	WORD i;
//	BYTE flag;
//	BYTE xdata *point;


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


  //  int j;

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

  	len = SETUPDAT[6];             //33页,是WLENGTH
	len |= (SETUPDAT[7] << 8);     //是长度的意思?左移动8位,然后或,是什么意思?
	switch(SETUPDAT[1])            //这个数据bmRequest,是否就是说把B0到B6等地址与应用软件通信?
	{ 
		case ZSUSB20DDB_VENDOR_REQUEST_LED_DISP:    //led显示
		{
			DispBuf[SETUPDAT[3]&3] = SETUPDAT[2];   //wValueH为第几位了led,wValueL为显示值


//////////////////////////////
/*	PORTACFG=0x00;
	OEA=0xFF;
	
	j++;
	if (j%2==0)
	IOA=0x55;
	if (j%2==1)
	IOA=0xAA;
	if (j>100)
	j=0;*/

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

			break;
		}
//------------------------------------------------------//

	case ZSUSB20DDB_VENDOR_REQUEST_PA_ON_OFF:    //-------------------
		{
	
/*		PORTACFG=0x00;
		OEA=0xFF;
		IOA = SETUPDAT[2]; 

*/
		//初始化A,B,C,D端口。
/*	
		j++;
		if (j%2==0)
		IOA=0x55;
		if (j%2==1)
		IOA=0xAA;
		if (j>100)
		j=0;
*/

/*		IFCONFIG=0x00;

		OED=0xFF;
		IOD = SETUPDAT[2]; 
*/


//		PORTCCFG=0x00;
//		OEC=0xFF;
//		IOC = SETUPDAT[2]; 

			
		break;
		}
//有可能是通过SETUPDAT[2]或者	EP0BUF[0]这样的数组来进行数据传递的。
case ZSUSB20DDB_VENDOR_REQUEST_AD:    //-------------------
		{
		char  ad;

		ad= AD_X;
//		ad=IOC;
		//最大 值好像只能是127,如果是128,变成-128,256时为0;
//		EP0BUF[0] = ad;             //把AD的值给	EP0BUF[0]
	
		EP0BUF[0] = ad;   
		EP0BCH = 0;
		EP0BCL = 1;               


//		SETUPDAT[2]=0x08;
			
		break;
		}
//------------------------------------------------------//






		case ZSUSB20DDB_VENDOR_REQUEST_KEY_READ:    //键盘读取
		{
			if(OKey_Value != 0xFF)                  //如果有键盘被按下去了
			{
				EP0BUF[0] = OKey_Value;             //把按件的值给	EP0BUF[0]
				EP0BCH = 0;
				EP0BCL = 1;                          //EP0BCL = 1的意思是有数据的长度
				OKey_Value = 0xFF;
			}
			else
			{
				EP0BCH = 0;
				EP0BCL = 0;
			}
			break;
		}
		case ZSUSB20DDB_VENDOR_REQUEST_EEPROM_ACCESS:
		case ZSUSB20DDB_VENDOR_REQUEST_SRAM_ACCESS:
		{
			addr = SETUPDAT[2];		// Get address and length得到地址和长度
			addr |= SETUPDAT[3] << 8;
			// Is this an upload command ?
			if(SETUPDAT[0] == VR_UPLOAD)//信号从单片机到计算机
			{
				while(len)					// Move requested data through EP0IN 

				//把请求的数据通过EP0IN传给计算机
				{							// one packet at a time.一次一包数据。
					if(len < 64)
						bc = len;           //把LEN的值传给BC
					else
						bc = 64;
					// Is this a RAM upload ?
					if(SETUPDAT[1] == ZSUSB20DDB_VENDOR_REQUEST_SRAM_ACCESS)//如果是SRAM通道
					{
						for(i=0; i<bc; i++)
							*(EP0BUF+i) = *((BYTE xdata *)addr+i);//意思 是把SRAM里的数据通过EP0BUF传给计算机
					}
					else
					{
						EEPROMRead(addr,bc,(WORD)EP0BUF);//否则调用EEPROMRead函数。把EEPROM里的数据给计算机
					}
					EP0BCH = 0;
					EP0BCL = (BYTE)bc; // Arm endpoint,表示要传输的数据长度。
					addr += bc;   //每次发送64个字,地址增加
					len -= bc;    //每次发送64个字,剩余的数据减小。

					while(EP0CS & bmEPBUSY);       //如果数据没有发送完,就等待。
				}
			}
			// Is this a download command ?
			else if(SETUPDAT[0] == VR_DOWNLOAD)    //信号从计算机到单片机
			{
				while(len)					// Move new data through EP0OUT 
				{							// one packet at a time.
					// Arm endpoint - do it here to clear (after sud avail)
					EP0BCH = 0;
					EP0BCL = 0; // Clear bytecount to allow new data in; also stops NAKing
					//使OUT端点有效,表示主机可以发送数据了

					while(EP0CS & bmEPBUSY);
					//等待主机发送数据

					bc = EP0BCL; // Get the new bytecount//数据的长度保存在EP0BCL中。
					// Is this a RAM download ?
					if(SETUPDAT[1] == ZSUSB20DDB_VENDOR_REQUEST_SRAM_ACCESS)
					{
						for(i=0; i<bc; i++)
							 *((BYTE xdata *)addr+i) = *(EP0BUF+i);
					}
					else
						EEPROMWrite(addr,bc,(WORD)EP0BUF);
					addr += bc;
					len -= bc;
				}
			}
			break;
		}



		default:return(TRUE);
	}
	return(FALSE);




}

//-----------------------------------------------------------------------------
// USB Interrupt Handlers
//   The following functions are called by the USB interrupt jump table.
//-----------------------------------------------------------------------------

// Setup Data Available Interrupt Handler
void ISR_Sudav(void) interrupt 0
{
   GotSUD = TRUE;            // Set flag
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmSUDAV;         // Clear SUDAV IRQ
}

// Setup Token Interrupt Handler
void ISR_Sutok(void) interrupt 0
{
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmSUTOK;         // Clear SUTOK IRQ
}

void ISR_Sof(void) interrupt 0
{
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmSOF;            // Clear SOF IRQ
}

void ISR_Ures(void) interrupt 0
{
   // whenever we get a USB reset, we should revert to full speed mode
   pConfigDscr = pFullSpeedConfigDscr;
   ((CONFIGDSCR xdata *) pConfigDscr)->type = CONFIG_DSCR;
   pOtherConfigDscr = pHighSpeedConfigDscr;
   ((CONFIGDSCR xdata *) pOtherConfigDscr)->type = OTHERSPEED_DSCR;
   
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmURES;         // Clear URES IRQ
}

void ISR_Susp(void) interrupt 0
{
   Sleep = TRUE;
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmSUSP;
}

void ISR_Highspeed(void) interrupt 0
{
   if (EZUSB_HIGHSPEED())
   {
      pConfigDscr = pHighSpeedConfigDscr;
      ((CONFIGDSCR xdata *) pConfigDscr)->type = CONFIG_DSCR;
      pOtherConfigDscr = pFullSpeedConfigDscr;
      ((CONFIGDSCR xdata *) pOtherConfigDscr)->type = OTHERSPEED_DSCR;
   }

   EZUSB_IRQ_CLEAR();
   USBIRQ = bmHSGRANT;
}
void ISR_Ep0ack(void) interrupt 0
{
}
void ISR_Stub(void) interrupt 0
{
}
void ISR_Ep0in(void) interrupt 0
{
}
void ISR_Ep0out(void) interrupt 0
{
}
void ISR_Ep1in(void) interrupt 0
{
}
void ISR_Ep1out(void) interrupt 0
{
}
void ISR_Ep2inout(void) interrupt 0
{
}
void ISR_Ep4inout(void) interrupt 0
{
}
void ISR_Ep6inout(void) interrupt 0
{
}
void ISR_Ep8inout(void) interrupt 0
{
}
void ISR_Ibn(void) interrupt 0
{
}
void ISR_Ep0pingnak(void) interrupt 0
{
}
void ISR_Ep1pingnak(void) interrupt 0
{
}
void ISR_Ep2pingnak(void) interrupt 0
{
}
void ISR_Ep4pingnak(void) interrupt 0
{
}
void ISR_Ep6pingnak(void) interrupt 0
{
}
void ISR_Ep8pingnak(void) interrupt 0
{
}
void ISR_Errorlimit(void) interrupt 0
{
}
void ISR_Ep2piderror(void) interrupt 0
{
}
void ISR_Ep4piderror(void) interrupt 0
{
}
void ISR_Ep6piderror(void) interrupt 0
{
}
void ISR_Ep8piderror(void) interrupt 0
{
}
void ISR_Ep2pflag(void) interrupt 0
{
}
void ISR_Ep4pflag(void) interrupt 0
{
}
void ISR_Ep6pflag(void) interrupt 0
{
}
void ISR_Ep8pflag(void) interrupt 0
{
}
void ISR_Ep2eflag(void) interrupt 0
{
}
void ISR_Ep4eflag(void) interrupt 0
{
}
void ISR_Ep6eflag(void) interrupt 0
{
}
void ISR_Ep8eflag(void) interrupt 0
{
}
void ISR_Ep2fflag(void) interrupt 0
{
}
void ISR_Ep4fflag(void) interrupt 0
{
}
void ISR_Ep6fflag(void) interrupt 0
{
}
void ISR_Ep8fflag(void) interrupt 0
{
}
void ISR_GpifComplete(void) interrupt 0
{
}
void ISR_GpifWaveform(void) interrupt 0
{
}

⌨️ 快捷键说明

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