BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 12:33 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Porting code from a BasicStamp to the Nano
PostPosted: Fri Sep 17, 2010 7:30 am 
Offline
Citizen

Joined: Mon Apr 19, 2010 12:12 pm
Posts: 6
Hello All,

I was wondering if anyone has listed the changes necessary to port code from a BasicStamp to the Nano?

Don


Top
 Profile  
 
 Post subject: Re: Porting code from a BasicStamp to the Nano
PostPosted: Fri Sep 17, 2010 8:51 am 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
Hello, R4. Such a guide is in the works, but has a ways to go yet before release. The good news is, the syntax for Studio largely follows the Stamp syntax. However, because the processors used in BasicMicro products have extra features built in, like ADC (Analog to digital Conversion), Studio has several extra commands not available in the Stamp series.

What are you trying to do? What devices are you using in your design? Can you post your code here or post a link to existing Stamp code to take a crack at translating?

We aimz to please! ;)

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Porting code from a BasicStamp to the Nano
PostPosted: Sat Sep 18, 2010 4:25 am 
Offline
Citizen

Joined: Mon Apr 19, 2010 12:12 pm
Posts: 6
Hi Kenjj,

It is really more of a general question. I was thinking it would be very helpful for anyone that wanted to use the Nano and had a background with the BS. Lots of hobbyists and teachers have BS 1s & 2s and moving to Nano would be much easier if there was a document that listed the differences in the programming.

For example the following is some BS code to drive a color LCD I am planning on converting to run on a NANO 18 or 28.


Code:
Black    CON 0
White    CON 255
Blue     CON 3
Green    CON 28
Red      CON 224
Cyan     CON Blue + Green
Yellow   CON Green + Red
Magenta  CON Blue + Red

Reset    PIN 3 ' Connect ALL pins with 10K resistors
SData    PIN 2
SClock   PIN 1
CS       PIN 0

dat      VAR Byte
ctr      VAR Word ' Used in InitLCD & Clear subroutines
x        VAR Byte ' X-Coordinate (0 to 129)
y        VAR Byte ' Y-Coordinate (0 to 129)
color    VAR Byte ' Color to plot

Start:
  GOSUB InitLCD
  color = Black : GOSUB Clear
  FOR x = 0 TO 129
    y = ((SIN (x * 2)) + 128) / 2
    color = Blue
    GOSUB Plot
    y = ((SIN (x * 2 + 128)) + 128) / 2
    color = Green
    GOSUB Plot
    y = ((COS (x * 2)) + 128) / 2
    color = Red
    GOSUB Plot
    y = ((COS (x * 2 + 128)) + 128) / 2
    color = Yellow
    GOSUB Plot
  NEXT
END

SendCommand:
  SHIFTOUT SData, SClock, MSBFIRST, [0\1, dat\8]
  RETURN


SendData:
  SHIFTOUT SData, SClock, MSBFIRST, [255\1, dat\8]
  RETURN


