I have a Panda II board and I need to interface it with PS/2 barcode scanner.
I connect the right pin to VCC, Clock, Data and GND, but something is wrong because I read some data that is wrong.
Someone has any experience with it?
Where can I found some protocol specifications?
Gus, thanks for the welcome.
I find the ps/2 schema on internet and I connect the ps/2 male connector’s pin like this:
1 - Data
2 - Not Implemented
3 - Ground
4 - Vcc (+5V)
5 - Clock
6 - Not Implemented
with panda I connect Data with pin D40 and clock to pin D20
The code is:
public static void Main()
{
// Blink board LED
InputPort clk = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di20, false, Port.ResistorMode.PullUp);
InputPort data = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di40, false, Port.ResistorMode.PullUp);
ArrayList al = new ArrayList();
while (true)
{
if (clk.Read() == false)
{
al.Clear();
for(int index=0;index<11;index++)
{
bool bit = data.Read();
al.Add(bit);
}
String s = "";
for (int i = 0; i < al.Count; i++)
{
s += al[i].ToString() + " ";
}
Debug.Print("Bits: " + s);
}
Thread.Sleep(1);
}
}
But the bit that I receved are not correct.
Have any ideas?
public class PS2Manager
{
private Cpu.Pin _clockPin;
private Cpu.Pin _dataPin;
private const int CLKFULL = 40;
private const int CLKHALF = 20;
private const int TICKS_PER_MICROSECOND = 10;
private TristatePort _clockPort;
private TristatePort _dataPort;
public PS2Manager(Cpu.Pin data, Cpu.Pin clock)
{
_clockPin = clock;
_dataPin = data;
_clockPort = new TristatePort((Cpu.Pin)_clockPin, false, false, Port.ResistorMode.PullDown);
_dataPort = new TristatePort((Cpu.Pin)_dataPin, false, false, Port.ResistorMode.PullDown);
}
void gohi(TristatePort port)
{
makePinOutput(port);
port.Write(true);
}
void golo(TristatePort port)
{
makePinOutput(port);
port.Write(false);
}
void makePinOutput(TristatePort port)
{
if (port.Active == false)
port.Active = true;
}
void makePinInput(TristatePort port)
{
if (port.Active == true)
port.Active = false;
}
private void _delayMicrosecond(int mics)
{
long totTicks = mics * TICKS_PER_MICROSECOND;
long end = Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks + mics;
while (Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks < end)
{ }
}
public int read(out int value)
{
int data = 0x00;
int i;
int bit = 0x01;
int parity = 1;
makePinInput(_dataPort);
// wait for data line to go low
while (_dataPort.Read() == true) ;
makePinInput(_clockPort);
// wait for clock line to go high
while (_clockPort.Read() == false) ;
_delayMicrosecond(CLKHALF);
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
for (i = 0; i < 8; i++)
{
makePinInput(_dataPort);
if (_dataPort.Read() == true) data |= bit;
bit = bit << 1;
_delayMicrosecond(CLKHALF);
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
parity = parity ^ (data & 0x01);
}
// we do the delay at the end of the loop, so at this point we have
// already done the delay for the parity bit
// stop bit
_delayMicrosecond(CLKHALF);
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
_delayMicrosecond(CLKHALF);
golo(_dataPort);
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
gohi(_dataPort);
value = data;
return 0;
}
public int write(int data)
{
int i;
int parity = 1;
//Serial.print("sending ");
//Serial.println(data, HEX);
makePinInput(_clockPort);
makePinInput(_dataPort);
if (_clockPort.Read() == false) return -1;
if (_dataPort.Read() == false) return -2;
golo(_dataPort);
_delayMicrosecond(CLKHALF);
// device sends on falling clock
// start bit
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
// data bits
for (i = 0; i < 8; i++)
{
if ((data & 0x01) == 0x01) gohi(_dataPort);
else golo(_dataPort);
_delayMicrosecond(CLKHALF);
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
parity = parity ^ (data & 0x01);
data = data >> 1;
}
// parity bit
if (parity == 0x01) gohi(_dataPort);
else golo(_dataPort);
_delayMicrosecond(CLKHALF);
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
// stop bit
gohi(_dataPort);
_delayMicrosecond(CLKHALF);
golo(_clockPort);
_delayMicrosecond(CLKFULL);
gohi(_clockPort);
_delayMicrosecond(CLKHALF);
_delayMicrosecond(50);
return 0;
}
}
The main code is:
PS2Manager ps2 = new PS2Manager((Cpu.Pin)FEZ_Pin.Digital.Di40, (Cpu.Pin)FEZ_Pin.Digital.Di20);
while (true)
{
int data = 0;
while (ps2.read(out data) != 0) ;
Debug.Print("New char:" + data.ToString());
Thread.Sleep(1);
}
I receved something but I think it is wrong.
Can someone help me?