picbasic de ps2 klavye okutmak istiyorum

Başlatan enderun, 08 Eylül 2006, 16:22:31

enderun

picbasic de ps2 klavye okutmak istiyorum.Bilgis olan arkadaşlar paylaşabilir mi....

Logan

Forumda biraz aratma yaparsan oldukça bol kaynağa ulaşabilirsin.Klavye konusu yakın zamanda işlenmişti.
İmza.

halukece

ScanKeys:          
    Gosub   ScanKeyboard 'Gets key pressed keycode 
    Gosub   ChrtoASCII 'keycode into ascii 
    Goto    ScanKeys

ScanKeyboard: 
    DumpReg = 0 
DumpKeys: 
    KeyPress = 0 
    Key_Clock_Tris = 1 : Keyboard_Clock = 1
    Gosub ClockCycle
    For TempReg = 0 to 7 
    Gosub ClockCycle 
    If Keyboard_Data = 1 Then
    Lookup TempReg,[1,2,4,8,16,32,64,128],Temp2Reg 
    KeyPress = KeyPress + Temp2Reg '
    Endif 
    Next TempReg 
    Gosub ClockCycle 'Dumps parity bit 
    Gosub ClockCycle 'Dumps stop bit 
    Key_Clock_Tris = 0 : Keyboard_Clock = 0
    If KeyPress = $F0 Then
    DumpReg = 1 
    Goto DumpKeys
    Endif 

    If Keypress=$12 || Keypress=$59 && DumpReg = 1 Then 'Turn off ShiftReg 
    ShiftReg = 0 'Sets the Shift Reg for Shift 
    Endif 

    If DumpReg = 1 Then
    DumpReg = 0 
    KeyPress = 255 
    Endif 

    If Keypress=$12 || Keypress=$59 Then 'Turn on ShiftReg 
    ShiftReg = 1 'Sets the Shift Reg for Shift 
    Endif 
    Return 

ClockCycle:
HoldClockLow: 
    If Keyboard_Clock = 0 Then HoldClockLow 
HoldClockHi: 
    If Keyboard_Clock = 1 Then HoldClockHi 
    Return 

ChrtoASCII: 'keycode into ASCII   
    AscKey = 255    

F1:
    If KeyPress=$05 then    'F1 tuşuna basılmışsa
    ' Kodlar
    ENDIF

F2:
    If KeyPress=$06 then    'F2 tuşuna basılmışsa
    'Kodlar
    ENDIF

    RETURN


KarakterOku:
    Lookdown Keypress,[$45,$16,$1E,$26,$25,$2E,$36,$3D,$3E,$46,_ ' "9" end
                       $1C,$32,$21,$23,$24,$2B,$34,$33,$43,$3B,_ ' "J" end
                       $42,$4B,$3A,$31,$44,$4D,$15,$2D,$1B,$2C,_ ' "T" end
                       $3C,$2A,$1D,$22,$35,$1A,_ ' "Z" end
                       $4E,$55,$5D,$4A,$29,$49,$54,$52,$41,$4C,$5B],AscKey 
    If shiftReg=0 then 
    Lookup AscKey,["0123456789abcdefgh",3,"jklmnopqrstuvwxyz*-,. ",1,2,"i",239,5,245],Key 
    Else 
    Lookup AscKey,["=!'#+%&/()ABCDEFGHIJKLMNOPQRSTUVWXYZ?_;:@",0,2,"i",239,4,245],Key 
    Endif
    RETURN



LCD projemde kullanmıştım. Çok güzel çalışıyor. Özel tuşlara kodlarıyla ulaşıyorsun. Örnek olarak F1 ve F2 tuşlarını yazdım. Rakam ve harfler ve shiftli olarak klavyeyi okuyabilirsin. Kendi ihtiyacıma göre böyle düzenlemiştim. Umarım işine yarar.

ibrsel

Daha basitçe bir program istersen aşağıdaki programı da bazı düzenlemeler yaparak kullanabilirsin..Klavyenin mantığını çözmek için ilk uğraştığım zamanlarda bu programı yazmıştım..Basitçe CLK ucundan Clock uygulayıp 9600 baud da bilgi okutuyorsun.Basılan tuşa karşılık gelen bilgiyi karşılaştırıp LCD ye yazdırıyorsun..


