libreria de ps2 para teclado

6
PS/2 Library El mikroC PRO for PIC proporciona una biblioteca para la comunicación con el mercado común teclado PS / 2. Importante: La biblioteca no utiliza las interrupciones para la recuperación de datos, y requiere que el reloj del oscilador a ser por lo menos 6MHz. Los pasadores al que está unido un teclado PS / 2 debe ser conectado a las resistencias pull-up. A pesar de PS / 2 es un bus de comunicación de dos vías, esta biblioteca no proporciona MCU a teclado de comunicación, por ejemplo, pulsando la tecla Caps Lock no se enciende el LED de bloqueo de mayúsculas. External dependencies of PS/2 Library The following variables must be defined in all projects using PS/2 Library: Description : Example : extern sfr sbit PS2_Data; PS/2 Data line. sbit PS2_Data at RC0_bit; extern sfr sbit PS2_Clock; PS/2 Clock line. sbit PS2_Clock at RC1_bit; extern sfr sbit PS2_Data_Direction; Direction of the PS/2 Data pin. sbit PS2_Data_Direction at TRISC0_bit; extern sfr sbit PS2_Clock_Direction; Direction of the PS/2 Clock pin. sbit PS2_Clock_Direction at TRISC1_bit; Library Routines Ps2_Config Ps2_Key_Read Ps2_Config

Upload: felipe-santiago-rojas-gutierrez

Post on 18-Apr-2015

204 views

Category:

Documents


2 download

DESCRIPTION

codigos para la programacion con pic

TRANSCRIPT

Page 1: Libreria de Ps2 Para Teclado

PS/2 Library

El mikroC PRO for PIC proporciona una biblioteca para la comunicación con el mercado común teclado PS / 2.

Importante:La biblioteca no utiliza las interrupciones para la recuperación de datos, y requiere que el reloj del oscilador a ser por lo menos 6MHz.Los pasadores al que está unido un teclado PS / 2 debe ser conectado a las resistencias pull-up.A pesar de PS / 2 es un bus de comunicación de dos vías, esta biblioteca no proporciona MCU a teclado de comunicación, por ejemplo, pulsando la tecla Caps Lock no se enciende el LED de bloqueo de mayúsculas.

External dependencies of PS/2 Library

The following variables must be defined in all projects

using PS/2 Library:Description : Example :

extern sfr sbit PS2_Data; PS/2 Data line.sbit PS2_Data at RC0_bit;

extern sfr sbit PS2_Clock; PS/2 Clock line.

sbit PS2_Clock at RC1_bit;

extern sfr sbit PS2_Data_Direction;

Direction of the PS/2 Data pin.

sbit PS2_Data_Direction at TRISC0_bit;

extern sfr sbit PS2_Clock_Direction;

Direction of the PS/2 Clock pin.

sbit PS2_Clock_Direction at TRISC1_bit;

Library Routines

Ps2_Config Ps2_Key_Read

Ps2_Config

Prototype void Ps2_Config();Returns Nothing.

Description Initializes the MCU for work with the PS/2 keyboard.Requires Global variables :

PS2_Data: Data signal line PS2_Clock: Clock signal line in PS2_Data_Direction: Direction of the Data pin PS2_Clock_Direction: Direction of the Clock pin

Page 2: Libreria de Ps2 Para Teclado

must be defined before using this function.Example sbit PS2_Data at RC0_bit;

sbit PS2_Clock at RC1_bit;sbit PS2_Data_Direction at TRISC0_bit;sbit PS2_Clock_Direction at TRISC1_bit;...Ps2_Config(); // Init PS/2 Keyboard

Ps2_Key_Read

Prototype unsigned short Ps2_Key_Read(unsigned short *value, unsigned short *special, unsigned short *pressed);

Returns 1 si la lectura de una tecla del teclado se ha realizado correctamente 0 si no ha pulsado la tecla

Description La función recupera información sobre la tecla pulsada.Parámetros:• Valor: contiene el valor de la tecla pulsada. Para los caracteres, números, signos de puntuación, y el valor del espacio va a almacenar el código ASCII correspondiente. Rutina "reconoce" la función de desplazamiento y de bloqueo de mayúsculas, y se comporta adecuadamente. Para ver las teclas de funciones especiales Teclas de Función Especial tabla.• especial: es una bandera para las teclas de funciones especiales (F1, Enter, Esc, etc.) Si tecla pulsada es una de ellas, se hará especial establecido en 1, en caso contrario 0.pulsado: se establece en 1, si se pulsa la tecla, y 0 si es liberado.

Requires PS / 2 para teclado tiene que ser inicializado. Ver la rutina Ps2_Config.Example unsigned short keydata = 0, special = 0, down = 0;

...// Press Enter to continue:do { if (Ps2_Key_Read(&keydata, &special, &down)) { if (down && (keydata == 16)) break; }} while (1);

Special Function Keys

Key Value returnedF1 1F2 2

Page 3: Libreria de Ps2 Para Teclado

Key Value returnedF3 3F4 4F5 5F6 6F7 7F8 8F9 9F10 10F11 11F12 12Enter 13Page Up 14Page Down 15Backspace 16Insert 17Delete 18Windows 19Ctrl 20Shift 21Alt 22Print Screen 23Pause 24Caps Lock 25End 26Home 27Scroll Lock 28Num Lock 29Left Arrow 30Right Arrow 31Up Arrow 32Down Arrow 33Escape 34Tab 35

Library Example

This simple example reads values of the pressed keys on the PS/2 keyboard and sends them via UART.

unsigned short keydata = 0, special = 0, down = 0; sbit PS2_Data at RC0_bit;

Page 4: Libreria de Ps2 Para Teclado

sbit PS2_Clock at RC1_bit;sbit PS2_Data_Direction at TRISC0_bit;sbit PS2_Clock_Direction at TRISC1_bit;

void main() {

ANSEL = 0; // Configure AN pins as digital I/O ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; UART1_Init(19200); // Initialize UART module at 19200 bps Ps2_Config(); // Init PS/2 Keyboard Delay_ms(100); // Wait for keyboard to finish UART1_Write_Text("Ready"); UART1_Write(10); // Line Feed UART1_Write(13); // Carriage return

do { if (Ps2_Key_Read(&keydata, &special, &down)) { if (down && (keydata == 16)) { // Backspace UART1_Write(0x08); } else if (down && (keydata == 13)) { // Enter UART1_Write('r'); // send carriage return to usart terminal //Usart_Write('n'); // uncomment this line if usart terminal also expects line feed // for new line transition } else if (down && !special && keydata) { UART1_Write(keydata); } } Delay_ms(1); // debounce } while (1);}

Page 5: Libreria de Ps2 Para Teclado