InitLCD:
  HIGH CS
  PAUSE 10
  HIGH SClock
  LOW SData
  LOW Reset
  PAUSE 1
  Reset = 1
  PAUSE 20
  CS = 0
  dat = $CA : GOSUB SendCommand
  dat = $03 : GOSUB SendData
  dat = 32 : GOSUB SendData
  dat = 12 : GOSUB SendData
  dat = $00 : GOSUB SendData
  dat = $BB : GOSUB SendCommand
  dat = $01 : GOSUB SendData
  dat = $D1: GOSUB SendCommand
  dat = $94 : GOSUB SendCommand
  dat = $81 : GOSUB SendCommand
  dat = $24 : GOSUB SendData  '5
  dat = $03 : GOSUB SendData  '$01
  dat = $20 : GOSUB SendCommand
  dat = $0F : GOSUB SendData
  PAUSE 100
  dat = $A7: GOSUB SendCommand
  dat = $BC : GOSUB SendCommand
  dat = $00 : GOSUB SendData
  dat = 0 : GOSUB SendData
  dat = $01 : GOSUB SendData
  dat = $00 : GOSUB SendData
  dat = $CE : GOSUB SendCommand
  dat = 0 : GOSUB SendData
  dat = 2 : GOSUB SendData
  dat = 4 : GOSUB SendData
  dat = 6 : GOSUB SendData
  dat = 8 : GOSUB SendData
  dat = 10 : GOSUB SendData
  dat = 12: GOSUB SendData
  dat = 15 : GOSUB SendData
  dat = 0 : GOSUB SendData
  dat = 2 : GOSUB SendData
  dat = 4 : GOSUB SendData
  dat = 6 : GOSUB SendData
  dat = 8 : GOSUB SendData
  dat = 10 : GOSUB SendData
  dat = 12 : GOSUB SendData
  dat = 15 : GOSUB SendData
  dat = 0 : GOSUB SendData
  dat = 4 : GOSUB SendData    '5
  dat = 9 : GOSUB SendData   '10
  dat = 15 : GOSUB SendData
  dat = $25 : GOSUB SendCommand
  dat = $AF : GOSUB SendCommand
  PAUSE 200
  'FOR ctr = 0 TO 130
  '  dat = $D6 : GOSUB SendCommand
  'NEXT
  RETURN

Plot:
  dat = $75 : GOSUB SendCommand
  dat = y + 2: GOSUB SendData
  dat = 131 : GOSUB SendData
  dat = $15 : GOSUB SendCommand
  dat = x : GOSUB SendData
  dat = 129 : GOSUB SendData
  dat = $5C : GOSUB SendCommand
  dat = color : GOSUB SendData
  RETURN

Clear:
  x = 0 : y = 0: GOSUB Plot
  FOR ctr = 1 TO 16899
    SHIFTOUT SData, SClock, MSBFIRST, [255\1, dat\8]
  NEXT
  RETURN




Don


Top
 Profile  
 
 Post subject: Re: Porting code from a BasicStamp to the Nano
PostPosted: Sat Sep 18, 2010 2:41 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
Hello Don.
Well, the first step for me was to create a new file in Studio and just import your code into it. Then I just started a compile for a Nano18 and watched the compile window for errors. And, oh yeah, there were errors! ;) But nothing catastrophic.
I kept a running set of notes of what I did to eliminate these problems, and the notes look like:
Quote:
1 Renamed PIN commands to CON using P#s.
2 Changed Clear label to Clear_LCD
3 Changed pin settings of "=1" and "= 0" to "HIGH pin" and "LOW pin".

As for 1, Studio doesn't have PIN commands. You use CON and use the P# for the pin, so
Reset PIN 3
becomes
Reset CON P3.
If you don't use the "P" then Studio considers it a numeric value, not a pin.

For 2, Clear is a command in Studio, so I simply renamed it.

For 3, I changed pin assignments of "=1" and "=0" to HIGH pin# and LOW pin#. Thus
Reset = 1
becomes
HIGH Reset.
CS = 0
becomes
LOW CS.

After those three mods/corrections, Studio compiled the code without squawk. That's not say this is good working code for your LCD, just that there are no glaring syntax problems. There are still timing considerations and the like. I include the modified code here. At this time you'll want to check the notes on the SPI commands in Studio's manual and see if these work the same between the two parts. Make any changes for differences you find. BEWARE: These notes are for the Pro line of parts, NOT the nano per se. Having said that, BMicro endeavours to make the commands work the same among all its products.
Then try it with a Nano and come back with any errors you see.
Luck on your project. :D
Code:
; 091810 - Don's BStamp code modified for Nano use

Black CON 0
White CON 255
Blue CON 3
Green CON 28
Red CON 224
Cyan CON Blue + Green
Yellow CON Green + Red
Magenta CON Blue + Red

Reset CON P3   ; Reset PIN 3 ' Connect ALL pins with 10K resistors
SData CON P2   ; SData PIN 2
SClock CON P1  ; SClock PIN 1
CS CON P0      ; CS PIN 0

dat VAR Byte
ctr VAR Word ' Used in InitLCD & Clear subroutines
x VAR Byte ' X-Coordinate (0 to 129)
y VAR Byte ' Y-Coordinate (0 to 129)
color VAR Byte ' Color to plot