basla:
pause 50
    clk=0:pauseus 10
    clk=1:pauseus 10
        serin in,T9600,a   
            if a=134 then
                c=c+1:gosub ckontrol
                lcdout "A" :yazi(c)="A"               
            endif
            if a=137 then
                c=c+1:gosub ckontrol
                lcdout "B" :yazi(c)="B"   
            endif
            if a=200 then
                c=c+1:gosub ckontrol
                lcdout "C" :yazi(c)="C"     
            endif
            if a=148 then
                c=c+1:gosub ckontrol
                lcdout "D"  :yazi(c)="D"   
            endif
            if a=202 then
                c=c+1:gosub ckontrol
                lcdout "E"  :yazi(c)="E"     
            endif
            if a=205 then
                c=c+1:gosub ckontrol
                lcdout "F"  :yazi(c)="F"     
            endif
             if a=138 then
                c=c+1:gosub ckontrol
                lcdout "G" :yazi(c)="G"     
            endif
            if a=201 then
                c=c+1:gosub ckontrol
                lcdout "H" :yazi(c)="H"     
            endif
            if a=145 then
                c=c+1:gosub ckontrol
                lcdout "I"  :yazi(c)="I"     
            endif
            if a=141 then
                c=c+1:gosub ckontrol
                lcdout "J"  :yazi(c)="J"     
            endif
            if a=209 then
                c=c+1:gosub ckontrol
                lcdout "K"  :yazi(c)="K"     
            endif
            if a=213 then
                c=c+1:gosub ckontrol
                lcdout "L"   :yazi(c)="L"     
            endif
             if a=208 then
                c=c+1:gosub ckontrol
                lcdout "M"  :yazi(c)="M"     
            endif
            if a=136 then
                c=c+1:gosub ckontrol
                lcdout "N"   :yazi(c)="N"     
            endif
            if a=210 then
                c=c+1:gosub ckontrol
                lcdout "O"   :yazi(c)="O"     
            endif
            if a=214 then
                c=c+1:gosub ckontrol
                lcdout "P"   :yazi(c)="P"     
            endif
            if a=206 then
                c=c+1:gosub ckontrol
                lcdout "R"   :yazi(c)="R"       
            endif
            if a=197 then
                c=c+1:gosub ckontrol
                lcdout "S"   :yazi(c)="S"     
            endif
            if a=142 then
                c=c+1:gosub ckontrol
                lcdout "T"   :yazi(c)="T"     
            endif
            if a=149 then
                c=c+1:gosub ckontrol
                lcdout "U"   :yazi(c)="U"     
            endif
            if a=146 then
                c=c+1:gosub ckontrol
                lcdout "V"   :yazi(c)="V"     
            endif
            if a=150 then
                c=c+1:gosub ckontrol
                lcdout "Y"   :yazi(c)="Y"   
            endif
            if a=133 then
                c=c+1:gosub ckontrol
                lcdout "Z"   :yazi(c)="Z"     
            endif
            if a=140 then '' space ''
                c=c+1:gosub ckontrol
                lcdout " "
            endif
            if a=219 then ''back space''
                c=c-1
                    iF c=-1 THEN c=0
                LCDOUT $FE, $10
                lcdout " "
                LCDOUT $FE, $10
            endif
                     
GOTO basla

ckontrol:
   if c>=16 then
    c=0
    lcdout $fe,1,"FAZLA METiN"
    LCDOUT $fe,192,"  GiRDiNiZ ":pause 1000
    goto main
   endif
return

ibrsel


buiba

ben de bu konuya merakliyim pic asm icin ornek verebilecek arkadaslar var mi?saygilar

enderun


mavi

Device-to-Host Communication:

