#include <reg51.h>
#define BYTE (7)
#define PARITY (1)
char D_Bus[BYTE+PARITY]={0};
sbit clockpin = P3^2;
sbit datapin = P3^7;
void sendbit(char b){
char i;
datapin=b;
for (i=0; i<5; i++); // Clock Time for Hi
clockpin=0;
for (i=0; i<9; i++); // Clock Time for Lo
datapin=b;
clockpin=1;
}
void D_Buffer(void)
{
char nbit=0, i=0;
do
{
clockpin=1;
for (i=0; i<5; i++) // Tck - Clock Time for Hi
{
D_Bus[nbit] = datapin;
}
clockpin=0;
for (i=0; i<9; i++) // Tck - Clock Time for Lo
{
D_Bus[nbit] = datapin;
}
}while(nbit++ <= 8);
sendbit(1); // HI
sendbit(0); // ACK
}
void Host_To_Drive(void) interrupt 0
{
char i=0;
while(!INT0);
datapin=0;
clockpin=1; // Raising Edge
for (i=0; i<5; i++); // Tck - Clock Time for Hi
clockpin=0; // Falling Edge
for (i=0; i<2; i++); // Tck - Clock Time for Lo
D_Buffer();
}
void main (void) {
IT0 = 0;
IE0 = 0;
EX0 = 1;
TF0 = 0;
EA = 1;
while(1)
{
}
#include <reg51.h>
sbit clockpin = P3^2;
sbit datapin = P3^7;
byte PCdata = 0;
void sendbit(char b){
char i;
datapin=b;
for (i=0; i<5; i++); // Clock Time for Hi
clockpin=0;
for (i=0; i<9; i++); // Clock Time for Lo
datapin=b;
clockpin=1;
}
char recvbit(char b){
char i;
for (i=0; i<5; i++); // Clock Time for Hi
i = datapin;
clockpin=0;
for (i=0; i<9; i++); // Clock Time for Lo
clockpin=1;
return i<<7;
}
void Host_To_Drive(void) interrupt 0
{
char i;
EX0 = 0; // disable interupt
recvbit(); // start bit
PCdata = 0;
for (i=0; i<8; i++){
PCdata += recvbit();
PCdata >>= 1;
}
recvbit(); // parity bit
sendbit(1);
sendbit(0);
sendbit(1);
}
void xmit(char a){
char i,mask;
char parity;
parity = 0;
sendbit(0); // start bit
mask = 1;
for (i=0; i<=8; i++) { // shift right -> b0.. b7
if (a & mask) {sendbit(1); parity++;}
else sendbit(0);
mask <<= 1;
}
parity &= 1; // least significant bit only
sendbit(!parity); // convert to odd parity
sendbit(1); // stop bit
}
void main (void) {
IT0 = 1;
EX0 = 1;
EA = 1;
while(1)
{
if PCdata!=0 {xmit(0xFA); PCdata=0; EX0=1;}
}
}
i PCdata
0 C0
1 20
2 D0
3 28
4 D4
5 2A
6 D5
7 2A
char recvbit(void){
char i,databit;
// MCU generate clock, but PC transmit data bit
for (i=0; i<5; i++); // waiting for data stable
databit = datapin; // get PC data bit to i
clockpin=0; // negative going edge
for (i=0; i<5; i++); // PC will transmit new data bit
clockpin=1; // clockpin standy state
return databit<<7; // shift to bit 7 -> return value to caller
}
void PCsendCommand(void) interrupt 0
{
char i;
EX0 = 0; // disable interupt
recvbit(); // start bit
PCdata = 0; // if PCdata!=0 -> there is new byte from PC
for (i=0; i<8; i++){
PCdata >>= 1; // shift right 1 position
PCdata += recvbit(); // new data bit inserto into 7th position
}
recvbit(); // parity bit, at this case I ignore it
sendbit(1); // reply acknowledge bit
sendbit(0); // reply acknowledge bit
datapin = 1; // datapin standby state = 1
}
When PC startup, the LEDs on keyboard blinking for a moment. That time the PC send command to turn on and off keyboard's LED.How can i test Host-to-KB? How to call the PC sending data to my 8051? Then how can i test/know the result after the PC sent data?
I have no idea on this part...the typematic delay, which is the short delay between the first and second "a",...
Welcome m.42, send your assembly code and we discuss it together.I have written some code in assembly for transmitting data and i need some of all your time to help me successfully emulate the keyboard operation.
NoAnother question. Do we need to add some resisters between PS2 to PC?
I think the problem is here.I use Switches to switch the Keyboard and Keypad clk n data signal. Initialize i switch it to Keyboard, but after OS i switch it to Keypad for testing.
m.42 wrote :Do we need to add some resisters between PS2 to PC?
The same thing i had done with my 89c52 micro controller. the data = p0.0 and clk= p0.1. i got the info that since the MCU's PORT 0 is open collector and most of the sites which i referred told that only open collector outputs need to be connected to the pc's kbd data and kbd clk lines. hence i didn't use any details special interface between the MCU's pins and the ps/2 lines. other than a simple 4.7k pull up resistor for both the lines. what "help" is also doing the only difference. To solve my final solution of emulating the keyboard, i put forth my immediate objective as to emulate only the key "a". i opened a notepad and hooked my probes for clk and data signals. i observed 2 bytes of transmission ( by connecting the kbd).
Try this new method to test your project. We place the board parallel with regular keyboard-PC connection, as this :I already try as you say. Let my Keypad initialized conversation when pc startup. After in the OS i open the notepad try to press the keypad. The 8051 can't generate the clock and nothing come out in the notepad. I attached 3 Key #1 #2 #3 sample waveform for you in Excel file. Please have a look.
void initialize(void){
LED = 0x00;
xmit(0xFA); PCdata=0; EX0=1;
LED = 0xFF;
}
if (PCdata) initialize();
clockpin = P3^2; // /INT0 (external interrup)
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?