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

📄 parallelportdlg.cpp

📁 Study source file that includes just the standard control of ParallelPort.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			nNewReg = (nNewReg<<2)+nBitTmp;
			Out32(CONTROL,nNewReg);
			//str.Format("0x%2X",nNewReg);
			//AfxMessageBox(str);
			Sleep(1);                  // Sleep Milli-Second

		/*	nBitTmp = 0x01;        // SCL 0, SDA OUT 1
			nNewReg = nNewReg>>2;
			nNewReg = (nNewReg<<2)+nBitTmp;
			Out32(CONTROL,nNewReg);
			//str.Format("0x%2X",nNewReg);
			//AfxMessageBox(str);
			Sleep(1);                  // Sleep Milli-Second*/
			
		}
		else                   // 0 is sent here
		{
			nBitTmp = 0x00;        // SCL 0, SDA OUT 0
			nNewReg = nNewReg>>2;
			nNewReg = (nNewReg<<2)+nBitTmp;
			Out32(CONTROL,nNewReg);
			//str.Format("0x%2X",nNewReg);
			//AfxMessageBox(str);
			Sleep(1);                  // Sleep Milli-Second

			nBitTmp = 0x02;        // SCL 1, SDA OUT 0
			nNewReg = nNewReg>>2;
			nNewReg = (nNewReg<<2)+nBitTmp;
			Out32(CONTROL,nNewReg);
			//str.Format("0x%2X",nNewReg);
			//AfxMessageBox(str);
			Sleep(1);                  // Sleep Milli-Second

		/*	nBitTmp = 0x00;        // SCL 0, SDA OUT 0
			nNewReg = nNewReg>>2;
			nNewReg = (nNewReg<<2)+nBitTmp;
			Out32(CONTROL,nNewReg);
			//str.Format("0x%2X",nNewReg);
			//AfxMessageBox(str);
			Sleep(1);                  // Sleep Milli-Second*/
		}
		count++;
	}
	////  Acknowledge Bit
	nBitTmp = 0x01;        // SCL 0, SDA OUT 1
	nNewReg = nNewReg>>2;
	nNewReg = (nNewReg<<2)+nBitTmp;
	Out32(CONTROL,nNewReg);
	//str.Format("0x%2X",nNewReg);
	//AfxMessageBox(str);
	Sleep(1);                  // Sleep Milli-Second
	
	nBitTmp = 0x03;        // SCL 1, SDA OUT 1
	nNewReg = nNewReg>>2;
	nNewReg = (nNewReg<<2)+nBitTmp;
	Out32(CONTROL,nNewReg);
	//str.Format("0x%2X",nNewReg);
	//AfxMessageBox(str);
	Sleep(1);                  // Sleep Milli-Second
	
	nBitTmp = 0x01;        // SCL 0, SDA OUT 1
	nNewReg = nNewReg>>2;
	nNewReg = (nNewReg<<2)+nBitTmp;
	Out32(CONTROL,nNewReg);
	//str.Format("0x%2X",nNewReg);
	//AfxMessageBox(str);
	Sleep(1);                  // Sleep Milli-Second

	// Check Acknowledge here . Status Register Format : 1111 0111 (S3)
	e = (unsigned char)Inp32(STATUS);
	ucStatus = e>>3;
	ucStatus = ucStatus<<7;

	// ret = 1 Write Fail , ret = 0 Write Pass;
	if(ucStatus==0x00)        //  Acknowledge Bit OK
		return 0;
	else
		return 1;
}

bool CParallelPortDlg::I2c_Initialize()
{
	int nOldData = 0;
	int nNewData = 0xFF;     // whatever for nNewData
	int nTemp = 0;
	nOldData = Inp32(DATA);  // Get Current value of DATA register.
	if(nOldData == nNewData)
		nNewData -= 1;

	Out32(DATA,nNewData);     // Output a new value to DATA to check its accessibility.
	//Sleep(5000);
	//AfxMessageBox("Wait");
	nTemp = Inp32(DATA);
	if(nTemp == nNewData)    // Initialize successful.
	{
		Out32(DATA,nOldData);
		return TRUE;
	}
	else                     // Initialize Failed.
	{
		return FALSE;
	}
}

int CParallelPortDlg::I2c_ReadByte(int nOldReg)
{
	int count = 8;
	int nRet = 0x00;
	unsigned char d,e,ucTmp = 0x00;
	CString str;
	int nBitTmp = 0x00;
	int nNewReg = nOldReg;

	while(count>0)
	{
		//  SCL C1, SDA OUT C0
		nBitTmp = 0x01;        // SCL 0, SDA OUT 1
		nNewReg = nNewReg>>2;
		nNewReg = (nNewReg<<2)+nBitTmp;
		Out32(CONTROL,nNewReg);
		//str.Format("0x%2X",nNewReg);
		//AfxMessageBox(str);
		Sleep(1); 

		nBitTmp = 0x03;        // SCL 1, SDA OUT 1
		nNewReg = nNewReg>>2;
		nNewReg = (nNewReg<<2)+nBitTmp;
		Out32(CONTROL,nNewReg);
		//str.Format("0x%2X",nNewReg);
		//AfxMessageBox(str);
		Sleep(1); 

		e = (unsigned char)Inp32(STATUS);
		//str.Format("Get Status Value:%d",e);
		//AfxMessageBox(str);
		d = e>>3;
		d = d<<7;

		if(d == 0x80)
			d = 0x01;
		ucTmp = ucTmp<<1;
		ucTmp = (ucTmp+d);
		count--;
	}
	nRet = int(ucTmp);
	//str.Format("Get Byte Value:%d",nRet);
	//AfxMessageBox(str);

	return nRet;
}

void CParallelPortDlg::I2c_SendAck(int nOldReg)
{
	int nBitTmp = 0x00;
	int nNewReg = nOldReg;
	CString str;

	////  Acknowledge Bit
	nBitTmp = 0x00;        // SCL 0, SDA OUT 0
	nNewReg = nNewReg>>2;
	nNewReg = (nNewReg<<2)+nBitTmp;
	Out32(CONTROL,nNewReg);
	//str.Format("0x%2X",nNewReg);
	//AfxMessageBox(str);
	Sleep(1);                  // Sleep Milli-Second
	
	nBitTmp = 0x02;        // SCL 1, SDA OUT 0
	nNewReg = nNewReg>>2;
	nNewReg = (nNewReg<<2)+nBitTmp;
	Out32(CONTROL,nNewReg);
	//str.Format("0x%2X",nNewReg);
	//AfxMessageBox(str);
	Sleep(1);                  // Sleep Milli-Second
	
	nBitTmp = 0x00;        // SCL 0, SDA OUT 0
	nNewReg = nNewReg>>2;
	nNewReg = (nNewReg<<2)+nBitTmp;
	Out32(CONTROL,nNewReg);
	//str.Format("0x%2X",nNewReg);
	//AfxMessageBox(str);
	Sleep(1);                  // Sleep Milli-Second
}

