Syntax

           

' =========================================================================
'
'   File...... SERIN.SXB
'   Purpose... Create simple 7-segment display controller (one digit)
'   Author.... (c) Parallax, Inc. -- All Rights Reserved
'   E-mail.... support@parallax.com
'   Started...
'   Updated... 05 JUL 2006
'
' =========================================================================

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' Waits for a command from a host controller and displays hex value or
' individual segments on a 7-segment LED display.
'
' Commands:
'   "!LEDH", value -- display hex digit value (0 .. F)
'   "!LEDS", value -- display individual segments

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ            4_000_000
ID              "SERIN"

' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

Sio             VAR     RA.0                    ' pull-up via 4.7K

LEDs            VAR     RB                      ' 7-segment display
TRIS_LEDs       VAR     TRIS_B

' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

Baud            CON     "OT9600"
Dash            CON     %01000000

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

serByte         VAR     Byte                    ' serial I/O byte
tmpB1           VAR     Byte                    ' parameter

' =========================================================================
  PROGRAM Start
' =========================================================================

' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------

RX_BYTE         SUB     0                       ' receive serial byte
MAKE_DIGIT      SUB     1                       ' make 7-segs digit

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
  PLP_A = %0001                                 ' pull up unused pins
  PLP_C = %00000000
  LEDs = %00000000                              ' clear display
  TRIS_Leds = %00000000                         ' make port pins outputs

Main:                                           ' wait for header
  serByte = RX_BYTE
  IF serByte <> "!" THEN Main
  serByte = RX_BYTE
  IF serByte <> "L" THEN Main
  serByte = RX_BYTE
  IF serByte <> "E" THEN Main
  serByte = RX_BYTE
  IF serByte <> "D" THEN Main

Check_Hex:
  serByte = RX_BYTE
  IF serByte = "H" THEN                         ' hex display?
    serByte = RX_BYTE                           ' get value
    LEDs = MAKE_DIGIT serByte
    GOTO Main
  ENDIF

Check_Segments:
  IF serByte = "S" THEN                         ' segments display?
    LEDs = RX_BYTE                              ' get segments
    GOTO Main
  ENDIF

Check_X:                                        ' add features here
  ' for future use
  GOTO Main

' -------------------------------------------------------------------------
' Subroutine Code
' -------------------------------------------------------------------------

' Use: rxInput = RX_BYTE
' -- reads byte from serial input and places it in 'rxInput'

RX_BYTE:
  SERIN Sio, Baud, tmpB1                        ' receive a byte
  RETURN tmpB1

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

' Use: destination = MAKE_DIGIT value
' -- converts number in 'value' to 7-segment digit pattern
'    and places it in 'destination'

MAKE_DIGIT:
  tmpB1 = __PARAM1                              ' copy value
  IF tmpB1 <= $F THEN                        ' check range
    READ SegMap + tmpB1, tmpB1                  ' read table value
  ELSE
    tmpB1 = Dash                                ' display dash
  ENDIF
  RETURN tmpB1

' =========================================================================
' User Data
' =========================================================================

SegMap:                                         ' segments maps
'        .gfedcba
  DATA  %00111111                               ' 0
  DATA  %00000110                               ' 1
  DATA  %01011011                               ' 2
  DATA  %01001111                               ' 3
  DATA  %01100110                               ' 4
  DATA  %01101101                               ' 5
  DATA  %01111101                               ' 6
  DATA  %00000111                               ' 7
  DATA  %01111111                               ' 8
  DATA  %01100111                               ' 9

  DATA  %01110111                               ' A
  DATA  %01111100                               ' b
  DATA  %00111001                               ' C
  DATA  %01011110                               ' d
  DATA  %01111001                               ' E
  DATA  %01110001                               ' F

The following BASIC Stamp 2 program can be used to to demonstrate the LED display.

' {$STAMP BS2}
' {$PBASIC 2.5}

' -----[ Program Description ]---------------------------------------------
'
' Test program for SERIN.SXB

' -----[ I/O Definitions ]-------------------------------------------------

Sio             PIN     15

' -----[ Constants ]-------------------------------------------------------

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T1200       CON     813
    T2400       CON     396
    T4800       CON     188
    T9600       CON     84
    T19K2       CON     32
    TMidi       CON     12
    T38K4       CON     6
  #CASE BS2SX, BS2P
    T1200       CON     2063
    T2400       CON     1021
    T4800       CON     500
    T9600       CON     240
    T19K2       CON     110
    TMidi       CON     60
    T38K4       CON     45
  #CASE BS2PX
    T1200       CON     3313
    T2400       CON     1646
    T4800       CON     813
    T9600       CON     396
    T19K2       CON     188
    TMidi       CON     108
    T38K4       CON     84
#ENDSELECT

SevenBit        CON     $2000
Inverted        CON     $4000
Open            CON     $8000

Baud            CON     Open + T9600

' -----[ Variables ]-------------------------------------------------------

idx             VAR     Nib
segments        VAR     Byte
value           VAR     Nib

' -----[ Program Code ]----------------------------------------------------

Main:
  DO
    FOR idx = 0 TO 5
      segments = (1 << idx)
      SEROUT Sio, Baud, ["!LEDS", segments]
      PAUSE 50
    NEXT
    SEROUT Sio, Baud, ["!LEDH", value]
    PAUSE 500
    value = value + 1 // 16
  LOOP
  END