The Data and Clock lines are both open collector (normally held at a high logic level.)  When the keyboard or mouse wants to send information, it first checks Clock to make sure it's at a high logic level.  If it's not, the host is inhibiting communication and the device must buffer any to-be- sent data until it regains control of the bus (the keyboard has a 16-byte buffer and the mouse's buffer stores only the last packet sent.)  If the Clock line is high, the device can begin to transmit its data.

As I mentioned in the previous section, the keyboard and mouse use a serial protocol consisting of
11-bit frames.  These bits are:

    1 start bit.  This is always 0.
    8 data bits, least significant bit first.
    1 parity bit (odd parity).
    1 stop bit.  This is always 1.

Each bit is read by the host on the falling edge of the clock, as is illustrated in Figures 2 & 3. Figure 2:  Device-to-host communication.  The Data line changes state when Clock is high and that data is latched on the falling edge of the clock signal.




Figure 3:  Scan code for the "Q" key (15h) being sent from a keyboard to the computer.  Channel
A is the Clock signal; channel B is the Data signal.



http://govschl.ndsu.nodak.edu/%7Eachapwes/PICmicro/PS2/ps2.htm   12.12.2002

The PS/2 Mouse/Keyboard Protocol   Seite 5 von 11


The clock frequency is 10-16.7kHz.  The time from the rising edge of a clock pulse to a Data transition should be at least 5 microseconds.  The time from a data transition to the falling edge of a clock pulse should be at least 5 microseconds and no greater than 25 microseconds.  This timing is very important--you should follow it exactly.  The host may pull the line low before the 11th clock pulse (stop bit), causing the device to abort sending the current byte (this is very rare.)  After the stop bit is transmitted, the device should wait at least 50 microseconds before sending the next packet.  This gives the host time to inhibit transmission while it processes the received byte (the host will usually automatically do this after each packet is received.)  The device should wait at least 50 microseconds after the host releases an inhibit before sending any data.

I would recommend the following process for sending a single byte from an emulated keyboard/mouse to the host:

1)  Wait for Clock = high.
2)  Delay 50 microseconds.
3)  Clock still = high?
       No--goto step 1
4)  Data = high?
       No--Abort (and read byte from host)
5)  Delay 20 microseconds (=40 microseconds to the time Clock is pulled low in sending the start bit.)
6)  Output Start bit (0)    \   After sending each of these bits, test
7)  Output 8 data bits       > Clock to make sure host hasn't pulled it
8)  Output Parity bit        /   low (which would abort this transmission.)
9)  Output Stop bit (1)
10)  Delay 30 microseconds (=50 microseconds from the time Clock is released in sending the stop bit)

The process for sending a single bit should then be as follows:

1)  Set/Reset Data
2)  Delay 20 microseconds
3)  Bring Clock low
4)  Delay 40 microseconds
5)  Release Clock
6)  Delay 20 microseconds


http://govschl.ndsu.nodak.edu/%7Eachapwes/PICmicro/PS2/ps2.htm   12.12.2002

The PS/2 Mouse/Keyboard Protocol   Seite 6 von 11