int CParallelPortDlg::I2c_PageWrite(int nDeviceSel, int nMemoryAddress, int nByteCount, int Data[], int nPageSize)
{
	int nOldReg = 0;
	int ret = 0;
	unsigned char ucTmp = 0;
	for(int i=0;i<nByteCount;i++)
	{
		if(Data[i]<0x00 || Data[i]>0xFF)
		{
			ret = 1; 
			return ret;
		}
	}
	// James, need to check nPageSize and "Roll-over" issue here and then proceed to write procedures. Finished......
	// If the bytes are more than one page, "Roll-over" occurs here.
	if(nByteCount>nPageSize)
	{
		ret = 1;
		return ret;
	}

	int nSizeBit = -1;
	int nTmp = 0;
	do
	{
		nSizeBit++;
		nTmp = nPageSize>>nSizeBit;
	}
	while(nTmp!=1);

	// If the Page Address of the Start and End memory is not the same, "Roll-over" occurs here.
	int nEndAddress = nMemoryAddress+nByteCount-1;
	if((nEndAddress>>nSizeBit) != (nMemoryAddress>>nSizeBit))
	{
		ret = 1;
		return ret;
	}

	nOldReg = Inp32(CONTROL);

	//Write Start here......
	I2c_Start();
	//Send the Device Select Code here......
	ucTmp = (unsigned char)nDeviceSel;
	if(I2c_WriteByte(nOldReg,ucTmp))
	{
		ret = 1;
		I2c_Stop();
		return ret;
	}
	//Send the Start Memory Address here......
	ucTmp = (unsigned char)nMemoryAddress;
	if(I2c_WriteByte(nOldReg,ucTmp))
	{
		ret = 1;
		I2c_Stop();
		return ret;
	}
	//Send the Data which will be written to the Memory......
	for(i=0;i<nByteCount;i++)
	{
		ucTmp = (unsigned char)Data[i];
		if(I2c_WriteByte(nOldReg,ucTmp))
		{
			ret = 1;
			I2c_Stop();
			return ret;
		}
	}

	//Stop condition here......
	I2c_Stop();

	// ret = 1 Write Fail , ret = 0 Write Pass;
	return ret;
}

int CParallelPortDlg::I2c_PageRead(int nDeviceSel1, int nMemoryAddress,int nDeviceSel2, int nByteCount, int *Data, int nPageSize)
{
	int nOldReg = 0;
	int ret = 0;
	unsigned char ucTmp = 0;

	//Prevent "Roll-over" occur here.
	if(nMemoryAddress+nByteCount-1>0xFF)
	{
		nByteCount = 0xFF+1-nMemoryAddress;
	}
	//Get the Current Control Register and remain the other bits values......
	nOldReg = Inp32(CONTROL);

	//Start here......
	I2c_Start();
	//Send the Device Select Code here......
	ucTmp = (unsigned char)nDeviceSel1;
	if(I2c_WriteByte(nOldReg,ucTmp))
	{
		ret = 1;
		I2c_Stop();
		return ret;
	}
	//Send the Start Memory Address here......
	ucTmp = (unsigned char)nMemoryAddress;
	if(I2c_WriteByte(nOldReg,ucTmp))
	{
		ret = 1;
		I2c_Stop();
		return ret;
	}

	//Start again here......
	I2c_Start();
	//Send the Device Select Code again here......
	ucTmp = (unsigned char)nDeviceSel2;
	if(I2c_WriteByte(nOldReg,ucTmp))
	{
		ret = 1;
		I2c_Stop();
		return ret;
	}

	//Read Sequential Memory here......
	for(int i=0;i<nByteCount-1;i++)
	{
		*Data = I2c_ReadByte(nOldReg);
		Data  ++;
		I2c_SendAck(nOldReg);
	}
	*Data = I2c_ReadByte(nOldReg);
	//Don't Send_Ack to Slave before Stop Condition......
	//Stop condition here......
	I2c_Stop();

	// ret = 1 Read Fail , ret = 0 Read Pass;
	return ret;
}

int CParallelPortDlg::ConvertHexToDec(CString strHex)
{
	int ret = 0;
	int nTemp = 0;
	strHex.MakeUpper();
	for(int i=0;i<strHex.GetLength();i++)
	{
		switch(strHex[i])
		{
		case '0':
			nTemp = 0;
			break;
		case '1':
			nTemp = 1;
			break;
		case '2':
			nTemp = 2;
			break;
		case '3':
			nTemp = 3;
			break;
		case '4':
			nTemp = 4;
			break;
		case '5':
			nTemp = 5;
			break;
		case '6':
			nTemp = 6;
			break;
		case '7':
			nTemp = 7;
			break;
		case '8':
			nTemp = 8;
			break;
		case '9':
			nTemp = 9;
			break;
		case 'A':
			nTemp = 10;
			break;
		case 'B':
			nTemp = 11;
			break;
		case 'C':
			nTemp = 12;
			break;
		case 'D':
			nTemp = 13;
			break;
		case 'E':
			nTemp = 14;
			break;
		case 'F':
			nTemp = 15;
			break;
		default:
			break;
		}
		ret = ret*16+nTemp;
	}
	return ret;
}

