Author Topic: How does a keyboard work -- 8 bit ASCII?  (Read 6460 times)

0 Members and 1 Guest are viewing this topic.

Offline Endzone

  • Thread Starter
  • Posts: 31
How does a keyboard work -- 8 bit ASCII?
« on: Thu, 20 June 2013, 16:34:19 »
Most keyboards generate 7-bit or 8-bit ASCII code?  The logic levels are +5DVC and Ground?  How wide are the high's and lows?  Are they 10mS pulses, etc.?  Are there stop and start bits?  Does anyone have a simple diagram of how 7-bit or 8-bit ASCII code is generated when you press a particular key on your keyboard? 

« Last Edit: Thu, 20 June 2013, 16:40:29 by Endzone »

Offline alaricljs

  • I be WOT'ing all day...
  • ** Moderator Emeritus
  • Posts: 3715
  • Location: NE US
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #1 on: Thu, 20 June 2013, 17:54:36 »
Keyboards do not generate ASCII.  They send key codes which then need to be translated by the machine they are attached to.  This is why you can take a bog standard keyboard, change the key caps to those of a different language and it works as expected if you also change the language settings on the PC.
Filco w/ Imsto thick PBT
Ducky 1087XM PCB+Plate, w/ Matias "Quiet Click" spring-swapped w/ XM Greens

Offline Findecanor

  • Posts: 5042
  • Location: Koriko
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #2 on: Thu, 20 June 2013, 18:22:15 »
Yep. The mapping from keycodes to ASCII is done somewhere in the operating system. The word "scancode" is sometimes used interchangeably, and sometimes not.

There are different keyboard protocols over the cables.
The "AT" and "PS/2" protocols are the same, just different-sized connectors. It is a serial protocol (but not RS-232) where the keyboard sends different codes for a key when it is pressed and when it is released. Codes can also be one or multiple bytes long.

The USB keyboard protocol(s) is quite complex. First there is the USB protocol, then there is the "HID Keyboard" protocol on top of USB where the keyboard periodically sends a report telling which keys are pressed. The report contains an array of six keycodes and a 8-bit bitfield . Each USB keycode is one byte long. The keycode 0 means "no key". The bitfield tells which modifiers are pressed: Shift, Alt, Ctrl and "GUI" (Windows key) for the left and right side - that makes eight. (The Menu key is not a modifier)
The size of this array is why some keyboards are said to be limited to "6-key rollover" over USB. If a keyboard wants to report more pressed keys, then it will have to pull a trick: one way is to present itself to the PC as a USB hub with two or more keyboards attached. but that can be quite complicated.
There is also a special USB "keyboard boot protocol" used only during boot, which is needed for getting access to the BIOS, but many keyboards don't support that.
I have used the word "PC" here, but USB keyboards work the same way when connected to a Macintosh. Both the Windows key and the Command key are interpreted as "GUI" keys.

Another thing you should know is that the Caps Lock, Scroll Lock and Num Lock lamps are controlled by the PC -- not by the keyboard. The states of the lamps don't affect how the keyboard operates.
« Last Edit: Thu, 20 June 2013, 18:29:10 by Findecanor »
🍉

Offline Endzone

  • Thread Starter
  • Posts: 31
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #3 on: Thu, 20 June 2013, 23:29:31 »
Alright, let's start at a more basic level then.  Let's assume a PS/2 keyboard.  The keyboard sends the codes to the computer (serial date) and the computer determines what the 8-bit ASCII letter/number/symbol that is?  Let's start at the beginning.  Exactly what code is sent for the letter "A"?  How is the code generated by the keyboard? 


Offline alaricljs

  • I be WOT'ing all day...
  • ** Moderator Emeritus
  • Posts: 3715
  • Location: NE US
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #4 on: Thu, 20 June 2013, 23:44:42 »
Start here and move on to here
Filco w/ Imsto thick PBT
Ducky 1087XM PCB+Plate, w/ Matias "Quiet Click" spring-swapped w/ XM Greens

Offline dorkvader

  • Posts: 6288
  • Location: Boston area
  • all about the "hack" in "geekhack"
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #5 on: Thu, 20 June 2013, 23:47:37 »
Firstly, I believe some terminal keyboards do just output ASCII. Look for something with a bit-paired numberrow (not always)

Secondly, keyboard protocols are pretty complicated. Just looking at the graphs kbdbabel has, you can see quite some differences.
http://www.kbdbabel.org/signaling/index.html

I dont know MUCH about this, but the way I understand it is the (USB for example) keyboard sends one of the USB HID input scancodes (listings of them all are available online, I only know of the ones a soarer converter can output) and the OS captures it and outputs the character you want (or, in the case of linux, it outputs the letter you tell it to. This is important for xkbmaps and dvorak)

As for PS/2, I dont know exactly what is different, rather than it uses interrupts instead of polling (what USB does).

Computer protocols and connections are a major weak point in my knowledge, so I'm interested to learn more from the experts that will no doubt be posting their expertise later.

Offline daerid

  • Posts: 4276
  • Location: Denver, CO
    • Rossipedia
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #6 on: Thu, 20 June 2013, 23:54:22 »
As I understand it:

The keyswitches are in a matrix (rows and columns) hooked up to a microprocessor. When a key is pressed, that row/col is signaled. The processor checks on the state of each row/col every loop, and then determines which scan code to send. I'm not sure exactly what the actual numeric values of the scan codes are, but they are pretty standard across OSes. A "programmable" keyboard allows you to reassign what scan code is sent from the various matrix positions.

In a PS/2 keyboard, the scan code is actually sent to the computer as a hardware interrupt (what an interrupt is is outside the scope of this explanation). The OS gets notified that a key has been pressed or released, along with the scan code of the key and the state of the modifiers (up/down). I believe the OS then will translate that scan code into a standardized even of some sort that tells the current program that a key was pressed/released.

Offline Endzone

  • Thread Starter
  • Posts: 31
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #7 on: Fri, 21 June 2013, 01:18:14 »
As I understand it:

The keyswitches are in a matrix (rows and columns) hooked up to a microprocessor. When a key is pressed, that row/col is signaled. The processor checks on the state of each row/col every loop, and then determines which scan code to send. I'm not sure exactly what the actual numeric values of the scan codes are, but they are pretty standard across OSes. A "programmable" keyboard allows you to reassign what scan code is sent from the various matrix positions.

In a PS/2 keyboard, the scan code is actually sent to the computer as a hardware interrupt (what an interrupt is is outside the scope of this explanation). The OS gets notified that a key has been pressed or released, along with the scan code of the key and the state of the modifiers (up/down). I believe the OS then will translate that scan code into a standardized even of some sort that tells the current program that a key was pressed/released.

That's pretty much what I understand from reading on the internet tonight.  The first place to start would be to look at a schematic of the keyboard matrix and how all the lines are tied to the microprocessor or microcontroller inside the keyboard.  I haven't been able to find a schematic like that yet.  I know that by pressing a key, you take at least one of those matrix lines to ground from its normal +5VDC level.  I don't know about the other line.  But the combination of these 2 changes on the input lines to the microcontroller on the keyboard cause the microcontroller to generate the "scan code" which is a hexadecimal number like "1C" to be sent to the BIOS on your computer.  "1C" is 8 bits of date with one start bit, one parity bit and one stop bit.  Each block of data is 11 bits long.  That's as far as I have gotten tonight. 

http://retired.beyondlogic.org/keyboard/keybrd.htm

Offline alaricljs

  • I be WOT'ing all day...
  • ** Moderator Emeritus
  • Posts: 3715
  • Location: NE US
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #8 on: Fri, 21 June 2013, 01:24:45 »
So what is it you're trying to achieve?
Filco w/ Imsto thick PBT
Ducky 1087XM PCB+Plate, w/ Matias "Quiet Click" spring-swapped w/ XM Greens

Offline Endzone

  • Thread Starter
  • Posts: 31
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #9 on: Fri, 21 June 2013, 01:59:32 »
So what is it you're trying to achieve?

A good basic understanding of how a keyboard sends letters/characters/numbers/function to the main computer.  That is as far as I want to take it.  I'm a radio technician, and I only have a 2-year degree in electronics technology/communications option.  So, I'm not too big on microprocessors or microcontrollers. 

I just saw a very good (and simple) diagram of how the key matrix on a keyboard works and how the microcontroller uses two lines (one row and one matrix line) to send 2 lows to the microcontroller at the same time.  The microcontroller on the keyboard knows what letter those 2 lines are then sends a "scan code" or a hexadecimal code such as "1C" to the main computer to tell it what letter or what key was pressed.  I'll post the diagram and circuit explanation tomorrow.  It is 0200 local here, and I'm tired.   

Offline Endzone

  • Thread Starter
  • Posts: 31
Re: How does a keyboard work -- 8 bit ASCII?
« Reply #10 on: Sat, 22 June 2013, 00:29:22 »
The diagram below is a 16 key keyboard.  From what I understand, the microcontroller is constantly clocking all the rows to logical lows at a high frequency one after another sequentially.  Each line of the output port (D0, D1, D2, D3) will go from a +5 volts to ground hundreds or thousands of times/second.  Say you press the "D" key.  When the row (D0 of the output port) goes low, this will also put a ground or low on D2 of the input port.  The microcontroller now knows that row D0 is low and also column D2 is low.  This tells the microcontroller to fetch the hexadecimal code for the D key.  This is going to be 8 bits of information--highs and lows.  This is also called the "Scan Code" and it is output on pin 3 of a PS/2 connector to the computer. 




But it isn't just 8 bits of information.  There is a start bit on the next negative going clock pulse, then the 8 bits of data, then a parity check bit, and then a stop bit.  So each letter transmits 11 bits of serial date out of pin 3 on the PS2 connector to the computer. 




Here is the technical explanation from the author.




If I have anything obviously wrong please correct me.  Next step is to see what the computer does with the scan code. 

Here are 2 good links where I got this info.

http://nptel.iitm.ac.in/courses/Webcourse-contents/IISc-BANG/Microprocessors%20and%20Microcontrollers/pdf/Teacher_Slides/mod3/M3L7.pdf

http://retired.beyondlogic.org/keyboard/keybrd.htm