Basic Stamp II code: (May 3, 2001 5:00 pm)

 

' Paul Contino

' Personal Robot Technologies

' May 3, 2001

' LS7166test2.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 +5V      { BS Vdd   / LS pin3  }

  ' A,B Gate/Reset Counter    (ABGT/RCTR) to +5V      { BS Vdd   / 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

 

  high WRbit                  ' Initialize BASIC Stamp pins as high to LS7166

  high RDbit

  high CDbit

 

'-----Main Routine-----

 

pause 1000                    ' Wait 1000ms for BASIC Stamp to initialize

 

main:                         ' Main routine

 

  ' Initialize the LS7166

  gosub MASTER_RESET          ' Master Control Register     - "00100000"

  gosub INPUT_SETUP           ' Input Control Register      - "01101000"

  gosub OUTPUT_SETUP          ' Output Control Register     - "10000000"

  gosub QUAD_X1               ' Quadrature Control Register - "11000001"

  gosub CNTR_RESET            ' Master Control Register     - "00000100"

  gosub ADDR_RESET            ' Master Control Register     - "00000001"

  gosub OUTPUT_PRESET         ' Preset Register             - "00001100","00000000","00000000"

  gosub PRESET_CTR            ' Master Control Register     - "00001000"

 

  ' Latch position with software

  gosub LATCH_CNTR            ' Master Control Register     - "00000010"

 

  ' Read position of encoder

  gosub ADDR_RESET            ' Master Control Register     - "00000001"

  gosub READ_POSITION         ' Output Latch

  gosub READ_DIRECTION        ' Output Status Register

 

stop

 

'---------------------

'=====SUBROUTINES=====

'---------------------

 

MASTER_RESET:

 

  high CDbit                  ' "1"       Setting for MCR

  high RDbit                  ' "1"       Setting for MCR

 

  low  Dbit0                  ' "0" LSB

  low  Dbit1                  ' "0"

  low  Dbit2                  ' "0"

  low  Dbit3                  ' "0"

  low  Dbit4                  ' "0"

  high Dbit5                  ' "1"       Master reset

  low  Dbit6                  ' "0"       Setting for MCR

  low  Dbit7                  ' "0" MSB   Setting for MCR

 

  low  WRbit                  ' Write to Master Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'----------------------

 

INPUT_SETUP:

 

  high CDbit                  ' "1"       Setting for ICR

  high RDbit                  ' "1"       Setting for ICR

 

  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

  low  Dbit4                  ' "0"

  high Dbit5                  ' "1"       Allow for transfer from Preset Register to 24-bit counter

  high Dbit6                  ' "1"       Setting for ICR

  low  Dbit7                  ' "0" MSB   Setting for ICR

 

  low  WRbit                  ' Write to Input Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'----------------------

 

OUTPUT_SETUP:

 

  high CDbit                  ' "1"       Setting for OCR

  high RDbit                  ' "1"       Setting for OCR

 

  low  Dbit0                  ' "0" LSB   Binary count mode

  low  Dbit1                  ' "0"       Normal operation setting

  low  Dbit2                  ' "0"       Normal operation setting

  high Dbit3                  ' "0"       Normal operation setting

  low  Dbit4                  ' "0"

  high Dbit5                  ' "0"      

  high Dbit6                  ' "0"       Setting for OCR

  low  Dbit7                  ' "1" MSB   Setting for OCR

 

  low  WRbit                  ' Write to Output Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'----------------------

 

QUAD_X1:

 

  high CDbit                  ' "1"       Setting for QCR

  high RDbit                  ' "1"       Setting for QCR

 

  high Dbit0                  ' "1" LSB   X1 mode

  low  Dbit1                  ' "0"       X1 mode

  low  Dbit2                  ' "0"

  low  Dbit3                  ' "0"

  low  Dbit4                  ' "0"

  low  Dbit5                  ' "0"

  high Dbit6                  ' "1"       Setting for QCR

  high Dbit7                  ' "1" MSB   Setting for QCR

 

  low  WRbit                  ' Write to Quadrature Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'----------------------

 

CNTR_RESET:

 

  high CDbit                  ' "1"       Setting for MCR

  high RDbit                  ' "1"       Setting for MCR

 

  low  Dbit0                  ' "0" LSB

  low  Dbit1                  ' "0"      

  high Dbit2                  ' "1"       Reset the 24-bit counter

  low  Dbit3                  ' "0"

  low  Dbit4                  ' "0"

  low  Dbit5                  ' "0"

  low  Dbit6                  ' "0"       Setting for MCR

  low  Dbit7                  ' "0" MSB   Setting for MCR

 

  low  WRbit                  ' Write to Master Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'----------------------

 

ADDR_RESET:

 

  high CDbit                  ' "1"       Setting for MCR

  high RDbit                  ' "1"       Setting for MCR

 

  high Dbit0                  ' "1" LSB   Reset 3-byte Address Pointer

  low  Dbit1                  ' "0"

  low  Dbit2                  ' "0"

  low  Dbit3                  ' "0"

  low  Dbit4                  ' "0"

  low  Dbit5                  ' "0"

  low  Dbit6                  ' "0"       Setting for MCR

  low  Dbit7                  ' "0" MSB   Setting for MCR

 

  low  WRbit                  ' Write to Master Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'----------------------

 

OUTPUT_PRESET:                ' Preset counter to "00001100" lowest significant byte

 

  low  CDbit                  ' "0"       Setting for PR

  high RDbit                  ' "1"       Setting for PR

 

  low  Dbit0                  ' "0" LSB

  low  Dbit1                  ' "0"

  high Dbit2                  ' "1"       random value

  high Dbit3                  ' "1"       random value

  low  Dbit4                  ' "0"

  low  Dbit5                  ' "0"

  low  Dbit6                  ' "0"

  low  Dbit7                  ' "0" MSB

 

  low  WRbit                  ' Write to Preset Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  '----------------------

 

  low  CDbit                  ' "0"       Setting for PR

  high RDbit                  ' "1"       Setting for PR

 

  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

 

  low  WRbit                  ' Write to Preset Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  '----------------------

 

  low  CDbit                  ' "0"       Setting for PR

  high RDbit                  ' "1"       Setting for PR

 

  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

 

  low  WRbit                  ' Write to Preset Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'----------------------

 

PRESET_CTR:

 

  high CDbit                  ' "1"       Setting for MCR

  high RDbit                  ' "1"       Setting for MCR

 

  low  Dbit0                  ' "0" LSB

  low  Dbit1                  ' "0"

  low  Dbit2                  ' "0"

  high Dbit3                  ' "1"       Transfer 24-bit Preset Register to 24-bit Counter

  low  Dbit4                  ' "0"

  low  Dbit5                  ' "0"

  low  Dbit6                  ' "0"       Setting for MCR

  low  Dbit7                  ' "0" MSB   Setting for MCR

 

  low  WRbit                  ' Write to Master Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'-------------------------

 

LATCH_CNTR:

 

  high CDbit                  ' "1"       Setting for MCR

  high RDbit                  ' "1"       Setting for MCR

 

  low  Dbit0                  ' "0" LSB

  high Dbit1                  ' "1"       Transfer 24-bit Counter contents to 24-bit Output Latch

  low  Dbit2                  ' "0"

  low  Dbit3                  ' "0"

  low  Dbit4                  ' "0"

  low  Dbit5                  ' "0"

  low  Dbit6                  ' "0"       Setting for MCR

  low  Dbit7                  ' "0" MSB   Setting for MCR

 

  low  WRbit                  ' Write to Master Control Register by sending LS7166 a low pulse

  pause 1

  high WRbit

 

  return

'-------------------------

 

READ_POSITION:

 

  'IMPORT COUNT DATA BYTE#1

 

  low  CDbit                  ' "0"       Setting for Output Latch READ

  high WRbit                  ' "1"       Setting for Output Latch READ

 

  low  RDbit                  ' Read counter byte by sending LS7166 a low pulse

  pause 1

  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 1: "

  gosub screen_output

 

  '-------------------------

 

  'IMPORT COUNT DATA BYTE#2

 

  low  CDbit                  ' "0"       Setting for Output Latch READ

  high WRbit                  ' "1"       Setting for Output Latch READ

 

  low  RDbit                  ' Read counter byte by sending LS7166 a low pulse

  pause 1

  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 2: "

  gosub screen_output

 

  '-------------------------

 

  'IMPORT COUNT DATA BYTE#3

 

  low  CDbit                  ' "0"       Setting for Output Latch READ

  high WRbit                  ' "1"       Setting for Output Latch READ

 

  low  RDbit                  ' Read counter byte by sending LS7166 a low pulse

  pause 1

  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 3: "

  gosub screen_output

 

  return

 

'-------------------------

 

READ_DIRECTION:

 

  'IMPORT Direction (bit-4)

 

  low  CDbit                  ' "1"       Setting for Output Status Register READ

  high WRbit                  ' "1"       Setting for Output Status Register READ

 

  low  RDbit                  ' Read counter byte by sending LS7166 a low pulse

  pause 1

  high RDbit

                              ' Read in 24-bit count value 1 byte at a time

 

  count_value.BIT4(read_loop) = in4 ' D4

 

  debug "Direction (1-forward / 0-backward): ",bin in4

 

  return

'-------------------------

 

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