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

📄 spi.cpp

📁 NI公司的PCI-6120四通道电压测量卡控制程序。该程序主要用VC实现对PCI-6120测量卡控制
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_RESET);		//RESET= 0;
}

void B_SET_RES()
{
	BYTE tmp= 0x00;
	//tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	SET_PORT_BIT(tmp, B_RESET);		//RESET= 1;
}

void B_SPI_Read(BYTE* data)
{	
	BYTE tmp= 0x00, ret= 0x00;
	
	//CS
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 0;
	wait_ns(20);				//WAIT 20ns	
	//CLK
	SET_PORT_BIT(tmp, B_SPI_CLK);		//SPI_CLK= 1;

	//RECEIVE FROM MISO
	for(BYTE i=0; i<8; i++)
	{
		CLR_PORT_BIT(tmp, B_SPI_CLK);		//SPI_CLK= 0;
		ret	= (BYTE)Inp32(PORT_ADDR);	
		if((ret&B_SPI_MISO)!=0x00)	SET_DATA_BIT(*data, byteBIT[7-i])	//first MBS, end LBS
		else	CLR_DATA_BIT(*data, byteBIT[7-i])
		wait_ns(15);				//WAIT 15ns
		SET_PORT_BIT(ret, B_SPI_CLK);	//SPI_CLK= 1;
	}
}

void B_SPI_Write(BYTE data)
{
	//CString msg; msg.Format("0x%x", data);	AfxMessageBox(msg);
	BYTE tmp= 0x00, ret= 0x00;
	
	//CS
//	CLR_PORT_BIT(tmp, SPI_CE);		//SPI_CE= 0;
//	wait_ns(20);				//WAIT 20ns	

	//CLK
	//tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CLK);			//SPI_CLK= 0;

	//WRITE MOSI
	for(BYTE i=0; i<8; i++)
	{
		//ret	= DlPortReadPortUchar(PORT_ADDR);	
		if((data&(byteBIT[7-i]))!=0x00)		SET_PORT_BIT(tmp, B_SPI_MOSI)
		else	CLR_PORT_BIT(tmp, B_SPI_MOSI)

		SET_PORT_BIT(tmp, B_SPI_CLK);	//SPI_CLK= 1;

		wait_ns(15);				//WAIT 15ns
		CLR_PORT_BIT(tmp, B_SPI_CLK);	//SPI_CLK= 0;
	}
	
}

void B_SPI_ReadWrite(BYTE writeByte, BYTE* readByte)
{
	BYTE tmp= 0x00, ret= 0x00;

	//CS
	//tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 0;
	wait_ns(20);				//WAIT 20ns	
	//CLK
	CLR_PORT_BIT(tmp, B_SPI_CLK);			//SPI_CLK= 0;
	//WRITE MOSI
	for(BYTE i=0; i<8; i++)
	{
		SET_PORT_BIT(tmp, B_SPI_CLK);	//SPI_CLK= 1;
		//ret	= DlPortReadPortUchar(PORT_ADDR);	
		//WRITE
		if((writeByte&(byteBIT[7-i]))!=0x00)	SET_PORT_BIT(tmp, B_SPI_MOSI);
		wait_ns(15);				//WAIT 15ns
		CLR_PORT_BIT(tmp, B_SPI_CLK);	//SPI_CLK= 0;
		//READ
		ret	= (BYTE)Inp32(PORT_ADDR);
		if((ret&B_SPI_MISO)!=0x00)				SET_DATA_BIT(*readByte, byteBIT[7-i]);	//first MBS, end LBS
		wait_ns(15);				//WAIT 15ns
	}
}

