PDA

View Full Version : Program Memory/Memkey issues



strassa
04-25-2007, 06:54 AM
We have a basic stamp set up and our keypad input is integrated with the memkey device. The look-up table for the memkey takes up a lot of space unfortunately and we are struggling to finish our code with the available program memory. Does anyone have any advice for streamlining our code?

Attached·is our code, we·are new to Stamp·so there are probably obvious things we can do:

·················································· ·· ' ================================================== =======================
'
'·· File...... Serial_LCD_Template.BS2
'·· Purpose... Template for Parallax Serial LCD
'·· Author.... (c) Parallax, Inc. -- All Rights Reserved
'·· E-mail.... support@parallax.com (mailto:support@parallax.com)
'·· Started...
'·· Updated... 11 FEB 2005
'
'·· {$STAMP BS2}
'·· {$PBASIC 2.5}
'
' ================================================== =======================

' -----[ Program Description ]---------------------------------------------

' -----[ Revision History ]------------------------------------------------

' -----[ I/O Definitions ]-------------------------------------------------
TX············· PIN···· 0·············· ' serial output to LCD

' -----[ Constants ]-------------------------------------------------------
#SELECT $STAMP
· #CASE BS2, BS2E, BS2PE
··· T2400······ CON···· 396
··· T9600······ CON···· 84
··· T19K2······ CON···· 32
· #CASE BS2SX, BS2P
··· T2400······ CON···· 1021
··· T9600······ CON···· 240
··· T19K2······ CON···· 110
#ENDSELECT
LcdBaud········ CON···· T19K2
'LcdBkSpc······· CON···· $08············ ' move cursor left
'LcdRt·········· CON···· $09············ ' move cursor right
'LcdLF·········· CON···· $0A············ ' move cursor down 1 line
LcdCls········· CON···· $0C············ ' clear LCD (use PAUSE 5 after)
'LcdCR·········· CON···· $0D············ ' move pos 0 of next line
'LcdBLon········ CON···· $11············ ' backlight on
LcdBLoff······· CON···· $12············ ' backlight off
LcdOff········· CON···· $15············ ' LCD off
LcdOn1········· CON···· $16············ ' LCD on; cursor off, blink off
'LcdOn2········· CON···· $17············ ' LCD on; cursor off, blink on
'LcdOn3········· CON···· $18············ ' LCD on; cursor on, blink off
'LcdOn4········· CON···· $19············ ' LCD on; cursor on, blink on
'LcdLine1······· CON···· $80············ ' move to line 1, column 0
'LcdLine2······· CON···· $94············ ' move to line 2, column 0
LcdCC0········· CON···· $F8············ ' define custom char 0
'LcdCC1········· CON···· $F9············ ' define custom char 1
'LcdCC2········· CON···· $FA············ ' define custom char 2
'LcdCC3········· CON···· $FB············ ' define custom char 3
'LcdCC4········· CON···· $FC············ ' define custom char 4
'LcdCC5········· CON···· $FD············ ' define custom char 5
'LcdCC6········· CON···· $FE············ ' define custom char 6
'LcdCC7········· CON···· $FF············ ' define custom char 7

' -----[ Variables ]-------------------------------------------------------
idx1··········· VAR···· Byte
'idx2··········· VAR···· Byte
char··········· VAR···· Byte
'newChar········ VAR···· Byte
IDX············· VAR···· Byte
' -----[ EEPROM Data ]-----------------------------------------------------
CC0············ DATA··· LcdCC0, $0E, $1F, $1C, $18, $1C, $1F, $0E, $00
'CC1············ DATA··· LcdCC1, $0E, $1F, $1F, $18, $1F, $1F, $0E, $00
'CC2············ DATA··· LcdCC2, $0E, $1F, $1F, $1F, $1F, $1F, $0E, $00
'Smiley········· DATA··· LcdCC3, $00, $0A, $0A, $00, $11, $0E, $06, $00
'Msg2··········· DATA··· "IS VERY COOL!· ", 3
' -----[ Initialization ]--------------------------------------------------
Reset:
· HIGH TX······························ ' setup serial output pin
· PAUSE 100···························· ' allow LCD to initialize
DnLoad_Custom_Chars:
· FOR idx1 = 0 TO 35··················· ' download 4 characters
··· READ CC0 + idx1, char·············· ' get data from table
··· SEROUT TX, LcdBaud, [char]········· ' send to LCD
· NEXT

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