Here is some sample code written for the PIC16F84 that follows the above algorithms to send a byte to the host.  "Delay" is a self-explanitory macro; "CLOCK" and "DATA" are the bits connected to the Clock and Data lines; "TEMP0", "PARITY", and "COUNTER" are all general purpose registers. Note that in the "PS2outBit" routine, the Data and Clock lines are brought low by setting the appropriate I/O pin to output (it's assumed their output was set to "0" at the beginning of the program.)  And they are allowed to float (high) by setting the I/O pin to input (and allow a pull-up resistor to pull the line high.)  This was written for a PIC running at 4.61 MHz +/- 25% (RC oscillator: 5k/20pF).  This is very important for timing considerations.

ByteOut     movwf   TEMP0   ;Save to-be-sent byte
InhibitLoop   
 btfss   CLOCK   ;Check for inhibit
 goto   InhibitLoop
 Delay   50   ;Delay 50 microseconds
 btfss   CLOCK   ;Check again for inhibit
 goto   InhibitLoop
 btfss   DATA   ;Check for request-to-send
 retlw   0xFF
 clrf   PARITY   ;Init reg for parity calc
 movlw   0x08
 movwf   COUNTER
 movlw   0x00
 call   BitOut   ;Output Start bit (0)
 btfss   CLOCK   ;Test for inhibit
 goto   ByteOutEnd
 Delay   4
ByteOutLoop
 movf   TEMP0, w
 xorwf   PARITY, f   ;Calculate parity
 call   BitOut   ;Output Data bits
 btfss   CLOCK   ;Test for inhibit
 goto   ByteOutEnd
 rrf   TEMP0, f
 decfsz   COUNTER, f
 goto   ByteOutLoop
 Delay   2
 comf   PARITY, w
 call   BitOut   ;Output Parity bit
 btfss   CLOCK   ;Test for inhibit
 goto   ByteOutEnd
 Delay   5
 movlw   0xFF
 call   BitOut   ;Output Stop bit (1)
 Delay   48
 retlw   0x00


ByteOutEnd
 bsf   STATUS, RP0   ;Host has aborted
 bsf   DATA   ;DATA=1
 bsf   CLOCK   ;CLOCK=1
 bcf   STATUS, RP0
 retlw   0xFE


BitOut     bsf   STATUS, RP0
 andlw   0x01



http://govschl.ndsu.nodak.edu/%7Eachapwes/PICmicro/PS2/ps2.htm   12.12.2002

The PS/2 Mouse/Keyboard Protocol   Seite 7 von 11


 btfss   STATUS, Z
 bsf   DATA
 btfsc   STATUS, Z
 bcf   DATA
 Delay   21
 bcf   CLOCK
 Delay   45
 bsf   CLOCK
 bcf   STATUS, RP0
 Delay   5
 return




Host to Device Communication:

The packet is sent a little differently in host-to-device communication...

First of all, the PS/2 device always generates the clock signal.  If the host wants to send data, it must first put the Clock and Data lines in a "Request-to-send" state as follows:

    Inhibit communication by pulling Clock low for at least 100 microseconds.
    Apply "Request-to-send" by pulling Data low, then release Clock.

The device should check for this state at intervals not to exceed 10 milliseconds.  When the device detects this state, it will begin generating Clock signals and clock in eight data bits and one stop bit.  The host changes the Data line only when the Clock line is low, and data is latched on the rising edge of the clock pulse.  This is opposite of what occours in device-to-host communication.

After the stop bit is sent, the device will acknowledge the received byte by bringing the Data line low and generating one last clock pulse.  If the host does not release the Data line after the 11th clock pulse, the device will continue to generate clock pulses until the the Data line is released
(the device will then generate an error.)

The Host may abort transmission at time before the 11th clock pulse (acknowledge bit) by holding
Clock low for at least 100 microseconds.

To make this process a little easier to understand, here's the steps the host must follow to send data to a PS/2 device:

1)   Bring the Clock line low for at least 100 microseconds.
2)   Bring the Data line low.
3)   Release the Clock line.
4)   Wait for the device to bring the Clock line low.
5)   Set/reset the Data line to send the first data bit
6)   Wait for the device to bring Clock high.
7)   Wait for the device to bring Clock low.
8)   Repeat steps 5-7 for the other seven data bits and the parity bit
9)   Release the Data line.
10) Wait for the device to bring Data low.
11) Wait for the device to bring Clock  low.
12) Wait for the device to release Data and Clock



http://govschl.ndsu.nodak.edu/%7Eachapwes/PICmicro/PS2/ps2.htm   12.12.2002

The PS/2 Mouse/Keyboard Protocol   Seite 8 von 11

Figure 3 shows this graphically and Figure 4 separates the timing to show which signals are generated by the host, and which are generated by the PS/2 device.  Notice the change in timing for the Ack bit--the data transition occours when the Clock line is high (rather than when it is low as is the case for the other 11 bits.)



Figure 4 shows two important timing considerations: (a), and (b).  (a), the time it takes the device to begin generating clock pulses after the host initially takes the Clock line low, must be no greater than 15ms; (b), the time it takes for the  packet to be sent, must be no greater than 2ms.  If either of these time limits is not met, the host will generate an error.  Immediately after the packet is received, the host may bring the Clock line low to inhibit communication while it processes data.  If the command sent by the host requires a response, that response must be received no later than 20ms after the host releases the Clock line.  If this does not happen, the host generates an error.  As was the case with Device-to-host communication, no Data transition may occur with 5 microseconds of a Clock transition.

If you want to emulate a mouse or keyboard, I would recommend reading data from the host as follows:

    In your main program, check for Data=low at least every 10 milliseconds.
    If Data has been brought low by the host, read one byte from the host
