Basic Stamp II code: (May 3, 2001 5:00 pm)
' Paul
Contino
'
Personal Robot Technologies, Inc.
' May
3, 2001
'
LS7166test.bs2
' This
code will allow for testing and implementation of the LS7166
' IC
for use between one wheel encoder and the BASIC Stamp II
'{$STAMP
BS2} ' Code programmed for
BS2
'-----Declarations-----
WRbit con
10 ' Write { BS pin10 / LS pin1 }
' Chip Select Input (CS) to GND { BS GND
/ LS pin2 }
' Load Counter/Load Latch (LCTR/LLTC) to GND { BS GND / LS
pin3 }
' A,B Gate/Reset Counter (ABGT/RCTR) to GND { BS GND / LS
pin4 }
' Vdd +5V {
BS Vdd / LS pin5 }
' A - Count Input from wheel encoder { Encoder A/ LS pin6 }
' B - Count Input from wheel encoder { Encoder B/ LS pin7 }
Dbit0 con
0 ' Data bus { BS pin0 / LS pin8
}
Dbit1 con
1 ' Data bus { BS pin1 / LS pin9
}
Dbit2 con
2 ' Data bus { BS pin2 / LS pin10 }
Dbit3 con
3 ' Data bus { BS pin3 / LS pin11 }
Dbit4 con
4 ' Data bus { BS pin4 / LS pin12 }
Dbit5 con
5 ' Data bus { BS pin5 / LS pin13 }
Dbit6 con
6 ' Data bus { BS pin6 / LS pin14 }
Dbit7 con
7 ' Data bus { BS pin7 / LS pin15 }
' Carry Output (CY) to GND { BS GND / LS pin16 }
' Borrow Output (BW) to GND {
BS GND / LS pin17 }
CDbit con
8 ' Control/Data Input { BS pin8
/ LS pin18 }
RDbit con
9 ' Read { BS pin9 / LS pin19 }
' Vss GND {
BS GND / LS pin20 }
' Reserved BASIC Stamp pins:
'
pin11 for miscellaneous use
' pin12
for servo
' pin13
for servo
' pin14
for RF output
' pin15
for RF input
count_value
var byte(3) ' 3-byte array for 24-bit counter value
read_loop
var nib ' Loop through reading cycle 3 time maximum
'-----Initialization-----
output Dbit0 '
Initialize BASIC Stamp pins as outputs to LS7166 bus
output Dbit1
output Dbit2
output Dbit3
output Dbit4
output Dbit5
output Dbit6
output Dbit7
output WRbit '
Initialize BASIC Stamp pins as outputs to LS7166
output RDbit
output CDbit
'-----Main
Routine-----
main: ' Main routine
debug "BASIC Stamp pins:" ' Inital pin state
gosub screen_output ' Call screen output pin subroutine
' LS7166 initialization for counting and
direction determination
gosub master_control ' Call Master Control Register subroutine
debug "Master Control Register:"
gosub screen_output ' Call screen output pin subroutine
gosub input_control ' Call Input Control Register subroutine
debug "Input Control Register:"
gosub screen_output ' Call screen output pin subroutine
gosub output_control ' Call Output Control Register subroutine
debug "Output Control Register:"
gosub screen_output ' Call screen output pin subroutine
gosub quadrature_control ' Call Quadrature Contol Register subroutine
debug "Quadrature Control
Register:"
gosub screen_output ' Call screen output pin subroutine
debug "Clearing out Preset Register . .
. ",cr,cr
gosub preset_register ' Call Preset Register subroutine to clear out counter
gosub output_latch ' Call Counter Output Latch subroutine to read in counter
debug cr,"END"
stop '
End main
'-----Subroutines-----
' Write To Register:
' Set high/low to correct pins.
' Send pulse low to WR to write to register.
' Register receives value at pin and writes
it in to register.
master_control: ' Set Master Control Register
[MCR]
high CDbit '
"1" Setting for MCR
high RDbit '
"1" Setting for MCR
' D7->D1
' 00111111
high Dbit0 '
"1" LSB Reset 3-byte Address
Pointer
high Dbit1 '
"?" Transfer 24-bit
Counter contents to 24-bit Output Latch
high Dbit2 '
"1" Reset 24-bit Counter
high Dbit3 '
"?" Transfer the 24-bit
Preset Register to the 24-bit Counter
high Dbit4 '
"1" Reset Comparator
Master Toggle Flip-flop
high Dbit5 '
"1" Master Reset
low
Dbit6 '
"0" Setting for MCR
low
Dbit7 '
"0" MSB Setting for MCR
pulsout WRbit,500 ' Write to Master
Control Register - 1ms pulse low
return '
End master_control
input_control: ' Set Input Control Register
[ICR]
high CDbit '
"1" Setting for ICR
high RDbit '
"1" Setting for ICR
' D7->D1
' 01111000
low
Dbit0 '
"0" LSB Normal operation
setting
low
Dbit1 '
"0" Normal operation
setting
low
Dbit2 '
"0" Normal operation
setting
high Dbit3 '
"1" Enable A&B inputs
high Dbit4 '
"?"
high Dbit5 '
"?"
high Dbit6 '
"1" Setting for ICR
low
Dbit7 '
"0" MSB Setting for ICR
pulsout WRbit,500 ' Write to Input Control Register - 1ms pulse low
return '
End input_control
output_control: ' Set Output Control Register
[OCR]
high CDbit '
"1" Setting for OCR
high RDbit '
"1" Setting for OCR
' D7->D1
' 10000000
low
Dbit0 '
"0" LSB Binary count mode
low
Dbit1 '
"0" Normal operation
setting
low
Dbit2 '
"0" Normal wrap-around
count mode
low
Dbit3 '
"0" Normal operation
setting
low
Dbit4 ' "?" Carry/Borrow
low
Dbit5 '
"?" Carry/Borrow
low
Dbit6 '
"0" Setting for OCR
high
Dbit7 '
"1" MSB Setting for OCR
pulsout WRbit,500 ' Write to Output Control Register - 1ms pulse low
return '
End output_control
quadrature_control: ' Set Quadrature Control Register
[QCR]
high CDbit '
"1" Setting for QCR
high RDbit '
"1" Setting for QCR
' D7->D1
' 11000001
high Dbit0 '
"1" LSB X1 mode setting
low
Dbit1 '
"0" X1 mode setting
low
Dbit2 '
"0" N/A
low
Dbit3 '
"0" N/A
low
Dbit4 '
"0" N/A
low
Dbit5 '
"0" N/A
high Dbit6 '
"1" Setting for QCR
high Dbit7 '
"1" MSB Setting for QCR
pulsout WRbit,500 ' Write to Quadrature Control Register - 1ms pulse low
return '
End quadrature_control
preset_register: ' Write to Preset Register 24-bit
value of "0"
low
CDbit '
"0" Setting for Preset
Register WRITE
high RDbit '
"1" Setting for Preset
Register WRITE
for read_loop = 1 to 3 ' Read byte1 then byte2 then byte3 then
reset pointer to byte1 address
pulsout WRbit,500 ' Write counter byte by sending LS7166 a 1ms low pulse
' Write to 24-bit
counter 1 byte at a time
' D7->D1
' 00000000
low
Dbit0 '
"0" LSB
low
Dbit1 '
"0"
low
Dbit2 '
"0"
low
Dbit3 '
"0"
low
Dbit4 '
"0"
low
Dbit5 '
"0"
low
Dbit6 '
"0"
low
Dbit7 '
"0" MSB
next '
Loop until all 3 bytes written
return '
End preset_register
output_latch: ' Read from Counter Output Latch
low
CDbit '
"0" Setting for Output
Latch READ
high WRbit '
"1" Setting for Output
Latch READ
high RDbit '
"1" Initial READ set to
high
for read_loop = 1 to 3 ' Read byte1 then byte2 then byte3 then
reset pointer to byte1 address
' pulsout RDbit,500 ' Read counter byte by sending LS7166 a 1ms low pulse
low RDbit '
Read counter byte by sending LS7166 a 1ms low pulse
high
RDbit
' Read in 24-bit
count value 1 byte at a time
count_value.BIT0(read_loop) = in0 ' D0 {LSB}
count_value.BIT1(read_loop) = in1 ' D1
count_value.BIT2(read_loop) = in2 ' D2
count_value.BIT3(read_loop) = in3 ' D3
count_value.BIT4(read_loop) = in4 ' D4
count_value.BIT5(read_loop) = in5 ' D5
count_value.BIT6(read_loop) = in6 ' D6
count_value.BIT7(read_loop) = in7 ' D7 {MSB}
debug "Byte ",DEC
read_loop,": ",BIN8 count_value(read_loop),cr
next '
Loop until all 3 bytes read
return '
End output_latch
screen_output: ' Use DEBUG to print pin LS7166
bus to screen
debug cr '
New line
debug "01234567" ' Pins corresponding to BASIC Stamp
debug cr '
New line
debug "--------" ' Line break
debug cr '
New line before pin values
debug bin in0,bin in1,bin in2,bin in3,bin
in4,bin in5,bin in6,bin in7
debug cr,cr '
Blank line
return '
End screen_output