YT2095 wrote:
yeah, SBC to PC is pretty easy with the FTDI chips (about £2 on ebay), or even the PIC 18f22k50, 18f2550/4550.
the s/ware I did was the other way around, where it parses a USB Keyboards data into Parallel and Serial output for the SBC to use.
as for the green adapters I`v no idea if they`re different at all, but it would be worth checking with a continuity tester to see if it is just a simple pass-through type.
on the off chance that it May help someone in the future doing a search for such a thing, my code is here:
;Chip Settings
#chip 18F4685,20
#config OSC=HS, PWRT=ON, WDT=OFF
;Defines (Constants)
#define PS2Clock PORTB.1 'to D+ on USB socket
#define PS2Data PORTB.0 'to D- on USB socket
#define SerOutPort PORTB.7
#define SendAHigh Set SerOutPort Off
#define SendALow Set SerOutPort On
dir portc out
DIR PS2Clock IN
DIR PS2Data IN
Dir SerOutPort Out
InitSer 1, r9600, 1 + WaitForStart, 8, 1, None, Invert
Main:
KeyIn = INKEY
if KeyIn = 0 then
goto Main
end if
portc = KeyIn
SerSend 1, KeyIn
wait 1 ms
goto Main
all you need is to download GCBasic (it`s free and quite small), hook up a PIC 18F4685 with a 20MHz crystal to a PicKit2 and go to `Compile an download with PicKit2` in the pulldown menu and it`s done!
this will do both USB and/or PS2 directly with or without the use of adapters.
it hardly even deserves to be called "Code"
Here is my code for the pic16f877 you can use any part or all of it. It uses the onboard PSP as the interface, and when you write a byte it writes it to the LCD. It supports caps lock, and numlock and scroll lock leds work as well, although they re useless. It also beeps when it receives char 7.
It should be also able to read time and date from an i2c rtc chip, but there is a bug in the i2c routine, so i never got to fixing it.
Here is the code, but i must give credit to 8BIT, since i took the character table from his keyboard interface mcu. For compiling you need Proton IDE.
Code:
'****************************************************************
'* Name : ATC.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyleft (c) 2011 *
'* : All Rights Reserved *
'* Date : 4.7.2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
Device 16F877
XTAL=20
Output PORTB
Output PORTA
Input PORTE
Output PORTA.7
Declare LCD_TYPE ALPHA
Declare LCD_DTPIN PORTB.4 ' Used for 4-line interface.
Declare LCD_ENPIN PORTB.3
Declare LCD_RSPIN PORTB.2
Declare LCD_INTERFACE 4
Declare LCD_LINES 4
TRISE.4=1
Symbol CLK=PORTC.1
Symbol DAT=PORTC.0
Dim MODE As Bit
Dim ZY As Bit
Symbol NC=PORTA.5
Symbol ACK=PORTC.6
Symbol IRQ=PORTC.7
Symbol OUT=PORTD
Symbol INP=PORTD
Dim cbuf[128] As Byte
Dim cbrp As Byte
Dim cbwp As Byte
cbrp=0
cbwp=0
Dim VAR1 As Byte
Dim ict As Byte
Symbol Control %11010000
Symbol SDA = PORTB.0 ' Alias the SDA (Data) line
Symbol SCL = PORTB.1 ' Alias the SSL (Clock) line
Dim ADDR As Byte ' The address of the I2C device you wish to access
Dim I2C_OUT[2] As Byte '[2] is maximum bytes being sent out, can change to send more
Dim I2C_IN[2] As Byte '[2] is maximum bytes being sent in, can chage to receive more
Dim TEMP As Word
Dim I2C_ACK As Bit 'ACK bit receive
Dim Nw As Byte '”FOR” loop Variable
Dim FLAGw As Byte 'FLAG Variable for Bytes to send/receive
Dim RWw As Bit 'Read/Write bit variable used by subroutines
ADDR=Control
Symbol IBF=TRISE.7
Symbol OBF=TRISE.6
ADCON1=7
'PSPIF rw operation ocurred
Declare FSR_CONTEXT_SAVE = On
On_Hardware_Interrupt GoTo Sio
' norm shift scancode
Data $00, $00 ' 00 no key pressed
Data $89, $C9 ' 01 F9
Data $87, $C7 ' 02 relocated F7
Data $85, $C5 ' 03 F5
Data $83, $C3 ' 04 F3
Data $81, $C1 ' 05 F1
Data $82, $C2 ' 06 F2
Data $8C, $CC ' 07 F12
Data $00, $00 ' 08
Data $8A, $CA ' 09 F10
Data $88, $C8 ' 0A F8
Data $86, $C6 ' 0B F6
Data $84, $C4 ' 0C F4
Data $09, $09 ' 0D tab
Data $60, $7E ' 0E `~
Data $8F, $CF ' 0F relocated Print Screen key
Data $03, $03 ' 10 relocated Pause/Break key
Data $A0, $A0 ' 11 left alt (right alt too)
Data $00, $00 ' 12 left shift
Data $00, $00 ' 13
Data $00, $00 ' 14 left ctrl (right ctrl too)
Data $71, $51 ' 15 qQ
Data $31, $21 ' 16 1!
Data $00, $00 ' 17
Data $00, $00 ' 18
Data $00, $00 ' 19
Data $7A, $5A ' 1A zZ
Data $73, $53 ' 1B sS
Data $61, $41 ' 1C aA
Data $77, $57 ' 1D wW
Data $32, $40 ' 1E 2@
Data $A1, $E1 ' 1F Windows 98 menu key (left side)
Data $00, $00 ' 20
Data $63, $43 ' 21 cC
Data $78, $58 ' 22 xX
Data $64, $44 ' 23 dD
Data $65, $45 ' 24 eE
Data $34, $24 ' 25 4$
Data $33, $23 ' 26 3#
Data $A2, $E2 ' 27 Windows 98 menu key (right side)
Data $00, $00 ' 28
Data $20, $20 ' 29 space
Data $76, $56 ' 2A vV
Data $66, $46 ' 2B fF
Data $74, $54 ' 2C tT
Data $72, $52 ' 2D rR
Data $35, $25 ' 2E 5%
Data $A3, $E3 ' 2F Windows 98 option key (right click, right side)
Data $00, $00 ' 30
Data $6E, $4E ' 31 nN
Data $62, $42 ' 32 bB
Data $68, $48 ' 33 hH
Data $67, $47 ' 34 gG
Data $79, $59 ' 35 yY
Data $36, $5E ' 36 6^
Data $A4, $E4 ' 37 Power Key
Data $00, $00 ' 38
Data $00, $00 ' 39
Data $6D, $4D ' 3A mM
Data $6A, $4A ' 3B jJ
Data $75, $55 ' 3C uU
Data $37, $26 ' 3D 7&
Data $38, $2A ' 3E 8*
Data $A5, $E5 ' 3F Sleep Key
Data $00, $00 ' 40
Data $2C, $3C ' 41 ,<
Data $6B, $4B ' 42 kK
Data $69, $49 ' 43 iI
Data $6F, $4F ' 44 oO
Data $30, $29 ' 45 0)
Data $39, $28 ' 46 9(
Data $00, $00 ' 47
Data $00, $00 ' 48
Data $2E, $3E ' 49 .>
Data $2F, $3F ' 4A /?
Data $6C, $4C ' 4B lL
Data $3B, $3A ' 4C ':
Data $70, $50 ' 4D pP
Data $2D, $5F ' 4E -_
Data $00, $00 ' 4F
Data $00, $00 ' 50
Data $00, $00 ' 51
Data $27, $22 ' 52 '"
Data $00, $00 ' 53
Data $5B, $7B ' 54 [{
Data $3D, $2B ' 55 =+
Data $00, $00 ' 56
Data $00, $00 ' 57
Data $00, $00 ' 58 caps
Data $00, $00 ' 59 r shift
Data $0D, $0D ' 5A <Enter>
Data $5D, $7D ' 5B ]}
Data $00, $00 ' 5C
Data $5C, $7C ' 5D \|
Data $A6, $E6 ' 5E Wake Key
Data $00, $00 ' 5F
Data $00, $00 ' 60
Data $00, $00 ' 61
Data $00, $00 ' 62
Data $00, $00 ' 63
Data $00, $00 ' 64
Data $00, $00 ' 65
Data $08, $08 ' 66 bkspace
Data $00, $00 ' 67
Data $00, $00 ' 68
Data $31, $91 ' 69 kp 1 {End}
Data $2f, $2f ' 6A kp / converted from E04A in code
Data $34, $94 ' 6B kp 4 {Left}
Data $37, $97 ' 6C kp 7 {Home}
Data $00, $00 ' 6D
Data $00, $00 ' 6E
Data $00, $00 ' 6F
Data $30, $90 ' 70 kp 0 {Ins}
Data $2E, $7F ' 71 kp . {Del}
Data $32, $92 ' 72 kp 2 {Down}
Data $35, $95 ' 73 kp 5
Data $36, $96 ' 74 kp 6 {Right}
Data $38, $98 ' 75 kp 8 {Up}
Data $1B, $1B ' 76 esc
Data $00, $00 ' 77 num lock
Data $8B, $CB ' 78 F11
Data $2B, $2B ' 79 kp +
Data $33, $93 ' 7A kp 3 {PgDn}
Data $2D, $2D ' 7B kp -
Data $2A, $2A ' 7C kp *
Data $39, $99 ' 7D kp 9 {PgUp}
Data $8D, $CD ' 7E scroll lock
Data $00, $00 ' 7F
Dim cline As Byte
cline=0
Dim cln As Byte
cln=5
Dim b As Bit
Dim q As Word
Dim w[11] As Byte
Dim tr[11] As Byte
Dim p As Byte
Dim o As Byte
Dim lky As Byte
Dim s As Bit
Dim c As Byte
Dim a As Byte
Dim nl As Byte
Dim sl As Byte
Dim ign As Byte
Dim pa As Byte
Dim tmx As Byte
Dim l As Byte
rs:
ign=1
q=0
b=0
p=0
o=0
c=0
s=0
sl=0
nl=0
pa=0
IRQ=0
OUT=0
If PCON>3 Then PCON=3
DelayMS 100
PIE1=0
PIE1.7=1
INTCON.7=1
INTCON.6=1
Enable
Cls
Print "Init "
doo:
If cbrp<>cbwp And q=0 Then
If cbuf[cbrp]=1 Then
Cls
cline=0
cln=0
ElseIf cbuf[cbrp]=2 Then
ZY=1
ElseIf cbuf[cbrp]=$0D Then
If l<>$0D And l<>$0A Then GoSub clin
ElseIf cbuf[cbrp]=$0A Then
If l<>$0D And l<>$0A Then GoSub clin
ElseIf cbuf[cbrp]=8 Then
Dec cln
If cln<0 And cline>0 Then
Dec cline
If cline=0 Then
Print $FE , 2
ElseIf cline=1 Then
Print $FE, $C0
ElseIf cline=2 Then
Print $FE, $94
EndIf
For cln=0 To 18
Print $FE, $14
Next
Print " "
Else
If cline>0 Or cln>0 Then Print $FE, $10, " ", $FE, $10
EndIf
ElseIf cbuf[cbrp]=7 Then
For tmx=0 To 250
DelayMS 1
PORTA.5=1
DelayMS 1
PORTA.5=0
Next
ElseIf cbuf[cbrp]="{" Then
'I2cin SDA, SCL, Control , Address , 0
FLAGw = 2 : I2C_OUT[0] = 0 : I2C_OUT[1] = 0
GoSub I2CTX
ElseIf cbuf[cbrp]="}" Then
'I2cIn SDA, SCL, Control , Address, ict ' Read the byte from the eeprom
FLAGw = 1 : I2C_OUT[0] = 0 ' 1 Byte out, R/W = 0 because TX subroutine used
GoSub I2CTXP
Print DEC1 I2C_IN[0]
Else
If cln>19 Then GoSub clin
Print cbuf[cbrp]
Inc cln
End If
l=cbuf[cbrp]
Inc cbrp
If cbrp>127 Then cbrp=0
End If
'If ACK=1 Then
'IRQ=0
'If MODE=0 Then OUT=0
'EndIf
'If RST=1 Then
'GoSub resx
'GoTo rs
'EndIf
If CLK=1 Then b=1
If b=1 And CLK=0 Then
b=0
Inc q
If q>1 And q<10 Then
w[q]=DAT
EndIf
If q=10 Then pa=DAT
If q=11 Then
o=0
For q=9 To 2 Step -1
o=o<<1
o=o+w[q]
If w[q]=1 Then pa=1-pa
w[q]=0
Next
If pa=0 Then
GoSub rese
o=0
EndIf
If ign=0 Then
If o=225 Then ign=8
If o=224 Then ign=1
EndIf
If ign>0 Then
Dec ign
o=0
EndIf
If lky=240 And o<>240 Then
If MODE=1 Then OUT=0
If o=88 Then
c=1-c
GoSub leds
EndIf
If o=119 Then
nl=1-nl
GoSub leds
EndIf
If o=126 Then
sl=1-sl
GoSub leds
EndIf
If o=89 Or o=18 Then
s=0
EndIf
o=0
EndIf
If o=89 Or o=18 Then s=1
'If o=102 Then Cls
If o>0 And o<128 Then
'a=0
'If s=1 Then a=1
o=(o*2)+s
Restore o
Read o
If ZY=1 Then
If o="Z" Then o="Y"
ElseIf o="Y" Then o="Z"
If o="z" Then o="y"
ElseIf o="y" Then o="z"
EndIf
If s=0 Then
If c=1 And o>96 And o<123 Then o=o-32
Else
If c=1 And o>64 And o<91 Then o=o+32
End If
OUT=o
o=0
EndIf
lky=o
q=0
EndIf
EndIf
GoTo doo
rese:
tr[0]=0
tr[1]=0
tr[2]=1
tr[3]=1
tr[4]=1
tr[5]=1
tr[6]=1
tr[7]=1
tr[8]=1
tr[9]=0
tr[10]=1
GoSub tra
Return
resx:
tr[0]=0
tr[1]=1
tr[2]=1
tr[3]=1
tr[4]=1
tr[5]=1
tr[6]=1
tr[7]=1
tr[8]=1
tr[9]=1
tr[10]=1
GoSub tra
Return
leds:
tr[0]=0
tr[1]=1
tr[2]=0
tr[3]=1
tr[4]=1
tr[5]=0
tr[6]=1
tr[7]=1
tr[8]=1
tr[9]=1
tr[10]=1
GoSub tra
tr[0]=0
tr[1]=sl
tr[2]=nl
tr[3]=c
tr[4]=0
tr[5]=0
tr[6]=0
tr[7]=0
tr[8]=0
tr[9]=c+nl+sl
If tr[9]>1 Then tr[9]=tr[9]-2
tr[9]=1-tr[9]
tr[10]=1
GoSub tra
Return
tra:
Output CLK
Output DAT
CLK=0
DAT=1
DelayUS 200
CLK=1
DAT=0
Input CLK
q=1
While q <= 11
If CLK=1 Then b=1
If b=1 And CLK=0 Then
b=0
DAT=tr[q]
tr[q]=0
Inc q
EndIf
Wend
Input CLK
Input DAT
DelayUS 200
Return
Sio:
Context SAVE
PIR1.7=0
If OBF=0 Then
PORTD=0
Else
cbuf[cbwp]=PORTD
Inc cbwp
If cbwp>127 Then cbwp=0
EndIf
Context Restore
retfie
clin:
If cline=0 Then
Inc cline
Print $FE , $C0
ElseIf cline=1 Then
Inc cline
Print $FE, $94
ElseIf cline=2 Then
Inc cline
Print $FE, $D4
Else
Print $FE, 1
cline=0
EndIf
cln=0
Return
I2CTX: ' Transmit I2C Routine
GoSub I2C_Start ' Issue Start Condition
RWw = 0 ' “Write” command
GoSub Set_ADDR ' Send Address from variable ADDR
For Nw = 1 To FLAGw ' Shifts out byte[0]..[1]… for number in FLAG times
SHOut SDA, SCL, 1, [I2C_OUT[Nw-1]\8]
SHIn SDA, SCL, 0, [I2C_ACK\1]
Next
GoSub I2C_Stop ' Issue Stop Command
Return
I2CTXP: ' Transmit I2C Routine
GoSub I2C_Start ' Issue Start Condition
RWw = 0 ' “Write” command
SHOut SDA, SCL, 1, [ADDR\8,RWw\1]
SHIn SDA, SCL, 0, [I2C_ACK\1] ' Send Address from variable ADDR
SHOut SDA, SCL, 1, [I2C_OUT[0]\8]
SHIn SDA, SCL, 0, [I2C_ACK\1]
RWw = 1 ' “Read” command
GoSub I2C_Start
SHOut SDA, SCL, 1, [ADDR\8,RWw\1]
SHIn SDA, SCL, 0, [I2C_ACK\1]
SHIn SDA, SCL, 0, [I2C_IN[0]\8]' *** Send last byte with NACK ***
SHOut SDA, SCL, 1, [%1\1]
GoSub I2C_Stop
Return
I2CRX: ' Receive I2C Routine
GoSub I2C_Start
RWw = 1 ' “Read” command
GoSub Set_ADDR
If FLAGw >= 2 Then ' If there is more then 1 byte, send all but last, then ACK
For Nw = 1 To (FLAGw - 1)
SHIn SDA, SCL, 0, [I2C_IN[Nw-1]\8]
SHOut SDA, SCL, 1, [%0\1]
Next
EndIf
SHIn SDA, SCL, 0, [I2C_IN[FLAGw-1]\8]' *** Send last byte with NACK ***
SHOut SDA, SCL, 1, [%1\1]
GoSub I2C_Stop ' Issue Stop command
Return
Set_ADDR: ' Send Address of device from Var ADDR
SHOut SDA, SCL, 1, [ADDR >> 1\7,RWw\1]' Shifts ADDR bits over, adds on R/W bit
SHIn SDA, SCL, 0, [I2C_ACK\1] ' Receives ACK
Return
I2C_Start: ' Start Condition
High SDA
High SCL
Low SDA
Low SCL
Return
I2C_Stop: ' Stop Condition
Low SDA
High SCL
High SDA
DelayMS 1
Return