int B_SPI_Write32(DWORD writeData)
{
	//CString msg; msg.Format("0x%x", writeData);	AfxMessageBox(msg);
	BYTE temp_byte[4]={0};

	temp_byte[0] = (BYTE)(writeData & 0x000000ff);
	temp_byte[1] = (BYTE)((writeData >> 8) & 0x000000ff);
	temp_byte[2] = (BYTE)((writeData >> 16) & 0x000000ff);
	temp_byte[3] = (BYTE)((writeData >> 24) & 0x000000ff);
	
	BYTE tmp= 0x00, ret= 0x00;

	//CS
	//tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 0;
	wait_ns(20);

	//write data 32bits
	for(int i= 3; i>=0; i--)
	{
		//CString msg; msg.Format("0x%x", temp_byte[i]);	AfxMessageBox(msg);
		B_SPI_Write(temp_byte[i]);
	}

	wait_ns(20);
	//CS
	//tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	SET_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 1;

	return 0;
}

int B_SPI_Write32_New(DWORD writeData)
{
	//CString msg; msg.Format("0x%x", writeData);	AfxMessageBox(msg);
	BYTE temp_byte[4]={0};

	temp_byte[0] = (BYTE)(writeData & 0x000000ff);
	temp_byte[1] = (BYTE)((writeData >> 8) & 0x000000ff);
	temp_byte[2] = (BYTE)((writeData >> 16) & 0x000000ff);
	temp_byte[3] = (BYTE)((writeData >> 24) & 0x000000ff);
	
	BYTE tmp= 0x00, ret= 0x00;

	//CS
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 0;
	wait_ns(20);

	//write data 32bits
	//CLK
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CLK);			//SPI_CLK= 0;
	for(int byte= 3; byte>=0; byte--)
	{
		//CString msg; msg.Format("0x%x", temp_byte[i]);	AfxMessageBox(msg);
		//spi_Write(temp_byte[i]);

		//WRITE MOSI
		for(BYTE i=0; i<8; i++)
		{
			//ret	= DlPortReadPortUchar(PORT_ADDR);	
			if((temp_byte[byte]&(byteBIT[7-i]))!=0x00)		SET_PORT_BIT(tmp, B_SPI_MOSI)
			else	CLR_PORT_BIT(tmp, B_SPI_MOSI)
	
			tmp= (BYTE)Inp32(PORT_ADDR);
			SET_PORT_BIT(tmp, B_SPI_CLK);	//SPI_CLK= 1;

			wait_ns(15);				//WAIT 15ns

			tmp= (BYTE)Inp32(PORT_ADDR);
			CLR_PORT_BIT(tmp, B_SPI_CLK);	//SPI_CLK= 0;
		}

	}

	//CS
	wait_ns(20);
	tmp= (BYTE)Inp32(PORT_ADDR);
	SET_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 1;

	return 0;
}

int B_SPI_Read32(DWORD* readData)
{
	BYTE temp_byte[4]={0};
	BYTE tmp= 0x00, ret= 0x00;	

	//CS
	//tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 0;
	wait_ns(20);		
	//read data 32bits
	for(BYTE i= 0; i<4; i++)
	{
		B_SPI_Read(&temp_byte[i]);
	}

	wait_ns(20);
	//CS
	//tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	SET_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 1;

	(*readData) = 0x00000000;
	(*readData) |= (DWORD)temp_byte[0];
	(*readData) |= ((DWORD)temp_byte[1])<<8;						
	(*readData) |= ((DWORD)temp_byte[2])<<16;	
	(*readData) |= ((DWORD)temp_byte[3])<<24;	

	return 0;
}

DWORD B_SPI_Read32_New()	//B_MISO modiy at 0x379
{
	BYTE temp_byte[4]={0};
	BYTE tmp= 0x00, ret= 0x00;	

	//CS
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 0;
	wait_ns(20);		
	//read data 32bits
	//CLK	
	tmp= (BYTE)Inp32(PORT_ADDR);
	SET_PORT_BIT(tmp, B_SPI_CLK);		//SPI_CLK= 1;
	for(BYTE byte= 0; byte<4; byte++)
	{
		//spi_Read(&temp_byte[i]);

		//RECEIVE FROM MISO
		for(BYTE i=0; i<8; i++)
		{
			CLR_PORT_BIT(tmp, B_SPI_CLK);		//SPI_CLK= 0;
			ret	= (BYTE)Inp32(PORT_ADDR+1);		//B_MISO, at 0x379
			//B_MISO= PIN11, ~PIN11
			//if((ret&B_SPI_MISO)!=0x00)	SET_DATA_BIT(temp_byte[byte], byteBIT[7-i])	//first MBS, end LBS
			if((ret&B_SPI_MISO)==0x00)	SET_DATA_BIT(temp_byte[byte], byteBIT[7-i])	//first MBS, end LBS
			else	CLR_DATA_BIT(temp_byte[byte], byteBIT[7-i])
			//wait_ns(15)			
			tmp= (BYTE)Inp32(PORT_ADDR);
			SET_PORT_BIT(tmp, B_SPI_CLK);	//SPI_CLK= 1;
		}
	}

	//CS
	wait_ns(20);
	tmp= (BYTE)Inp32(PORT_ADDR);
	SET_PORT_BIT(tmp, B_SPI_CE);		//SPI_CE= 1;

	DWORD readData = 0;
	readData |= (DWORD)temp_byte[0];
	readData |= ((DWORD)temp_byte[1])<<8;						
	readData |= ((DWORD)temp_byte[2])<<16;	
	readData |= ((DWORD)temp_byte[3])<<24;	

	return (readData);
}
//////////////////////////////////////////////// TEST ////////////////////////////
LONGLONG testSPI_CLK()
{
	BYTE tmp= 0x00;

	LONGLONG ret= 0;
	LONGLONG freq= cpuFrequent();

//	tmp= DlPortReadPortUchar(PORT_ADDR);
	tmp= (BYTE)Inp32(PORT_ADDR);
	CLR_PORT_BIT(tmp, SPI_CLK);	//ce= 0
	for(int i=0; i<100; i++)
	{
//		DlPortWritePortUchar(PORT_ADDR, 0x01);
//		DlPortWritePortUchar(PORT_ADDR, 0x00);
		SET_PORT_BIT(tmp, SPI_CLK);	//ce= 1
	//	ret= wait_ns(20000);
		//ret= wait_us(20);
		ret= wait_ns2(5000, freq);
		CLR_PORT_BIT(tmp, SPI_CLK);	//ce= 0
		//wait_ns(1);
		wait_us(1);
	}
	return ret;
}

//////////////////////////////////driver///////////////////////////////////////////////////
/////////////////// A
DLL_EXPORT void PowerOn() 
{	
	DWORD PN[]=
	{
	 0xfaf1b042,0xd4d90000,0x82192100,0x84192120,
	 0x86400000,0x56000000
	};
/*
	DWORD PN2[]=
	{
	 0xd4100000,0xd4900000,0xd4d00000,0xd4d80000,
	 0x94100000,0x82192100,0x84192100,0x86400000,
	 0x88080000,0x80000000,0x80800000,0x88000000
	};
*/
	//reset
	CLR_RES();	
	wait_us(10000);	//10ms
	SET_RES();	

	//init data
	for(BYTE i=0; i<sizeof(PN)/sizeof(DWORD); i++)
		SPI_Write32_New(PN[i]);
}

void PowerOn2() 
{	
	DWORD PN[]=
	{
		//0xd4100000,
		//0xd4900000,0xd4d00000,0xd4d80000,0x94100000,
		0xfaf1b042,
		0xd4d80000,	//(0x0A)VCI, DC/DC, bias divider 
		//0x94d80000,
		0xa0000000,0xa2000000,0xa4000000,0xa6000000,	//white data
		0xe0000000,0xe2000000,0xe4000000,0xe6000000,	//white data
		//0x82192100,	//local updata timing scalling = 10ms*0x19= 250ms(min 10ms, 0x00= 2560ms),v1-v0= 20ms*0x2, v0-v1= 20ms*0x1(min 0ms)
		//0x84192120,	//global updata timing scalling= 10ms*0x19= 250ms(min 10ms, 0x00= 2560ms),v1-v0= 20ms*0x2, v0-v1= 20ms*0x1(min 0ms), number of times of global update= 0x2(000)
		0x82190000,	//local updata timing scalling = 10ms*0x19= 250ms(min 10ms, 0x00= 2560ms),v1-v0= 20ms*0x0, v0-v1= 20ms*0x0(min 0ms)
		0x84190020,	//global updata timing scalling= 10ms*0x19= 250ms(min 10ms, 0x00= 2560ms),v1-v0= 20ms*0x0, v0-v1= 20ms*0x0(min 0ms), number of times of global update= 0x2(000)
		0x86000000,	//(0x03)overall timing scalling CLK0= 00, 2.5ms 
		0x80800000, //global updata
		0x88000000	//(0x04)force transtion disable
	};

	//reset
	CLR_RES();	
	wait_us(10000);	//10ms
	SET_RES();	

	//init data
	for(BYTE i=0; i<sizeof(PN)/sizeof(DWORD); i++)
		SPI_Write32_New(PN[i]);
}

