CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

About ps2 keyboard emulator

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
thurein



Joined: 15 Sep 2016
Posts: 4

View user's profile Send private message

About ps2 keyboard emulator
PostPosted: Thu Sep 15, 2016 5:03 am     Reply with quote

I'm newbie in ccs pic c and pic micro-controllers.
I search in this forum and google about ps2 keyboard emulators.
But no code work well.
Can someone help me with working code for 16F628A and ccs c.

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Thu Sep 15, 2016 6:57 am     Reply with quote

This worked fine:

<http://www.ccsinfo.com/forum/viewtopic.php?t=25673&highlight=ps2>

If it doesn't, you have a problem with your hardware.
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Sep 15, 2016 7:13 am     Reply with quote

Well there's 2 different 'emulators'...

The one Mr T points to goes between a PS2 kbd and 'something else'.

The ones I did go between a custom keyboard switch array and send PS2 formatted data to a PC.

You need to tell us more of what you really need to do.

Jay
thurein



Joined: 15 Sep 2016
Posts: 4

View user's profile Send private message

PostPosted: Thu Sep 15, 2016 8:31 am     Reply with quote

Thanks for your reply.

My project is read data from IR Remote and send these to computer as PS2 keyboard.
Get from IR is ok, but can't send to PC.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Sep 15, 2016 4:53 pm     Reply with quote

what does your circuit hardware look like ?
your working code ?
and what compiler version?
thurein



Joined: 15 Sep 2016
Posts: 4

View user's profile Send private message

PostPosted: Fri Sep 16, 2016 2:13 am     Reply with quote

Circuit is very simple.
Pin B0 is connect to PS2 Clock line and B1 is to Data Line.
Compiler version is 5.015 and I use following code I found in this post

http://www.ccsinfo.com/forum/viewtopic.php?t=27544

guntor wrote:
Maybe someone need a PS2 Keyboard emulator

Code:

#include <12F675.h>   
                       
#fuses INTRC_IO,NOWDT,NOMCLR,NOPROTECT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8,STREAM=DEBUG)
#use rs232(baud=9600,parity=N,xmit=PIN_A3,rcv=PIN_A2,bits=8,STREAM=RFID)
                                                                                                                                                                                                 
//#define DEBUGLONG
//#define DEBUGSHORT

#define KEYCLK PIN_A5
#define KEYDATA  PIN_A4     

typedef unsigned char uchar;
typedef boolean bit;
/*
                                                  -----                                                                   
                                      [+5V] VDD -|1   8|- VSS [GND]
   [KEYCLK] (with Pullup) GP5/T1CKI/OSC1/CLKIN  -|2   7|- GP0/AN0/CIN+/ICSPDAT [XMIT DEBUG]                               
[KEYDATA] (with Pullup) GP4/AN3/T1G/OSC2/CLKOUT -|3   6|- GP1/AN1/CIN-/VREF/ICSPCLK [RCV DEBUG]
                                   GP3/MCLR/VPP -|4   5|- GP2/AN2/T0CKI/INT/COU
                                                  -----   
*/                                                                                                                       
       

// A-Z, völlig willkürliche anordnung...
// http://www.computer-engineering.org/ps2keyboard/scancodes2.html
// Für break Code: vorher 0xF0 senden, dann wieder Code
const uchar t_a_z[26]={0x1C,0x32,0x21,0x23,0x24,0x2B,0x34,0x33,0x43,0x3B,0x42,0x4B,0x3A,0x31,0x44,0x4D,0x15,0x2D,0x1B,0x2C,0x3C,0x2A,0x1D,0x22,0x35,0x1A};

// 0-9, völlig willkürliche anordnung...
const uchar t_0_9[26]={0x45,0x16,0x1E,0x26,0x25,0x2E,0x36,0x3D,0x3E,0x46};

// ASCII translatedtoScantable
uchar atrsc[2];
uchar lastbyte;   


void sendNibble(boolean t);
bit getNibble();
void sendByte(byte sendbyte);
uchar receiveByte();
void senduchar(uchar sendb) ;
void translateASCIItoScanTable(uchar ASCII,uchar extended);
void sendString(uchar* str);                                                       

           
 
