Meter Stamp Box Set Time Date Program

Variables and Declarations

' SetTimeDate.bs2

' Used because lack of program space to keep in MeterStampBox.bs2

' Box with Meter and speaker

' John Saunders 3/2/2009

' {$STAMP BS2}

' {$PBASIC 2.5}



'Stamp Ports

DS1302CE  PIN        0       'Output: DS1302 Chip Enable

DS1302IO  PIN        1       'Bi-directional:DS1302 Data

DIO1      PIN        2       'Input or Output:Digital I/O #1, D-25 pin 19

DIO2      PIN        3       'Input or Output:Digital I/O #2, D-25 pin 20

UPSW      PIN        4       'Input: UP Switch

DNSW      PIN        5       'Input: Down Switch

SDOut     PIN        6       'Output to SD Interface

SDIn      PIN        7       'Input from SD Interface

SelPort   VAR        OUTC    'Selector for 138 and 259 chips, pins 8,9,10

En138     PIN        11      'Output:High pulse to enable the 138

CLK       PIN        12      'Output:Clocks all

InPort    PIN        13      'Input:166 and ADC Data

En259     PIN        14      'Output:259 gate

OutPort   PIN        15      'Output:Display RS,74HC164 & 166 serial in,259 D


'Initialization

Dispinitlen      CON    3

DispInitAddr     CON    0

DispInitData     DATA   $38,$0C,$01,$06 'Need $38(2-line) and use cmd $C0 for second 8 char pos

DateInitAddr     CON    4

SetDateTime      DATA(8)


'Time parameters

TimeDate       VAR      Byte(9) 'Seconds,Minutes,Hours,Date,Month,Weekday,Year,Write Protect,spare


'Display parameters

DECdata   VAR    Word 'Analog value

SignBit   VAR    DECdata.BIT15

div       VAR    Byte  'dividend used in writing numbers

rem       VAR    Word 'remainder used in writing multiple-digit numbers

DispMode  VAR    Nib  'Controls the way a value is to be displayed, bits 0, 1 = no of digits - 1

DPLoc     VAR    DispMode.BIT2  '0=1 digit after DP, 1 = 2

DPOnOff   VAR    DispMode.BIT3  '0 = no decimal point, 1 = DP

DSAddr    VAR    Byte 'DS1302 register address

DSData    VAR    Byte 'DS1302 data is in BCD format

Tens      VAR    DSData.HIGHNIB

Units     VAR    DSData.LOWNIB

DigIn1    VAR    DSData.LOWNIB.BIT3

DigIn2    VAR    DSData.HIGHNIB.BIT0

DigIn3    VAR    DSData.HIGHNIB.BIT1

DigIn4    VAR    DSData.HIGHNIB.BIT2

DigIn5    VAR    DSData.HIGHNIB.BIT3

Char      VAR    Byte 'For display of one character

OldUp     VAR    Bit  'Previous value of Up switch

OldDown   VAR    Bit  'Previous value of Down switch

NoPrompt  VAR    Bit  'SD interface did not give a prompt

BallFlag  VAR    Bit  'One selects White

UPAction  VAR    Bit  'The UP switch was pressed

DNAction  VAR    Bit  'The UP switch was pressed



'Control variables and External Digital Inputs

IOdata    VAR    Byte  'Data to be either input or output

Mode      VAR    Nib   'Bits 0-2 are the mode switch encoded

OldMode   VAR    Nib

Iter      VAR    Nib   'for loop variable


'138 select constants

DispEn    CON    0

SHLD      CON    9      'Also sets gate high since 166 has synchronous load

ADCICE    CON    2

ADCVCE    CON    3

Chime     CON    4

CLR259    CON    5

Beep      CON    6


'259 select constants

Black     CON    0

White     CON    1

PSEn      CON    2

DigOutEn  CON    3

DigAOut   CON    7

DigBOut   CON    5

DIGCOut   CON    4



'1302 Parameters

WRProtData  CON         0

WRProtAddr  CON         $8E

OSCEnAddr   CON         $80

DSRangeAddr CON         $D0

TCHData     CON         %10100101

TCHAddr     CON         $90

BurstRD     CON         $BF


'Rogue Secure Digital Interface parameters

DoneFlag    VAR         Bit  'Recording has been completed

FormatFlag  VAR         Nib  'Add after the display or record: 0=none,1=space,2=slash,3=colon


'Analog parameters

voltoffset  CON         4    'Correction for amplifier offset

ampoffset   CON         4

Range       VAR         Nib  'Index into AmpParams for the selected shunt

                             'Display value,multiplier,divider,Decimal Display mode

AmpParams   DATA   1,4,4,$F,2,4,2,$F,3,14,4,$F,4,4,1,$F,5,24,4,$F,6,14,2,$F,10,4,4,$B,12,2,2,$B,20,4,2,$B

 Start and Main

Start:

DIRS  = %1101111101000001

Selport = CLR259

LOW En138

LOW En259

PULSOUT En138,10                        'Clear the 259

LOW DS1302CE

DoneFlag = 1

LOW En138

HIGH En259

DECData = 0

GOSUB InitDisp

SelPort = PSen

Outport = 1

PULSOUT En259, 10

Outport = 0

SelPort = DigOutEn

Outport = 1

PULSOUT En259, 10

Outport = 0

DsAddr = DsRangeAddr + 1

GOSUB DS1302OUT

Range = Units

IF Range > 8 THEN

  range = 8

ENDIF


Main:                  'Main loop

GOSUB GetInputData

GOSUB GetTimeDate

' Erase and Return Cursor to left if mode changes, else return cursor

Char = 2

IF Mode <> OldMode THEN

  GOSUB SoundBeep

  Char = 1

ENDIF

GOSUB DispCmd

OldMode = Mode

SELECT Mode

CASE 0

Char = $80

GOSUB DispCmd

GOSUB DispAmp

Char = $C2

GOSUB DispCmd

GOSUB DispVolt

CASE 1

  GOSUB Dispdate

CASE 5

  GOSUB DispSetTimedate

  IF DNSW = 0 THEN

   GOSUB ReadTimeDate    'Read the SETTIME.TXT & SETDATE.TXT files on the SD,

  'process the values to BCD and store in 1302RAM

  ELSEIF UPSW = 0 THEN

    GOSUB InitDS

  ENDIF

CASE ELSE

  GOSUB DispTime

ENDSELECT

OldUp = UPSW

OldDown = DNSW

GOTO main

STOP

Subroutines

'subroutines:


'subroutines for Samsung KS0066 LCD display controllers:


InitDisp:

FOR Iter = 0 TO DispInitLen

  READ DispInitAddr + Iter,Char

  GOSUB DispCmd

  PAUSE 200

NEXT

RETURN