PS/2 Keybard sending data to the keyboard.

Board: Arduino uno.

Sending a command to the keyboard does not work correctly.
When I send the 0xEE command, I get a 0xFE response.
The keyboard interprets the sent command 0xFE as 0xF4 (disable input).
What’s my mistake?

volatile uint8_t q = 1, w;

volatile uint8_t output_command  = 0;
uint8_t party_bit_keyboard = 0;
bool led_command = false;

void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);
  digitalWrite(2, HIGH);
  attachInterrupt(1, psKeyboard, FALLING);
}

//Клавиша NumPad 0x77

void loop() {
  delay(5000);
  //q = 1;
  
  //q = 0;
  //w = 0;
  //Serial.println("Send command");
  //OutputData(0xFF);
}
void psKeyboard(){
  if (q > 1 && q < 11) w |= digitalRead(2) << q - 2;
  if (++q > 11){
    //Serial.print("Key: ");
    Serial.println(w, HEX);
    if (w == 0x69) digitalWrite(13, HIGH);
    if (w == 0x72) digitalWrite(13, LOW);
    if (w == 0x7B){q = 0; w = 0; OutputData(0xEE); return;};
    if (w == 0x7C){q = 0; w = 0; led_command = true; OutputData(0xFE); return;};
    if (w == 0xFA){
      Serial.println("Command accepted command");
      if (led_command)
        led_command = false;
        q = 0; w = 0; OutputData(0x1); return;
      }
    q = 1; w = 0;
  }
}
void OutputData(uint8_t command){
  detachInterrupt(1);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  q = 0;
  output_command = command;
  digitalWrite(3, HIGH);
  
  attachInterrupt(1, KeypadOut, FALLING);
}
void KeypadOut()
{
  //Serial.print("Out: ");
  if (q == 0){
    digitalWrite(2, LOW);}
    
  else if (1 <= q && q <= 8){
    if (output_command & (1 << q-1))
    {
      digitalWrite(2, HIGH);
      party_bit_keyboard++;
    }
    else
    {
      digitalWrite(2, LOW);
    }
  }
  
  else if (1 == 9){
    if (party_bit_keyboard & (1 << 0))
      digitalWrite(2, HIGH);
    else
      digitalWrite(2, LOW);
  }
  else if (q == 10){
     digitalWrite(2, HIGH);}
  else if (q == 11){
     party_bit_keyboard = 0;
     output_command = 0;
     digitalWrite(2, LOW);
     pinMode(2, INPUT);
     q = 1;
     w = 0;
     attachInterrupt(1, psKeyboard, FALLING);
  }
  q++;
}

I need to turn on certain LEDs.

Found a bug.
Final code:

volatile uint8_t q = 1, w, mode = 0;

volatile uint8_t output_command  = 0;
uint8_t party_bit_keyboard = 0;
bool led_command = false;
uint8_t led_data = 1;

void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);
  digitalWrite(2, HIGH);
  attachInterrupt(1, KeyboardInteruts, FALLING);
}

//Клавиша NumPad 0x77

void loop() {
  delay(5000);
  //q = 1;
  
  //q = 0;
  //w = 0;
  //Serial.println("Send command");
  //OutputData(0xED);
}
void KeyboardInteruts()
{
  if (mode == 0)
  {
    psKeyboard();
  }
  else
  {
    KeypadOut();
  }
}

void psKeyboard(){
  if (q > 1 && q < 11) w |= digitalRead(2) << q - 2;
  if (++q > 11){
    //Serial.print("Key: ");
    Serial.println(w, HEX);

    if (w == 0x69) digitalWrite(13, HIGH);
    if (w == 0x72) digitalWrite(13, LOW);
    if (w == 0x7B) {OutputData(0xEE); return;}
    if (w == 0x6c){q = 0; w = 0; led_command = true; led_data = 0b10; OutputData(0xED); return;};
    if (w == 0x75){q = 0; w = 0; led_command = true; led_data = 0b100; OutputData(0xED); return;};
    if (w == 0x7D){q = 0; w = 0; led_command = true; led_data = 0b1; OutputData(0xED); return;};
    if (w == 0xFA){
      Serial.println("Command accepted command");
      if (led_command)
        led_command = false;
        q = 0; w = 0; OutputData(led_data); return;
      }
    q = 1; w = 0;
  }
}
void OutputData(uint8_t command){
  //detachInterrupt(1);
  noInterrupts();
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  q = 0;
  output_command = command;
  digitalWrite(3, HIGH);

  mode = 1;
  interrupts();
  //attachInterrupt(1, KeypadOut, FALLING);
}
void KeypadOut()
{
  //Serial.print("Out: ");
  //if (q == 0){
    //digitalWrite(2, LOW);}
    
  if (0 <= q && q <= 7){
    if (output_command & (1 << q))
    {
      digitalWrite(2, HIGH);
      party_bit_keyboard++;
    }
    else
    {
      digitalWrite(2, LOW);
    }
  }
  
  else if (1 == 8){
    if (party_bit_keyboard & (1 << 0))
      digitalWrite(2, HIGH);
    else
      digitalWrite(2, LOW);
  }
  else if (q == 9){
     digitalWrite(2, HIGH);}
  else if (q == 10){
  }
  else if (q == 11){
     pinMode(2, INPUT);
     party_bit_keyboard = 0;
     output_command = 0;
     q = 1;
     w = 0;
     mode = 0;
  }
  q++;
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.