SPI PIC 18F4620 and BMP280 (In Oshonsoft)

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
I tried swapping lines around to test if it was a timing problem.
First, I swapped the 2x READs around no difference
Next, I swapped the 2x WRITEs around no difference.

I re-checked the 0x74 Register setting, which are tricky to read, and found that I had perhaps the incorrect setting, which seems to have defaulted to 0x00.
Now the 0xF4 is set to 0x2F, it now shows in the CSV results, Here:

Next, change the rest of the BMP280 SPI calculation settings, and checking the Temp/Press output.
C
 

Attachments

jjw

Joined Dec 24, 2013
823
When you get it working, you could clean the program by making two functions ( or procedures) like
BMP_read( adr ), BMP_write(adr, data) or whatever name you like for the functions.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
When you get it working, you could clean the program by making two functions ( or procedures) like
BMP_read( adr ), BMP_write(adr, data) or whatever name you like for the functions.
Hi J,
Ok, if I don't remember, perhaps you would remind me please.

With the correct value showing in the analyser, using the terminal, would show the last READ, untill I added [ ALTMTR = 0 then 1 ] in between the READS.

I'll carry on down, and hope the TP PR READings are ok after the calculation.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
Here is the results from the TEMP-PRESS READings: (First SWSPI then HWSPI, with results of TERMINAL shown at the bottom of each section)

I tried the same settings (please check) for the 'compensation parameters' but it doesn't seem to work, so next I'll try adding in ALTMETER = 0-1. between each READ.
C
 

Attachments

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi J and E,
OK, thanks.

As I copied the earlier 'compensation parameter routine from earlier, which worked, I returned to it, to find it missed the last READing of the list.

Hopefully I've corrected it so now it READs all of the 3xT and 9xP parameters. HERE:

1/ Is this 'Burst READing' ?

2/ Here is the CODE for it:
Does it look the correct format?
-------------------------------------------------------------------------------------------------------------------
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA BMP280 ALTIMETER AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Symbol altmtr = LATD.1 'BMP280 BAROMETER/TEMP
'BMP280 CALC
Dim dummy As Byte
Dim test As Byte
Dim dta As Byte

Dim var1 As Single
Dim var2 As Single
Dim t_fine As Single
Dim t_raw As Long
Dim p_raw As Long
Dim adc_t As Single
Dim adc_p As Single
Dim tp As Single 'TEMP result in CALC
Dim pr As Single 'PRESS result in CALC
Dim strtp As String
Dim strpr As String
Dim adr As Byte 'ADDRESS FOR BMP280
Dim t1 As Word 'Compensation parameters
Dim t2 As Word
Dim t3 As Word
Dim p1 As Word
Dim p2 As Word
Dim p3 As Word
Dim p4 As Word
Dim p5 As Word
Dim p6 As Word
Dim p7 As Word
Dim p8 As Word
Dim p9 As Word
Dim dig_t1 As Single 'Compensation parameters
Dim dig_t2 As Single
Dim dig_t3 As Single
Dim dig_p1 As Single
Dim dig_p2 As Single
Dim dig_p3 As Single
Dim dig_p4 As Single
Dim dig_p5 As Single
Dim dig_p6 As Single
Dim dig_p7 As Single
Dim dig_p8 As Single
Dim dig_p9 As Single

'Compensation parameters
altmtr = 0
For i = 0 To 25
adr = 0x88 + i
SSPBUF = adr
While Not SSPSTAT.BF
Wend
data = SSPBUF
WaitUs 2
b(i) = data
Next i
altmtr = 1

'Compensation parameters
'Temp coefficents
dummy = b(0)
t1.LB = b(1)
t1.HB = b(2)
t2.LB = b(3)
t2.HB = b(4)
t3.LB = b(5)
t3.HB = b(6)
'pressure coefficients
p1.LB = b(7)
p1.HB = b(8)
p2.LB = b(9)
p2.HB = b(10)
p3.LB = b(11)
p3.HB = b(12)
p4.LB = b(13)
p4.HB = b(14)
p5.LB = b(15)
p5.HB = b(16)
p6.LB = b(17)
p6.HB = b(18)
p7.LB = b(19)
p7.HB = b(20)
p8.LB = b(21)
p8.HB = b(22)
p9.LB = b(23)
p9.HB = b(24)
dummy = b(25)