void CParallelPortDlg::OnButton2Read() 
{
	// TODO: Add your control notification handler code here
	CString strTemp;
	int nLength;
	GetDlgItem(IDC_EDIT5_DEVICEREAD)->GetWindowText(strTemp);
	nLength = strTemp.GetLength();
	if(nLength == 2)
	{
		for(int i=0;i<strTemp.GetLength();i++)
		{
			if(!((strTemp[i]<='9' && strTemp[i]>='0') || (strTemp[i]<='f' && strTemp[i]>='a') || (strTemp[i]<='F' && strTemp[i]>='A')))
			{
				AfxMessageBox("Wrong Device Select Code for Read Operation !");
				GetDlgItem(IDC_EDIT5_DEVICEREAD)->SetWindowText("");
				GetDlgItem(IDC_EDIT5_DEVICEREAD)->SetFocus();
				return;
			}
		}
	}
	else
	{
		AfxMessageBox("Wrong Device Select Code for Read Operation !");
		GetDlgItem(IDC_EDIT5_DEVICEREAD)->SetWindowText("");
		GetDlgItem(IDC_EDIT5_DEVICEREAD)->SetFocus();
		return;
	}
	
	GetDlgItem(IDC_EDIT1_READADDR)->GetWindowText(strTemp);
	nLength = strTemp.GetLength();
	if(nLength == 2)
	{
		for(int i=0;i<strTemp.GetLength();i++)
		{
			if(!((strTemp[i]<='9' && strTemp[i]>='0') || (strTemp[i]<='f' && strTemp[i]>='a') || (strTemp[i]<='F' && strTemp[i]>='A')))
			{
				AfxMessageBox("Wrong Start Memory Address for Read Operation !");
				GetDlgItem(IDC_EDIT1_READADDR)->SetWindowText("");
				GetDlgItem(IDC_EDIT1_READADDR)->SetFocus();
				return;
			}
		}
	}
	else
	{
		AfxMessageBox("Wrong Start Memory Address for Read Operation !");
		GetDlgItem(IDC_EDIT1_READADDR)->SetWindowText("");
		GetDlgItem(IDC_EDIT1_READADDR)->SetFocus();
		return;
	}
	UpdateData(TRUE);

	m_ctrRead.EnableWindow(FALSE);
	
	int *buffer = new int[m_nReadCount];
	if(!I2c_PageRead((ConvertHexToDec(m_strDeviceRead)&0xFE),ConvertHexToDec(m_strReadAddr),(ConvertHexToDec(m_strDeviceRead)|0x01),m_nReadCount,buffer,16))
	{
		strTemp.Empty();
		CString result;
		result.Empty();
		for(int i=0;i<m_nReadCount;i++)
		{
			strTemp.Format("%02X ",*(buffer+i));
			result+=strTemp;
		}
		m_ctrReadList.AddString(result);
		strTemp.Format("%d Read Operation Succeed !",m_nStatusCount++);
		m_ctrStatusList.AddString(strTemp);
	}
	else
	{
		//m_ctrReadList.AddString("Read Operation Failed !");
		strTemp.Format("%d Read Operation Failed !",m_nStatusCount++);
		m_ctrStatusList.AddString(strTemp);
	}

	//Sleep(5000);
	m_ctrRead.EnableWindow(TRUE);
}