Start:
GOSUB InitLCD
color = Black : GOSUB Clear_LCD
FOR x = 0 TO 129
y = ((SIN (x * 2)) + 128) / 2
color = Blue
GOSUB Plot
y = ((SIN (x * 2 + 128)) + 128) / 2
color = Green
GOSUB Plot
y = ((COS (x * 2)) + 128) / 2
color = Red
GOSUB Plot
y = ((COS (x * 2 + 128)) + 128) / 2
color = Yellow
GOSUB Plot
NEXT
END

SendCommand:
SHIFTOUT SData, SClock, MSBFIRST, [0\1, dat\8]
RETURN


SendData:
SHIFTOUT SData, SClock, MSBFIRST, [255\1, dat\8]
RETURN


InitLCD:
HIGH CS
PAUSE 10
HIGH SClock
LOW SData
LOW Reset
PAUSE 1
HIGH RESET
PAUSE 20
LOW CS
dat = $CA : GOSUB SendCommand
dat = $03 : GOSUB SendData
dat = 32 : GOSUB SendData
dat = 12 : GOSUB SendData
dat = $00 : GOSUB SendData
dat = $BB : GOSUB SendCommand
dat = $01 : GOSUB SendData
dat = $D1: GOSUB SendCommand
dat = $94 : GOSUB SendCommand
dat = $81 : GOSUB SendCommand
dat = $24 : GOSUB SendData '5
dat = $03 : GOSUB SendData '$01
dat = $20 : GOSUB SendCommand
dat = $0F : GOSUB SendData
PAUSE 100
dat = $A7: GOSUB SendCommand
dat = $BC : GOSUB SendCommand
dat = $00 : GOSUB SendData
dat = 0 : GOSUB SendData
dat = $01 : GOSUB SendData
dat = $00 : GOSUB SendData
dat = $CE : GOSUB SendCommand
dat = 0 : GOSUB SendData
dat = 2 : GOSUB SendData
dat = 4 : GOSUB SendData
dat = 6 : GOSUB SendData
dat = 8 : GOSUB SendData
dat = 10 : GOSUB SendData
dat = 12: GOSUB SendData
dat = 15 : GOSUB SendData
dat = 0 : GOSUB SendData
dat = 2 : GOSUB SendData
dat = 4 : GOSUB SendData
dat = 6 : GOSUB SendData
dat = 8 : GOSUB SendData
dat = 10 : GOSUB SendData
dat = 12 : GOSUB SendData
dat = 15 : GOSUB SendData
dat = 0 : GOSUB SendData
dat = 4 : GOSUB SendData '5
dat = 9 : GOSUB SendData '10
dat = 15 : GOSUB SendData
dat = $25 : GOSUB SendCommand
dat = $AF : GOSUB SendCommand
PAUSE 200
'FOR ctr = 0 TO 130
' dat = $D6 : GOSUB SendCommand
'NEXT
RETURN

Plot:
dat = $75 : GOSUB SendCommand
dat = y + 2: GOSUB SendData
dat = 131 : GOSUB SendData
dat = $15 : GOSUB SendCommand
dat = x : GOSUB SendData
dat = 129 : GOSUB SendData
dat = $5C : GOSUB SendCommand
dat = color : GOSUB SendData
RETURN

Clear_LCD:
x = 0 : y = 0: GOSUB Plot
FOR ctr = 1 TO 16899
SHIFTOUT SData, SClock, MSBFIRST, [255\1, dat\8]
NEXT
RETURN

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Porting code from a BasicStamp to the Nano
PostPosted: Sun Sep 19, 2010 5:01 am 
Offline
Citizen

Joined: Mon Apr 19, 2010 12:12 pm
Posts: 6
Hi Kenjj,

Yes, this is right on the money. A set of notes that listed the major differences between the Nano coding requirements and the BS or PBP would be a big help to a Nano-Newbie.

I think this would really help push the Nano.

Thanks, Don


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

All times are UTC - 8 hours [ 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO