Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interfacing PS2 keyboard with uC....

Status
Not open for further replies.

koolguy

Active Member
Hi,

I want to interface PS2 with pic16f877a i have seen my link in Google but i am suck with code how to do this...the ps2 has been tested in terminal window it was sending different ascii/hex number for different keys.
please tell for half duplex communication i.e. Device to Host.

i was trying using this..
Code:
 unsigned char Data=0,temp; 
    while(CLK==0);   //Wait for First Falling Edge
     
    for(char i=0;i<7;i++)                       
    {
 
        while(CLK==0); //Capture the next 7 bits of data 
        
        temp=RC0;
        temp<<=i;
        Data|=temp;
    }

For testing code i have connected two switch on board for clk and data rather than connecting ps2 it self..but the serial window does not show the data enter from switch as i want...at while it wait for clk to 0 for starting and after 0 it run for loop ....then the process go on unnecessary.


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.
 
You need to re-check the serial comms that PS2 uses. The data needs to be sampled on the edge of the clock signal, not all the time the clock is 1.

Your code is faulty as you are not handling BOTH edges of the clock pulse.

Like this;
wait for clock to go /
get a bit
wait for clock to go \ again
then repeat
:)
 
Read this PPT... http://www.exploringpic32.com/9.html

If you look at the last one "Capture" He is using capture to read a PS2 keyboard.

I know its for pic32, but it will still work for others with a little modification.
 
OK, i need one suggestion for making a up-down counter for led rate list board...
up to 3 digit. i will use 7447 to save uC port terminal.
with up switch down and reset.

if(sw1==1){
unsigned int counter;
counter =counter+1;
then

PORTB=counter; // 8 bit i.e. 2 digit will
for getting upper 4 bit of counter
what should i do, for getting it in another port?


Note: I am not using PS2 on;ly simple switch..
 
i have done i think it will work fine as in MPsim...


Code:
void main (void){//program for counter for rate list 
	
	TRISB=0X00;
	TRISC=0b11110000;
PORTB=0X00;
		PORTC=0X00;
	while(1){
		unsigned int count;
unsigned int count1;
		PORTB=count;
count1=count;
count1>>=8;//shift to get upper bit of int
PORTC=count1;
	
		if(RC7==1){ //incremment counter
__delay_ms(50);
			count=count+1;
			}
			
		if(RC6==1){//decrement counter 
__delay_ms(50);
			count=count-1;
			}
			
		if(RC5==1){ //reset to zero
__delay_ms(200);
			count=0;
			}
			
					}
}
 
HI,

I need some help....for making up counter...i am using upper bits of PRTB connected to 7447 i want to remove cont above >9 but the cond is not working as shown in code ...please tell why?

Code:
while(1){


PORTB=count;
	
		if((RD1==1)){   //incremment counter
__delay_ms(20);//debounce
RC5=0;
if(count<9){// this is not working why?? with cond it is working?????
			count++;
}
			}
 
Dear Mr Kakkar

Unfortunately, it seems that you have chosen the wrong career. Programming and all does not sit well with you. That is why you are battling with everything.

Kindly remember, later on in life, ETO will not always be here to bail you out when you need to provide answers to your employer when things you built/designed don't work properly. No ETO to ask for help....just the guy paying your huge salary looking at you wanting answers.

Rather do what I did. Accept the fact that there are certain things that I am useless at....acknowledge it and move on. Don't try to force yourself to do stuff that you are incapable of understanding......properly. Properly being the important thing here.

Anyone can just make something work........
 
OK, its working properly now............

Code:
	while(1){
RC5=1;

	RD3=1;
RD2=0;
count1=count%10;
count1<<=4;

PORTB=count1;
__delay_ms(4);
	RD3=0;
RD2=1;
PORTB=0x00;
count1=(count%100)/10;
count1<<=4;
PORTB=count1;
__delay_ms(4);
PORTB=0x00;





PORTB=count1;


	
		if((RD1==1)){ //incremment counter
__delay_ms(50);
RC5=0;

			count++;

			}
			
	
			
					}
}
 
Hi again,

I have solved the problem of counter but now i want to save the result after power off, i have used const but showing error because i am modifing it...please tell how to do this i have used internal E2PROM before can't we do anything else?
 
I think this eeprom_write(0x02, count1);

Code:
void main (void)
{
		unsigned char count;
 unsigned char count1;
const unsigned char count2=2;
	TRISB=0X00;
TRISC=0X00;
	TRISD=0b00000010;
PORTB=0X00;
		PORTD=0X00;
count=0;
	while(1){
RC5=1;

	RD3=1;
RD2=0;
count1=count%10;
count1<<=4;

PORTB=count1;
__delay_ms(4);
PORTB=0x00;
	RD3=0;
RD2=1;

count1=(count%100)/10;
count1<<=4;
PORTB=count1;
__delay_ms(4);
PORTB=0x00;
eeprom_write(0x02, count1);







	
		if((RD1==1)){ //incremment counter
__delay_ms(50);
RC5=0;

			count++;

			}
			
	
			
					}
}
 
Hi again,

This code is is for SPI in slave mode clk from PS2...WILL THIS BE OK?





Code:
void initialise_SPI(void);
unsigned char spi_r(void);


void main (){
unsigned char temp ;
init_io();
initialise_SPI();
while(1)
{temp=spi_r();
PORTD=temp;
}
} 
void init_io(void){ 
  TRSID=0X00;
TRISC = 0b00011000;   

}
void initialise_SPI(){ 
SSPSTAT=0b01000000;   
SSPCON=0b00100100;   
SSPEN = 1;
}

unsigned char spi_r(){
unsigned char temp;
while(!SSPIF);
temp=SSPBUF; 
return temp;
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top