SPI PIC 18F4620 and BMP280 (In Oshonsoft)

ericgibbs

Joined Jan 29, 2010
18,766
hi C,
Consider it this way, it's easier to remember what is the action sequence.
As you often say, you are having a problem in remembering, what is doing what, in the program.
This is just a suggestion to help you recall the program sequence.

Some peripheral devices are Read only, so if you made a Proc spi_rd() Call, you would remember that you need to Read the input data and store it for later processing.

If you made a Proc spi_wr Call, you would know that you have to Write to the peripheral , Command and Data and possibly not store the data Read from the peripheral

A more expanded method would be to have a Proc for each SPI connected peripheral.

eg:
Proc spi_comp()
Proc spi_bmp()
Proc sp_slave()

These Proc's would contain the Wr/Rd Address of the peripheral and the required unique named data buffers,

It sounds a little bloated, but its purpose is to help you keep track in your program

If you look back over the many years we have posted, it is constantly coming back to what you have forgotten

E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi C,
Consider it this way, it's easier to remember what is the action sequence.
As you often say, you are having a problem in remembering, what is doing what, in the program.
This is just a suggestion to help you recall the program sequence.

Some peripheral devices are Read only, so if you made a Proc spi_rd() Call, you would remember that you need to Read the input data and store it for later processing.

If you made a Proc spi_wr Call, you would know that you have to Write to the peripheral , Command and Data and possibly not store the data Read from the peripheral

A more expanded method would be to have a Proc for each SPI connected peripheral.

eg:
Proc spi_comp()
Proc spi_bmp()
Proc sp_slave()

These Proc's would contain the Wr/Rd Address of the peripheral and the required unique named data buffers,

It sounds a little bloated, but its purpose is to help you keep track in your program

If you look back over the many years we have posted, it is constantly coming back to what you have forgotten

E
Hi E,
True, but improving :)
I have been concentrating on this, and kind of came up with the same conclusion, but didn't know if a 'Call spi_wr()' then a Call spi_rd() would take up more time than a 'Call spi_wr_rd() '.
Then I realised that some of the peripherals are only READ, so Call spi_wr_rd() would be a bit confusing.
Thanks.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi E,
My first 'go' is to re-write the SLAVE READ WRITE.

I think the method you outlined has a 'Call spi_wr' and a 'Call spi_wr_rd' for each peripheral.
Here I tried to change only the Peripheral variable e,g, [ spi_var_rd = compdegs/compdegs = spi_var_rd ] before and after the 'Call spi_rd' is this ok, or long winded?
____________________________________________________________
Proc spi_rd()
slave_cs = 0
WaitUs 10 'wait 10us
While Not SSPSTAT.BF 'If buffer empty do nothing
Wend
spi_var_rd = SSPBUF 'compdegs = SSPBUF 'if buffer full then get the character
WaitUs 2
slave_cs = 1
End Proc
_________________________________________________________

C
 

ericgibbs

Joined Jan 29, 2010
18,766
hi.
2s compliment.?
Do you mean High Byte and Low Byte of a Word .
Example: Word as 0x0167 in Hi and Lo = 0x01, 0x67 == 359 decimal.
Definition:
To get 2's complement of binary number is 1's complement of given number plus 1 to the least significant bit (LSB). For example 2's complement of binary number 10010 is (01101) + 1 = 01110.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi.
2s compliment.?
Do you mean High Byte and Low Byte of a Word .
Example: Word as 0x0167 in Hi and Lo = 0x01, 0x67 == 359 decimal.
Definition:
To get 2's complement of binary number is 1's complement of given number plus 1 to the least significant bit (LSB). For example 2's complement of binary number 10010 is (01101) + 1 = 01110.
Hi E,
Here is a section from the Compass D/S (JPG)
Slight mixup. 2s compliment is in the FULL program, HERE:
C.
___________________________________________________________________________
'READ COMPASS XYZ REGISTER
read_compass:
SPICSOn
For i = 0 To 18 '5 'READ XYZ Xx2 Yx2 Zx2
compss_cs = 0 'CHIP SELECT COMPASS ON
SPISend addr
SPIReceive data
b(i) = data
compss_cs = 1 'CHIP SELECT COMPASS OFF
addr = addr + 1
If addr = 0x93 Then addr = 0x80 '???CHECK???
Next i
SPICSOff
w1a_raw = b(0)
info_raw = b(1)
st_1_raw = b(2)