void CParallelPortDlg::OnButton1Write() 
{
	// TODO: Add your control notification handler code here
	CString strTemp;
	int nLength;
	GetDlgItem(IDC_EDIT6_DEVICEWRITE)->GetWindowText(strTemp);
	nLength = strTemp.GetLength();
	if(nLength == 2)
	{
		for(int i=0;i<strTemp.GetLength();i++)
		{
			if(!((strTemp[i]<='9' && strTemp[i]>='0') || (strTemp[i]<='f' && strTemp[i]>='a') || (strTemp[i]<='F' && strTemp[i]>='A')))
			{
				AfxMessageBox("Wrong Device Select Code for Write Operation !");
				GetDlgItem(IDC_EDIT6_DEVICEWRITE)->SetWindowText("");
				GetDlgItem(IDC_EDIT6_DEVICEWRITE)->SetFocus();
				return;
			}
		}
	}
	else
	{
		AfxMessageBox("Wrong Device Select Code for Write Operation !");
		GetDlgItem(IDC_EDIT6_DEVICEWRITE)->SetWindowText("");
		GetDlgItem(IDC_EDIT6_DEVICEWRITE)->SetFocus();
		return;
	}
	
	GetDlgItem(IDC_EDIT2_WRITEADDR)->GetWindowText(strTemp);
	nLength = strTemp.GetLength();
	if(nLength == 2)
	{
		for(int i=0;i<strTemp.GetLength();i++)
		{
			if(!((strTemp[i]<='9' && strTemp[i]>='0') || (strTemp[i]<='f' && strTemp[i]>='a') || (strTemp[i]<='F' && strTemp[i]>='A')))
			{
				AfxMessageBox("Wrong Start Memory Address for Write Operation !");
				GetDlgItem(IDC_EDIT2_WRITEADDR)->SetWindowText("");
				GetDlgItem(IDC_EDIT2_WRITEADDR)->SetFocus();
				return;
			}
		}
	}
	else
	{
		AfxMessageBox("Wrong Start Memory Address for Write Operation !");
		GetDlgItem(IDC_EDIT2_WRITEADDR)->SetWindowText("");
		GetDlgItem(IDC_EDIT2_WRITEADDR)->SetFocus();
		return;
	}

	GetDlgItem(IDC_EDIT3_DATAINARRAY)->GetWindowText(strTemp);
	nLength = strTemp.GetLength();
	if(((nLength+1)%3) != 0)
	{
		AfxMessageBox("Wrong Format of Data In Array !");
		GetDlgItem(IDC_EDIT3_DATAINARRAY)->SetFocus();
		return;
	}
	else
	{
		for(int i=0;i<strTemp.GetLength();i++)
		{
			if(!((strTemp[i]<='9' && strTemp[i]>='0') || (strTemp[i]<='f' && strTemp[i]>='a') || (strTemp[i]<='F' && strTemp[i]>='A') || (strTemp[i]==' ')))
			{
				AfxMessageBox("Wrong Format of Data In Array !");
				GetDlgItem(IDC_EDIT3_DATAINARRAY)->SetFocus();
				return;
			}
		}
	}

	UpdateData(TRUE);

	m_ctrWrite.EnableWindow(FALSE);
	nLength = m_strDataInArray.GetLength();
	int nDataCount = (nLength+1)/3;
	int *DataInBuffer = new int[nDataCount];
	int nTmp = 0;
	CString str;
	char *cTmp = new char[nDataCount*3];
	memset(cTmp,0x00,nDataCount*3);
	strncpy(cTmp,m_strDataInArray,nLength);
	for(int i=0;i<nDataCount;i++)
	{
		str.Format("%c%c",*(cTmp+i*3),*(cTmp+i*3+1));
		nTmp = ConvertHexToDec(str);
		*(DataInBuffer+i) = nTmp;
	}

//	int x= *(DataInBuffer+0);
//	int y= *(DataInBuffer+1);

	if(!I2c_PageWrite(ConvertHexToDec(m_strDeviceWrite),ConvertHexToDec(m_strWriteAddr),nDataCount,DataInBuffer,16))
	{
		strTemp.Format("%d Write Operation Succeed !",m_nStatusCount++);
		m_ctrStatusList.AddString(strTemp);
	}
	else
	{
		strTemp.Format("%d Write Operation Failed !",m_nStatusCount++);
		m_ctrStatusList.AddString(strTemp);
	}
	//Sleep(5000);
	m_ctrWrite.EnableWindow(TRUE);
}

void CParallelPortDlg::OnButton3Refresh() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	UpdatePins();
}

⌨️ 快捷键说明

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