# SPI PIC 18F4620 and BMP280 (In Oshonsoft)

#### camerart

Joined Feb 25, 2013
2,600
Hi,
I've been 'round the houses' looking at old programs and posts, where names have been changed CPOL CPHA is now CKP AND CKE AND SSPCON is now SSPCON1. (I think?)
anyway, I've found some issues with the SPI timing.
I changed SSPSTAT.CKE from '0' to '1' and now the PIC READs the DATA.
As can be seen in the attachment, there is still an issue, but I'll re-check the settings.
C

#### Attachments

• 318.6 KB Views: 3

#### jjw

Joined Dec 24, 2013
650
88 (0x58)decimal is the correct ID.
What is the problem?

#### camerart

Joined Feb 25, 2013
2,600
88 (0x58)decimal is the correct ID.
What is the problem?
Hi J,
True, but in SPI #221 there is an arrow showing a timing error.
I've got to figure out what's causing it. It could be SSPCON1 or SSPSTAT or the Digital analyser settings, I'm not sure.
I'll have a clearer head tomorrow
C

#### camerart

Joined Feb 25, 2013
2,600
Hi,
I tweaked each of the 3xWAITUS settings till it errored in some way, then added '1' to each.
How much should I add to each of the WAITUS to make it safe?
C
__________________________________________________________________
altmtr_cs = 0
WaitUs 1 '<1= Logic analyser 'The initial state of the CLK line does not atch the settings.
SSPBUF = 0xd0 '0xd0 address of ID =0x58
data = SSPBUF
WaitUs 17 '<17= Incorrect result.
SSPBUF = 0x00
WaitUs 16 '<16= Incorrect result.
data = SSPBUF
altmtr_cs = 1

#### Attachments

• 204.6 KB Views: 3

#### jjw

Joined Dec 24, 2013
650
Hi,
I tweaked each of the 3xWAITUS settings till it errored in some way, then added '1' to each.
How much should I add to each of the WAITUS to make it safe?
C
__________________________________________________________________
altmtr_cs = 0
WaitUs 1 '<1= Logic analyser 'The initial state of the CLK line does not atch the settings.
SSPBUF = 0xd0 '0xd0 address of ID =0x58
data = SSPBUF
WaitUs 17 '<17= Incorrect result.
SSPBUF = 0x00
WaitUs 16 '<16= Incorrect result.
data = SSPBUF
altmtr_cs = 1
It depends on SPI clock speed.
What it is now?
In analyzer picture it seems that data is sampled at falling edge of the clock?
Change it to rising edge?
Change the initial value of the clock pin to 1 ( LATxx.y=1)

#### camerart

Joined Feb 25, 2013
2,600
It depends on SPI clock speed.
What it is now?
In analyzer picture it seems that data is sampled at falling edge of the clock?
Change it to rising edge?
Change the initial value of the clock pin to 1 ( LATxx.y=1)
Hi J,
I am a bit confused about CPOL CPHA CKP and CKE, and I've just found this: http://www.rosseeld.be/DRO/PIC/SPI_Timing.htm, which I've got to understand first.

I'll revisit this reply later, thanks.
C.

#### camerart

Joined Feb 25, 2013
2,600
Hi,
I'm working through the SPI protocol and would like to clarify the SSPSTAT.SMP BIT.

Regarding the SSPBUF DATA transfer, do the MOSI and MISO tranfer at the same time, or does MOSI go first then MISO is returned? So in the middle of the data output time or in the middle?
C

#### Attachments

• 184.1 KB Views: 2

#### jjw

Joined Dec 24, 2013
650
I have no experience of the HWSPI and can't test it, but I think
they transfer simultaneously by the clock pulses.
There might be some delay during the clock pulse.

#### camerart

Joined Feb 25, 2013
2,600
I have no experience of the HWSPI and can't test it, but I think
they transfer simultaneously by the clock pulses.
There might be some delay during the clock pulse.
Hi J,
It's a bit ambiguous to me.
I think SPI can SEND-RECEIVE at the same time, so perhaps the SMP BIT can be: [ 0 = Input data sampled at middle of data output time]
This is a bit strange when you look at 'WAIT' in #224.
Let's see!
C

#### Attachments

• 292.9 KB Views: 2

#### jjw

Joined Dec 24, 2013
650
I don't understand this wait_us tuning.
What is PIC clock speed?
What is SPI clock speed?
If you know the SPI speed, you can calculate the needed waiting time or use:
While Not SSPSTAT.BF
Wend
If PIC clock is slow ( 8Mhz or less) the wait_us accuracy is a few microseconds.

#### camerart

Joined Feb 25, 2013
2,600
I don't understand this wait_us tuning.
What is PIC clock speed?
What is SPI clock speed?
If you know the SPI speed, you can calculate the needed waiting time or use:
While Not SSPSTAT.BF
Wend
If PIC clock is slow ( 8Mhz or less) the wait_us accuracy is a few microseconds.
Hi J,
The WAIT was just getting it to work by experiment. This tells me a few things.
While getting it to work, I switched from 8MHz to 32, as a test, but perhaps it's better back at 8, I'm not sure about the SPI speed.
After a few more things, I'll try :
While Not SSPSTAT.BF
Wend

again.
Still working through the procol, almost there.
C.

#### jjw

Joined Dec 24, 2013
650
The SPI clock speed:

Bits 3:0 in SSPCON1
0011 = SPI Master mode, clock = TMR2 output/2
0010 = SPI Master mode, clock = FOSC/64
0001 = SPI Master mode, clock = FOSC/16
0000 = SPI Master mode, clock = FOSC/4

