Propeller Chip and DataloggerSPI
For a quick background, I know my way around with the BS2, but needed something with more speed for this current projects, so I decided to go with the Propeller chip. The spin language is new to me, so this might be easy for you guys.
I am using the propeller chip to read a value from a few sensors (already got that part working).
The problem arises when I try to use dataloggerSPI with the parallax memory stick datalogger. I have no problem writing a string to the USB drive, but the value I need to write is a number (a byte).
The program I have checks for the directory, deletes it if present and recreates it (from the test code), opens it, writes a file header to it, then goes in to the data loop.
The data loop reads the sensor (coming from an ADC0831 so I believe it may be binary in form), opens the file, writes the value, closes the file.
When i open the file all I see is a bunch of random characters.
Can anyone show me how to write a byte value using dataloggerSPI, or possibly convert it to numerical and save it with CR's between each value?
The code i am using right now is:
logger.openFileForWrite(string("Data.txt"),0)
logger.writeToFile(byte[lm34temp], 8 ,0)
logger.closeFile(string("Data.txt"))
i've tried writeToFile(@lm34temp, 4,0), as well as a few others.
Thanks
-Steven
I am using the propeller chip to read a value from a few sensors (already got that part working).
The problem arises when I try to use dataloggerSPI with the parallax memory stick datalogger. I have no problem writing a string to the USB drive, but the value I need to write is a number (a byte).
The program I have checks for the directory, deletes it if present and recreates it (from the test code), opens it, writes a file header to it, then goes in to the data loop.
The data loop reads the sensor (coming from an ADC0831 so I believe it may be binary in form), opens the file, writes the value, closes the file.
When i open the file all I see is a bunch of random characters.
Can anyone show me how to write a byte value using dataloggerSPI, or possibly convert it to numerical and save it with CR's between each value?
The code i am using right now is:
logger.openFileForWrite(string("Data.txt"),0)
logger.writeToFile(byte[lm34temp], 8 ,0)
logger.closeFile(string("Data.txt"))
i've tried writeToFile(@lm34temp, 4,0), as well as a few others.
Thanks
-Steven
Comments
PUB dec(value, address) | i '' Print a decimal number. There's no checking whether the resulting string will fit. '' If the string buffer is at least 12 bytes long, there should be no problem. '' This routine returns the address of the terminating zero byte to make it easy to add on to the end of the string. if value < 0 -value byte[address++] := "-" i := 1_000_000_000 repeat 10 if value => i byte[address++] := value / i + "0" value //= i result~~ elseif result or i == 1 byte[address++] := "0" i /= 10 byte[address]~ ' mark end of string return address
To use this, you'd do:
VAR byte buffer[12] '... dec(byte[lm34temp],@buffer) logger.writeToFile(@buffer,strsize(@buffer),0)
Finally recording correct, legible data!
What a relief. Any insight on writing a carriage return after each value? right now they are all in a line.
I have tried multiple things, none of these worked;
VAR
· word CRLF
CRLF = $0A0D
logger.writeToFile(@CRLF,2,0)
logger.writeToFile(10,1,0)
logger.writeToFile(13,1,0)
logger.writeToFile(string(" \r\n").....)
etc.
thanks!
I have a program to test the output, and am getting '21' as my value.
When i save this to the USB drive it saved all the numbers as '8'
Any idea why it might do this?
Thanks
This is the code im using right now, after creating the folder and file...
logger.openFileForWrite(string("Data.txt"),0) repeat 100 !outa[17] waitcnt(2_000_000+cnt) dira[21] :=0 'set pin 21 as input lm34temp :=0 'starts the lm34temp variable as 0 temp:=ina[21] 'sets the value of temp to the value of pin 21 (one or zero) dira[20]~~ 'sets pin as output outa[20]~~ 'sets pin 20 high outa[20]~ 'sets pin 20 low 'takes chip select low activating the adc 'the ADC0831 repeat 8 dira[19]~~ 'sets pin as output outa[19]:=1 'sets clock (pin 19) high outa[19]:=0 'sets clock (pin 19)low 'takes the adc clk low if ina[21]==1 temp:=1 else temp:=0 lm34temp:=lm34temp*2 lm34temp:=lm34temp+temp dec(byte[lm34temp],@buffer2) 'logger.openFileForWrite(string("Data.txt"),0) logger.writeToFile(@buffer2,strsize(@buffer),0) logger.closeFile(string("Data.txt"))
I have thePUB dec(value, address) | i
routine at the bottom of the program
logger.writeToFile(@buffer2,strsize(@buffer),0)
Shouldn't that read strsize(@buffer2)? There may be other issues though.To write a carriage return / line feed (needed for Windows), do: logger.writeToFile(string(13,10), 2, 0)
If you want just a carriage return (like for MacOS), leave out the ", 10" and change the ", 2" to ", 1"
Thank you! finally have a carriage return.
I tested the sensor, current output is 65.
I run the program and this is what is saved to the flash drive.
Datalog Test
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
... etc
any ideas?
thanks
-Steven
repeat 8 dira[19]~~ 'sets pin as output outa[19]:=1 'sets clock (pin 19) high outa[19]:=0 'sets clock (pin 19)low 'takes the adc clk low if ina[21]==1 temp:=1 else temp:=0 [COLOR="red"]lm34temp:=lm34temp*2 lm34temp:=lm34temp+temp[/COLOR] dec([COLOR="blue"]byte[lm34temp][/COLOR],@buffer2)
You assemble the byte value in lm34temp and then use this as an address??? The dec() function takes a value/buffer pair so you should get away with dec(lm34temp, @buffer2).lm34temp := lm34temp*2 + ina[21]
Will be sufficientTook out the byte[] and now it saves the temperature value the test program is outputting.
Thank you!
Just to verify, the dec() can record any number of digits?
Now all i have to do is find a way to record the time value. Can anyone point me in the right direction?
Thank you all
You can obviously append to the buffer provided you make it bigger and pass the correct address to subsequent conversion calls.
Any hints on outputting time steps?
Thank you!
-Steven
and since _xinfreq = 6_250_000 i an just taking the change between current and last and dividing by 6,250,000
dec(cnt,@buffer3)
the cnt im getting back is about 8,500,000 (coordinating to about 1.3 seconds) different than the one before it. But i can see the LED blinking about 10 times per second.
I have it change the state of a pin connected to the LED every time it finishes the loop.
on/off = 2 data points.
What frequency does cnt operate at?
thanks
_clkmode = xtal1 + pll16x
is that still 100MHz?
Thanks, everything is coming together!
Where can I read up on the PCB care?
Thanks
-Steven
I have yet another question though:
With your help, I got the previous code working for an ADC0831 and LM34 temp sensor.
But when i take the code from the LISY300 gyro test program (used it to make sure the circuit is working, checks out) the values from the gyroscope all save as a 0 or 8191.
Everything up to this point in the code works:
repeat 100 Measure dec(ADC,@buffer2) logger.writeToFile(@buffer2,strsize(@buffer2),0) logger.writeToFile(string(", "),2,0) dec(cnt,@buffer3) logger.writeToFile(@buffer3,strsize(@buffer3),0) logger.writeToFile(string(13,10), 2, 0) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' PUB Measure ' Clock data in for 13 bits. (First three are zero, next ten are data) result~ ' Clear result outa[nCS]~ ' nCS = low repeat 13 ' repeat 13 times outa[SCLK]~ ' clk low outa[SCLK]~~ ' clk high ADC := result := result << 1 | ina[DOUT] ' Save value in ADC variable outa[nCS]~~ ' cCS = high ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' PUB dec(value, address) | i if value < 0 -value byte[address++] := "-" i := 1_000_000_000 repeat 10 if value => i byte[address++] := value / i + "0" value //= i result~~ elseif result or i == 1 byte[address++] := "0" i /= 10 byte[address]~ ' mark end of string return address
But this is where it goes bad.
Data saved looks like this:
0, -876833898
0, -862191530
0, -847551098
8191, -832909450
8191, -817573610
0, -802933178
0, -788292554
0, -773128138
Thanks again!
-Steven
CON DOUT = 16 SCLK = 18 nCS = 17
There is the problem. Forgot to add:
dira[SCLK..nCS]~~
to the code!
thank you again! Looks like everything is in business now. Time for field testing!
Thanks
-Steven