Hi all, after briefly dabbling with basic 30 years ago im trying to get back into it and learn via the basic micro chips. The original code as not written by me but i believe sort off works. Reading through it makes sence (to me atleast). I have tested it via isis proteus with a schematic running the bs2sx and lcds. Other than possible improvements it seems to work. Running the code in build mode within basicmicro studio in it comes up with errors which im struggling to work out and would appreciate any help, suggestions or solutions. Note my intention is to use a nano 18 with 3 switches on P0,P1,P2, stepper motor on P3,P4,P5,P6 lcd in 4bit mode on the rest. Most errors seem to be about the idx, the first being line 100 "idx=welcm_msg". then 108 "idx=standby" next line 125 "IF hours>23 THEN hours=0" which seems to be solved by droping "hours=0" onto the next line. Then line 144 "LOOKUP setstate,[set_feed, set_time, set_temp, set_feednow, set_end],idx" again idx? Any solutions please provided me with an explanation so i can look an learn. Many thanks in advance.
Code:
'AFF Software for BSX2-OEM
' {$STAMP BS2sx}
' {$PBASIC 2.5}
'#SELECT $STAMP
' #CASE BS2
' #ERROR "BS2e or greater required."
' #CASE BS2E, BS2SX
' Slot CON 63
'#CASE BS2P, BS2PE, BS2PX
' Slot CON 127
'#ENDSELECT
' defines variables to use in the program
lcdchar VAR Byte ' character to be output by the LCD routine
lcd_low VAR lcdchar.LOWNIB ' break up byte for 4 bit lcd interface
lcd_hi VAR lcdchar.HIGHNIB ' break up byte for 4 bit lcd interface
idx VAR Byte ' counter for printout looping
hours VAR Byte ' hours - time (main loop)
mins VAR Byte ' counts minutes to keep time (main loop)
fd_hours VAR Byte ' feed time hours
fd_mins VAR Byte ' ' feed time minutes
secs VAR Byte ' seconds - time (main loop)
tmp VAR Byte ' temprary storage for calculations
setstate VAR Byte ' current state that we are setting
' I/O definitions
' Three push buttons ( IN )
sw1 VAR IN0
sw2 VAR IN1
sw3 VAR IN2
' Motor Control
mtout VAR OUTB
' LCD control lines
L_ENA VAR OUT13
L_RS VAR OUT14
L_NIBOUT VAR OUTC ' Nibble for out8-out11
L_RW VAR OUT12
' constants
debounce CON 200 ' 200 ms debounce
stpdly CON 30 ' delay between steps
stp1 CON %1001 ' stepper motor h-bridge combinations
stp2 CON %0101
stp3 CON %0110
stp4 CON %1010
moff CON %1111 ' turn motor off
Init: ' Resets Direction registers, motor outputs to 1 (i.e. 0)
' program the IO direction register to our needs
' input=0 output=1
' 5432109876543210
DIRS = %1111111111111000
' ---RDDDDREMotSwt
' resets outputs (inverter buffers on motor transistors)
' 5432109876543210
OUTS = %0000000000111000 ' motor output low
' ---RDDDDREMotSwt
'init motor
GOSUB fwd
' initialize LCD display
PAUSE 1000
L_RS=%0
L_RW=%0
L_ENA=%0
L_NIBOUT=%0011
GOSUB lcdout '1
PAUSE 6 ' wait for more than 4.1ms
L_NIBOUT=%0011
GOSUB lcdout '2
PAUSE 1 ' wait for more than 100us
L_NIBOUT=%0011
GOSUB lcdout '3
L_NIBOUT=%0010 ' last init nibble this is a 8-bit instruction
GOSUB lcdout '4
L_NIBOUT=%0010 ' 001x:0= 4bit interface
GOSUB lcdout '5a
L_NIBOUT=%1000 ' N=1(2 lines) F=0 (5x8dots)
GOSUB lcdout '5b
L_NIBOUT=%0000 ' Display on
GOSUB lcdout '6a
L_NIBOUT=%1111 ' 1DCB display, cursor and blink on=1/off=0
GOSUB lcdout '6b
L_NIBOUT=%0000 ' Display clear
GOSUB lcdout '7a
L_NIBOUT=%0001 ' =0001
GOSUB lcdout '7b
L_NIBOUT=%0000 ' Entry mode set
GOSUB lcdout '8a
L_NIBOUT=%0110 ' 0 1 I/D S cursor behaviour and shifting
GOSUB lcdout '8b
L_RS=%1 ' we are assuming printing all times, no commands
lcdchar=0 ' prints welcome message
GOSUB setpos
idx=welcm_msg
GOSUB prt_str
PAUSE 1000 ' waits for 5 seconds and goes to standby
stdby:
lcdchar=0 ' prints standby
GOSUB setpos
idx=standby
GOSUB prt_str
stdbyloop:
GOSUB printtim ' prints time on second line
' detect buttons
FOR secs = 0 TO 5 ' seconds
PAUSE 980 ' 980ms delay
IF (sw1 | sw2 | sw3) THEN GOTO uiset 'jumps to setup ui if pressed
NEXT
' update clock
mins=mins+1
IF mins>59 THEN
hours=hours+1
mins=0
ENDIF
IF hours>23 THEN hours=0
' checks feed time
IF ((fd_hours=hours) & (fd_mins=mins)) THEN
GOSUB feednow
GOTO stdby
ENDIF
GOTO stdbyloop ' branches back
'****************************************
'*** UISET user interface to set opts ***
'****************************************
uiset:
setstate=0 'Option currently being set up = 0
topuiloop:
GOSUB clrs
LOOKUP setstate,[set_feed, set_time, set_temp, set_feednow, set_end],idx
GOSUB setpos
GOSUB prt_str ' print message for setstate
PAUSE debounce
lup1:
IF (sw1 | sw2 | sw3)=0 THEN GOTO lup1
setstate=(setstate-sw1+sw2)
IF setstate>4 THEN setstate=0
lcdchar=0 ' adjusts position of message
IF sw3=0 THEN GOTO topuiloop 'loops ntil set pressed
IF setstate=4 THEN GOTO stdby ' EXITS TO STANDBY LOOP
IF setstate=3 THEN GOSUB feednow ' feeds the
IF setstate=3 THEN GOTO uiset
IF setstate=0 THEN 'set feed
hours=fd_hours
mins=fd_mins
ENDIF
' sets option (= sets time)
lcdchar=64 ' button operation txt on 2nd line
GOSUB setpos
idx= set_tim_msg
GOSUB prt_str
set_tim_opt:
GOSUB printtim ' prints time to be set
PAUSE debounce
lup2:
IF (sw1 | sw2 | sw3)=0 THEN GOTO lup2
mins=mins+sw2
IF mins>59 THEN
hours=hours+1
mins=0
ENDIF
hours=hours+sw1
IF hours>23 THEN
hours=0
ENDIF
IF sw3=0 THEN GOTO set_tim_opt 'loops until set pressed
IF setstate=0 THEN
fd_hours=hours
fd_mins=mins
ENDIF
GOSUB clrs
idx= setsuccess
GOSUB prt_str ' print message for setstate
PAUSE 1000
GOTO uiset
'****************************************
'*** Useful routines ***
'****************************************
feednow: ' prints message and rotates forward 360
GOSUB clrs
idx= feeding
GOSUB prt_str
GOSUB fwd
RETURN
printtim: ' prints the time (from hours:mins)
lcdchar=64 ' sets position second line
GOSUB setpos
tmp=(hours/10) ' form ascii char (hours 1st digit)
lcdchar=tmp+48
GOSUB prt_char
tmp=(hours-(tmp*10))' form ascii char (hours 2nd digit)
lcdchar=tmp+48
GOSUB prt_char
lcdchar=%00111010 ' colon
GOSUB prt_char
tmp=(mins/10) ' form ascii char (hours 1st digit)
lcdchar=tmp+48
GOSUB prt_char
tmp=(mins-(tmp*10)) ' form ascii char (hours 2nd digit)
lcdchar=tmp+48
GOSUB prt_char
RETURN
clrs: 'clears the LCD screen
lcdchar=%00000001 'clears the screen
L_RS=%0 ' command
GOSUB prt_char
lcdchar=%00000000 'position zero
GOSUB setpos
RETURN
prt_str: 'prints a zero-terminated string on the LCD located at idx
READ idx, lcdchar
idx=idx+1
IF lcdchar=0 THEN RETURN
L_NIBOUT= lcd_hi
GOSUB lcdout
L_NIBOUT= lcd_low
GOSUB lcdout
GOTO prt_str
prt_char: 'outputs a byte to the LCD
L_NIBOUT= lcd_hi
GOSUB lcdout
L_NIBOUT= lcd_low
GOSUB lcdout
RETURN
lcdout: '4bit transfer to lcd routine
PAUSE 1
L_ENA=%1
PAUSE 1
L_ENA=%0
RETURN
setpos: ' sets the position of the next character to be printed LCD
L_RS=%0 ' Changes mode to command
L_NIBOUT= %0000 ' return home
GOSUB lcdout
L_NIBOUT= %0010
GOSUB lcdout
' move to desired pos
L_NIBOUT= (lcd_hi | 8)
GOSUB lcdout
L_NIBOUT= lcd_low
GOSUB lcdout
L_RS=%1 ' And back to data
RETURN
fwd: 'moves the motor 360deg fwd (feeding motion)
FOR tmp=0 TO 12
mtout= stp1
PAUSE stpdly
mtout= stp2
PAUSE stpdly
mtout= stp3
PAUSE stpdly
mtout= stp4
PAUSE stpdly
NEXT
mtout= moff ' turns off current to coils
RETURN
'Messages
' 1 2
' 123456789012345678901234
welcm_msg DATA " ** GUPPY LUV **",0
standby DATA "STANDBY - (Press to set)",0
set_time DATA "TIME OF DAY <> (set)",0
set_feed DATA "FEED TIME <> (set)",0
set_temp DATA "SET TEMPERATURE <> (set)",0
set_feednow DATA "FEED NOW ",0
set_end DATA "Exit set menu ",0
set_tim_msg DATA " +hrs +mins (set)",0
setsuccess DATA " SETUP SUCCESSFUL ",0
feeding DATA " ** FEEDING ** ",0