Hserout "TI-P9 ", #t1.LB, " ", #t1.HB, " ", #t2.LB, " ", #t2.HB, " ", #t3.LB, " ", #t3.HB, " ", #p1.LB, " ", #p1.HB, " ", #p2.LB, " ", #p2.HB, " ", #p3.LB, " ", #p3.HB, " ", #p4.LB, " ", #p4.HB, " ", #p5.LB, " ", #p5.HB, " ", #p6.LB, " ", #p6.HB, " ", #p7.LB, " ", #p7.HB, " ", #p8.LB, " ", #p8.HB, " ", #p9.LB, " ", #p9.HB, CrLf
'convert to singles with sign
dig_t2 = t2
If dig_t2 > 32767 Then
dig_t2 = dig_t2 - 65536
Endif
dig_t3 = t3.................................etc
-----------------------------------------------------------------------------------------------------------
C
 

Attachments

Last edited:

ericgibbs

Joined Jan 29, 2010
18,766
hi,
The datasheet for the BMP 'suggests' that if CS is held Low during the Read sequence, using only the Reg 'block' start address at the start, then after each byte is Read the internal Register address pointer auto increments, as a 'burst read'
It also states that during a burst it will not change the content of its internal registers, thats how you want it.
If you addressed every Register individually , using CS Low then High between each Read, you may get a 'mixed' Read of current and new data, which could cause problems with your final calculations.

BTW: how have you defined the b(x) array, byte , word etc..?
E

Update:
Ideally you should also check 0xF3 'status' bits before doing a P or T read cycle, check that its not busy.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,
The datasheet for the BMP 'suggests' that if CS is held Low during the Read sequence, using only the Reg 'block' start address at the start, then after each byte is Read the internal Register address pointer auto increments, as a 'burst read'
It also states that during a burst it will not change the content of its internal registers, thats how you want it.
If you addressed every Register individually , using CS Low then High between each Read, you may get a 'mixed' Read of current and new data, which could cause problems with your final calculations.

BTW: how have you defined the b(x) array, byte , word etc..?
E

Update:
Ideally you should also check 0xF3 'status' bits before doing a P or T read cycle, check that its not busy.
Hi E,
Previously, I had to use the CS LOW/HIGH between each READ, so now with only one at the beginning and one at the end, I presume that this is 'Burst READing'

Re-Defining: I have updated #67

My second question in #67.
Does the CODE comply with what you and 'J' were explaining in #65-66?
C.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi,
It is burst read.
Your code at initial check looks OK, I need to check further to be sure.

This clip.
'Compensation parameters
altmtr = 0
For i = 0 To 25
adr = 0x88 + i
SSPBUF = adr
While Not SSPSTAT.BF
Wend
data = SSPBUF ' have you checked that OSH will accept b(i)= SSPBUF
WaitUs 2
b(i) = data ' if yes, this line is redundant
Next i
altmtr = 1

Update:
Reference the Pressure and Temperature values

I guess you realise that the 3 off Byte, Register parameter values are weighted as msb,lsb and xlsb , with the xlsb value in the upper 4 bits of the xlsb Byte.??

So ideally after data read, you have a 20Bit Binary value 'embedded' in a 24Bit LONG , which has to be Right shifted >> 4 places.

Please post an example on how you are doing that.?
E

BTW: how have you defined the b(x) array, byte , word etc..?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,
It is burst read.
Your code at initial check looks OK, I need to check further to be sure.

This clip.
'Compensation parameters
altmtr = 0
For i = 0 To 25
adr = 0x88 + i
SSPBUF = adr
While Not SSPSTAT.BF
Wend
data = SSPBUF ' have you checked that OSH will accept b(i)= SSPBUF
WaitUs 2
b(i) = data ' if yes, this line is redundant
Next i
altmtr = 1

