البرنامج التقليدى :
كود:
' Serial I/O Interface-74164-74165
Device = 16F628A
Xtal 4
All_Digital true
'Initialize Ports(s)
TRISB = %00000100
Dim Var1 As Byte
Low PORTB.5 'Set CLK low
High PORTB.4 'Bring CLK inhibit high , actice
High PORTB.3 'Bring shift/load high , load deactive
start:
GoSub serial_in 'Get number from 74LS165
GoSub serial_out 'Send it out on 74LS164
Pause 1000 'Wait so I can see it
GoTo start 'Do it again
'Serial In Routine
serial_in:
PulsOut PORTB.3,1 'Bring shift/load down momentarily , toggel from high to low for 1x 10usec then toggle to low again
Low PORTB.4 'Bring CLK inhibit low , enable clock
Var1.7 = PORTB.2 'Read RB2 input and Load bit into Var1 ,begin from MSB
PulsOut PORTB.5,1 'Bring CLK pin high, then low' , Clock for next pulse
Var1.6 = PORTB.2 'Load Bit into Var1 , SO bit 6
PulsOut PORTB.5,1
Var1.5 = PORTB.2
PulsOut PORTB.5,1
Var1.4 = PORTB.2
PulsOut PORTB.5,1
Var1.3 = PORTB.2
PulsOut PORTB.5,1
Var1.2 = PORTB.2
PulsOut PORTB.5,1
Var1.1 = PORTB.2
PulsOut PORTB.5,1
Var1.0 = PORTB.2
High PORTB.4 'Bring CLK inhibit high
Return
'Serial Out Routine
serial_out:
PORTB.0 = Var1.7 'Bring pin 0 high or low, depending upon bit Var1.7 (MSB)
PulsOut PORTB.1,1 'Bring CLK line high, then low , PORT.Pin , Period x 10 usec at 4MHz
PORTB.0 = Var1.6 'Same as above
PulsOut PORTB.1,1
PORTB.0 = Var1.5
PulsOut PORTB.1,1
PORTB.0 = Var1.4
PulsOut PORTB.1,1
PORTB.0 = Var1.3
PulsOut PORTB.1,1
PORTB.0 = Var1.2
PulsOut PORTB.1,1
PORTB.0 = Var1.1
PulsOut PORTB.1,1
PORTB.0 = Var1.0
PulsOut PORTB.1,1
Return
البرنامج المعدل مع استخدام الأمر SHOut والأمر SHIn :
كود:
' Serial I/O Interface-74164-74165-mod
Device = 16F628A
Xtal 4
All_Digital true
'Initialize Ports(s)
TRISB = %00000100
Dim Var1 As Byte
Symbol DTA_out = PORTB.0
Symbol CLK_out = PORTB.1
Symbol DTA_in = PORTB.2
Symbol CLK_in = PORTB.5
Low PORTB.5 'Set CLK low
High PORTB.4 'Bring CLK inhibit high , actice
High PORTB.3 'Bring shift/load high , load deactive
start:
GoSub serial_in 'Get number from 74LS165
GoSub serial_out 'Send it out on 74LS164
Pause 1000 'Wait so I can see it
GoTo start 'Do it again
'Serial In Routine
serial_in:
PulsOut PORTB.3,1 'Bring shift/load down momentarily
Low PORTB.4 'Bring CLK inhibit low , enable clock
SHIn DTA_in, CLK_in, MsbPre, [Var1] ' Shift in msb-first, pre-clock
High PORTB.4 'Bring CLK inhibit high
Return
'Serial Out Routine
serial_out:
SHOut DTA_out, CLK_out, MsbFirst, [Var1]
Return