# How to interface PC keyboard to uC...

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
HI,

I am using uart at 9600 baund rate....assynchronous mode.

#### blueroomelectronics

Joined Jul 22, 2007
1,757
HI,

I am using uart at 9600 baund rate....assynchronous mode.
Did you connect a PS/2 keyboard directly to the serial port on a PIC?

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
No, i have tested it by USB module for serial data, directly PS2 to PC terminal window for testing purpose...,

#### t06afre

Joined May 11, 2009
5,934
First RRitesh, I know you have been on this site http://www.computer-engineering.org/ps2protocol/ Here we can read that PS/2 use a synchronous serial protocol Did you absorb ANY of that!! So then using asynchronous aproach. Do you not think that is most likely to fail. If you have no idea of what you do, then you will not be able to create anything either. Spending a lot of time using Google and then posting any code you think may work. And then asking for help will not either work. For crying out loud. Can you not just for once. Gather information, then based on that information. DO SOME CODING ON YOUR OWN. Even how painful you must think it is to actually read the datasheet, and then convert that information into code. Start simple by just setting up the PIC to use synchronous serial protocol. Then read any data from the latter port, convert it a alphanumeric hex string. And then send it to LCD

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi again,

Here it is:

1 start bit. This is always 0.
8 data bits, least significant bit first.
1 parity bit (odd parity).
1 stop bit. This is always 1.

this is to be written in code but the problem is how to clock or data should be capture because clk is generated in device at unknown timing i don't know it perfect..
and how to make logic for parity test?

#### blueroomelectronics

Joined Jul 22, 2007
1,757
Hi again,

Here it is:

1 start bit. This is always 0.
8 data bits, least significant bit first.
1 parity bit (odd parity).
1 stop bit. This is always 1.

this is to be written in code but the problem is how to clock or data should be capture because clk is generated in device at unknown timing i don't know it perfect..
and how to make logic for parity test?
Is this how you think a PS/2 keyboard communicates?

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Is this how you think a PS/2 keyboard communicates?
yes only from device to uC....and clk is generated from device only!
so, while coding clk should be input or output because we have to low or high the clk line.

#### t06afre

Joined May 11, 2009
5,934
Have you read this section of the datasheet? 10.0 ADDRESSABLE UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER (USART)
What does section 10.4 say? Have you tried to convey this information into some working code?

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

I have this code for USART 9600 baund rate slave mode.....not sure of baud rate of PS2..
Rich (BB code):
 #include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
void  HSerinit(void),Hserout(unsigned char ch);
unsigned char HSerin(void)  ;
void main(void)
{
TRISC = 0b10000000;

HSerinit();
__delay_ms(200);
while(1)
{
HSerin();

if(RCREG=='b');
{

RC4 =1;

__delay_ms(250);
}
}
}

void HSerinit(void)
{
TRISC = 0b10000000;                   // TX was an input!
SPBRG = 51;                    // 20Mhz xtal 9600 BAUD
TXSTA = 0b00110000;  //Synchronous mode Slave mode
RCSTA = 0b10010000;//Serial port enabled Enables continuous receive
}

unsigned char HSerin(void)
{     while(!RCIF);                    // Wait for a character
return RCREG;                    // return character
}

#### blueroomelectronics

Joined Jul 22, 2007
1,757
PS/2 doesn't have a baud rate, it's synchronous clock & data. But you're not listening so let me tell you about what a nice day outside it is. I'm going to pickup my awesome Pedego Interceptor bicycle from the shop after getting its wheels trued. Did I mention it's red! Isn't that exciting!. Wheeeee.

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
they how to this?

#### MrChips

Joined Oct 2, 2009
27,669
NRZI, UART, ACIA, SCI, USART, RS-232, COMM PORT, are all one of many ways to do serial communications. To learn about PS/2 read the PS/2 serial specifications.

#### bertus

Joined Apr 5, 2008
22,119
Hello,

Did you ever read the pages I gave you in post #27 ?
One of them even uses a microcontroller.

Bertus

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Yes, i have seen many link but need help in programming code how to capture the data with device clock?

#### tshuck

Joined Oct 18, 2012
3,534
Yes, i have seen many link but need help in programming code how to capture the data with device clock?
Start at the beginning. Learn how to program, learn how to use a microcontroller, read the relevant information, then, using your understanding from the previous three experiences, write a program that will accomplish what you want instead of asking others to do it for you.

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
OK, i am doing one thing ....i have connected switch across data and clock for testing code but terminal window is not showing the data iam feeding from switch is there and problem with code?

Rich (BB code):
 #include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define CLOCK RC1
//#define DATA  RC0
void  HSerinit(void),Hserout(unsigned char Data);
unsigned char Scan_Data(void);
unsigned char ch;
void main()
{
//PORTC=0xFF;     //Pull up the inputs to High
TRISC4=0;
TRISC0=1;
TRISC1=1;
HSerinit();
__delay_ms(100);
ch='a';
Hserout(ch);
while(1)
{
// ch=Scan_Data();    //Scan for a incoming data

RC4=0;

RC4=1;
__delay_ms(200);

unsigned char Data=0,temp;
if(CLOCK==0);   //Wait for First Falling Edge
// CLOCK=0;     //Clear the Flag--Bcoz this is Start Pulse
RC4=0;
RC4=1;
__delay_ms(200);
for(char i=0;i<7;i++)
{
RC4=0;
RC4=1;
__delay_ms(200);
while(CLOCK==0); //Capture the next 7 bits of data
//CLOCK=0;
temp=RC0; //DATA using switch
temp<<=i;
Data|=temp;
}
RC4=0;
RC4=1;
__delay_ms(400);
ch=Data;

Hserout(ch);
RC4=0;
RC4=1;
__delay_ms(200);

ch,Data,temp=0;
RC4=0;
RC4=1;
__delay_ms(200);

}
}

void HSerinit(void)
{
TRISC5 = 0;

//Pull up the inputs to High
// TX was an input!
SPBRG = 129;                    // 20Mhz xtal 9600 BAUD
TXSTA = 0x24;                    // TXEN and BRGH
RCSTA = 0x90;                    // SPEN and CREN
}

void Hserout(unsigned char Data)
{
while(!TXIF);
TXREG = Data;
}

Last edited:

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

My code suck here after while..i was testing the clock and data is input with pull ups of 4.7k using switch to gnd.
so, net voltage on uC pin is 2V but it should be 5V...and i don't understand why RC4 is not going high after while statement i have connected switch with gnd pressing it is also not doing any thing ,why?
Rich (BB code):
void main()
{

TRISC4=0;
TRISC0=1;
TRISC1=1;
HSerinit();
__delay_ms(150);
ch='a';
Hserout(ch);

while(1)
{
RC4=0;
RC4=1;
__delay_ms(200);
RC4=0;
unsigned char Data=0,temp;

while(RC1==0);  {   RC4=1;

__delay_ms(400);
RC4=0;

__delay_ms(400);
}

#### Attachments

• 55 KB Views: 11