// SetBit: Einzelnes Bit senden...
//
//  ....        .........        ........
//  |  |...,....|       |........|      |........
//      10 w 30    40
//
void sendBit(boolean t){
   delay_us(10);//15
   output_bit(KEYDATA,t);
   delay_us(30);    //20             
   output_low(KEYCLK);
   delay_us(40);        //40             
   output_high(KEYCLK);
}

// getBit: Einzelnes Bit empfangen...
// 
//  ....        .........        ........
//  |  |........|   ,   |........|      |........
//         40    15 r 20
bit getBit(){
   bit t;
   output_low(KEYCLK);
   delay_us(40);
   output_high(KEYCLK);
   delay_us(15);
   t=input(KEYDATA);
   delay_us(20);

   return t;                                               
}

void sendByte(byte sendbyte){
                        
   uchar i;
   byte parity = 0;                             
   bit t = 0;   
   // ausgangslage
   output_high(KEYCLK);
   output_low(KEYDATA);
   
   // Delay, falks mehrere Bytes hintereinander gesendet werden...
   delay_us(250);
   
   // startbit
   output_high(KEYCLK);
   sendBit(0);

   for(i=0;i<8;i++){
      t=bit_test(sendbyte,i);       
      if(t)parity++;                                                     
      sendBit(t);
   }
   
   // Parity Bit
   t=~bit_test(parity,0);
   sendBit(t);
   
   // Stop bit
   sendBit(0);                 
   
   lastbyte=sendbyte;
   
   input(KEYCLK);
   input(KEYDATA);
}

uchar receiveByte(){
   
   uchar empf;                                                                                                                                 
   uchar parity;
   uchar i;                                                                                                                                   
   bit t;
   bit stopbit;                                                                                                                   
   // ignore startbit???
   // Generate Clock and receive bits.. LSB first...
   for(i=7;i!=0;i--){
      t=getBit();
      if(t){
         parity++;
         empf|=t<<i;
      }
   }
   // parity bit...
   t=getBit();
   
   // stop bit...
   stopbit=getBit();
   if(stopbit!=1){     
      #ifdef  DEBUGLONG
         fprintf(DEBUG,"Falsches Stopbit");
      #endif
      #ifdef  DEBUGSHORT
         fprintf(DEBUG,"fsb");
      #endif               
   }           
                  
   // ACK senden...
   delay_us(10);
   output_low(KEYDATA);
   delay_us(25);
   output_low(KEYCLK);
   delay_us(40);
   output_high(KEYCLK);
                    
   if(t!=~bit_test(parity,0)){     
      #ifdef  DEBUGLONG
         fprintf(DEBUG,"Haha, partity falsch");
      #endif
      #ifdef  DEBUGSHORT
         fprintf(DEBUG,"fp");
      #endif               
   }               
   #ifdef  DEBUGLONG                   
      fprintf(DEBUG,"empfanges byte %x",empf);
   #endif
   #ifdef  DEBUGSHORT
      fprintf(DEBUG,"eb %x",empf);
   #endif                                         
   return empf;     
}                     

                                       
                                                                             