DLL_EXPORT void PowerOff() 
{
	DWORD PD[]=
	{
		0x80200000,0xd4d00000,0xd4900000,0xd4100000,0xd4000000,
		0x94000000
	};
	for(BYTE i=0; i<sizeof(PD)/sizeof(DWORD); i++)
		SPI_Write32_New(PD[i]);

	Sleep(200);
}

DLL_EXPORT void UpdateIC(BYTE nSeg) 
{
	//check busy status
	//SPI_Write32(0x56000000);
	
	//set local update segment
	//set segment
	//SPI_Write32(GET_SEG_REG(nSeg));	
	SPI_Write32_New(GET_SEG_REG(nSeg));	

	//local update
	//SPI_Write32(0x80400000);	
	SPI_Write32_New(0x80400000);		
}

DLL_EXPORT void UpdateIC_Array(BYTE* pSegArray, BYTE nArraySize) 
{
	//check busy status
	//SPI_Write32(0x56000000);
	
	//set local update segment
	//DWORD	segCMD[4];
	DWORD   TMP=0x0;

	for(BYTE t=0; t<nArraySize; t++)
	{
	//	CString s; s.Format("segTable[%d]= %d",t, pSegArray[t]); MessageBox(s);
		//segCMD[t]= GetCommand(pSegArray[t]);
		//if(segCMD[t]!=0)	TMP = segCMD[t] | TMP;
		if(pSegArray[t]!=0)	TMP = GET_SEG_REG(pSegArray[t]) | TMP;
	//	CString ss; ss.Format("TMP= 0x%x",TMP); MessageBox(ss);
	}

	//set segment
	if(TMP!=0)	
		SPI_Write32_New(TMP);	
		//SPI_Write32(TMP);	

	//local update
	SPI_Write32_New(0x80400000);	
	//SPI_Write32(0x80400000);	
}

DLL_EXPORT void UpdateICArrayn(CString tmpStr) 
{
	//CString tmpStr,
	tmpStr.TrimLeft();tmpStr.TrimRight();	//trim blank 
	CString tmp2;
	if(tmpStr.IsEmpty())	{AfxMessageBox("Input segs(like: 1,2,3,5)"); return;}

	int beforePos=0, currentPos=beforePos;
	CByteArray	byteArray;
	while(currentPos!=-1 && currentPos!= tmpStr.GetLength())
	{
		currentPos= tmpStr.Find(TCHAR(','),beforePos+1);
		if(currentPos==-1 )	currentPos= tmpStr.GetLength();
		if(currentPos>beforePos)	
		{
			tmp2= tmpStr.Mid(beforePos, currentPos);
			tmp2.TrimLeft(TCHAR(','));	tmp2.TrimRight(',');
			if(!tmp2.IsEmpty())	byteArray.Add((BYTE)atoi(tmp2));
		}
		beforePos=currentPos;
	}
	
	if(byteArray.GetSize()==0)	{AfxMessageBox("Input Invailable");	return;}
	DWORD	segDW=0x0;
	for(int x=0; x<byteArray.GetSize(); x++)
	{
		segDW |= GET_SEG_REG(byteArray.GetAt(x));
		//CString msg; msg.Format("byteArray[%d]= %d",x, byteArray.GetAt(x));	MessageBox(msg);

⌨️ 快捷键说明

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