x_raw.LB = b(3) 'BYTE
x_raw.HB = b(4) 'BYTE
y_raw.LB = b(5)
y_raw.HB = b(6)
z_raw.LB = b(7)
z_raw.HB = b(8)

x_nc = x_raw 'SINGLE = WORD
y_nc = y_raw
z_nc = z_raw

st_2_raw = b(9)
cntl_1_raw = b(10)
cntl_2_raw = b(11)
astc_raw = b(12)

i2c_dis_raw = b(15) 'i2c???????
asax_raw = b(16)
asay_raw = b(17)
asaz_raw = b(18)

str_asax = #asax_raw
str_asay = #asay_raw
str_asaz = #asaz_raw

str_w1a = #w1a
str_info = #info
str_st_1 = #st_1

'2'S COMPLIMENT CALC
If x_nc > 32767 Then 'ALL SINGLES
x_nc = x_nc - 65536
Endif
If y_nc > 32767 Then
y_nc = y_nc - 65536
Endif
If z_nc > 32767 Then
z_nc = z_nc - 65536
Endif
'2'S COMPLIMENT CALC

_________________________________________________________________________
 

Attachments

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,
Taking the compass example, how many bytes of data do you read from the compass.?
E
Hi E,
Was this a question or a prompt?
I now think that, just changing the VARIABLE isn't enough, and the whole compass glean routine has to be done in it's own PROCESS.:)
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
Am I correct that an SSPBUF WRITE is independant of an SSPBUF READ?
In other words, if I READ from an SSPBUF and there's nothing to WRITE, is there is still an exchange, but DATA only flows one way?
C
 

ericgibbs

Joined Jan 29, 2010
18,766
hi,
There can only be one Clock source, ie: the Master.

As you have more than one peripheral device on the SPI bus, the program has to Enable the individual Select line for a device , BEFORE it starts a Clock sequence.

If the peripheral device requires an Address Byte BEFORE it has a Byte of Data ready to send to the Master.

The Master outputs 8 Clock pulses, which Clock the Peripheral Address Byte from Master to Slave.

The Master then outputs 8 more clock pulses, which clock OUT of the Peripheral the Data byte. the Master reads the data byte { Bit by Bit on each clock pulse] into its SSPBUF [ the Master program reads and saves the Data from the SSPBUF]

IF there is more than one Data Byte, the Master outputs a further 8 pulses, which clock out the second Byte of Data from the peripheral device , into the SSPBUF, [ the Master program reads and saves the Data from the SSPBUF]

The Data read sequence will be repeated for any remaining Data Bytes 'expected' from the peripheral.

When all the Bytes of Data have been Read from the Peripheral, the Master program disables that devices Select line.

E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,
There can only be one Clock source, ie: the Master.

As you have more than one peripheral device on the SPI bus, the program has to Enable the individual Select line for a device , BEFORE it starts a Clock sequence.

If the peripheral device requires an Address Byte BEFORE it has a Byte of Data ready to send to the Master.

The Master outputs 8 Clock pulses, which Clock the Peripheral Address Byte from Master to Slave.

The Master then outputs 8 more clock pulses, which clock OUT of the Peripheral the Data byte. the Master reads the data byte { Bit by Bit on each clock pulse] into its SSPBUF [ the Master program reads and saves the Data from the SSPBUF]

IF there is more than one Data Byte, the Master outputs a further 8 pulses, which clock out the second Byte of Data from the peripheral device , into the SSPBUF, [ the Master program reads and saves the Data from the SSPBUF]

The Data read sequence will be repeated for any remaining Data Bytes 'expected' from the peripheral.

When all the Bytes of Data have been Read from the Peripheral, the Master program disables that devices Select line.

E
Hi E,
Ok, I understand, thanks.

I'm guessing then, that the WRITE and READ are independant, so they don't need to be carried out at the same time.
C
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,
Explain what you mean by:
that the WRITE and READ are independant, so they don't need to be carried out at the same time.
Hi E,
If there is no DATA to WRITE to a SLAVE, but there needs to be a READ, it can be carried out. EDIT: Actually, they must be independant.(penny dropping)

I will change the name of PIC2 from SLAVE, as all of the peripherals are SLAVES, so is confusing.
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
I'm working through READ WRITE in steps.
Here is step1 that works, but with some lines commented out. Are they not needed for WRITE?
C.
___________________________________________________________________________________
Proc spi_compdeg1_wr()

'pic4431_cs = 0
'WaitUs 10 'wait 10us
'While Not SSPSTAT.BF 'If buffer empty do nothing
'Wend
SSPBUF = compdeg1
'WaitUs 2
'pic4431_cs = 1

End Proc
Proc spi_compdeg2_rd()

pic4431_cs = 0
WaitUs 10 'wait 10us
While Not SSPSTAT.BF 'If buffer empty do nothing
Wend
compdeg2 = SSPBUF 'if buffer full then get the character
WaitUs 2
pic4431_cs = 1

End Proc
________________________________________________________________________________________
 
Last edited:

ericgibbs

Joined Jan 29, 2010
18,766
hi,

You have two different Proc's in that post.?
All the Write Proc is Remmed out

E

Proc spi_compdeg2_rd() ' READ remote
pic4431_cs = 0
WaitUs 10 'wait 10us
While Not SSPSTAT.BF 'If buffer empty do nothing
Wend
compdeg2 = SSPBUF 'if buffer full then get the character
WaitUs 2
pic4431_cs = 1

End Proc
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,

You have two different Proc's in that post.?
All the Write Proc is Remmed out

E

Proc spi_compdeg2_rd() ' READ remote
pic4431_cs = 0
WaitUs 10 'wait 10us
While Not SSPSTAT.BF 'If buffer empty do nothing
Wend
compdeg2 = SSPBUF 'if buffer full then get the character
WaitUs 2
pic4431_cs = 1

End Proc
hi,

You have two different Proc's in that post.?
All the Write Proc is Remmed out

E

Proc spi_compdeg2_rd() ' READ remote
pic4431_cs = 0
WaitUs 10 'wait 10us
While Not SSPSTAT.BF 'If buffer empty do nothing
Wend
compdeg2 = SSPBUF 'if buffer full then get the character
WaitUs 2
pic4431_cs = 1

End Proc
Hi E,
In the top section, I had to REM out all except 1xline to get it to work, I've highlited it in red (EDIT).
To me it looks like WRITE and READ are controlled by 1xBIT in READ.

NOTE: I had to change the VARIABLES from MASTER PIC and SLAVE PIC to 1 and 2.
C.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi,
How can a Proc like this NOT work, its empty!!!

Proc spi_compdeg1_wr()

'pic4431_cs = 0
'WaitUs 10 'wait 10us
'While Not SSPSTAT.BF 'If buffer empty do nothing
'Wend
SSPBUF = compdeg1
'WaitUs 2
'pic4431_cs = 1

End Proc
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi,
How can a Proc like this NOT work, its empty!!!

Proc spi_compdeg1_wr()

'pic4431_cs = 0
'WaitUs 10 'wait 10us
'While Not SSPSTAT.BF 'If buffer empty do nothing
'Wend
SSPBUF = compdeg1
'WaitUs 2
'pic4431_cs = 1

End Proc
Hi E,
Sorry, I edited #156 and forgot to tell you.
In the WRITE SSPBUF = compdeg1 is still there and this works, but not with the lines uncommented. I've now removed them, as they were for my memory, while testing.
C
 
Top