BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Tue Sep 26, 2017 5:11 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: newbe help with a parallax stampbasic conversion to basicmic
PostPosted: Wed Apr 10, 2013 3:05 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
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


Top
 Profile  
 
 Post subject: Re: newbe help with a parallax stampbasic conversion to basi
PostPosted: Fri Apr 12, 2013 9:52 pm 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
Hi there and thanks for the purchase of the Nano-18 from BasicMicroUK,

Lots of the code you are having problems with is dealing with the LCD Display BUT the Basic Micro range has some powerfull commands to simplify this task and this will enable you to replace large portions of Basic Stamp code with far simpler Basic Micro Commands, for example the section of you code that deals with the LCD Setup as shown below :-

Stamp code :-
' initialize LCD display
PAUSE 1000
L_RS=%
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

can simply be replaced with :-

lcdinit p0\p1\p7\p6\p5\p4,p2

And then the section where your first error shows up at line 100 :-

lcdchar=0 ' prints welcome message
GOSUB setpos
idx=welcm_msg
GOSUB prt_str

can be replaced with :-

lcdwrite p0\p1\p7\p6\p5\p4,p2, [LCDCLEAR, HOMELCD,SCR,TWOLINE,"" ** GUPPY LUV **"]

You will notice the part to set the cursor position above, they are setting lcdchar to 0 for the first character on the first line of the display and later on they set up to print on the second line of the display,

The Basic Micro version of the above would simply be :-

lcdwrite p0\p1\p7\p6\p5\p4,p2, [scrram+64, "Text for second line"]

This assumes you have the LCD connected to the pins on the Nano-18 as shown in the example connection diagram,

Once you have replaced all the LCD write calls in the main program you can get rid of all the LCD subroutines later on in the program,

These 2x16 Line LCD displays can be purchased at BasicMicroUK,

The full write-up on the Basic Micro LCD Commands can be found in the Basic Micro Studio Syntax manual on page 136, this can be downloaded from the Basic Micro web site,

I hope this helps you get up and running with your project,

Many thanks

Dave
BasicMicroUK


Attachments:
File comment: Connecting an LCD Display to the Nano Range
LCD Connections.png
LCD Connections.png [ 45.36 KiB | Viewed 6248 times ]
Top
 Profile  
 
 Post subject: Re: newbe help with a parallax stampbasic conversion to basi
PostPosted: Sun Apr 14, 2013 5:42 pm 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
HI,

FYI I have just located some of the original BS2, BS2SX and some 40 pin STAMP IC's and some development PCB's for 24 and 40 pin versions of the same, we also have the driver IC's for stepper motor control in stock, all are brand new,

Call us on 01623 720730 if you think using the original BS2 to build you automatic fish feeder is a better option for you than porting over to Nano IC's,

Many thanks

Dave
BasicMicroUK
www.basicmicro.co.uk


Top
 Profile  
 
 Post subject: Re: newbe help with a parallax stampbasic conversion to basi
PostPosted: Wed Jul 03, 2013 9:53 am 
Offline
New User

Joined: Wed Jul 03, 2013 8:59 am
Posts: 1
Hey what is this discussion all about? Can anyone please explain me? I am new here.


Top
 Profile  
 
 Post subject: Re: newbe help with a parallax stampbasic conversion to basi
PostPosted: Tue Dec 02, 2014 10:56 am 
Offline
New User

Joined: Tue Dec 02, 2014 10:54 am
Posts: 1
This is very nice post thanks! :geek: :lol: :ugeek:





____________________
www.pass4-sure.me


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO