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 CCS Technical Support

Understanding-Generating PS/2 keyboard signal with PIC

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



Joined: 03 Sep 2007
Posts: 7

View user's profile Send private message

Understanding-Generating PS/2 keyboard signal with PIC
PostPosted: Wed Sep 05, 2007 11:38 am     Reply with quote

Hello,

I need a help to project a circuit to running in paralell interface with a keyboard PS/2.

I read about PS/2 protocoll. I understood that the computer generates one clock for the keyboard. Its realy?

In pinage from PS/2 cable, i read clock signal, and my oscilloscope show AC signal, and not DC signal. Its correct?

I am usign a source code that I found here in CCS forum and google. But, its not works correct. Crying or Very sad

Please, help. I need generate keyboard commands in PIC to PC.


Lock my code:

Code:

#include <16F628A.h>
#use delay (clock=4000000)
#fuses INTRC_IO, NOWDT, NOPUT, NOBROWNOUT, NOMCLR, NOLVP, NOPROTECT

#define CLOCK PIN_A1
#define DATA  PIN_B1

#define KEYCLK CLOCK
#define KEYDATA DATA

 unsigned char convert_ascci_to_scancode(unsigned char c){

  unsigned char scancode;

       if (c == 'a' || c == 'A') {scancode = 0x1c;}
  else if (c == 'b' || c == 'B') {scancode = 0x32;}
  else if (c == 'c' || c == 'C') {scancode = 0x21;}
  else if (c == 'd' || c == 'D') {scancode = 0x23;}
  else if (c == 'e' || c == 'E') {scancode = 0x24;}
  else if (c == 'f' || c == 'F') {scancode = 0x2b;}
  else if (c == 'g' || c == 'G') {scancode = 0x34;}
  else if (c == 'h' || c == 'H') {scancode = 0x33;}
  else if (c == 'i' || c == 'I') {scancode = 0x43;}
  else if (c == 'j' || c == 'J') {scancode = 0x3b;}
  else if (c == 'k' || c == 'K') {scancode = 0x42;}
  else if (c == 'l' || c == 'L') {scancode = 0x4b;}
  else if (c == 'm' || c == 'M') {scancode = 0x3a;}
  else if (c == 'n' || c == 'N') {scancode = 0x31;}
  else if (c == 'o' || c == 'O') {scancode = 0x44;}
  else if (c == 'p' || c == 'P') {scancode = 0x4d;}
  else if (c == 'q' || c == 'Q') {scancode = 0x15;}
  else if (c == 'r' || c == 'R') {scancode = 0x2d;}
  else if (c == 's' || c == 'S') {scancode = 0x1b;}
  else if (c == 't' || c == 't') {scancode = 0x2c;}
  else if (c == 'u' || c == 'U') {scancode = 0x3c;}
  else if (c == 'v' || c == 'V') {scancode = 0x2a;}
  else if (c == 'w' || c == 'W') {scancode = 0x1d;}
  else if (c == 'x' || c == 'X') {scancode = 0x22;}
  else if (c == 'y' || c == 'Y') {scancode = 0x35;}
  else if (c == 'z' || c == 'Z') {scancode = 0x1a;}

  else if (c == '0') {scancode = 0x45;}
  else if (c == '1') {scancode = 0x16;}
  else if (c == '2') {scancode = 0x1e;}
  else if (c == '3') {scancode = 0x26;}
  else if (c == '4') {scancode = 0x25;}
  else if (c == '5') {scancode = 0x2e;}
  else if (c == '6') {scancode = 0x36;}
  else if (c == '7') {scancode = 0x3d;}
  else if (c == '8') {scancode = 0x3e;}
  else if (c == '9') {scancode = 0x46;}

  else if (c == '/') {scancode = 0x5a;} // ENTER
  else if (c == ' ') {scancode = 0x29;} // ESPACIO
  else if (c == '>') {scancode = 0x0d;} // TABULADOR
  else if (c == '<') {scancode = 0x66;} // BORRADOR
  else if (c == '$') {scancode = 0x58;} // TAG DE MAYUSCULA $m$yuscula
  else if (c == '-') {scancode = 0x4a;} // -_

  else {scancode = 0x29;} // Cualquier otra cosa ESPACIO

  return scancode;
}



void sendByte(byte b)
{
    byte a=0;
    byte p = 0;
    boolean t = 0;

   disable_interrupts(GLOBAL);
    output_low(KEYCLK);
    delay_us(80);
    output_low(KEYDATA);
    output_high(KEYCLK);
    input(KEYCLK);

    for(a=0; a<8; a++)
    {
        while(input(KEYCLK) == 1); //wait for 0
        t = bit_test(b,a);
        output_bit(KEYDATA, t);
        if(t)
            p++;
        while(input(KEYCLK) == 0); //wait for 1
    }

    while(input(KEYCLK) == 1); //wait for 0
    t = bit_test(p,0);
        t = ~t;
    while(input(KEYCLK) == 0); //wait for 1
     output_bit(KEYDATA, t);   // input(KEYDATA);
    while(input(KEYCLK) == 1); //wait for 0
     output_float(KEYDATA);
    enable_interrupts(GLOBAL);
}

