P2 FlexProp Basic ADC & Comm drivers
Rsadeika
Posts: 3,837
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
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
Comments
This Spin2 code: ...translates to... The Spin2 interpreter is included in the PNut download -- this is a great resource for translating Spin2 to discrete assembly.
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.
I agree with Eric, get it working first with whatever way is easiest.
This is a long way of saying that with FlexBASIC you’ve probably got a lot more elbow room than you might first think.
would you mind sharing your ds3231 driver here?
jim
Just something to think about.
Ray
Ray
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
Ray
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.
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!
Can do! I’ll be home tonight and post it. It also supports setting and retrieving via UNIX-style datestamps.
No deposit/no return. But if you break it, please let me know how so I can fix it.
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.
-- http://forums.parallax.com/discussion/172535/p2-christmas-lighting-project-beginner-intermediate-spin2-coding-week-3-files-available-now/p1?new=1
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
(File removed by JM. Submitted new version that is compatible with DHT11/DHT22 to Parallax).
Ray
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: 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
In the object: 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: 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.
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
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
Try PRINT USING, e.g. to print 1 digit before the decimal and two after, do:
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).
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
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#
loop
print "Program Ended!"
print "00"
_reboot '' Reset
end
'' Subroutines
''
'' Subroutine for the CPU(COG)
sub rpi_comm()
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]