Update:
Reference the Pressure and Temperature values

I guess you realise that the 3 off Byte, Register parameter values are weighted as msb,lsb and xlsb , with the xlsb value in the upper 4 bits of the xlsb Byte.??

So ideally after data read, you have a 20Bit Binary value 'embedded' in a 24Bit LONG , which has to be Right shifted >> 4 places.

Please post an example on how you are doing that.?
E

BTW: how have you defined the b(x) array, byte , word etc..?
Hi E,
Re-SHIFT
-------------------------------------------------------------------------------------------------
'''''''''''''''''''''' READ BMP280 '''''''''''''''''''''''''''''''''''''''''

For i = 0 To 2 'Altimeter TEMPERATURE
altmtr = 0 'CHIP SELECT BMP280 ON
adr = 0xfa + i 'Altimeter
'SPISend adr
'SPIReceive data 'dataa Altimeter
b(i) = data 'Altimeter
altmtr = 1 'CHIP SELECT BMP280 OFF
Next i 'ia altimeter

t_raw.LB = b(2) 'Altimeter TEMPERATURE
t_raw.HB = b(1) 'Altimeter
t_raw.3B = b(0) 'Altimeter
t_raw = ShiftRight(t_raw, 4)
adc_t = t_raw

For i = 0 To 2 'Altimeter PRESSURE
altmtr = 0 'CHIP SELECT BMP280 ON
adr = 0xf7 + i 'altimeter
'SPISend adr
'SPIReceive data 'altimeter
b(i) = data 'Altimeter
altmtr = 1 'CHIP SELECT BMP280 OFF
Next i 'Altimeter


p_raw.LB = b(2) 'ba Altimeter TEMPERATURE
p_raw.HB = b(1) 'ba Altimeter
p_raw.3B = b(0) 'ba Altimeter
p_raw = ShiftRight(p_raw, 4)
adc_p = p_raw

Gosub calca

strtp = #tp
strpr = #pr
Hserout "TP ", #tp, " ", "PR, ", #pr, CrLf
WaitMs 1
-----------------------------------------------------------------------------------
Re-DEFINE:
I've updated #67 showing the DIMS, is this what you're asking?

I'll check the RED lines!
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi E,
------------------------------------------------------------------------------------
While Not SSPSTAT.BF
Wend
data = SSPBUF ' have you checked that OSH will accept b(i)= SSPBUF
WaitUs 2
b(i) = data ' if yes, this line is redundant
Next i
altmtr = 1
--------------------------------------------------------------------------------------------
I COMMENTED OUT the REDUNDANT line, and many of the digits disappeared, with it in, it works.
C
 

ericgibbs

Joined Jan 29, 2010
18,766
hi,
In that case, you may have to adjust/increase the WaitUs value.
Recall what I said about the Slave should ideally be faster than the Master, so that its Ready to Read in time.
So slow down the Read rate, WaitUs longer.
E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,
In that case, you may have to adjust/increase the WaitUs value.
Recall what I said about the Slave should ideally be faster than the Master, so that its Ready to Read in time.
So slow down the Read rate, WaitUs longer.
E
Hi E,
I recall now you've reminded me!
I tried waitms 10 (No difference, should I go longer?

There seems to be a problem READing P9.HB. Shown here [ 23 ]

It was working earlier, and without changing that section of CODE, [ P9.HB ] disappeared.

I tried:
---------------------------------------------------------------------
While Not SSPSTAT.BF
Wend
b(i) = SSPBUF
WaitUs 2
altmtr = 1
---------------------------------------------------------------------
This gave the same sometimes faulty result.
C.
 

Attachments

Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi J and E,
I chose the wrong image, here's a faulty one:

I kept trying longer time, to see if WAITMS worked. It didn't.
C
 

Attachments

jjw

Joined Dec 24, 2013
823
I don't know if this has anything to do with the problem, but you are now reading without separate dummy byte, the next address is the dummy byte.
Try to read a few times only the P9:

SSPBUF = adr ( of P9 )
While
...
Wend
SSPBUF = 0x00 ( or 0xff whichever works) dummy byte
While
...
Wend
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
I don't know if this has anything to do with the problem, but you are now reading without separate dummy byte, the next address is the dummy byte.
Try to read a few times only the P9:

SSPBUF = adr ( of P9 )
While
...
Wend
SSPBUF = 0x00 ( or 0xff whichever works) dummy byte
While
...
Wend
Hi J,
I played around with the above, and the CODE definitely needs and extra READ.
With this: NOTE, some of it is commented out
--------------------------------------------------------------------------------------------
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA BMP280 ALTIMETER AAAAAAAAAAAAAAAAAA
Symbol altmtr = LATD.1 'BMP280 BAROMETER/TEMP
'BMP280 CALC
Dim dummy As Byte
dummy = 0xff
Dim test As Byte
Dim dta As Byte

Dim var1 As Single
Dim var2 As Single
Dim t_fine As Single
Dim t_raw As Long
Dim p_raw As Long
Dim adc_t As Single
Dim adc_p As Single
Dim tp As Single 'TEMP result in CALC
Dim pr As Single 'PRESS result in CALC
Dim strtp As String
Dim strpr As String
Dim adr As Byte 'ADDRESS FOR BMP280
Dim t1 As Word 'Compensation parameters
Dim t2 As Word
Dim t3 As Word
Dim p1 As Word
Dim p2 As Word
Dim p3 As Word
Dim p4 As Word
Dim p5 As Word
Dim p6 As Word
Dim p7 As Word
Dim p8 As Word
Dim p9 As Word
Dim dig_t1 As Single 'Compensation parameters
Dim dig_t2 As Single
Dim dig_t3 As Single
Dim dig_p1 As Single
Dim dig_p2 As Single
Dim dig_p3 As Single
Dim dig_p4 As Single
Dim dig_p5 As Single
Dim dig_p6 As Single
Dim dig_p7 As Single
Dim dig_p8 As Single
Dim dig_p9 As Single

'Compensation parameters
altmtr = 0
For i = 0 To 25
adr = 0x88 + i
SSPBUF = adr
While Not SSPSTAT.BF
Wend
b(i) = SSPBUF 'data = SSPBUF
WaitUs 2
'b(i) = data
Next i

SSPBUF = dummy '0xa0 '0x9e
While Not SSPSTAT.BF
Wend
SSPBUF = dummy
While Not SSPSTAT.BF
Wend

'SSPBUF = 0x9f
'While Not SSPSTAT.BF
'Wend
'SSPBUF = dummy
'While Not SSPSTAT.BF
'Wend

altmtr = 1

'Compensation parameters
'Temp coefficents
dummy = b(0)
t1.LB = b(1) '0x88
t1.HB = b(2) '0x89
t2.LB = b(3) '0x8a
t2.HB = b(4) '0x8b
t3.LB = b(5) '0x8c
t3.HB = b(6) '0x8d
'pressure coefficients
p1.LB = b(7) '0x8e
p1.HB = b(8) '0x8f
p2.LB = b(9) '0x90
p2.HB = b(10) '0x91
p3.LB = b(11) '0x93
p3.HB = b(12) '0x94
p4.LB = b(13) '0x95
p4.HB = b(14) '0x96
p5.LB = b(15) '0x97
p5.HB = b(16) '0x98
p6.LB = b(17) '0x99
p6.HB = b(18) '0x9a
p7.LB = b(19) '0x9b
p7.HB = b(20) '0x9c
p8.LB = b(21) '0x9d
p8.HB = b(22) '0x9e
p9.LB = b(23) '0x9f
p9.HB = b(24) '0xa0
dummy = b(25)
-------------------------------------------------------------------------------------
[ 23 ] is READ and shown in the Terminal.

(Would you proof read my 'compensation parameter' commented out REG addresses please)
C
 
Top