main()
{

    int i=0;
    unsigned char k=0;


  set_tris_a(0b11111111);
  set_tris_b(0b00000000);

    output_b(0x00);
    input(CLOCK);

    delay_ms(100);

    ext_int_edge(H_to_L);
    enable_interrupts(INT_EXT);
    enable_interrupts(GLOBAL);

    while(1)
    {



    sendByte(convert_ascci_to_scancode('a'));
   delay_ms(500);

     sendByte(convert_ascci_to_scancode('B'));
   delay_ms(500);

     sendByte(convert_ascci_to_scancode('C'));
   delay_ms(500);

     sendByte(convert_ascci_to_scancode('d'));
   delay_ms(500);

    sendByte(convert_ascci_to_scancode('e'));
   delay_ms(500);



    delay_ms(2000);



    }
}

amcfall



Joined: 20 Oct 2005
Posts: 44

View user's profile Send private message

PostPosted: Thu Sep 06, 2007 8:55 am     Reply with quote

Not sure what you're doing here, need some more info. What does "a circuit to running in paralell interface with a keyboard PS/2 " mean?

The clock is generated by the keyboard if I remember correctly. If you are emulating a keyboard you will need to generate the clock. It looks like you're waiting for the clock but are trying to act as a keyboard.

I wrote my implementation (owned by the company I work for so I cannot post any of it) based mostly on this site:

http://www.computer-engineering.org/ps2protocol/

Avery
b101101011011011



Joined: 03 Sep 2007
Posts: 7

View user's profile Send private message

PostPosted: Thu Sep 06, 2007 7:37 pm     Reply with quote

Hello amcfall!

Thanks for look this topic.
For more informations i make a picture schematic.

See:
http://img160.imageshack.us/img160/9578/esquemabv1.jpg

My doubt is about CLOCK generation... I use a clock from keyboard? OR need to generate a PWM signal with 10 Khz from my PIC circuit?

Thanks amcfall.
Guest








PostPosted: Fri Sep 07, 2007 1:55 am     Reply with quote

The PS2 interface is a type of the synchron serial IF. Becouse of the muster - who want to send - must generate the synchron clock for associated data.

NOT PWM !

If I understand, you must send data from PIC OR PS2 on paralellised cable. If you push key in keyboard, the keyboard generate clock, if a PIC want sending anything, the PIC will generate the clock !!!
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Fri Sep 07, 2007 2:35 am     Reply with quote

Hi,

What if both PIC and keyboard send character...both will generate clock ??

arunb
Guest








PostPosted: Fri Sep 07, 2007 4:15 am     Reply with quote

I think the problem is a keyboard response in the clock-data bus.
The keyboar is not ready to a multi master enviroment.
If the PIC use the clock and data line (quasi bus) both the PC and the keybord is a receiver. But the keyboard will not recognise the event and make error. PIC will not recognise from where get it. From PC? or from Keyboard? The communication protocoll is ready for one PC and one kbd connection.
Guest








PostPosted: Fri Sep 07, 2007 4:20 am     Reply with quote

The solution:
KBD->PIC->PC

PIC receive KBD scan codes and transmit to PC (repeater), and if PIC must send anithing, it does too.
Ttelmah
Guest







PostPosted: Fri Sep 07, 2007 4:30 am     Reply with quote

The clock, and data lines, are 'open collector' drives. There are pull-up resistors, to bring the signals to 5v, when not driven, and a transistor to pull the signal down when a drive is needed. These need to be implemented, like the I2C bus (when software driven) on the PIC. What you do, is set the output latch register on the PIC pins, to '0', and then turn the TRIS register 'on', when you want to drive the signal low (enables the output driver), and 'off' when you want the signal to be pulled up. This way, the other device, can pull the signal 'down' at any time, and not cause damage. To do this, you will need to use fixed, or fast_io mode on these pins.
The slave device must monitor the lines before trying to send. The host will pull the clock low, to inhibit slave communication, before trying to send. The slave is required to check that the clock _is_ high, and stays high for at least 50uSec, before it is allowed to start sending. At _any_ time, the host can pull the clock low to abort the communication. Hence the slave _must_ check that the pin actually goes high, when it releases the clock on each bit. If the transmission is inhibited before 11 clcok bits of the packet have been sent, the slave ios required to send again latter.

Best Wishes
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