void senduchar(uchar sendb) {
   uchar empf;

   unsigned int16 breaker;                 
   
   disable_interrupts(GLOBAL);


//Summary: Bus States
//Data = high, Clock = high:  Idle state.
//Data = high, Clock = low:  Communication Inhibited.
//Data = low, Clock = high:  Host Request-to-Send
                         
   while(true){
      // Host will was...
      if(input(KEYCLK)==0 && input(KEYDATA)==1){
         #ifdef  DEBUGLONG
            fprintf(DEBUG,"dammed, not free");
         #endif
         #ifdef  DEBUGSHORT
            fprintf(DEBUG,"nf");
         #endif         
         // 50ms zeit, um Data low zu ziehen und Clock auf high
         // Dann dürfen wir Clock spielen und nibbles empfangen...
         breaker=0;while(input(KEYDATA)==1&&input(KEYCLK)==0&&breaker<0xFFFFF)breaker++; // Wait for 0
         breaker=0;while(input(KEYCLK)==0&&breaker<0xFFFFF)breaker++; // Wait for 0
         #ifdef  DEBUGLONG
            fprintf(DEBUG,"ready to receive");
         #endif
         #ifdef  DEBUGSHORT
            fprintf(DEBUG,"rr");
         #endif
         
         // receive Byte
         empf=receiveByte();
         
         #ifdef  DEBUGLONG
            fprintf(DEBUG,"empfanges byte %x",empf);
         #endif
         #ifdef  DEBUGSHORT             
            fprintf(DEBUG,"eb %x",empf);
         #endif
         
         // Reset Keyboard (Too fast, false parity etc...)
         if(empf==0xFF){
            // Acknowledge                       
            sendbyte(0xFA);
                                 
            // BAT erfolgreich...                                                                                 
            sendbyte(0xAA);
            
            #ifdef  DEBUGLONG
               fprintf(DEBUG,"Bytes sent.");
            #endif
            #ifdef  DEBUGSHORT
               fprintf(DEBUG,"bs");
            #endif
         
         // Resend last byte...
         }else if(empf==0xFE){
              sendbyte(lastbyte);
         }     

      // Wenn Frei (CLK = HIGH),
      // Schauen, ob nach 50us immer noch frei, dann frei zur verwendung...
      }else{                                                                             
         delay_us(50);
         if(input(KEYCLK)==1){
            break;
         }                                                               
      }
   }                                 
   #ifdef  DEBUGLONG
         fprintf(DEBUG,"yeah, free");
   #endif
   #ifdef  DEBUGSHORT
         fprintf(DEBUG,"f");
   #endif                 
   
   // Todo: special caracters   
   translateASCIItoScanTable(sendb,0x00);
   
   // Todo: uppercase
   if(atrsc[1]!=0x00){
       sendbyte(atrsc[1]);
   }
   // Send Make Key...
   sendbyte(atrsc[0]);
   
   // Ssend Break Key...
   sendbyte(0xF0);
   sendbyte(atrsc[0]);   
   
   if(atrsc[1]!=0x00){
      sendbyte(0xF0);                                             
      sendbyte(atrsc[1]); 
   }
   
   // Not too many keys per second
   delay_ms(20);
                                       
   enable_interrupts(GLOBAL);
}           

void translateASCIItoScanTable(uchar ASCII,uchar extended){
   if(ASCII>='A' && ASCII<='Z'){
      atrsc[0]=t_a_z[ASCII-'A'];
      atrsc[1]=0x12;
       
   }else if(ASCII>='a' && ASCII<='z'){
      atrsc[0]=t_a_z[ASCII-'a'];
      atrsc[1]=0x00;                                                     
        
   }else if(ASCII>='0' && ASCII<='9'){
      atrsc[0]=t_0_9[ASCII-'0'];
      atrsc[1]=0x00;
   
   // Rest of Translation Table
   // layout spefific!!
   // layout: http://upload.wikimedia.org/wikipedia/commons/2/22/KB_US-International.svg
   // http://www.computer-engineering.org/ps2keyboard/scancodes2.html
   }else if(ASCII>=' ' && ASCII<=' '){                                     
      //atrsc[0]=t_a_z[ASCII-'A'];                                                                                           
      atrsc[0]=0x29;               
      atrsc[1]=0x00;
                   
   }
}
     
void sendString(uchar* str){
   while(*str!=0x00){
      senduchar(*str);
      str++;                                                                                                                             
   }                                                                                                                                 
}

void main() {

   while(TRUE){                           
      strcpy(RFID_rec.data,"ABCDE532d"); 
      sendString(RFID_rec.data);                                                                                           
      delay_ms(2500);
   }
                                  
}       
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 16, 2016 10:16 am     Reply with quote

Quote:
Circuit is very simple.
Pin B0 is connect to PS2 Clock line and B1 is to Data Line.

The program you posted uses Pins A5 and A4 for clock and data.
You need to modify the program as shown below. Then recompile it
and program it into your PIC:
Code:
#define KEYCLK PIN_B0
#define KEYDATA  PIN_B1     
thurein



Joined: 15 Sep 2016
Posts: 4

View user's profile Send private message

PostPosted: Fri Sep 16, 2016 11:23 pm     Reply with quote

PCM programmer wrote:
Quote:
Circuit is very simple.
Pin B0 is connect to PS2 Clock line and B1 is to Data Line.

The program you posted uses Pins A5 and A4 for clock and data.
You need to modify the program as shown below. Then recompile it
and program it into your PIC:
Code:
#define KEYCLK PIN_B0
#define KEYDATA  PIN_B1     


I already change PINs.
Now I can send keyboard codes from PS2 to USB controller.
But when I send one code sometime others characters are appear in Notepad . Sometime repeated characters.
I can't send key codes directly to PS2 port of computer.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group