SPI PIC 18F4620 and BMP280 (In Oshonsoft)

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi, (Oshonsoft)
EDIT: Because HW and SW always are difficult to understand, I've changed the title to suit.
PIC 18F4620 and peripheral BMP280 (Temperature/pressure chip)
I've been using Oshonsoft SWSPI successfully but from advice, I want to change over to HWSPI.

First I'm trying to READ the 'Compensation parameters'
Results from previous T1-3 and P1-9 SWSPI (ignore CH line):

Here's my program:
So far only CS is showing on a 'data analyser' no CLC, SDO, SDI

Tests within [ LOOP ]
Any clues please.
 

Attachments

Last edited:

jjw

Joined Dec 24, 2013
823
There is no Read command for hw spi.
for example :
Read 0x88, data reads the internal EEPROM at address 0x88
 

sagor

Joined Mar 10, 2019
903
You have to use and control the hardware SPI via the registers. Read section 17 of the datasheet. Section 17.3 explains the registers and what flags to check. along with how to set up the SPI in various modes.
Oshonsoft does not have hardware SPI instructions, they only implement software SPI. To use the hardware SPI, you have to directly use the SPI registers.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
You have to use and control the hardware SPI via the registers. Read section 17 of the datasheet. Section 17.3 explains the registers and what flags to check. along with how to set up the SPI in various modes.
Oshonsoft does not have hardware SPI instructions, they only implement software SPI. To use the hardware SPI, you have to directly use the SPI registers.
Hi S,
Ok, does it use [ SSPBUF ] in a similar way to: https://forum.allaboutcircuits.com/...ve-pics-using-spi-in-oshonsoft.156175/page-16 #318 ?

C.
 

sagor

Joined Mar 10, 2019
903
Without looking at your link, yes, it uses SSPBUF to both send and receive data. It uses the "buffer full" indicator flag (SSPSTAT.BF) to indicate it has received something. Since I don't use SPI (not yet anyway...), that is about as far as I can suggest based on a quick read of the datasheet.....
Good luck.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Without looking at your link, yes, it uses SSPBUF to both send and receive data. It uses the "buffer full" indicator flag (SSPSTAT.BF) to indicate it has received something. Since I don't use SPI (not yet anyway...), that is about as far as I can suggest based on a quick read of the datasheet.....
Good luck.
Hi S,
Here's the relevant section from the link:
__________________________________________________
y = Len(msg1)
For x = 1 To y
m2s = MidStr(msg2, x, 1)
SSPBUF = m2s
Call spi_wr_rd()
s2m = SSPBUF
dta = dta + Chr(s2m)
Next x
Hserout "From Slave2=", dta
dta = ""
WaitMs 500

Toggle PORTB.0 'run indicator
Goto main

End

Proc spi_wr_rd()

ss = 0
WaitUs 10
While Not SSPSTAT.BF
Wend
s2m = SSPBUF
WaitUs 2
ss = 1

End Proc
______________________________________________
Somewhere there needs to be the peripheral REGISTER to either WRITE or READ from.
The peripheral DATA SHEET suggests burst READ, so perhaps it's from one REG to another?
Thanks.
C._
 

sagor

Joined Mar 10, 2019
903
The register you "read" from is SSPBUF. When it has valid data, you simply copy that register into one of your program variables. as above (ie: s2m = SSPBUF)
For writing, you copy your variable into SSPBUF.
SSPBUF is your register....
 

jjw

Joined Dec 24, 2013
823
Hi S,
Here's the relevant section from the link:
__________________________________________________
y = Len(msg1)
For x = 1 To y
m2s = MidStr(msg2, x, 1)
SSPBUF = m2s
Call spi_wr_rd()
s2m = SSPBUF
dta = dta + Chr(s2m)
Next x
Hserout "From Slave2=", dta
dta = ""
WaitMs 500

Toggle PORTB.0 'run indicator
Goto main

End

Proc spi_wr_rd()

ss = 0
WaitUs 10
While Not SSPSTAT.BF
Wend
s2m = SSPBUF
WaitUs 2
ss = 1

End Proc
______________________________________________
Somewhere there needs to be the peripheral REGISTER to either WRITE or READ from.
The peripheral DATA SHEET suggests burst READ, so perhaps it's from one REG to another?
Thanks.
C._
In the Oshonsoft sw spi you sent the register addresses of BMP280 with SPISend( addr ) and received the data with SPIReceive()
With hw Spi, you will put the address into SSPBUF and use the procedure spi... () to send it.
Then send a dummy byte and the data will be in SSPBUF.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi S and J,
1/ Am I correct that when the 'dummy' byte is sent to the peripheral, it depends on whether the peripheral REG is 'WRITE' or 'READ only', whether any changes are made to the peripheral.

2/ Here is the SPI READ section from the peripheral:
It's confusing that the first BYTE I want to READ is [ 0x88 = 1011000 ] Removing the BIT7 then adding it back in seems wrong, but is that what should happen?
C
 

Attachments

ericgibbs

Joined Jan 29, 2010
18,766
hi,
The first 8 Bit Control Byte sent to the BMP uses Bit7 to tell the BMP whether the Control Byte is a Read or Write Command, for the following two 8Bit data Bytes.

A BMP Write Command Bit7 = 0
A BMP Read Command Bit7 =1

So if the Write Command is sent to the BMP thats 0x7X, followed by two 8Bit Bytes ie: a 16Bit Data, that are written into the BMP

So if the Read Command is sent to the BMP thats 0xFX, followed by two 8Bit Bytes ie: a 16Bit Data, that are Read from the BMP

Update: the value of FX will determine the actual register being used.
003 Aug. 21 09.29.gif
 
Last edited:

jjw

Joined Dec 24, 2013
823
2/
Something like this.
It is almost equal to sw spi, which has been working.
I can't test this so maybe it is better trust Eric in hw spi programs.

For i= 0 to 23
adr=0x88+i
SSPBUF=adr
Call Spi_wr_rd() ' send address
SSPBUF = 0
Call Spi_wr_rd() ' send dummy byte to read from address
data= SSPBUF
b(i) = data
next i

Try first to read the chip id unless you know what should come from 0x88, 0x89 etc.
 

jjw

Joined Dec 24, 2013
823
Hi E, and J,
Thanks, I can see that it looks promising, but J's CODE doesn't look as if it has the REG ADDR plus 2x BYTEs'

Here's the result of #12 and the result of the previous SWSPI
C
Show your code ( not the whole program, but around BMP280 hw Spi)
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi E and J,
Converting the SWSPI which is working, to HWSPI.

There isn't much sensible CODE to post, unless I post the whole program, but itemising as I see it:

After setting up the PIC. (TRISs ETC)
EDIT: Here is how the BMP280 is set up and READ
1/ From MEMORY MAP #11, READ CALIBRATION DATA (X23) 'turquoise line'
2/ WRITE to the 'white lines'
3/ READ the 'yellow lines'
4 CONVERT and CALIBRATE DATA and out 'pops' TEMP and PRESSURE.

I use CS (ALTMTR).

NOTE: The BMP280 DATA SHEET, suggests using 'burst READ' So far I've not used it, possibly because of Oshonsoft tests failing.

(Actually, many moons ago 'J' amazed me by working out how high my second floor is, using his ALTITUDE calculation, after I gave him the down then up pressures:) )
C.
 
Last edited:

jjw

Joined Dec 24, 2013
823
Hi E and J,
Converting the SWSPI which is working, to HWSPI.

There isn't much sensible CODE to post, unless I post the whole program, but itemising as I see it:

After setting up the PIC. (TRISs ETC)
1/ From MEMORY MAP #11, READ CALIBRATION DATA (X23) 'turquoise line'
2/ WRITE to the 'white lines'
3/ READ the 'yellow lines'
4 CONVERT and CALIBRATE DATA and out 'pops' TEMP and PRESSURE.

I use CS (ALTMTR).

NOTE: The BMP280 DATA SHEET, suggests using 'burst READ' So far I've not used it, possibly because of Oshonsoft tests failing.

(Actually, many moons ago 'J' amazed me by working out how high my second floor is, using his ALTITUDE calculation, after I gave him the down then up pressures:) )
C.
Sw spi did not use the burst mode.
Show just the code, which includes the code SPI_wr_rd()
Maybe it is better to read only one calibration value ( + chip id )
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Sw spi did not use the burst mode.
Show just the code, which includes the code SPI_wr_rd()
Maybe it is better to read only one calibration value ( + chip id )
Hi J,
I I have never used 'burst mode' but the BMP280 DATA SHEET suggests it.

My tests so far have been only for one REGISTER ADDRESS { T1.LB ]

I haven't been able to write much CODE. So far, I've only tried yours and 'E's ' suggestions.
Here's the whole program:
The relevant bits are commented by '########################
C
 

Attachments

Last edited:
Top