#### camerart

Joined Feb 25, 2013
2,600
The SPI clock speed:

Bits 3:0 in SSPCON1
0011 = SPI Master mode, clock = TMR2 output/2
0010 = SPI Master mode, clock = FOSC/64
0001 = SPI Master mode, clock = FOSC/16
0000 = SPI Master mode, clock = FOSC/4
Hi J,
Of course!
The PIC SLAVE is 32MHz
At the moment the MASTER is 32MHz.
I recall the MASTER should be faster than the SLAVE, so either leave the MASTER for now, or lower it. At the moment, I only have the option of 8MHz.

SPI FOSC/16
C

#### jjw

Joined Dec 24, 2013
650
Hi J,
Of course!
The PIC SLAVE is 32MHz
At the moment the MASTER is 32MHz.
I recall the MASTER should be faster than the SLAVE, so either leave the MASTER for now, or lower it. At the moment, I only have the option of 8MHz.

SPI FOSC/16
C
I recall the MASTER should be faster than the SLAVE.
Do you have a link to this?
The master sets the speed.

#### camerart

Joined Feb 25, 2013
2,600
I recall the MASTER should be faster than the SLAVE.
Do you have a link to this?
The master sets the speed.
Hi J,
I recall the MASTER should be faster than the SLAVE.
MASTER/SLAVE speed is the OSC

MASTER sets the speed of the SPI.
I'll look for the link. I think it was a suggestion that the SLAVE would cope better if the OSC is faster.

With the CPOL-CPHA SMP CKP-CKE SMP settings, I read that the MASTER should be set to the SLAVE.
I've been going through 1st the BMP280 then the 18F46K20 and copying the setting visually. (Many mistakes) hopefully this is correct, would you have a look please? Next I'll check the 18F46K20 setting to see if they are the same.

I just tried removing the WAITUS timings, and replacing with
While Not SSPSTAT.BF
Wend

This worked apart from the first one after C/S, and this only errors with the data analyser.
C.

#### Attachments

• 373.5 KB Views: 1

#### camerart

Joined Feb 25, 2013
2,600
Hi,
Here is a terminal view + Analyer view.
Note: The PIC is 'happy' but the analyser CLK is not at the beginning of some of sequences. This corrects, if [WAITUS 1] is added after C/S
C

#### Attachments

• 223.6 KB Views: 1
Last edited:

#### jjw

Joined Dec 24, 2013
650
The link says that the SLAVE ( PIC ) should have faster clock.

#### camerart

Joined Feb 25, 2013
2,600
The link says that the SLAVE ( PIC ) should have faster clock.
Hi J,
I think of it more of a guide, to note, if errors occur.
C.

#### camerart

Joined Feb 25, 2013
2,600
Hi,
Here are the READings from this: CODE.

In the BMP280 D/S there is a not that it is better to BURST READ the registers. In the CODE shown, if I remove the [altmtr_cs =] from between the READings, and I get numbers, but I don't think they are READings, but errors??, so how is BURST READ done?
C
Code:
altmtr_cs = 0
'WaitUs 1
SSPBUF = 0xf7  '0xd0  '0xd0 address of ID =0x58
data = SSPBUF
While Not SSPSTAT.BF
Wend
SSPBUF = 0x00
While Not SSPSTAT.BF
Wend
data = SSPBUF
altmtr_cs = 1
Hserout "SSPBUF READ data F7  ", #data, CrLf

altmtr_cs = 0
'WaitMs 1
SSPBUF = 0xf8  '0xd0  '0xd0 address of ID =0x58
data = SSPBUF
While Not SSPSTAT.BF
Wend
SSPBUF = 0x00
While Not SSPSTAT.BF
Wend
data = SSPBUF
altmtr_cs = 1
Hserout "SSPBUF READ data F8  ", #data, CrLf

altmtr_cs = 0
'WaitMs 1
SSPBUF = 0xf9  '0xd0  '0xd0 address of ID =0x58
data = SSPBUF
While Not SSPSTAT.BF
Wend
SSPBUF = 0x00
While Not SSPSTAT.BF
Wend
data = SSPBUF
altmtr_cs = 1
Hserout "SSPBUF READ data F9  ", #data, CrLf

altmtr_cs = 0
'WaitUs 1
SSPBUF = 0xfa  '0xd0  '0xd0 address of ID =0x58
data = SSPBUF
While Not SSPSTAT.BF
Wend
SSPBUF = 0x00
While Not SSPSTAT.BF
Wend
data = SSPBUF
altmtr_cs = 1
Hserout "SSPBUF READ data FA  ", #data, CrLf

altmtr_cs = 0
'WaitMs 1
SSPBUF = 0xfb  '0xd0  '0xd0 address of ID =0x58
data = SSPBUF
While Not SSPSTAT.BF
Wend
SSPBUF = 0x00
While Not SSPSTAT.BF
Wend
data = SSPBUF
altmtr_cs = 1
Hserout "SSPBUF READ data FB  ", #data, CrLf

altmtr_cs = 0
'WaitMs 1
SSPBUF = 0xfc  '0xd0  '0xd0 address of ID =0x58
data = SSPBUF
While Not SSPSTAT.BF
Wend
SSPBUF = 0x00
While Not SSPSTAT.BF
Wend
data = SSPBUF
altmtr_cs = 1
Hserout "SSPBUF READ data FC  ", #data, CrLf

#### Attachments

• 292.4 KB Views: 1