PC keyboard to ASCII


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    Any Typing errors are all mine !
    I didn't see them, but I knew they were there.
    Here is the corrected code, Thank You again Alan !
    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [Alan Paton]                                      *
    '*  Notice  : Copyright (c) 2009                                *
    '*          : All Rights Reserved                               *
    '*  Date    : 2/17/2009                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*  http://www.interak.pwp.blueyonder.co.uk/PCtoASCII.htm       *
    '****************************************************************
    @MyConfig = _HS_OSC  & _LVP_OFF & _WDT_OFF & _CP_OFF
    @MyConfig = MyConfig & _BODEN_OFF & _MCLRE_ON & _PWRTE_ON  
    @ __CONFIG   MyConfig
    ;@ device    INTRC_OSC_NOCLKOUT ;use internal oscillator
    ; DEFINE OSC 4
    CMCON = 7
    DEFINE OSC 20
    PBIT VAR BYTE
    D    VAR BYTE[12]
    SC   VAR BYTE
    CNT  VAR BYTE
    J    VAR BYTE
    CLK  VAR PORTA.0
    DAT  VAR PORTA.1
    CL   VAR BYTE
    SH   VAR BYTE
    AC   VAR BYTE
    LN   VAR BYTE
    LB   VAR BYTE
    'initialise
    PAUSE 1000 ' wait for keyboard to start up
    TRISB = 0
    TrisA = 0
    SH = 0
    CNT = 0
    CL = 0
    GOTO SCAN
    START:
    IF SC=$12 OR SC = $59 THEN ' shift key up
        SH = 0                 ' reset shift
        ENDIF
        IF SC = $14 THEN       ' control key up
        CNT = 0                ' reset control flag
        ENDIF
    lb = 0                     'clear last-byte flag
    'Setup ports to read keyboard ******************
    ' ************ SCAN shifts serial scancode into SC *********
    SCAN:
        sc = 0                 ' clear scan code
        TrisA.1 = 1            ' set DAT to input
        TrisA.0 = 1            ' set CLK to input
    ' Wait for clock transistion
    LP1:                       ' make sure clock is high
        IF CLK = 0 THEN        
        GOTO LP1
        ENDIF
    
    
    LP2:
        IF CLK = 1 THEN        ' clock idles high
        GOTO LP2               ' wait for clock to go low
        ENDIF
        'clock now low - Start bit of scan code
        FOR J = 0 TO 9         ' get scan code
    
    LP3:
        IF CLK = 0 THEN        ' make sure clock is high
        GOTO LP3
        ENDIF
        D(J) = DAT              'read data bit
        NEXT J
     ' shift bits into variable SC   
        FOR J = 7 TO 0 STEP -1  ' D(8) & D(9) are parity & stop bits
        SC = SC << 1            ' shift 1 place left
        SC = SC + D(J)          ' set bit 0 of SC LSB = 0 MSB = 7
        NEXT J
        
        TrisA.0 = 0             ' set CLK to output
        CLK = 0                 ' and hold it
        
        
        IF LB = 2 THEN          ' last byte of up - code - don't display
            GOTO START
        ENDIF  
            IF SC = $F0 THEN    ' Check for key-up code
            LB = 2              ' Set last byte flag
                GOTO SCAN
            ENDIF
                IF SC = $12 OR SC = $59 THEN  ' left and right shift keys
                SH = $80                      ' Set shift flag
                    GOTO SCAN
                ENDIF
                    IF SC = $58 THEN          ' Caps lock on
                        GOSUB CPLOCK
                        GOTO SCAN
                    ENDIF
                        IF SC = $E0 THEN      ' Extended keys
                            GOTO SCAN         ' Get next byte
                        ENDIF
                            IF SC = $14 THEN   ' Left control key (& right . . $EO $14 )
                                CNT = 2        ' Set flag
                                GOTO SCAN
                            ENDIF
              ' and into SCAN CODE to ACSII conversion
              ' **************** CONVERT TO ASCII CODE ***************************
              
              ' scan code in SC
            ASCII:
                IF SC = $5A THEN   'is it CR ?
                    AC = $0D
                    GOTO ASOUT     ' Yes send it
                ENDIF
                    SC = SC + SH   'SH=$80 if shift key down (0 if not)
                    LN = $FF       ' Set List No. to high value
                    
    ' convert to ASCII (codes from 20H to 7FH)                       
                    LOOKDOWN SC,[$29,$96,$9E,$5D,$A5,$BD,$52,$C6,$C5,$BE,$D5,_
            $41,$4E,$49,$4A,$45,$16,$1E,$26,$25,$2E,$36,$3D,$3E,$46,$CC,$4C,$C1,$55,_
            $C9,$CA,$D2,$9C,$B2,$A1,$A3,$A4,$AB,$B4,$B3,$C3,$BB,$C2,$CB,$BA,$B1,$C4,_
            $CD,$95,$AD,$9B,$AC,$BC,$AA,$9D,$A2,$B5,$9A,$54,$61,$5B,$B6,$CE,$A6,$1C,_
            $32,$21,$23,$24,$2B,$34,$33,$43,$3B,$42,$4B,$3A,$31,$44,$4D,$15,$2D,$1B,_
            $2C,$3C,$2A,$1D,$22,$35,$1A,$D4,$E1,$DB,$DD,$66],LN
    
    
        IF LN = $FF THEN    ' Scan code not found
            GOTO NUMPAD     ' Now check number pad
        ENDIF
    
    '  Matching scan code has been found
    
        IF CNT = 2 THEN     'Is control key pressed ?
            AC = LN - $40   ' Get ASCII code
            GOTO ASOUT      ' Send it
        ENDIF
    '  CAPS LOCK ON - check for the following conditions
    
        IF LN>$40 AND LN<$5B THEN   '  caps lock on & lower case selected
            AC=LN
        GOTO ASOUT
        ENDIF
        IF LN=$1B THEN        '  caps lock on & (semi)colon selected
            AC=$3A 'ASCII code for colon
        GOTO ASOUT
        ENDIF
        GOTO AS2 'send out to computer keyboard port
    
    '  check number pad
    
    NUMPAD:
            LOOKDOWN SC,[$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$2A,$2D,$2B,$2E,$09,$1B],AC
            GOTO ASOUT
    AS2:
        AC = LN + $20                 ' Convert List No. to ASCII
    
    ASOUT:
          PortB.7 = 1                 ' Strobe - normally high - pulse low
          PAUSE 2                     
          PortB = AC                  ' Send ASCII code to Port B
          PAUSE 2                     ' Bit 7 (strobe) is zero
          pORTb.7 = 1                 
          GOTO SCAN
          
          END
    ' ********************* CAPS LOCK *********************************************     
    CPLOCK:
         IF CL = 0 THEN               ' Toggle caps lock
            CL = 4                    ' 4 IS BIT 2 0000 0100
         ELSE
            CL = 0
         ENDIF
         RETURN
    Last edited by Archangel; - 19th February 2009 at 17:17.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  2. #2
    interak's Avatar
    interak Guest


    Did you find this post helpful? Yes | No

    Post

    Hi Sorry Joe there is another bit not quite right. The PDF on the website definitely is OK because I printed it from the program listing.

    Just above the CAPS LOCK ON code should be......

    GOTO ASOUT 'send it
    ENDIF
    '''''''''''''''''''''''''insert
    IF CL=0 THEN 'Caps Lock OFF
    GOTO AS2 '& matching code found
    ENDIF
    ''''''''''''''''''''''''''
    'CAPS LOCK ON

  3. #3
    Join Date
    Dec 2008
    Location
    Los Angeles, CA
    Posts
    156


    Did you find this post helpful? Yes | No

    Wink

    Wow! Thanks! I've been looking for some sample code, and it looks like you've got it!

    I did buy one of these (just to avoid the work and get my project done quickly), and must say that it works perfectly (if they only had a USB version!!) :
    http://www.multilabs.net/ezKEY.html
    Last edited by circuitpro; - 1st October 2009 at 18:03.

Similar Threads

  1. AT/PS2 Keybord - PIC Interface?
    By Kamikaze47 in forum Code Examples
    Replies: 73
    Last Post: - 9th August 2009, 16:10
  2. One USB keyboard to Two USB Ports
    By picnaut in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 11th June 2009, 00:04
  3. PC Keyboard question
    By HatchetHand in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 18th September 2007, 06:03
  4. Pic to PC AT keyboard caracters
    By sezgin05 in forum General
    Replies: 5
    Last Post: - 27th March 2007, 10:45
  5. Making a Keyboard Encoder
    By TonyA in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th December 2006, 16:25

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts