Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 21:20 27 Apr 2024 Privacy Policy
Jump to

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: Germany
Posts: 813
Posted: 11:30am 01 Dec 2020
Copy link to clipboard 
Print this post

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 States
Posts: 309
Posted: 05:18pm 01 Dec 2020
Copy link to clipboard 
Print this post

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 States
Posts: 1421
Posted: 06:54pm 01 Dec 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 813
Posted: 07:18pm 01 Dec 2020
Copy link to clipboard 
Print this post

  Justplayin said  Pressure calculations should be done using
data%(2), data%(3) and data%(4) as  data%(1) is the status byte.
--Curtis

I would have understood it that way - but the numerical values speak against it.

  CircuitGizmos said  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.

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 States
Posts: 309
Posted: 07:47pm 01 Dec 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 813
Posted: 09:04am 02 Dec 2020
Copy link to clipboard 
Print this post

Hmmmm,

I tried your code
  Justplayin said  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)


...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 Kingdom
Posts: 8582
Posted: 10:21am 02 Dec 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 813
Posted: 11:07am 02 Dec 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 8582
Posted: 11:34am 02 Dec 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 813
Posted: 01:13pm 02 Dec 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 8582
Posted: 01:26pm 02 Dec 2020
Copy link to clipboard 
Print this post

Some devices need SPI(&HFF) - worth trying
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 813
Posted: 02:32pm 02 Dec 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 8582
Posted: 02:36pm 02 Dec 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 813
Posted: 10:04am 03 Dec 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 8582
Posted: 10:14am 03 Dec 2020
Copy link to clipboard 
Print this post

  Quote  ..is it possible that something is wrong with "inbyte=SPI(outbyte)" command??? I use MM.VER 5.0503.


Very unlikely. Connect spi-out to spi-in and try ? spi(123)

  Quote  Why did I get "Error : Invalid variable" with "SPI READ 1, status%" or "SPI READ 1, dontcare%" ???


From the manual:
  Quote  Data can also be received in bulk:
SPI READ nbr, array()
Where 'nbr' is the number of data items to be received and array() is a single dimension integer array where the received data items will be saved.  This command sends zeros while reading the data from the slave.


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: Germany
Posts: 813
Posted: 10:44am 03 Dec 2020
Copy link to clipboard 
Print this post

  Quote  
Read can only use arrays as the input data item

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 Kingdom
Posts: 8582
Posted: 11:29am 03 Dec 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 813
Posted: 12:24pm 03 Dec 2020
Copy link to clipboard 
Print this post

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: Australia
Posts: 5905
Posted: 02:08am 04 Dec 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 813
Posted: 10:32am 09 Dec 2020
Copy link to clipboard 
Print this post

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
© JAQ Software 2024