Shop OBEX P1 Docs P2 Docs Learn Events
P2 FlexProp Basic ADC & Comm drivers — Parallax Forums

P2 FlexProp Basic ADC & Comm drivers

Since I will be using FlexProp Basic with my new project, I am looking for some kind of method to be able to use the P2 ADC function of the pins and maybe a P2 Comm driver/function.

I know that there is some Spin2 code for the ADC functionality, but I am hoping that somebody has created something that will not use Spin2 code, but maybe some PASM2 code. I think that FlexBasic can handle PASM2 code, not sure about that.

Same thing would be true for the Comm aspect, not to use Spin2 code, but be able to use some PASM2 code.

Basically, I am thinking that by using the PASM2 code instead of the Spin2 code, that would result in a smaller overall program. Even 512KB of space available, I think that memory will start to become scarce in a hurry. Also you have to consider the overall footprint of FlexBasic itself.

Thanks
Ray
«13

Comments

  • JonnyMacJonnyMac Posts: 9,159
    edited 2021-01-05 17:55
    The P2 is new, and most of the general-purpose drivers are written in Spin2. Spin2 compiles with the same compiler that FlexBASIC uses, hence will give you the same code output. Given both languages are simple, you should have no difficulty translating Spin2 objects to FlexBASIC libraries. My jm_fullduplexserial.spin2 uses smart pins for the UART function and a small bit of PASM2 to manage buffers. Smart pins can be used for analog inputs and outputs. The FlexProp compiler has the Parallax constants for smart pins built in, so any Spin2 code that uses them will easily translate.
    I am hoping that somebody has created something that will not use Spin2 code, but maybe some PASM2 code.
    This Spin2 code:
      pinstart(pin, mode, x, y)
    
    ...translates to...
                    fltl      pin
                    wrpin     mode, pin
                    wxpin     x, pin
                    wypin     y, pin
                    drvl      pin
    
    The Spin2 interpreter is included in the PNut download -- this is a great resource for translating Spin2 to discrete assembly.
  • Rsadeika wrote: »
    I know that there is some Spin2 code for the ADC functionality, but I am hoping that somebody has created something that will not use Spin2 code, but maybe some PASM2 code. I think that FlexBasic can handle PASM2 code, not sure about that.

    FlexBasic can use Spin2 objects directly, so there's no need to translate them (although as Jon said it should be straightforward to do, if you want to). Moreover, it compiles Spin2 (and BASIC) into PASM code anyway, so you won't get much performance improvement by translating the code to PASM by hand, unless you're an experienced assembly programmer.

    My advice would very much be use what's available, and only worry about optimizing once you have something working (and know where the bottlenecks are). I doubt that the ADC or Comm code is going to consume the most space, and probably not even the most time, since the smart pins will do most of the work.
  • Cluso99Cluso99 Posts: 18,069
    edited 2021-01-05 21:15
    Proper spin2 code will use less hub space using pnut or PropTool because FlexProp compiles spin2 to pasm which takes more hub ram space, but will be faster.

    I agree with Eric, get it working first with whatever way is easiest.
  • Just as a point of reference, I have a FlexBASIC program now pushing 12k BASIC lines. Add to this a video driver (ERSmith’s), a comm port driver (JonnyMac), and a DS3231 driver (mine), etc. There are multiple data structures and a bunch of string handling all over the place. All of this currently compiles to ~160k.

    This is a long way of saying that with FlexBASIC you’ve probably got a lot more elbow room than you might first think.
  • JRoark,
    would you mind sharing your ds3231 driver here?
    jim
  • This is a long way of saying that with FlexBASIC you’ve probably got a lot more elbow room than you might first think.
    I remember back when the P1 became available, and we were all blinking the LED, and making comments about the massive 32KB of memory. :smile:

    Just something to think about.

    Ray
  • Where is all of the Jon McPhalen Spin2 code located, I did a search for jm_fullduplexserial.spin2, and I only found a file in a driverbob thread, luckily. Not having much success finding all his other spin2 code.

    Ray
  • dgatelydgately Posts: 1,631
    edited 2021-01-08 15:19
    There are stable versions of many of JonnyMac's spin2 drivers, library & demo code on Parallax's github OBEX replacement. I will warn you that in order to access individual files in the repository, you'll either need to download all of the repository or find solutions in the forums for single file access.

    Before going to github, checkout this Parallax P2 webpage first: https://parallax.com/propeller-2/

    Here's a link to the propeller (P1 & P2) github page where you'll find 'all' of the current code samples:
    https://parallax.com/propeller-2/documentation/


    You'll want to look inside this path: propeller/libraries/community/p2/All/ to see the code for: jm_full_duplex_serial.spin2
  • Thanks, I did not know that there was a P2 area in github.

    Ray
  • JonnyMacJonnyMac Posts: 9,159
    edited 2021-01-10 18:11
    Let me save you some trouble and any more of the invariable hand-wringing you suffer when trying any new Parallax or associated product. The attached P2 file has a bunch of basic objects connected -- they are the latest on my system. That is to say that most are newer than what's on GitHub (ObEx had an advantage of making updates very easy).

    I've not tried all, but you should be able to compile with FlexProp without problems. The next step is converting my libraries to your favorite language, and then sharing those with the community.
  • ersmithersmith Posts: 6,068
    edited 2021-01-09 20:45
    Rsadeika wrote: »
    This is a long way of saying that with FlexBASIC you’ve probably got a lot more elbow room than you might first think.
    I remember back when the P1 became available, and we were all blinking the LED, and making comments about the massive 32KB of memory. :smile:

    Just something to think about.

    Ray

    I think you need to re-read @JRoark 's comment. He's got 12000 lines of BASIC code, including VGA and serial drivers. That's a far cry from "blinking the LED"!

    Seriously, the time to worry about running out of memory is when you've done some programming and are finding memory tight. Don't waste time trying to optimize if you don't have to!
  • RS_Jim wrote: »
    JRoark,
    would you mind sharing your ds3231 driver here?
    jim

    Can do! I’ll be home tonight and post it. It also supports setting and retrieving via UNIX-style datestamps.
  • @RS_Jim Here you go. This is my DS3231 driver written in SPIN2 for FlexProp. It is still under development, but it works. I've got it talking to a DS3231MV+ presently.

    No deposit/no return. :) But if you break it, please let me know how so I can fix it.
  • JRoark wrote: »
    @RS_Jim Here you go. This is my DS3231 driver written in SPIN2 for FlexProp.

    A minor nit: it looks like it's actually written in Spin 1. Spin 2 users will have to make some (minor) changes to use it with PNut.
  • ersmith wrote: »
    JRoark wrote: »
    @RS_Jim Here you go. This is my DS3231 driver written in SPIN2 for FlexProp.

    A minor nit: it looks like it's actually written in Spin 1. Spin 2 users will have to make some (minor) changes to use it with PNut.

    *THIS* is what you get when you turn a non-Spin programmer loose without adult supervision. :)

    I've never actually laid my eyes on PNut, so I'll defer to Eric.
  • @RS_Jim: There is a Spin2 DS3231 driver and demo in my training files -- you can find them in the first post of this thread (Week 3 files).
    -- http://forums.parallax.com/discussion/172535/p2-christmas-lighting-project-beginner-intermediate-spin2-coding-week-3-files-available-now/p1?new=1
  • Today I will be doing some experiments using the jm_ez_analog with FlexBasic.

    In the jm_ez_analog_demo, Jon is using a POT and working with 3.0V. In my solar station project I will use, as a starter, a 12V battery voltage. I have done some looking into voltage dividers, and I think I will be using a setup based on 25V input, 3.0V output. I think I have a 1000ohm and a 140ohm resisters. For a safety precaution does anybody know what the final results are for the pin voltage tolerances. Some time back I thought I saw something like ~4.0V, and not sure if this is a steady or short peak range.

    The other item that I have handy in that area is a cm2302 module, anybody see any P2 code for that module.

    Ray
  • JonnyMacJonnyMac Posts: 9,159
    edited 2021-01-15 07:00
    I'm not feeling well, but I had a DHT22 on my desk and in between headaches and naps managed to get this work. I have a DHT11 on order to test with. I used a logic analyzer to look at the signal and verified that occasional bits are out of spec, hence I loosened the timing requirements. My code matches the protocol decoder in PulseView.

    (File removed by JM. Submitted new version that is compatible with DHT11/DHT22 to Parallax).
  • Thanks Jon, I hope you are feeling better. My stuff is not that important, so appreciate the effort.

    Ray
  • It was on my task list -- your need just inspired me to give it a go when I could focus. Thanks for the well-wishes.
  • On my other computer, which I have connected to the P2 Edge system, I have installed the latest Propeller Tool so I could run some of the Spin2 program examples.

    The first one that I ran was the jm_ez_analog_dmo.spin2. I have "POT" set at P0 which is connected to a voltage divider, which is connected to a 12V battery. I read the voltage at the output pins on the voltage divider - ~2.465V. Now when I run the ez_analog program I get:
    P2 EZ Analog Demo
    Level: 82
    I am not sure what "Level 82" represents. What I was expecting to see is ~2.465, that is what the voltage divider is supposedly providing. So, I guess I need some explanation as to what ez_analog is showing me.

    Ray
  • JonnyMacJonnyMac Posts: 9,159
    edited 2021-01-12 18:53
    I am not sure what "Level 82" represents.
    Well... because you didn't take the time to read the listings. You really have to do that with any new code.

    In the object:
    pub start(pin, lo, hi) | clo, chi
    
    '' Setup pin for analog input
    '' -- lo and hi define user range
    ''    * scaled from Gnd to Vio (3.3v)
    
    The start method takes a pin #, and the low and high values of your desired output range. If you wanted to know the pin voltage, you could use 0 and 330 to get 1/100ths volts (see new demo).

    In the code:
      LO =   0_0                                                    ' scaled range for pot
      HI = 100_0
    
    pub setup()
    
      analog.start(POT, LO, HI)                                     ' configure analog input
    
    In this case, the output range is 0 to 100, which you could consider percent of 3.3v. So, 82 represents ~2.7 volts.

    I updated jm_ez_analog.spin2 after Chip's recent online discussion on analog inputs, and renamed it jm_analog_in.spin2. I also beefed-up some of the comments to make them more verbose, but you still have to read them. :) The demo uses volts as the output, but remember that you can scale the 0-3.3v from the analog circuitry to any output range you want. The purpose of this object is to save a step for the programmer; in most cases, we take the raw value from an ADC and scale it to something usable. This object does that for us.

    Delete the old files and use what I've attached here. As you can see, it runs fine in FlexProp.
  • Thanks Jon for the detailed explanation, hopefully this helps out other people that may be interested in ez_analog.

    Just for the sake of clarity, I am not a Spin programmer, and my intentions are to move to C when that gets fully developed for the P2. At that point, maybe we will start to see more drivers that are developed in C as opposed to having Spin2 code translated to FlexBasic or FlexC. I fully appreciate the work that Jon is putting into Spin2 development, and I am sure that he will keep doing it.

    For the time being I will be porting some of the Jon Spin2 code over to FlexBasic. I want to see how well that language fits in with the P2. I am not sure if FlexBasic can be used to develop P2 driver code directly, maybe something will click in that regard when I do the porting. We shall see.

    Thanks again Jon.

    Ray
  • Using the new and improved jm_analog_in, I made this port over to FlexBasic.

    In the jm_analog_in object, Jon uses Pub read(), I had to change this to Pub readit(). For some reason FlexBasic does not recognize read(), and throws an error.

    I would like to able to control the amount of digits that are shown of the float value, but the only thing that I saw was rtrim$() , and that only works for strings.

    Anybody have any suggestions as to how I can trim this code?

    Ray

    ' solsta.bas
    '
    ' January 12, 2021
    
    dim adc_ez as class using "jm_analog_in.spin2"
    
    dim inBuff as string
    dim test#
    dim test1#
    dim val1#
    
    adc_ez.start(0, 0, 100)  ' Start ez_analog P0,0,100, reading in percentage
    
    do
    	print "> ";
    	input inBuff
    	if inBuff = "quit" then
    		exit
    	else if inBuff = "barray" then
    		test# = (adc_ez.readit()/1)   ' Get correct percentage
    		test1# = ((test# * 3.3)/100)  ' Get actual value of percentage
    		val1# = (test1# * 5.04434)    ' Calibration to show actual voltage
    		print val1#; " Volts"         ' Display the value
    	else
    		print "Invalid Command!"
    	end if
    loop
    
    print "Program Ended!"
    
    end
    
  • Rsadeika wrote: »
    In the jm_analog_in object, Jon uses Pub read(), I had to change this to Pub readit(). For some reason FlexBasic does not recognize read(), and throws an error.
    "read" is a reserved keyword in BASIC. This is one of the hazards of cross-language development; words like PUB and DAT are reserved in Spin but usable in BASIC, and conversely PRINT and READ are reserved in BASIC but usable in Spin. The BASIC language documentation that comes with FlexProp has a list of all of the reserved words.
    I would like to able to control the amount of digits that are shown of the float value, but the only thing that I saw was rtrim$() , and that only works for strings.

    Try PRINT USING, e.g. to print 1 digit before the decimal and two after, do:
    print using "#.## Volts"; val#
    
  • Reading through the FlexBasic docs, I came across a command called - RDPIN. It states: "rdpin(p) reads the current value of the smartpin Z register for pin p."

    Not sure what the Z register will provide in the context of FlexBasic. Is there a register in the smartpin that would read analog value for the pin. If so, could this RDPIN command be expanded to do some ADC? Will the RDPIN command see some expansion for doing other things, not knowing exactly how I would use this.

    Ray
  • Rsadeika wrote: »
    Reading through the FlexBasic docs, I came across a command called - RDPIN. It states: "rdpin(p) reads the current value of the smartpin Z register for pin p."

    Not sure what the Z register will provide in the context of FlexBasic. Is there a register in the smartpin that would read analog value for the pin. If so, could this RDPIN command be expanded to do some ADC? Will the RDPIN command see some expansion for doing other things, not knowing exactly how I would use this.

    Ray

    The Z register is used for sending data from the smartpin back to the COG, so yes, ADC values would be read from there if the smartpin is configured in ADC mode. I haven't done much smartpin programming, but Jon Titus wrote a document about the smart pins, and there's some information in Chip's hardware manual and in the Spin2 manual. The Parallax P2 documentation at https://www.parallax.com/propeller-2/documentation is where all of this is located (a handy link to that is in the Help menu in FlexProp 5.0.6).
  • I think I have the ADC to work as expected. Below I am using FlexBasic as a prototyping language, I can put things together and test it in a quick way, at least for me.

    The program has a UI and I am able to test the three ADC hook ups that I have. When I get my Edge breadboard, then I will be able to expand on this program some more, when I start to add more components. At the moment it is a little cumbersome with having a small breadboard close to the P2 Edge mini system, everything kind of moves around to much.

    Next I will probably try to add some comms, not sure how I will approach this. FlexBasic has the sendrecvdevice command, not sure if that will fit what I will be trying to do.

    Ray
    ' solsta.bas
    '
    ' January 12, 2021
    
    dim adc_ez as class using "jm_analog_in.spin2"
    
    '' Variables
    dim inBuff as string
    dim test#,test1#,val1#,test2#,test3#,val2#
    
    '' Stack
    dim blink_stack(8)
    
    '' Main
    '' A visual blinking of LED 57 to show that 
    '' there is a program that is running.
    var a = cpu(blink(57,80_000_000), @blink_stack(1))
    
    do
    	print "> ";
    	input inBuff
    	if inBuff = "quit" then
    		exit
    	else if inBuff = "barray" then  '' Battery array
    	  ' Start ez_analog P0,0,100, reading in percentage		
    		adc_ez.start(0, 0, 100)  '' This uses P0
    		pausems 250
    		test# = (adc_ez.readit()/1)		
    		' Get actual value of percentage
    		test1# = ((test# * 3.3)/100)  
    		' Calibration to show actual voltage
    		val1# = (test1# * 5.04928)    
    	  ' Display the value
    		print using "##.### BA Volts";val1#
    		
    	else if inBuff = "apanel" then  '' Solar panel array
    		adc_ez.start(2, 0, 100)  '' This uses P2
    		pausems 250
    		test3# = (adc_ez.readit()/1)
    		'print test3#
    		test2# = ((test3# * 3.3)/100)
    		'print test2#
    		val2# = (test2# * 38.56749)
    		print using "##.### SA Volts";val2#
    	else if inBuff = "dcpwr" then   '' Backup DC power source
    		adc_ez.start(1, 0, 100)  '' This uses P1
    		pausems 250
    		test3# = (adc_ez.readit()/1)
    		print test3#
    		test2# = ((test3# * 3.3)/100)
    		print test2#
    		val2# = (test2# * 5.14015)
    		print using "##.### DC Volts";val2#	
    	else
    		print "Invalid Command!"
    	end if
    loop
    
    print "Program Ended!"
    print "00"
    _reboot  '' Reset
    end
    
    '' Subroutines
    ''
    '' Subroutine for the CPU(COG)
    sub blink(pin, freq)
    	direction(pin) = output
    	do
    		output(pin) = not output(pin)
    		waitcnt(getcnt() + freq)
    	loop
    end sub
    
  • Below is my latest program for testing different spin2 attachment drivers. The program compiles, but ...
    I added two cpu(COG) routines, and the main do...loop UI is malfunctioning, not sure why that is occurring. It is almost working like a thread and not a cpu(COG) routine.

    Ray
    [CODE]
    ' solsta.bas
    '
    ' January 12, 2021

    dim adc_ez as class using "jm_analog_in.spin2"
    dim dht as class using "jm_dhtxx.spin2"
    dim rpi as class using "jm_fullduplexserial.spin2"

    rpi.start(4,6,0,115200)
    open SendRecvDevice(@rpi.tx,@rpi.rx,@rpi.stop) as #2

    '' Variables
    dim inBuff as string
    dim test#,test1#,val1#,test2#,test3#,val2#,tempf%,humid%

    '' Stack
    dim blink_stack(8)
    dim com_stack(32)

    '' Main
    '' A visual blinking of LED 57 to show that
    '' there is a program that is running.
    var b = cpu(rpi_comm(),@com_stack(32))
    pausems 500
    var a = cpu(blink(57,80_000_000), @blink_stack(1))

    print "Type help for system menu."

    do
    print "> ";
    input inBuff
    if inBuff = "quit" then
    exit
    else if inBuff = "barray" then '' Battery array
    ' Start ez_analog P0,0,100, reading in percentage
    adc_ez.start(0, 0, 100) '' This uses P0
    pausems 250
    test# = (adc_ez.readit()/1)
    ' Get actual value of percentage
    test1# = ((test# * 3.3)/100)
    ' Calibration to show actual voltage
    val1# = (test1# * 5.04928)
    ' Display the value
    print using "##.### Battery Array Volts";val1#

    else if inBuff = "apanel" then  '' Solar panel array
        adc_ez.start(2, 0, 100)  '' This uses P2
        pausems 250
        test3# = (adc_ez.readit()/1)
        'print test3#
        test2# = ((test3# * 3.3)/100)
        'print test2#
        val2# = (test2# * 38.56749)
        print using "##.### Solar Array Volts";val2#
    else if inBuff = "dcpwr" then   '' Backup DC power source
        adc_ez.start(1, 0, 100)  '' This uses P1
        pausems 250
        test3# = (adc_ez.readit()/1)
        'print test3#
        test2# = ((test3# * 3.3)/100)
        'print test2#
        val2# = (test2# * 5.14015)
        print using "##.### DC Power Volts";val2#   
    else if inBuff = "tempf" then
        dht.start(30,22,0)
        tempf% = dht.read_tempf()
        tempf% = (tempf%/10)
        print using "##.# Degrees F";tempf%
    else if inBuff = "humid" then
        dht.start(30,22,0)
        humid% = dht.read_humidity()
        humid% = (humid%/10)
        print using "##.# Percent Humidity";humid%
    else if inBuff = "help" then
        menu()
    else
        print "Invalid Command!"
    end if
    

    loop

    print "Program Ended!"
    print "00"
    _reboot '' Reset
    end

    '' Subroutines
    ''
    '' Subroutine for the CPU(COG)
    sub rpi_comm()

    do
        print #2, "A"
        pausems 2000
    loop
    

    end sub
    ''''''''''''''''''''
    sub blink(pin, freq)
    direction(pin) = output
    do
    output(pin) = not output(pin)
    waitcnt(getcnt() + freq)
    loop
    end sub

    sub menu()
    print " Menu"
    print "quit - End the program"
    print "barray - Battery array"
    print "apanel - Solar panel array"
    print "dcpwr - Power from DC"
    print "tempf - Teperature"
    print "humid - Humidity"
    end sub

    [/CODE]

  • I cannot figure out why this program is not working as expected.


    I am not sure where the problem is, I put in a "print debug1" in the main do...loop and it never gets to that point. Is there a problem with the way the cpu function is written? Is there a problem with the way jm_dhtxx.spin2 is working with FlexBasic, or a problem with my code that I am not seeing.


    The program should be getting a value from the dhtxx, and then it should be pushing that value out the ser connection. Somewhere it is falling down.


    Ray


    [code]

    ' solsta1.bas


    ' January 27, 2021

    '

    '

    dim adc_ez as class using "jm_analog_in.spin2"

    dim dht as class using "jm_dhtxx.spin2"

    dim rpi as class using "jm_fullduplexserial.spin2"


    rpi.start(4,6,0,115200)


    '' Variables

    dim inBuff as string

    dim shared tempf%


    '' Stack

    dim blink_stack(8)

    dim com_stack(32)


    '' Main

    print "Type help for system menu."

    'var a = cpu(blink(57,80_000_000),@blink_stack(1))

    var b = cpu(rpi_comm(),@com_stack(32))

    'dht.start(30,22,1)


    do

    dht.start(30,22,1)

    tempf% = dht.read_tempf()

    pausems 250

    tempf% = (tempf%/10)

    print "debug 1"

    'print tempf%

    ' print "> ";

    ' input inBuff

    ' if inBuff = "quit" then

    ' exit

    ' else if inBuff = "help" then

    ' menu()

    ' else

    ' print "Invalid Command!"

    ' end if

    loop


    print "Program Ended!"

    print "0"

    _reboot

    end


    '' Subroutines

    '' Subroutine for the CPU(COG)

    sub rpi_comm()


    do


    rpi.str("Temp ")

    rpi.dec(tempf%)

    pausems 2000

    loop

    end sub

    ''''''''''''''''''''


    sub blink(pin, freq)


    do

    'tempf% = dht.read_tempf()

    'pausems 250

    'tempf% = (tempf%/10)

    pausems 2000

    loop

    end sub

    ''''''''''''''''''''


    sub menu()

    print "     Menu"

    print "quit - End the program."

    end sub

    ''''''''''''''''''''


    'sub temp1()


    'end sub

    [/code]

Sign In or Register to comment.