Z_score1 DATA -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1········· 'Setting aside space for patient's first z-score
Z_score2 DATA -1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1············· 'Setting aside memory space for patient's second z-score
AGE1 DATA -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1········· 'Setting aside space for patient's first age
'AGE2 DATA -1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1,-1 , -1, -1, -1, -1············· 'Setting aside memory space for patient's second age
Z_mean DATA 3, 3, 4, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 7, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 10, 10, 11, 11, 10, 11, 10, 11, 9, 10, 11, 12, 10, 11, 13, 11, 12, 12, 12, 11, 12, 12, 13, 12, 14, 14, 12, 15, 13, 14, 12, 14, 15, 15, 13, 14··············· 'Hardcoding mean z scores for age. CONSTANTS.
Z_sd DATA 1, 0, 1, 1, 1, 2, 2, 2, 2, 1, 1, 0, 3, 3, 2, 0, 3, 2, 3, 3, 3, 3, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 4, 3, 2, 4, 2, 2, 3, 2, 3, 2, 4, 2, 3, 3, 3, 2, 2, 3, 0, 3, 2, 3, 2, 1, 2, 3, 3·············· 'Hardcoding z score standard deviations. CONSTANTS


Main:
· ' your code here
· 'dummy VAR Byte (8)
· 'dummy(0) ="Z"
· 'dummy(1) ="-"
· 'dummy(2) ="S"
· 'dummy(3) ="c"
· 'dummy(4) ="o"
· 'dummy(5) ="r"
· 'dummy(6) ="e"
· 'dummy(7)="="
· INPUT 15 'TM
OUTPUT 14 'FM
INPUT 13 'KEY
'
BEGIN:
'
HIGH 14 'let noise settle
PAUSE 1000
SEROUT 14,396,[$02,$F0,$0F] 'program typematic: delay = 600mS, rate = 37.5mS
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$03] 'read typematic
SERIN 15,396,[B0,B1]
'DEBUG "delay is: ",DEC B0, CR, "rate is: ",DEC B1,CR,CR
'
SEROUT 14,396,[$04,$10] 'program debounce: time = 40mS
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$05] 'read debounce
SERIN 15,396,[B0]
'DEBUG "debounce is: ",DEC B0,CR,CR
'
SEROUT 14,396,[$08,$18,$AA] 'program user EEPROM address $18 with $AA
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$09,$18] 'read user EEPROM to ensure wrote correctly
SERIN 15,396,[B0]
'DEBUG "EEPROM value is: ",DEC B0,CR,CR
'
SEROUT 14,396,[$0A,$00,$40] 'program serial key 0 to return $40
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$01,$41] 'program serial key 1 to return $41
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$02,$42] 'program serial key 2 to return $42
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$03,$43] 'program serial key 3 to return $43
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$04,$44] 'program serial key 4 to return $44
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$05,$45] 'program serial key 5 to return $45
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$06,$46] 'program serial key 6 to return $46
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$07,$47] 'program serial key 7 to return $47
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$08,$48] 'program serial key 8 to return $48
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$09,$49] 'program serial key 9 to return $49
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$0A,$4A] 'program serial key 10 to return $4A
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$0B,$4B] 'program serial key 11 to return $4B
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$0C,$4C] 'program serial key 12 to return $4C
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$0D,$4D] 'program serial key 13 to return $4D
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$0E,$4E] 'program serial key 14 to return $4E
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$0F,$4F] 'program serial key 15 to return $4F
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$10,$50] 'program serial key 16 to return $50
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$11,$51] 'program serial key 17 to return $51
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$12,$52] 'program serial key 18 to return $52
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0A,$13,$53] 'program serial key 19 to return $53
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0B,$13] 'read serial key 19
SERIN 15,396,[B0]
'DEBUG "Key 19 is: ",DEC B0,CR,CR
'
SEROUT 14,396,[$0E,$00] 'program config byte: poll mode, typematic off
PAUSE 10 'allow EEPROM to write
SEROUT 14,396,[$0F] 'read configuration byte
SERIN 15,396,[B0]
'DEBUG "Config byte is: ",BIN B0,CR,CR
'
SEROUT 14,396,[$10] 'test mode
SERIN 15,396,[B0]
'DEBUG "Firmare version is: ",DEC B0,CR,CR
'
DIGIT VAR Nib
OLD VAR Byte
CURRENT VAR Byte
CIT VAR Nib
GENDER VAR Bit
WEIGHT VAR Byte
LENGTH VAR Byte
AGE VAR Byte
AGE=0
WEIGHT=0
LENGTH=0

'SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " Enter Gender "]
'PAUSE 250
SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " MALE=0 FEMALE=1 "]
PAUSE 250

OLD=0
CIT=0
CURRENT=0

'DEBUG "CIT is: ",DEC CIT, " C ", DEC CURRENT, " OLD ", DEC OLD, " G ", DEC Gender," A ", DEC AGE, " W ", DEC weight, "L", DEC length, CR,CR
'DEBUG "CURRENT is: ",DEC CURRENT,CR,CR
'DEBUG "OLD is: ", DEC OLD,CR,CR
'DEBUG "GENDER is: ",DEC GENDER,CR,CR
'DEBUG "AGE is: ",DEC AGE,CR,CR
'DEBUG "WEIGHT is: ", DEC weight,CR,CR
'DEBUG "length is: ",DEC length,CR,CR
KeyScanStart:
'DEBUG "Press any key",CR,CR
KeyScanLoop:
SEROUT 14,396,[$01] 'read # of keys
SERIN 15,396,[B0]
IF B0 = 0 THEN KeyScanLoop 'if no keys in buffer keep looking
'
ReadKeys: 'keys in buffer so read buffer
CIT = (CIT+1)
'DEBUG " OLD4 ", DEC OLD, CR,CR
SEROUT 14,396,[$00]
SERIN 15,396,[B0] 'get key value
'DEBUG "Keypress is: ",DEC B0,CR,CR
' SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
'· PAUSE 250
'· SEROUT TX, LcdBaud, [? B0]
'· PAUSE 250
'FindDIG:
'D=ENTER

'DEBUG "BO is: ",DEC B0,CR,CR
'DEBUG " OLD1 ", DEC OLD, CR,CR
IF B0=79 THEN DIGIT = 1
IF B0=78 THEN DIGIT=4
IF B0=77 THEN DIGIT=7
IF B0=76 THEN DIGIT="*"
IF B0=75 THEN DIGIT=2
IF B0=74 THEN DIGIT=5
IF B0=73 THEN DIGIT=8
IF B0=72 THEN DIGIT=0
IF B0=71 THEN DIGIT=3
IF B0=70 THEN DIGIT=6
IF B0=69 THEN DIGIT=9
IF B0=68 THEN DIGIT="#"
IF B0=67 THEN DIGIT="A"
IF B0=66 THEN DIGIT="B"
IF B0=65 THEN DIGIT="C"
IF B0=64 THEN DIGIT="D"
'DEBUG "digit ", DEC DIGIT, CR, CR
'DEBUG " OLD2 ", DEC OLD, CR,CR
IF (CIT=1 AND DIGIT<>"D" AND DIGIT<>"A" AND DIGIT<>"B" AND DIGIT<>"C" AND DIGIT<>"*" AND DIGIT<>"#") THEN
· GENDER=DIGIT
ELSEIF (CIT<>1 AND DIGIT<>"D" AND DIGIT<>"A" AND DIGIT<>"B" AND DIGIT<>"C" AND DIGIT<>"*" AND DIGIT<>"#")· THEN
· CURRENT=((OLD*10) + DIGIT)
ENDIF

IF (CIT//2=0 AND CIT<>1) THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ", ? CURRENT]
'IF(CIT=2) THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ", ? CURRENT]
PAUSE 250
· 'IF DIGIT<>"D" THEN CURRENT=((OLD*10)+DIGIT)
'DEBUG "CIT is: ",DEC CIT,CR,CR
'DEBUG "CURRENT is: ",DEC CURRENT,CR,CR
'DEBUG "OLD is: ",DEC OLD,CR,CR
'DEBUG "CIT is: ",DEC CIT, " C ", DEC CURRENT, " OLD ", DEC OLD, CR, CR
IF CIT=1 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ", ? GENDER]
'IF CIT=1 THEN OLD=0
'DEBUG " OLD3 ", DEC OLD, CR, CR

IF CIT=3 THEN AGE=CURRENT
IF CIT=3 THEN CURRENT=0
IF CIT=3 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ", ? AGE]
IF CIT=5 THEN WEIGHT=CURRENT
IF CIT=5 THEN CURRENT=0
IF CIT=5 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ", ? WEIGHT]
IF CIT=7 THEN LENGTH=CURRENT
IF CIT=7 THEN CURRENT=0
IF CIT=7 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ", ? LENGTH]
IF (CIT=9 AND CURRENT=0) THEN IDX=IDX+1
IF CIT=9 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ", ? IDX]
PAUSE 1000

'DEBUG "GENDER is: ",DEC GENDER,CR,CR
'DEBUG "AGE is: ",DEC AGE,CR,CR
'DEBUG "WEIGHT is: ",DEC weight,CR,CR
'DEBUG "length is: ",DEC length,CR,CR
'DEBUG " G ", DEC Gender," A ", DEC AGE, " W ", DEC weight, " L ", DEC length, CR,CR
IF CIT=1 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " ---ENTER AGE IN MONTHS---"]
IF CIT=3 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " --ENTER WEIGHT IN POUNDS--"]
IF CIT=5 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " --ENTER LENGTH IN INCHES--"]
IF CIT=7 THEN SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls, " --ENTER IDX as II--"]
IF CIT=9 THEN GOTO Zscore
OLD = CURRENT
IF DIGIT="A" THEN CIT=0
IF DIGIT="A" THEN CURRENT=0
IF DIGIT="A" THEN OLD=0
IF (DIGIT<>"D") THEN
· GOTO KeyScanStart
ELSE
· GOTO Zscore
ENDIF
PAUSE 2000

'****HERE IS WHERE OLD IS THE VALUE FOR ZSCORE CALCULATION******
'dummy VAR Byte (8)
'· dummy(0) ="Z"
'· dummy(1) ="-"
'· dummy(2) ="S"
'· dummy(3) ="c"
'· dummy(4) ="o"
'· dummy(5) ="r"
'· dummy(6) ="e"
'· dummy(7)="="
· Z_score VAR Byte
Zscore:
· Z_ref VAR Byte (2)
· READ (IDX),Z_ref(1)
· READ (IDX+100),Z_ref(2)
· 'Z_ref(0) = 0
· 'Z_ref(1) = 1
· 'Z_ref(2) = 2
· 'Z_ref(3) = 3
· Temp VAR Byte
· READ (AGE+400), Temp
· Temp2 VAR Byte
· READ (AGE+460), Temp2
· 'DEBUG "IDX, TEMP, TEMP2, ", DEC IDX, " ", DEC Temp, " ", DEC TEMP2, CR, CR
· Z_score = (LENGTH-Temp)/Temp2
· IF Z_ref(1)=-1 THEN WRITE IDX, Z_score
· IF(Z_ref(1)<>-1 AND Z_ref(2)=-1) THEN WRITE IDX+100, Z_score
· 'Z_score = STR Z_score

· '=LOOKUP (Z_score) , newChar
· SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
· PAUSE 250
· SEROUT TX, LcdBaud, [? Z_score]
· PAUSE 10000



· GOTO Main
· END

'SEROUT TX, LcdBaud, ["Begin again: " ]
'OLD=0
'CURRENT=0
'GOTO KeyScanStart

'GOTO KeyScanStart



· '=LOOKUP (Z_score) , newChar
· 'SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
· 'PAUSE 250
· 'SEROUT TX, LcdBaud, [? B0]
· 'PAUSE 250
· ' GOTO Main

' -----[ Subroutines ]-----------------------------------------------------·

strassa
04-25-2007, 07:59 AM
In addition: How much program memory is there on the basic stamp?

PJ Allen
04-25-2007, 08:38 AM
The BS-2 has 2KBytes of program memory (the EEPROM.)

Update -- Right off, to economize on ROM usage, you can start by changing a lot of the text that you're printing (use abbreviations -- Wgt., Len.;·use 'Sex' instead of 'gender'.)

Update 2 -- All that lookup table (ZData, etc.) "set-aside".· Why is all of that necessary, what's it all about?

Post Edited (PJ Allen) : 4/25/2007 12:47:08 AM GMT

strassa
04-25-2007, 08:55 AM
The entire first part of the code was taken off the MEMKey website. I am unsure exactly what it does I just know it is working. I thought it was allowing the inputs and outputs of the memkey to register with the numpad. Not sure...does anyone know any easy look up table to do instead?