📄 parallel.cpp
字号:
// parallel.cpp: implementation of the parallel class.
// p25.1:SCL
// P25.2:SDA OUT
// P25.13:SDA IN
// output is reversed bit by bit.
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IIC.h"
#include "parallel.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
parallel::parallel()
{
mPortAddr = 0x0378;
m_bFlag = InitializeWinIo();
WriteByte(0xff);
m_Speed = 0;
m_bSwapLine = FALSE;
m_bBusy = false;
m_bBusState = false;
}
parallel::parallel(WORD PortAddr)
{
parallel();
mPortAddr = PortAddr;
}
parallel::~parallel()
{
WriteByte(0xff);
ShutdownWinIo();
}
void parallel::InitPort()
{
WriteByte(0xff);
}
void parallel::Init(BYTE value)
{
WriteByte(value);
}
void parallel::WriteByte(BYTE Value)
{
/*Use WINIO.LIB's function.
SetPortVal(
WORD wPortAddr,
DWORD dwPortVal,
BYTE bSize//Can be 1 (BYTE), 2 (WORD) or 4 (DWORD).
);
*/
BYTE tmp = 0;
if(m_bSwapLine){//Swap SDA & SCL
tmp = (Value<<7)>>6;
tmp += (Value<<6)>>7;
Value = Value&0xFC;
Value = Value|tmp;
}
DWORD dwPortVal = (DWORD)(~Value);
// for(int i =0;i<5;i++)
SetPortVal(mPortAddr,dwPortVal,1);
}
BYTE parallel::ReadByte()
{
/*bool _stdcall GetPortVal(
WORD wPortAddr,
PDWORD pdwPortVal,
BYTE bSize//Can be 1 (BYTE), 2 (WORD) or 4 (DWORD).
);*/
DWORD Value=0;
// WORD ReadAddr = mPortAddr + 1;
WORD ReadAddr = 0x0379;
GetPortVal(ReadAddr,&Value,1);//read byte
return (BYTE)(~Value);
}
void parallel::SendStart()
{
m_bBusy = true;
WriteByte(0x03);// SCL = 1;SDA = 1;
WriteByte(0x03);// SCL = 1;SDA = 1;
WriteByte(0x01);// SCL = 1;SDA = 0;
WriteByte(0x01);// SCL = 1;SDA = 0;
WriteByte(0x00);// SCL = 0;SDA = 0;
WriteByte(0x00);// SCL = 0;SDA = 0;
}
void parallel::SendStop()
{
WriteByte(0x00);// SCL = 0;SDA = 0;
WriteByte(0x00);// SCL = 0;SDA = 0;
WriteByte(0x01);// SCL = 1;SDA = 0;
WriteByte(0x01);// SCL = 1;SDA = 0;
WriteByte(0x03);// SCL = 1;SDA = 1;
WriteByte(0x03);// SCL = 1;SDA = 1;
m_bBusy = false;
}
BOOL parallel::SendByte(BYTE byte)//bit7-bit0-ack
{
BYTE bit = 0;
BOOL flag = false;
for(int i = 0;i < 8;i++){
if((byte&(0x80>>i))!=0)
bit = 2;
else
bit = 0;
WriteByte((0|bit));
WriteByte((0|bit));//SCL = 0, change SDA
WriteByte((1|bit));
WriteByte((1|bit));
WriteByte((1|bit));//SCL = 1, hold SDA
WriteByte((0|bit));
WriteByte((0|bit));////SCL = 0, ready for next bit
}
WriteByte(0x02);
WriteByte(0x02);//SCL = 0,SDA = 1
flag = GetAck();//ACK READ,wait Client pull SDA to low
WriteByte(0x03);
WriteByte(0x03);//SCL = 1,SDA = 1
WriteByte(0x02);
WriteByte(0x02);//SCL = 0,SDA = 1
return flag;
}
bool parallel::GetAck()
{
//Return true if ack ok
//ACK: client pull data line to low
BYTE val;
for(int i =0;i<10;i++){
val = ReadByte();
//SDA in 0x0379 bit 4
if ( (val&0x10) == 0 )
return true;
}
return false;
}
BYTE parallel::ReceiveByte()
{
//SDA == 1
BYTE val = 0,tmp =0;
for(int i=0;i<8;i++){
WriteByte(0x02);//SCL = 0
WriteByte(0x02);//SCL = 0
WriteByte(0x03);//SCL = 1
WriteByte(0x03);//SCL = 1
tmp = ReadByte();
tmp = (tmp & 0x10)>>4;
val = val << 1;
val += tmp;
WriteByte(0x02);//SCL = 0
}
WriteByte(0x00);//SCL = 0,SDA =0 ,ACK
WriteByte(0x00);//SCL = 0,SDA =0 ,ACK
WriteByte(0x01);//SCL = 1
WriteByte(0x01);//SCL = 1
WriteByte(0x00);//SCL = 0
WriteByte(0x02);//SCL = 0
return val;
}
BYTE parallel::ReceiveByteNoAck()
{
//SDA == 1
BYTE val = 0,tmp =0;
for(int i=0;i<8;i++){
WriteByte(0x02);//SCL = 0
WriteByte(0x02);//SCL = 0
WriteByte(0x03);//SCL = 1
WriteByte(0x03);//SCL = 1
tmp = ReadByte();
tmp = (tmp & 0x10)>>4;
val = val << 1;
val += tmp;
WriteByte(0x02);//SCL = 0
}
WriteByte(0x02);//SCL = 0
SendStop();
return val;
}
bool parallel::CheckBus()
{
if(!m_bBusy)
m_bBusState=!GetAck();
return m_bBusState;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -