Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : MM2: Problems with MPRLS0015PA0000SA SPI pressure sensor
Page 1 of 2 | |||||
Author | Message | ||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Hi to all, i am desperately trying to read out a MPRLS0015PA0000SA pressure sensor here with a Micromite. MPRLS0015PA0000SA_Datasheet.pdf Somehow this does not work as expected. :-( Can anyone help me with this? According to the data sheet the following must be sent: This is my testcode: Option EXPLICIT Option BASE 1 ' our array will start with the index 1 Option Default Integer Dim data%(4) ' define the array for receiving the data Dim Value Dim Float PSI Dim Float hPA Pause 50 'SPI OPEN speed (kHz), mode, bits SPI OPEN 100000, 0, 8 Const EOC = 4 'End-of-conversion Const MPRL_sel = 2 'MPRLS0015PA0000SA Chip Select SetPin EOC, din SetPin MPRL_sel, dout Do Pin(MPRL_sel)=0 'activate chip (Chip Select Low) Pause 5 SPI write 1, &HAA SPI write 1, 0 SPI write 1, 0 Pause 30 SPI write 1,&HF0 SPI READ 1, data%(1) SPI write 1,&H00 SPI READ 1, data%(2) SPI write 1,&H00 SPI READ 1, data%(3) SPI write 1,&H00 SPI READ 1, data%(4) ' data%(1)=SPI(&HF0) ' data%(2)=SPI(0) ' data%(3)=SPI(0) ' data%(4)=SPI(0) Value=data%(1)+(data%(4)<<8)+(data%(3)<<16) PSI=(Value-&H19999A)*15 PSI=PSI/(&HE66666-&H19999A) hPA=PSI*68.947572932 Print data%(1)," ",data%(2)," ",data%(3)," ",data%(4) Print "Value: ",Value Print "PSI: ",PSI Print "hPA: ",hPA Pause 500 Pin(MPRL_sel)=1 'Chip deaktivieren (Chip Select Low) Pause 5 Loop Here is the result: > run 0 0 64 92 Value: 4217856 PSI: 2.83882 hPA: 195.73 128 0 64 88 Value: 4216960 PSI: 2.83782 hPA: 195.661 128 0 64 89 Value: 4217216 PSI: 2.83811 hPA: 195.68 128 0 64 83 Value: 4215680 PSI: 2.83639 hPA: 195.562 128 0 64 84 Value: 4215936 PSI: 2.83668 hPA: 195.582 128 0 64 90 Value: 4217472 PSI: 2.83839 hPA: 195.7 > It seems that data%(2) is the status byte?!? (The status byte should be the first byte?) data%(4) seems to be the lowest byte but what is the highest? When I use data%(1)=SPI(&HF0) data%(2)=SPI(0) data%(3)=SPI(0) data%(4)=SPI(0) I get always "0" for all data% - WHY??? What am I doing wrong? The air pressure should be around 1000 hPa - so the calculation seems to be wrong... Any help is apreciated!!! Frank |
||||
Justplayin Guru Joined: 31/01/2014 Location: United StatesPosts: 309 |
It looks like you need to read the Status byte and 2 Data bytes after you send the command to wake up. Pressure calculations should be done using data%(2), data%(3) and data%(4) as data%(1) is the status byte. --Curtis Edited 2020-12-02 04:48 by Justplayin I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
CircuitGizmos Guru Joined: 08/09/2011 Location: United StatesPosts: 1421 |
Your choice of mode 0 looks to be the right choice, but I've found that a few datasheets list this incorrectly. Might be worth the time experimenting with the other modes just to be sure. Does the SPI routine not work correctly with BASE 1? Try BASE 0 (and adjust your offset/index accordingly) as a test. Micromites and Maximites! - Beginning Maximite |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
I would have understood it that way - but the numerical values speak against it. I tried all other modes - only mode 0 worked... I will try another BASE. According to the manual this should be the same data%(1)=SPI(&HF0) data%(2)=SPI(0) data%(3)=SPI(0) data%(4)=SPI(0) as this SPI write 1,&HF0 SPI READ 1, data%(1) SPI write 1,&H00 SPI READ 1, data%(2) SPI write 1,&H00 SPI READ 1, data%(3) SPI write 1,&H00 SPI READ 1, data%(4 Why do I only get "0" with the first expression??? :-( Frank |
||||
Justplayin Guru Joined: 31/01/2014 Location: United StatesPosts: 309 |
Did you try something like this? SPI write 1,&HAA SPI READ 1, status% SPI write 1,&H00 SPI READ 1, dontcare% SPI write 1,&H00 SPI READ 1, dontcare% Pause 30 SPI write 1,&HF0 SPI READ 1, data%(1) SPI write 1,&H00 SPI READ 1, data%(2) SPI write 1,&H00 SPI READ 1, data%(3) SPI write 1,&H00 SPI READ 1, data%(4) I could be wrong, but I think you need to write a byte then read a byte for this device. --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Hmmmm, I tried your code ...but I get always: [54] SPI READ 1, status% Error : Invalid variable > Why??? I tried it with and without OPTION EXPICIT and with "Dim status%" and "Dim dontcare%" (...it also doesn't work with other variable names...) Frank |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8579 |
Frank Use the SPI function rather than the READ and WRITE. I think you will then find it works |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Hi Matherp, thank you very much for your response! Which SPI function did you mean? Did you mean the C-SPI function? Where can I find it? I had addressed SPI devices before (especially with the Maximite) and never had such problems... Frank |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8579 |
use: inbyte=SPI(outbyte) Remember SPI isn't like I2C. Every byte sent in SPI also receives a byte When you use SPI READ you are also sending a byte (probably 0) to cause the read When you do SPI WRITE you are also reading a byte and discarding it The SPI function returns the byte that was received at the same time as the byte in the brackets was sent |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Dear Peter, thanks for your suggestion - I know and I tried this first. Please take a look on my code above. I tried: data%(1)=SPI(&HF0) data%(2)=SPI(0) data%(3)=SPI(0) data%(4)=SPI(0) ...but I have always read only "0" with this construction. :-( Frank |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8579 |
Some devices need SPI(&HFF) - worth trying |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Peter, instead of what? The sensor would like to see: &HAA,&H00,&H00 (Wait) &HF0,&H00,&H00,&H00 Now I tried &HFF instead of &H00, &HFF instead of &HF0, &HFF instead of &HAA - it's always the same: All read back values are "0". :-( I get only other values with "SPI write 1" and "SPI READ 1" :-( Frank |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8579 |
Frank I assume you have tried the other SPI modes? Assuming none of this works my next step would be to bitbang the I/F in Basic so you can see exactly what is happening |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Hi Peter, I have just tried all modes again: I get always "0" with data%(1)=SPI(&HF0) data%(2)=SPI(0) data%(3)=SPI(0) data%(4)=SPI(0) 0 0 0 0 Value: 0 PSI: -1.875 hPA: -129.277 0 0 0 0 Value: 0 PSI: -1.875 hPA: -129.277 0 0 0 0 Value: 0 PSI: -1.875 hPA: -129.277 I only get values when I use READ and WRITE with mode 0: 128 0 64 255 Value: 4259712 PSI: 2.8856 hPA: 198.955 128 0 64 253 Value: 4259200 PSI: 2.88503 hPA: 198.916 128 0 64 254 Value: 4259456 PSI: 2.88531 hPA: 198.935 128 0 64 2 Value: 4194944 PSI: 2.81321 hPA: 193.964 128 0 64 0 Value: 4194432 PSI: 2.81264 hPA: 193.925 ...is it possible that something is wrong with "inbyte=SPI(outbyte)" command??? I use MM.VER 5.0503. Why did I get "Error : Invalid variable" with "SPI READ 1, status%" or "SPI READ 1, dontcare%" ??? Frank |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8579 |
Very unlikely. Connect spi-out to spi-in and try ? spi(123) From the manual: Read can only use arrays as the input data item Other thoughts: - why not try I2C - I assume you are pulling SS low. Try controlling SS as you do the transactions. Some devices don't like SS just pulled continuously low Edited 2020-12-03 20:20 by matherp |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Thanks for your explanation!!! I shorted pin 3 with pin 14 (it's a 28 pin device) and changed my code: Do Data %(1)=SPI(123) Print Data%(1) Pause 50 Loop This is the result: 0 0 0 0 0 0 0 0 0 > With this... Do ' Data %(1)=SPI(123) SPI write 1, (123) SPI READ 1, data%(1) Print Data%(1) Pause 50 Loop ...I get this: 0 0 64 26 0 0 64 26 0 0 64 26 0 > ???? Frank |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8579 |
Frank We need to go back to first principles as there is something wrong. Disconnect everything from the MM Connect pin 3 to pin 14 At the command prompt type SPI OPEN 1000000,3,8 ? SPI(123) If you don't get back 123 then you have a H/W problem. I've just tested on a 28-pin MM2 running a fresh install of 5.05.03 downloaded from Geoff's site and it works perfectly. Until this works everything else is just adding to the confusion Edited 2020-12-03 21:30 by matherp |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Peter, SUCCESS! This works: SPI open 1000000,3,8 Do Print SPI(123) Loop Result: 123 123 123 123 123 123 123 123 123 123 > Frank |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5904 |
Try using integers instead of an array. data1% = SPI(&HF0) data2% = SPI(0) data3% = SPI(0) data4% = SPI(0) Jim VK7JH MMedit  MMBasic Help |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 813 |
Hi Jim, thanks for your suggestion. Unfortunately that did not help either! ...I will probably have to connect my logic analyzer... Frank |
||||
Page 1 of 2 |
Print this page |