1)  Wait for Clock=high
2)  Is Data still low?
      No--An error occurred; Abort.
3)  Read 8 data bits      \  After reading each of these bits, test
4)  Read parity bit          >   Clock to make sure host hasn't pulled it


http://govschl.ndsu.nodak.edu/%7Eachapwes/PICmicro/PS2/ps2.htm   12.12.2002

The PS/2 Mouse/Keyboard Protocol   Seite 9 von 11


5)  Read stop bit           /   low (which would abort this transmission.)
6)  Data still equals 0?
      Yes--Keep clocking until Data=1 then generate an error
7)  Output Acknowledge bit
8)  Check Parity bit.
      Generate an error if parity bit is incorrect
9)  Delay 45 microseconds (to give host time to inhibit next transmission.)

   Read each bit (8 data bits, parity bit, and stop bit) as follows:
1)  Delay 20 microseconds
2)  Bring Clock low
3)  Delay 40 microseconds
4)  Release Clock
5)  Delay 20 microsecond
6)  Read Data line
   Send the acknowledge bit as follows:
1)  Delay 15 microseconds
2)  Bring Data low
3)  Delay 5 microseconds
4)  Bring Clock low
5)  Delay 40 microseconds
6)  Release Clock
7)  Delay 5 microseconds
8)  Release Data

Here is some sample code written for the PIC16F84 that implements the above algorithms to read data from a PS/2 host.  "Delay" is a self-explanitory macro; "CLOCK" and "DATA" are the port bits connected to the Clock and Data lines; "TEMP0", "PARITY", and "COUNTER" are all general purpose registers.  Note that in the "PS2inBit" routine, Clock is brought low by setting the appropriate
I/O pin to output (it's assumed they were set to "0" at the beginning of the program.)  And it is allowed to float (high) by setting the I/O pin to input (and allow a pull-up resistor to pull the line high.)  Timing was worked out for a PIC running at 4.61 MHz +/- 25% (RC oscillator with values
5k/20 pF).  Will work for any oscillator between 3.50 MHz - 5.76 MHz.

ByteIn
 btfss   CLOCK   ;Wait for start bit
 goto   ByteIn
 btfsc   DATA
 goto   ByteIn
 movlw   0x08
 movwf   COUNTER
 clrf   PARITY   ;Init reg for parity calc
 Delay   28
ByteInLoop     call   BitIn   ;Clock in Data bits
 btfss   CLOCK   ;Test for inhibit
 retlw   0xFE
 bcf   STATUS, C
 rrf   RECEIVE, f
 iorwf   RECEIVE, f
 xorwf   PARITY,f
 decfsz   COUNTER, f
 goto   ByteInLoop
 Delay   1
 call   BitIn   ;Clock in Parity bit



http://govschl.ndsu.nodak.edu/%7Eachapwes/PICmicro/PS2/ps2.htm   12.12.2002

The PS/2 Mouse/Keyboard Protocol   Seite 10 von 11


 btfss   CLOCK   ;Test for inhibit
 retlw   0xFE
 xorwf   PARITY, f
 Delay   5
ByteInLoop1     Delay   1
 call   BitIn   ;Clock in Stop bit
 btfss   CLOCK   ;Test for inhibit
 retlw   0xFE
 xorlw   0x00
 btfsc   STATUS, Z   ;Stop bit = 1?
 clrf   PARITY   No--cause an error condition.
btfsc   STATUS, Z     ;Stop bit = 1?
 goto   ByteInLoop1   ;   No--keep clocking.


 bsf   STATUS, RP0   ;Acknowledge
 bcf   DATA
 Delay   11
 bcf   CLOCK
 Delay   45
 bsf   CLOCK
 Delay   7
 bsf   DATA
 bcf   STATUS, RP0


 btfss   PARITY, 7   ;Parity correct?
 retlw   0xFF   ;   No--return error


 Delay   45
 retlw   0x00


BitIn
 Delay   8
 bsf   STATUS, RP0
 bcf   CLOCK
 Delay   45
 bsf   CLOCK
 bcf   STATUS, RP0
 Delay   21
 btfsc   DATA
 retlw   0x80
 retlw   0x00



kodlar denenmemiştir
Telecommander Monster Of Network