Shop OBEX P1 Docs P2 Docs Learn Events
I/O Pin as Serial — Parallax Forums

I/O Pin as Serial

G33k499G33k499 Posts: 1
edited 2008-12-20 02:46 in Propeller 1
I know for basic stamp, I can use the·'serout' and 'serin' commands to·listen for or send data.· Is this possible with the Propeller Chip and if so, what is the syntax?· I see the Propeller being a more cost effective solution for my projects.· Also does anyone have any suggestions when it would be best to use Basic Stamp over Propeller and vice versa?
·
Thanks,
Adam

Comments

  • sylvie369sylvie369 Posts: 1,622
    edited 2008-12-18 21:59
    Yes, Propeller pins can be used for serial communication. Take a look at the various serial objects in the object exchange to see how. There are different ones you can use, with different syntaxes: basic_serial and full_duplex_serial.

    My standard comment about the Propeller vs. Stamp is that with the Propeller, it's harder to do the easy things (e.g., blink an LED), but MUCH easier to do the hard things (e.g., read a mouse position, display things on a screen, etc.).

    I bought a Propeller protoboard and then let it sit there largely unused for months before I decided to wade in and figure it out. Now I've got three of them, plus four Propsticks, and a Propeller chip that I just wired up on my own. I suggest you buy a protoboard and a Prop Plug, and work your way through Chapter 3 of the manual. Of course you could read that chapter first to see if you think you're up to it.

    As to your syntax question, the idea of object-oriented programming is that you (or others...) write "objects" to handle things like serial communications, I2C, LCD screens, etc. There isn't really a "Serin" or "Serout" command in the Propeller's Spin language, but instead, you find an object in the exchange that does that, then do something that amounts to an "include" statement (yeah, I know, the purists are screaming now) in the declarations in your program to give your program access to the object's methods, and then use them to your heart's content. For example, take a look at this:


    Con
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    Var
      Byte  Counter
      Byte  X
    Obj
      LCD   : "Debug_LCD"
    Pub Main
      LCD.Init(3,9600,2)
      Waitcnt(30_000_000 + cnt)
      LCD.cls
    
      Counter := 123
      Addr    := @Counter
      LCD.gotoxy(0,0)
      LCD.dec(Counter)
    



    The lines
    Obj
      LCD   : "Debug_LCD"
    



    allow my program to use the methods contained in the Debug_LCD object (which someone else wrote, and put up on the object exchange). Later on when I write LCD.gotoxy(0,0), I'm using one of those methods. A typical serial i/o method is this one, in the Full_Duplex_Serial object:

    PUB str(stringptr)
    
    '' Send string                    
    
      repeat strsize(stringptr)
        tx(byte[noparse][[/noparse]stringptr++])
    



    which you use like this:

    Obj
      Debug : "FullDuplexSerial"
      I2C   : "Basic_I2C_Driver"
    Pub Main
      Debug.start(31,30,0, 57600)          ' Initialize Prop Serial Terminal
      LIS302Init   
      X := I2C.ReadByte(16, $39, $0F)
      Debug.str(String(13, "Here is the Hello byte:"))
    



    That last line uses the str method to send that string out through a pin as serial data.

    Post Edited (sylvie369) : 12/19/2008 7:40:13 PM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-18 22:47
    sylvie369 - it is most timely you have posted this explanation as I'm also struggling with the concept of objects, particularly serial. I come from the picaxe world, which just happen to use Serin and Serout as well. So translating these to spin is a bit of a challenge.

    Are objects like subroutines? And if so, do objects have a standard syntax to pass variables to them or can the syntax vary depending on who wrote the object?

    I'm looking at the objects and noting they look a bit like Calls in .net, where you pass variables inside brackets. Is this how it is done, and if it is, does one put a comment when one includes an object that describes some of the syntax for that object (so you can remember how to use it).

    Also, should one note how much space the object takes up and the resources it uses. I'm thinking one could have a three line program that references three large objects and suddenly there is no space left.

    And is it possible to copy and paste the entire referenced object code into the main program? Is this allowed or is the proper protocol to reference it and then look it up externally?

    Lots of really dumb questions I'm sure.

    I'm mindful of how QB went in the early 1990s where a basic program went from one single linear text file into a program that referenced subs that one could visualise seperately via the interpreter but were not contiguous. I remember at the time it was confusing, then it became very natural, and now, looking back at those programs 15 years later, they are confusing again.

    I'd like to put half a page of comments next to that object reference describing how it works. Is that the way to do things?
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-18 22:52
    Wow, Sylvie, what a clear and articulate explanation. I don't know whether the original poster needed that, but I certainly did. Many thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • SRLMSRLM Posts: 5,045
    edited 2008-12-19 00:37
    Another object that I've found useful (from a PBASIC background) is the BS2 functions. It has all the main PBASIC commands implemented in spin. Very helpful.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-12-19 00:50
    Quick replies:

    - The objects vary in syntax depending on the author, however most come with simple example programs to explain usage, and the objects you find in the object exchange are generally documented within the source code.

    - The size of an object is completely dependent on the content of the object. SPIN / assembly code are only ever created once, while variables within an object are created per instance of that object in your code. The Propeller tool can tell you how much code / data are used by a given object. If an object contains functions you don't use, removing them will reduce the size of the object.

    - There is nothing wrong with creating modified copies of existing objects to suit your needs, just make sure to differentiate them from the original so you don't go blaming some poor author for bugs you created. [noparse]:)[/noparse]

    - There is no reason you can't copy the code from the object directly into your program if the object is simply SPIN functions you need (like Simple_Serial).· If the object contains data as well as code, then it's a little more complicated. Some apps use objects like C++ uses classes, as a way to keep data and related functions together, and allow the creation of more than one object of the same type. In this case, copying the object code into your own would break that. It's also a little different when the object uses assembly code, as the assembly portion has to be started on its own cog, which means you have to HAVE a spare cog.

    Jason


    Post Edited (JasonDorie) : 12/19/2008 12:55:43 AM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2008-12-19 03:11
    SRLM - that is a little one line post that nearly slipped through under the radar!

    Ok, I searched for BS2 in the objects library and downloaded it.

    There are quite a few functions there, including Serin and Serout.

    So this object allows one to code in Basic? Or at least use many of the familiar commands. And given the format looks pretty straightforward, it should be possible to add more commands?

    Sweet!
  • SRLMSRLM Posts: 5,045
    edited 2008-12-19 06:17
    Yep. You can't use the code exactly the same (since they are function calls, rather than commands) but you can use it in a similar vein. I like it because it's all in spin (so it's easy to modify) and simple/short (so I don't get lost...)
  • mikedivmikediv Posts: 825
    edited 2008-12-19 17:50
    sylvie369·you have no idea how helpfull this has been while I am not sure I fully understand it all I think you cleared up the object thing for me I thought they were just a bunch of user programs everyone was sharing to run stand alone now if I understand it we can use them "objects" like little parts to suplement are programs , for instance I am writing a program to say hello world to an lcd screen I write the hello world part then call/load the object program say" lcd interface" into my hello world code and it saves me from having to write the lcd driver part of the code? if this is right that is awesome I could write some simple stuff and just load in the more complicated part of the program from the many object programs??
    Thank you very much
    ·
  • sylvie369sylvie369 Posts: 1,622
    edited 2008-12-19 18:09
    Thanks, all.

    Pay it forward, man.

    Mikediv said...
    if I understand it we can use them "objects" like little parts to suplement are programs , for instance I am writing a program to say hello world to an lcd screen I write the hello world part then call/load the object program say" lcd interface" into my hello world code and it saves me from having to write the lcd driver part of the code? if this is right that is awesome I could write some simple stuff and just load in the more complicated part of the program from the many object programs??

    Bingo.

    Of course you have some details to pay attention to, like what kinds of parameters a method requires, and whether or not it returns a value (as a function, for us old-school C/PASCAL types). But yup, when you want to do something, go straight to the object exchange and look for an object that covers what you need. Then just read through that object's methods to see which ones you need to use in your own programs. Using a DS1620 temperature chip? Look - Jon Williams put an object for it up there in the Exchange. An LIS302DL three-axis accelerometer? These methods are already available in an object there:

    PUB Init(i2cSCL,_deviceAddress)
    PUB GetStatus(i2cSCL,_deviceAddress)
    PUB SetRange(i2cSCL,_deviceAddress, range) | ctrl, reg
    PUB GetId(i2cSCL,_deviceAddress)
    PUB GetAcceleration(i2cSCL,_deviceAddress,XPtr,YPtr,ZPtr)
    



    Nunchuck controller?

    http://forums.parallax.com/showthread.php?p=751847

    PUB init
    PUB readNunchuck | data[noparse][[/noparse]6]
    PUB joyX
    PUB joyY
    PUB accelX
    PUB accelY
    PUB accelZ
    PUB buttonC
    PUB buttonZ
    



    Like I said, the hard stuff is much easier with the Propeller (provided of course that someone else has already done the work)

    By the way, all of this stuff is right there in Chapter 3 of the Propeller manual.

    Post Edited (sylvie369) : 12/19/2008 7:41:48 PM GMT
  • mikedivmikediv Posts: 825
    edited 2008-12-20 02:46
    Thank you again you just cant imagine how easy you made that I am new to the prop and downloaded the classes they are actualy very well laid out but sometimes you just need a simple explanation· to tip you over, not to mean your knowledge is simple you know what I mean thank you very much I look forward to reading your posts ..G33k499·I did not mean to hijack your post I am very glad you asked the question you did thanks again guys
    ·
Sign In or Register to comment.