|
|
View previous topic :: View next topic |
Author |
Message |
moacirmn
Joined: 14 Jan 2006 Posts: 1
|
As to connect pic to the connector mini din (ps/2) |
Posted: Sun Jan 15, 2006 1:15 pm |
|
|
have a work that necessary to connect mine pic16f874 to one mouse ps/2... looking at the archives here of f�rum I found good materials however all the tentivas had been frustrated. First pq I program in C and the materials are in assembly and second pq I do not know in which bolts to connect the connector mini din (ps/2) to pic. Good then I decided to postar here in f�rum my doubts that are in which bolts of pic must connect the bolt of data and of clock of mouse and that it forms I can be sending given and reading the data received from mouse. I thank since already the patience and the aid of the colleagues |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Sun Jan 15, 2006 10:38 pm |
|
|
I have never interfaced a PS/2 mouse to a PIC (I used RS-232 instead), but here's a page which might shed some light on your problem. http://pinouts.ru/data/PS2Mouse_pinout.shtml
Good Luck! |
|
|
JohnLeung
Joined: 18 May 2004 Posts: 15
|
PS/2 keypad driver with buffer |
Posted: Mon Jan 16, 2006 7:43 pm |
|
|
I have a PS/2 keypad driver along with an example to work with. Not sure if it works for PS/2 mouse, hope it will be helpful for you / somebody.
For more detailed description and PS/2 keypad-related materials, pls refer to my web page http://www.TechToys.com.hk under Downloads section.
Code: |
/*
**************************************************************************************************
* Embedded Systems Building Blocks
* PS/2 Keyboard 101/104 keys driver
*
* Original obtained from www.picant.com, C2C complier web site
* by Julio Cesar Silva Briano, modified by John Leung
*
* Filenmae : ps2key.h
* Programmer : John Leung, www.TechToys.com.hk
* Version : Version 1.1 - 17th June 2004
* Remarks : Consult www.beyong.org for PS/2 keyboard protocol
**************************************************************************************************
*
// DESCRIPTION
// The keyboard is assumed to be a standard PS/2 104 keyboard, or NumPad
/************************************************************************************************/
/* PORT configuration
* Hardware pin map:
* B0 CLOCK signal from PS/2, interrupt on falling edge.
* A4 DATA signal from PS/2.
*/
/************************************************************************************************/
#byte TRISA = 0x85
#bit TRISA4 = TRISA.4
#byte TRISB = 0x86
#bit TRISB0 = TRISB.0
#define PS2Key_Clk PIN_B0 /* The pin address of the CLK signal (input) */
#define PS2Key_ClkTris TRISB0 /* The pin direction of the CLK signal (1=input) */
#define PS2Key_Data PIN_A4 /* The pin address of the DATA signal (input) */
#define PS2Key_DataTris TRISA4 /* The pin direction of the DATA signal (1=input) */
/*
**************************************************************************************************
* CONSTANTS *
**************************************************************************************************
*/
#define PS2Key_BUF_SIZE 10 /* Size of PS2 keyboard buffer */
#define PS2Key_START 1 /* Start bit for PS/2 protocol */
#define PS2Key_PARITY 10 /* Parity bit for PS/2 protocol */
#define PS2Key_STOP 11 /* Stop bit for PS/2 protocol */
/*
**************************************************************************************************
* FUNCTION PROTOTYPES *
**************************************************************************************************
*/
INT8U PS2KeyGetKey(void); /* Get a PS/2 scan code from driver if one is present, -1 else */
void PS2SendComm(INT8U cmd); /* (not finished) Send a one byte command to PS/2 keyboard */
BOOLEAN PS2KeyHit(void); /* See if a key has been pressed (TRUE if so, FALSE if not) */
void PS2KeyInit(void); /* Initialize the PS2 Keyboard handler */
/*
**************************************************************************************************
* FUNCTION PROTOTYPES
* HARDWARE SPECIFIC
**************************************************************************************************
*/
void PS2KeyIntHandler( void ); /* Function for B0 INT, edge feom HIGH to LOW */
|
Code: |
/*
************************************************************************************************************
* Embedded Systems Building Blocks
* PS/2 Keyboard 101/104 keys driver
*
* Original obtained from www.picant.com, C2C complier web site
* by Julio Cesar Silva Briano, modified by John Leung
*
* Filenmae : ps2ScanCode.h
* Programmer : John Leung
* Version : Version 1.1 - 17th June 2004
* Remarks : Consult www.beyong.org for PS/2 keyboard protocol
************************************************************************************************************
* DESCRIPTION
* Three lookup tables PS2KeyUnshifted[], PS2KeyShifted[], and PS2KeyNumPad[] map PS2Key_Code to ASCII chars
* If it is going to be the Numpad ONLY, PS2KeyUnshifted[] and PS2KeyShifted[] can be undefined to save ROM
* PS2KeyNumPad[] provided for ASCII character map for Num pad, should choose % #define PS2_NUM_PAD_SCH1/2
* Application must declare PS2ScanCode.h if ASCII should be returned
************************************************************************************************************
*/
/*
************************************************************************************************************
SCAN CODE TABLE
************************************************************************************************************
*/
/*
*****************************************************
* *
* No-Shift - Lookup Table when Shift not hold *
* *
*****************************************************
*/
//Uncomment the following #define if a full 104 keyboard ASCII map is included
//#define PS2_FULL_104KEY
#ifdef PS2_FULL_104KEY
const INT8U PS2KeyUnshifted[]={
0x00, //00
0x00, //01 F9
0x00, //02
0x00, //03 F5
0x00, //04 F3
0x00, //05 F1
0x00, //06 F2
0x00, //07 F12
0x00, //08
0x00, //09 F10
0x00, //0A F8
0x00, //0B F6
0x00, //0C F4
0x09, //0D TAB
'`', //0E ` or ~
0x00, //0F
0x00, //10
0x00, //11 Left ALT
0x00, //12 Left SHIFT
0x00, //13
0x00, //14 Left Ctrl
'q', //15 Q
'1', //16 1 or !
0x00, //17
0x00, //18
0x00, //19
'z', //1A Z
's', //1B S
'a', //1C A
'w', //1D W
'2', //1E 2 or @
0x00, //1F
0x00, //20
'c', //21 C
'x', //22 X
'd', //23 D
'e', //24 E
'4', //25 4 or $
'3', //26 3 or #
0x00, //27
0x00, //28
' ', //29 Space
'v', //2A V
'f', //2B F
't', //2C T
'r', //2D R
'5', //2E 5 or %
0x00, //2F
0x00, //30
'n', //31 N
'b', //32 B
'h', //33 H
'g', //34 G
'y', //35 Y
'6', //36 6 or ^
0x00, //37
0x00, //38
0x00, //39
'm', //3A M
'j', //3B J
'u', //3C U
'7', //3D 7 or &
'8', //3E 8 or *
0x00, //3F
0x00, //40
',', //41 , or <
'k', //42 K
'i', //43 I
'o', //44 O
'0', //45 0 or )
'9', //46 9 or (
0x00, //47
0x00, //48
'.', //49 . or >
'/', //4A / or ?
'l', //4B L
';', //4C ; or :
'p', //4D P
'-', //4E - or _
0x00, //4F
0x00, //50
0x00, //51
0x27, //52 ' or "
0x00, //53
'[', //54 [ or {
'=', //55 = or +
0x00, //56
0x00, //57
0x00, //58 Caps Lock
0x00, //59 Right Shift
0x0D, //5A Enter
']', //5B ] or }
0x00, //5C
'\'', //5D \ or |
0x00, //5E
0x00, //5F
0x00, //60
0x00, //61
0x00, //62
0x00, //63
0x00, //64
0x00, //65
0x08, //66 Backspace
0x00, //67
0x00, //68
'1', //69 NUM - 1 or END
0x00, //6A
'4', //6B NUM - 4 or LEFT
'7', //6C NUM - 7 or HOME
0x00, //6D
0x00, //6E
0x00, //6F
'0', //70 NUM - 0 or INS
'.', //71 NUM - . or DEL
'2', //72 NUM - 2 or DOWN
'5', //73 NUM - 5
'6', //74 NUM - 6 or RIGHT
'8', //75 NUM - 8 or UP
0x1B, //76 ESC
0x00, //77 NUM LOCK
0x00, //78 F11
'+', //79 NUM - + (Plus)
'3', //7A NUM 3 or PAGE DOWN
'-', //7B NUM - - (Minus)
'*', //7C NUM - *
'9', //7D NUM - 9 or PAGE UP
0x00, //7E SCROLL LOCK
0x00, //7F
0x00, //80
0x00, //81
0x00, //82
0x00, //83 F7
};
/*
*****************************************************
* *
* Shift - Lookup Table Used when Shift Hold *
* *
*****************************************************
*/
const INT8U PS2KeyShifted[]={
0x00 //00
0x00 //01 F9
0x00 //02
0x00 //03 F5
0x00 //04 F3
0x00 //05 F1
0x00 //06 F2
0x00 //07 F12
0x00 //08
0x00 //09 F10
0x00 //0A F8
0x00 //0B F6
0x00 //0C F4
0x09 //0D TAB
'~' //0E ` or ~
0x0 //0F
0x00 //10
0x00 //11 Left ALT
0x00 //12 Left SHIFT
0x00 //13
0x00 //14 Left Ctrl
'Q' //15 Q
'!' //16 1 or !
0x00 //17
0x00 //18
0x00 //19
'Z' //1A Z
'S' //1B S
'A' //1C A
'W' //1D W
'@' //1E 2 or @
0x00 //1F
0x00 //20
'C' //21 C
'X' //22 X
'D' //23 D
'E' //24 E
'$' //25 4 or $
'#' //26 3 or #
0x00 //27
0x00 //28
' ' //29 Space
'V' //2A V
'F' //2B F
'T' //2C T
'R' //2D R
'%' //2E 5 or %
0x00 //2F
0x00 //30
'N' //31 N
'B' //32 B
'H' //33 H
'G' //34 G
'Y' //35 Y
'^' //36 6 or ^
0x00 //37
0x00 //38
0x00 //39
'M' //3A M
'J' //3B J
'U' //3C U
'&' //3D 7 or &
'*' //3E 8 or *
0x00 //3F
0x0 //40
'<' //41 , or <
'K' //42 K
'I' //43 I
'O' //44 O
')' //45 0 or )
'(' //46 9 or (
0x00 //47
0x00 //48
'>' //49 > or .
'?' //4A / or ?
'L' //4B L
':' //4C ; or :
'P' //4D P
'_' //4E - or _
0x00 //4F
0x00 //50
0x00 //51
0x22 //52 ' or "
0x00 //53
'{' //54 [ or {
'+' //55 = OR +
0x00 //56
0x00 //57
0x00 //58 Caps Lock
0x00 //59 Right Shift
0x0D //5A Enter
'}' //5B ] or }
0x00 //5C
'|' //5D \ or |
0x00 //5E
0x00 //5F
0x00 //60
0x00 //61
0x00 //62
0x00 //63
0x00 //64
0x00 //65
0x08 //66 Backspace
0x00 //67
0x00 //68
'1' //69 NUM - 1 or END
0x00 //6A
'4' //6B NUM - 4 or LEFT
'7' //6C NUM - 7 or HOME
0x00 //6D
0x00 //6E
0x00 //6F
'0' //70 NUM - 0 or INS
'.' //71 NUM - . or DEL
'2' //72 NUM - 2 or DOWN
'5' //73 NUM - 5
'6' //74 NUM - 6 or RIGHT
'8' //75 NUM - 8 or UP
0x1B //76 ESC
0x00 //77 NUM LOCK
0x00 //78 F11
'+' //79 NUM - + (Plus)
'3' //7A NUM 3 or PAGE DOWN
'-' //7B NUM - - (Minus)
'*' //7C NUM - *
'9' //7D NUM - 9 or PAGE UP
0x00 //7E SCROLL LOCK
0x00 //7F
0x00 //80
0x00 //81
0x00 //82
0x00 //83 F7
};
#endif
/*
*****************************************************
* *
* Numeric KeyPad only - Lookup Table *
* *
*****************************************************
*/
//Uncomment the following #define if NumPad is used
//Should use a Decode() sub-routine to decode the array index.
//This method for Flash Rom space saving
//#define PS2_NUM_PAD_SCH1
#ifdef PS2_NUM_PAD_SCH1
const INT8U PS2KeyNumPad[17] ={0x70, 0x69, 0x72, 0x7A, 0x6B, 0x73, 0x74, 0x6C,\
0x75, 0x7D, 0x79, 0x7B, 0x7C, 0x4A, 0x71, 0x5A, 0x77};
/*
Array index
0 //70 NUM - 0 or INS
1 //69 NUM - 1 or END
2 //72 NUM - 2 or DOWN
3 //7A NUM - 3 or PAGE DOWN
4 //6B NUM - 4 or LEFT
5 //73 NUM - 5
6 //74 NUM - 6 or RIGHT
7 //6C NUM - 7 or HOME
8 //75 NUM - 8 or UP
9 //7D NUM - 9 or PAGE UP
10 //79 NUM - + (Plus)
11 //7B NUM - - (Minus)
12 //7C NUM - *
13 //4A or E04A NUM - /(division), extended code ignore here
14 //71 NUM - . or DEL
15 //5A or E05A NUM - ENT, extended code ignore here
16 //77 NUM LOCK
*/
#endif
/*
******************************************************
* *
* Numeric KeyPad only - Lookup Table to ASCII *
* Remarks: NumLock is mapped to 0x77 in this table *
******************************************************
*/
//Uncomment the following #define if NumPad is used
//Should choose between PS2_NUM_PAD_SCH1 or SCH2
//Should shift by 0x4A to map the ASCII character
#define PS2_NUM_PAD_SCH2
#ifdef PS2_NUM_PAD_SCH2
#define OFFSET_SCH2 0x4A
const INT8U PS2KeyNumPad[] = {
'/', //4A / or ?
'l', //4B L
';', //4C ; or :
'p', //4D P
'-', //4E - or _
0x00, //4F
0x00, //50
0x00, //51
0x27, //52 ' or "
0x00, //53
'[', //54 [ or {
'=', //55 = or +
0x00, //56
0x00, //57
0x00, //58 Caps Lock
0x00, //59 Right Shift
0x0D, //5A Enter
']', //5B ] or }
0x00, //5C
'\'', //5D \ or |
0x00, //5E
0x00, //5F
0x00, //60
0x00, //61
0x00, //62
0x00, //63
0x00, //64
0x00, //65
0x08, //66 Backspace
0x00, //67
0x00, //68
'1', //69 NUM - 1 or END
0x00, //6A
'4', //6B NUM - 4 or LEFT
'7', //6C NUM - 7 or HOME
0x00, //6D
0x00, //6E
0x00, //6F
'0', //70 NUM - 0 or INS
'.', //71 NUM - . or DEL
'2', //72 NUM - 2 or DOWN
'5', //73 NUM - 5
'6', //74 NUM - 6 or RIGHT
'8', //75 NUM - 8 or UP
0x1B, //76 ESC
0x77, //77 NUM LOCK
0x00, //78 F11
'+', //79 NUM - + (Plus)
'3', //7A NUM 3 or PAGE DOWN
'-', //7B NUM - - (Minus)
'*', //7C NUM - *
'9', //7D NUM - 9 or PAGE UP
};
#endif
|
Code: |
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
* (c) Copyright 2002, Nathan Brown, Longview, TX
* http://www.sputnickonline.com/
* All Rights Reserved
*
* PIC18xxxx Specific Code
* V1.00 - October 27, 2002
*
* File : OS_CPU.H
* By : Nathan Brown, modified by John Leung for CCS PICC complier for low & mid - range CPUs
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* DATA TYPES
* (Compiler Specific, CCS PICC in this case)
*********************************************************************************************************
*/
/* #define BOOLEAN short int (8 bits, -128<->127) as per 16F877A */
typedef unsigned char INT8U; /* Unsigned 8 bit quantity */
typedef signed char INT8S; /* Signed 8 bit quantity */
typedef unsigned long int INT16U; /* Unsigned 16 bit quantity */
typedef long int INT16S; /* Signed 16 bit quantity */
typedef unsigned int32 INT32U; /* Unsigned 32 bit quantity */
typedef signed int32 INT32S; /* Signed 32 bit quantity */
typedef float FP32; /* Single precision floating point */
/* end */
|
Code: |
/*
**************************************************************************************************
* Embedded Systems Building Blocks
* PS/2 Keyboard 104 keys, NumPad driver
*
* Original obtained from www.picant.com, C2C complier web site
* by Julio Cesar Silva Briano, modified by John Leung
*
* Filenmae : ps2key.c
* Programmer : John Leung
* Version : Version 1.1 - 17th June 2004
* Remarks : Consult www.beyong.org for PS/2 keyboard protocol
**************************************************************************************************
* DESCRIPTION
* This is a PS/2 keyboard driver for standard 101/104 keyboard or Numerical Keypad
* Application software must include PS2Key.h and PS2ScanCode.h
* PS2Key.h contains hardware port configuration
* PS2ScanCode.h configures if it is a complete 101 keyboard, or just the Numpad
* Two lookup tables PS2KeyUnshifted[] and PS2KeyShifted[] map the PS2Key_Code to ASCII characters
* If it is going to be the Numpad ONLY, PS2KeyUnshifted[] and PS2KeyShifted[] can be undefined to save ROM
* Application must declare the followings (see ps2key.h)
* PS2key.c is PS/2 keyboard driver to handle ordinary PS/2 or AT-style keyboard with mcu
* Global variable PS2Key_Code returns the raw key code of a key
* Key Buffer is defined by constant PS2KEY_BUF_SIZE (in PS2Key.h)
*
* PS2Key_Clk The port address of the PS/2 CLK signal
* PS2Key_Data The port address of the PS/2 Data signal
* PS2Key_BUF_SIZE Size of the PS/2 keyboard buffer
**************************************************************************************************
*/
/*
**************************************************************************************************
GLOBAL VARIABLES
**************************************************************************************************
*/
INT8U PS2KeyBuf[PS2KEY_BUF_SIZE]; //PS2 Keyboard buffer, the register to store characters key in
INT8U PS2KeyBufInIx; //Index into PS/2 key buf where next scan code will be inserted
INT8U PS2KeyBufOutIx; //Index into PS/2 key buf where next scan code will be removed
INT8U PS2KeyNRead; //Number of keys read from the PS/2 keyboard
INT8U PS2Key_Code; //Key code from PS/2 keyboard
INT8U PS2Key_bit_Ix; //bit index of the byte stream, Start_bit(1), Parity(10), Stop_bit(11)
BOOLEAN PS2_SHIFT; //TRUE if left or right shift pressed
BOOLEAN PS2_BREAKCODE; //TRUE if a BREAKCODE received
/*
**************************************************************************************************
LOCAL FUNCTION PROTOTYPES
**************************************************************************************************
*/
void PS2KeyBufIn(INT8U code); /* Insert scan code into keyboard buffer */
INT8U PS2KeyDecode(void); /* Function to decode a key stroke */
/*
**************************************************************************************************
* INSERT KEY CHARACTER INTO KEYBOARD BUFFER
* Description: This function inserts a key character into the keyboard buffer from a PS/2 keyboard
**************************************************************************************************
*/
void PS2KeyBufIn (INT8U code)
{
//OS_ENTER_CRITICAL(); //Start of critical section of code, disable ints
if (PS2KeyNRead < PS2Key_BUF_SIZE) { //make sure that we don't overflow the buffer
PS2KeyNRead++; //Increment the number of keys read
PS2KeyBuf[PS2KeyBufInIx++] = code; //Store the scan code into the buffer
if (PS2KeyBufInIx >= PS2Key_BUF_SIZE) { //Adjust index to the next scan code to put in buffer
PS2KeyBufInIx = 0;
}
//OS_EXIT_CRITICAL();
//Signal semaphore if an RTOS exist!
} else {
//OS_EXIT_CRITICAL();
}
}
/*
**************************************************************************************************
* DECODE PS/2 KEYBOARD SCAN CODE
* Description: This function is called to determine the key scane code of the PS/2 keyboard
* Arguments: none
* Returns: the key scan code
**************************************************************************************************
*/
INT8U PS2KeyDecode(void)
{
;
}
/*
**************************************************************************************************
* GET KEY
* Description: Get a PS/2 keyboard scan code from the keyboard driver
* Arguments: none
* Returns: !=0xFF is the key scan code of the PS/2 keyboard key pressed
* ==0xFF indicates that there is on key in the PS2KeyBuf[PS2KEY_BUF_SIZE] buffer
**************************************************************************************************
*/
INT8U PS2KeyGetKey (void)
{
INT8U code;
//OS_ENTER_CRITICAL();
if (PS2KeyNRead > 0) { /* See if we have keys in PS2KeyBuf[] */
PS2KeyNRead--; /* Decrement the number of keys in the buffer */
code = PS2KeyBuf[PS2KeyBufOutIx]; /* Get scan code from the buffer */
PS2KeyBufOutIx++;
if (PS2KeyBufOutIx >= PS2Key_BUF_SIZE) {
PS2KeyBufOutIx = 0;
}
//OS_EXIT_CRITICAL();
return (code); /* return the scan code of the key pressed */
} else {
//OS_EXIT_CRITICAL();
return (0xFF); /* No scan codes in buffer, return -1 */
}
}
/*
**************************************************************************************************
* SEND COMMAND
* Description: Send command to the PS/2 keyboard
* Arguments: cmd is the command to send
* $ED Set Status LED's - This command can be used to turn on and off the Num Lock, Caps Lock & Scroll Lock LED's.
* After Sending ED, keyboard will reply with ACK (FA) and wait for another byte which determines their Status.
* Bit 0 controls the Scroll Lock, Bit 1 the Num Lock and Bit 2 the Caps lock. Bits 3 to 7 are ignored.
* $EE Echo - Upon sending a Echo command to the Keyboard, the keyboard should reply with a Echo (EE)
* $F0 Set Scan Code Set. Upon Sending F0, keyboard will reply with ACK (FA) and wait for another byte,
01-03 which determines the Scan Code Used.
Sending 00 as the second byte will return the Scan Code Set currently in Use
* $F3 Set Typematic Repeat Rate. Keyboard will Acknowledge command with FA and wait for second byte,
which determines the Typematic Repeat Rate.
* $F4 Keyboard Enable - Clears the keyboards output buffer, enables Keyboard Scanning and returns an Acknowledgment.
* $F5 Keyboard Disable - Resets the keyboard, disables Keyboard Scanning and returns an Acknowledgment.
* $FE Resend - Upon receipt of the resend command the keyboard will re- transmit the last byte sent.
* $FF Reset - Resets the Keyboard.
* Returns: none
* Remarks: Refer to AN1723 of Motorola for detailed description of Host-to-KBD protocol
* Is NOT working as per 17th June 2004. Should work more on this function.
**************************************************************************************************
*/
void PS2SendComm(INT8U cmd)
{
PS2Key_bit_Ix = 0; //bit index of the byte stream, Start_bit(1), Parity(10), Stop_bit(11), ACK_bit(12)
disable_interrupts(INT_EXT); //disable external interrupt B0 on falling edge
output_low(PS2Key_Clk); //host forces PS2 clock line low.
delay_us(35); //After approx. 35us, the host pulls PS2 data line low
output_low(PS2Key_Data); //This sequence of event signals the keyboard that
//the host is about to transfer a command
delay_us(125); //The clock signal is released and pulled high by the keyboard's pullup
PS2Key_ClkTris = 1; //resisitor after the falling edge of the data signal
/* Transfer of data from host to keyboard will start approx. 1ms after the rising edge of the clock */
/* shift bit to PS2Key_Data on every falling edge of the PS2Key_Clk line */
do
{ while (input(PS2Key_Clk))
; //hold when PS2Key_Clk is high
output_bit(PS2Key_Data, cmd&0x01);
cmd = cmd>>1;
//PS2Key_DataTris=1;
PS2Key_bit_Ix++;
} while(PS2Key_bit_Ix<=12);
PS2Key_bit_Ix = 0;
/* send data from host complete */
PS2Key_DataTris = 1; //release data line, reset to input
ext_int_edge(0, H_TO_L); //set external interrupt to falling edge
enable_interrupts(INT_EXT); //release PS2 KBD clock line. driven high by PS2 KBD
}
/*
**************************************************************************************************
* SEE IF ANY KEY IN BUFFER
* Description: This function checks to see if a key was pressed
* Arguments: none
* Returns: TRUE if a key has been pressed
* FALSE if no key pressed
**************************************************************************************************
*/
BOOLEAN PS2KeyHit(void)
{
BOOLEAN hit;
//OS_ENTER_CRITICAL();
hit = (BOOLEAN)(PS2KeyNRead > 0)? TRUE : FALSE;
//OS_EXIT_CRITICAL();
return(hit);
}
/*
*********************************************************************************************************
* PS2 KEYBOARD DRIVER INITIALIZATION
*
* Description : This function initializes the PS2 driver.
* Arguments : None.
* Returns : None.
*********************************************************************************************************
*/
void PS2KeyInit(void)
{
PS2KeyNRead =0; //Clear the number of keys read
PS2KeyBufInIx =0; //Clear the key codes inserted at the beginning of the buffer
PS2KeyBufOutIx =0; //Clear the key codes removed at the beginning of the buffer
PS2_BREAKCODE =FALSE;
PS2_SHIFT =FALSE;
PS2Key_DataTris =1; //Set Data line an input
PS2Key_ClkTris =1; //Set clock line an input
ext_int_edge(0, H_TO_L); //set external interrupt to falling edge
enable_interrupts(INT_EXT); //enable external interrupt B0
}
/*
*********************************************************************************************************
* PS2 KEYBOARD INTERRUPT HANDLER
* Description: This function is called by an interrupt handler (e.g. B0 EXT INT on falling edge)
* Detect the clock pulse (High_to_Low) for new bit
* Arguments : none
* Returns : none
* Notes : Put a scan code to PS2KeyBuf[] if a valid byte acquired
* Set PS2KeyNRead then
*********************************************************************************************************
*/
void PS2KeyIntHandler(void)
{
BOOLEAN PS2Key_Data_State; //Temp variable for Data pin, PS2Key_Data_State
BOOLEAN PS2Status_Parity; //Parity bit
PS2Key_Data_State = input(PS2Key_Data);
PS2Key_bit_Ix++;
switch (PS2Key_bit_Ix)
{
case PS2Key_START:
if (PS2Key_Data_State || input(PS2Key_Clk))
PS2Key_bit_Ix=0; // Start bit clock is a high_to_low transition.
// Start bit is a low bit.
PS2Key_Code=0;
PS2Status_Parity=0;
break;
case PS2Key_PARITY:
PS2Status_Parity = PS2Key_Data_State;
break;
case PS2Key_STOP: // Stop bit got, put key in buffer
if (PS2Key_Code==0x12||PS2Key_Code==0x59) // Key=Left shift or right shift
{ if (PS2_BREAKCODE==TRUE)
PS2_SHIFT=FALSE;
else
PS2_SHIFT=TRUE;
}
if (PS2Key_Code==0xF0) // Is a break code!
PS2_BREAKCODE=TRUE;
else if (PS2_BREAKCODE==TRUE)
PS2_BREAKCODE=FALSE;
else
PS2KeyBufIn (PS2Key_Code); // Put valid byte in PS2KeyBuf[], ignore break code
PS2Key_bit_Ix=0;
break;
default:
PS2Key_Code = PS2Key_Code >> 1;
if (PS2Key_Data_State)
PS2Key_Code = PS2Key_Code | 0x80;
break;
}//end of switch ofr KDB send to Host
}
|
Code: |
/*
**************************************************************************************
* PS2Main.c to demonstrate the power of PS2Key.c and PS2Key.h drivers
* Date : 17th June 2004
* Programmer : John Leung
**************************************************************************************
*/
#include <16f877a.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOLVP
#include <OS_CPU.h> //typedef compatible with uCOS-II style
#include <PS2Key.h>
#include <PS2ScanCode.h>
#include <PS2Key.c>
INT8U PS2Key_Char;
#int_EXT
void ext_isr(void)
{
PS2KeyIntHandler();
}
void main(void)
{
INT8U outs = 0;
PS2KeyInit(); //Init PS2 Keyboard
enable_interrupts(global); //enable global interrupts
while(1)
{
if(PS2KeyHit())
{
/* PS2KeyNumPad[] ONLY, & OFFSET_SCH2 from PS2ScanCode.h */
PS2Key_Char = PS2KeyNumPad[PS2KeyGetKey()-OFFSET_SCH2];
switch (PS2Key_Char){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
//Handle the numeric input, display to LCD for dig N.
outs=~outs;
output_bit(PIN_D0,outs);
break;
case 0x0D:
//ENT Key, should print tag
outs=~outs;
output_bit(PIN_D1,outs);
break;
case 0x77:
//NumLock, toggle between FreeHand mode & sequential mode
outs=~outs;
output_bit(PIN_D2,outs);
break;
case '+':
//Increment on the sequential tag number, but no printed yet
outs=~outs;
output_bit(PIN_D3,outs);
break;
case '-':
//Decrement on the sequential tag number, but no printed yet
outs=~outs;
output_bit(PIN_D4,outs);
break;
case '/':
//Cancel key for clearing numeric input field
outs=~outs;
output_bit(PIN_D5,outs);
break;
case '*':
//MENU and USER SETUP
outs=~outs;
output_bit(PIN_D6,outs);
break;
case '.':
//cursor, for position toggle by repeating key, only for MENU and USER SETUP
outs=~outs;
output_bit(PIN_D7,outs);
break;
} //end of switch
} //end of if(PS2KeyHit())
} //end of while (1)
}
|
|
|
|
hadeelqasaimeh
Joined: 05 Jan 2006 Posts: 105
|
|
Posted: Sun Dec 17, 2006 2:40 am |
|
|
hi
i try to use this new library,,when i add an lcd library "lcd_kbd1.c" its make an error!! while "lcd.c" don't!! why? |
|
|
4ever
Joined: 07 Jan 2008 Posts: 1
|
|
Posted: Mon Jan 07, 2008 6:14 am |
|
|
Hi, my problem is about PS2Key_Char=PS2KeyUnshifted[PS2KeyGetKey()];
line. It appears "Undifined Identifier PS2KeyUnshifted" error
I included PS2ScanCode.h
When I try
PS2Key_Char = PS2KeyNumPad[PS2KeyGetKey()-OFFSET_SCH2];
its working. i can see the numbers. whats the difference PS2KeyUnshifted[] and PS2KeyNumPad[]. They are both in PS2ScanCode.h
Code: | #include <16f877.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOLVP
#use fast_io(b)
#include <OS_CPU.h> //typedef compatible with uCOS-II style
#include <PS2Key.h>
#include <PS2ScanCode.h>
#include <PS2Key.c>
#include <flex_lcd.c>
#define PS2_FULL_104KEY
INT8U PS2Key_Char;
#int_EXT
void ext_isr(void)
{
PS2KeyIntHandler();
}
void main(void)
{
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
PS2KeyInit(); //Init PS2 Keyboard
lcd_init(); //Init 2x16 characters LCD module
lcd_putc('\f'); //clear the module
lcd_gotoxy(1,1); //goto position 1,1 of the LCD module
lcd_putc("PS2 Keypad Test");//Put a fixed string on LCD
enable_interrupts(global); //enable global interrupts
while(1)
{
if(PS2KeyHit())
{
PS2Key_Char=PS2KeyUnshifted[PS2KeyGetKey()];
lcd_gotoxy(1,2);
printf(lcd_putc,"%x",PS2Key_Char);
lcd_gotoxy(5,2);
printf(lcd_putc,"%c",PS2Key_Char);
} //end of if(PS2KeyHit())
} //end of while (1)
} | [/code] |
|
|
|
|
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
|