SPI PIC 18F4620 and BMP280 (In Oshonsoft)

Thread Starter

camerart

Joined Feb 25, 2013
2,600
Why do you want to simulate UART, if it works live?
Hi J,
This is for looking at ASM not simulating. EDIT: I did try to simulate, realising that it wouldn't sim fully, but just to see if it HSEROUT

I've hardly ever looked at ASM, as it's baffling, but I was interested to see the differences between 2x PICs, that I think are almost the same.
These 3x PICs have slightly different voltages, but are PIN compatible.

C
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
2,600
Hi,
J, I've clarified #201.

I've tried many different iterations, of the attached CODE section, trying to get SSPBUF to work.
The image shows the 'old' OSH SPI method in the same LOOP, but doesn't RECEIVE the ID from ALTIMETER.
C
Code:
main_loop:  '/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

'ALTIMETER IN
    Gosub read_BMP280  'update altimeter readings

WaitMs 10  '?????????????????????
    
Gosub show_BMP280  'output current air data

'###########################################

Toggle rled
WaitMs 100

SSPBUF = 0xd0  'ID

altmtr_cs = 0
    WaitUs 10
    'While Not SSPSTAT.BF  'If buffer empty do nothing
    'Wend
    data = SSPBUF
    WaitUs 2
altmtr_cs = 1

SSPBUF = 0

altmtr_cs = 0
    WaitUs 10
    'While Not SSPSTAT.BF  'If buffer empty do nothing
    'Wend
    data = SSPBUF
    WaitUs 2
altmtr_cs = 1

Hserout "TEST READ ID  ", data, CrLf
'#########################################
Goto main_loop
    
End                                               

'----------- PERIPHERAL UTILITIES  -------------
'HC-12 INITIALISATION
init_HC12:
                                    'PRESS BUTTON (OR RADSET)
    radset = RADIO_COMMAND_MODE  'SET HC-12 COMMAND ON
    Hserout "AT+C002", CrLf  '433.800
    radset = RADIO_DATA_MODE  'SET HC-12 RUN ON
    Return
'------------------
'BMP280 UTILITIES
'Initialize BMP280
init_BMP280:
'Compensation parameters
    SPICSOn
    For i = 0 To 23
        altmtr_cs = 0  'CHIP SELECT BMP280 ON
        adr = 0x88 + i  '00=WRITE 80=READ
        SPISend adr
        SPIReceive data
        b(i) = data
        altmtr_cs = 1  'CHIP SELECT BMP280 OFF
    Next i
    SPICSOff
    
'Temp coefficents
    t1.LB = b(0)
    t1.HB = b(1)
    t2.LB = b(2)
 

Attachments

jjw

Joined Dec 24, 2013
650
Should'nt loading the SSPBUF be after chip select?
Have you initialized the PIC SPI (clock speed, clock polarity etc.)
 

Thread Starter

camerart

Joined Feb 25, 2013
2,600
Should'nt loading the SSPBUF be after chip select?
Have you initialized the PIC SPI (clock speed, clock polarity etc.)
Hi J,
I put both SSPBUF = inside the C/S
Initialise 'SPI_INIT'

Here's the result:
Note: that I had to comment out 1x WHILE/WEND
and OSH SPI has stopped working. (It doesn't really matter, but it helped me swap from one method to the other)
C
Code:
Call spi_init()  'initialise spi
WaitMs 1000

main:  '/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

'ALTIMETER IN
Gosub read_BMP280  'update altimeter readings

WaitMs 10  '?????????????????????
   
Gosub show_BMP280  'output current air data

'###########################################

Toggle rled
WaitMs 100

altmtr_cs = 0
    SSPBUF = 0xd0  'ID = 0x58 =88
    WaitUs 10
    'While Not SSPSTAT.BF  'If buffer empty do nothing
    'Wend
    data = SSPBUF
    WaitUs 2
altmtr_cs = 1

SSPBUF = 0

altmtr_cs = 0
    SSPBUF = 0x00
    WaitUs 10
    While Not SSPSTAT.BF  'If buffer empty do nothing
    Wend
    data = SSPBUF
    WaitUs 2
altmtr_cs = 1

Hserout "SSPBUF READ ID  ", data, CrLf
'#########################################
Goto main
   
End                                              

'----------- PERIPHERAL UTILITIES  -------------
'HC-12 INITIALISATION
init_HC12:
                                    'PRESS BUTTON (OR RADSET)
    radset = RADIO_COMMAND_MODE  'SET HC-12 COMMAND ON
    Hserout "AT+C002", CrLf  '433.800
    radset = RADIO_DATA_MODE  'SET HC-12 RUN ON
    Return
'------------------
Proc spi_init()  'looks like straight from the datasheet

'4620
TRISC.3 = 0  'SCK to Slave
TRISC.4 = 1  'MISO
TRISC.5 = 0  'MOSI

'MODE 0,0
SSPSTAT.SMP = 0  'sample a mid data
SSPSTAT.CKE = 1
SSPSTAT.5 = 0  'I2C only
SSPSTAT.4 = 0  'I2C only
SSPSTAT.3 = 0  'I2C only
SSPSTAT.2 = 0  'I2C only
SSPSTAT.1 = 0  'I2C only
SSPSTAT.BF = 0  'If SSPBUF=1=Full

SSPCON1.WCOL = 0  'Collision detect
SSPCON1.SSPOV = 0  'Overflow
SSPCON1.SSPEN = 1  'Configure SCK,SD0,SDI,/SS
SSPCON1.CKP = 0  'Clock Idle Low, Active High
SSPCON1.SSPM3 = 0  '0010 = SPI Master mode, clock = FOSC/64
SSPCON1.SSPM2 = 0
SSPCON1.SSPM1 = 1
SSPCON1.SSPM0 = 0

End Proc                                        


'BMP280 UTILITIES
'Initialize BMP280
init_BMP280:
'Compensation parameters
    SPICSOn
    For i = 0 To 23
        altmtr_cs = 0  'CHIP SELECT BMP280 ON
        adr = 0x88 + i  '00=WRITE 80=READ
        SPISend adr
        SPIReceive data
        b(i) = data
        altmtr_cs = 1  'CHIP SELECT BMP280 OFF
    Next i
    SPICSOff
 

Attachments

jjw

Joined Dec 24, 2013
650
Still SSPBUF=0 before the second chip select
I think SSPCON1.CKP = 0 should be 1, clock idle is high?
Is wait 10us too short when spi clock is 1/64 of PIC clock
 

Thread Starter

camerart

Joined Feb 25, 2013
2,600
Still SSPBUF=0 before the second chip select
I think SSPCON1.CKP = 0 should be 1, clock idle is high?
Is wait 10us too short when spi clock is 1/64 of PIC clock
Hi J,
Yes, I copied and pasted intead of cut, so there are 2x SSPBUF = 0
I tried reverse SSPCON1.CKP
Timing changed to 1 MS, plenty long enough I guess.

There is no: 'While Not SSPSTAT.BF - Wend, with this peripheral, removed.

Result = no change.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
2,600
Hserout data ----> Hserout #data
Hi J,
C.

SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 36
SSPBUF READ ID 2 77
TP= 188.46 PR= -25882.66
SSPBUF READ ID 1 128
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 36
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 77
SSPBUF READ ID 2 128
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 0
SSPBUF READ ID 2 0
TP= -136.92 PR= 113833.33
SSPBUF READ ID 1 36
SSPBUF READ ID 2 77
TP= 188.46 PR= -25882.66
SSPBUF READ ID 1 128
SSPBUF READ ID 2 0
 

jjw

Joined Dec 24, 2013
650
Even the values from Oshonsoft SPI functions are wrong.
Can't you just make a short test program to read only the ID from BMP280. No other data.

Code:
... Configs etc.

Call SPI_init() ' here SSPEN=1 sets the pin directions for SPI
altmeter_cs=0
SSPBUF=0x0D ' address of ID
Waitms(1)
SSPBUF=0
Waitms(1)
data=SSPBUF
Hserout .....
 

Thread Starter

camerart

Joined Feb 25, 2013
2,600
Even the values from Oshonsoft SPI functions are wrong.
Can't you just make a short test program to read only the ID from BMP280. No other data.

Code:
... Configs etc.

Call SPI_init() ' here SSPEN=1 sets the pin directions for SPI
altmeter_cs=0
SSPBUF=0x0D ' address of ID
Waitms(1)
SSPBUF=0
Waitms(1)
data=SSPBUF
Hserout .....
Hi J,
I reverted back to SSPCON1.CKP = 0, as it was like this, when the 2x PICs were 'talking' to each other. Is this ok?

Here's the CODE and result:
C
Code:
'18F46K20 8MHz EXT PCB_5 BASE 010621 1430 ALT SSPBUF ONLY
'Open up main loop to sequence the various functions
'Add 2 scheduling timers for air data/compass report and $BIRO message as main cycles too fast to
'send on each loop.

'------------- TARGET SETUP  ------------------
'Const PR2set = 125  'use this for the live board: gets 5ms per interrupt
Const PR2set = 2  'use this for fast timing to speed up simulator
Define SIMULATION_WAITMS_VALUE = 1  'Comment in for SIM out for PIC

'----------- SYSTEM CONFIG  --------------------
'PIC Config
Define CONFIG1L = 0x00
Define CONFIG1H = 0x02  '8=INT  2=EXT
Define CONFIG2L = 0x1e
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x81  'Set for HVP
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
Define CLOCK_FREQUENCY = 8

'OSH config
Define SINGLE_DECIMAL_PLACES = 2
Define STRING_MAX_LENGTH = 20  'long enough?

'IO MAP
'SET BITS ON/OFF before TRIS!
Const LATAinit = %00000000  'ON/OFF
Const LATBinit = %00000000
Const LATCinit = %00000000
Const LATDinit = %00000000
Const LATEinit = %00000000  'POSS MCLR RE3

'SET PIN IN/OUT
Const TRISAinit = %00101111  '7=XTL 6=XTL 5=ANALOGUE IN (POT) 4=SERIN 18f4431 3210= ANALOGUE INPUTS (POTS)
Const TRISBinit = %00000000  '7=PGD 6=PGC 543210 SPARE
Const TRISCinit = %10010000  '7=RX=1, 6=TX=0, 5=SDO, 4=SDI=1, 3=SCK=0, 2=RLED, 1=YLED
Const TRISDinit = %00000000  '7=CS SPARE, 6=CS DATASW_S1, 5=CS DATASW_S0, 4=CS RADSET HC-12, 3=CS BMP280, 2=CS AK8963C_DRDY, 1=AK8963C_TRIG, 0=CS AK8963C_CS
Const TRISEinit = %00000000  '=Button


'HC-12 RADIO
Symbol radset = LATD.4  'HC-12 0=COMMAND ON 1=DATA ON
Const RADIO_COMMAND_MODE = 0
Const RADIO_DATA_MODE = 1

'BMP280 ALTIMETER
Symbol altmtr_cs = LATD.3  'BMP280 BAROMETER/TEMP
Dim data As Byte

'================= CODE BEGINS ====================================
'IO Config
'Init LATx flip flops before TRIS regs
    LATA = LATAinit
    LATB = LATBinit
    LATC = LATCinit
    LATD = LATDinit
    LATE = LATEinit

    TRISA = TRISAinit
    TRISB = TRISBinit
    TRISC = TRISCinit
    TRISD = TRISDinit
    TRISE = TRISEinit
    
    SPIPrepare
    
Symbol yled = LATE.1
Symbol rled = LATE.2

'START UP LEDS
    yled = 1
    rled = 1
    WaitMs 1000
    yled = 0
    rled = 0
    WaitMs 1000

'Set up UART and RX interrupt for immediate use
    Hseropen 9600  'does a basic config of TX and RX including BAUDCON, SPEN, CREN, TXEN etc.
    RCSTA.CREN = 0  'not ready to receive yet
    INTCON = 0  'kill any interrupts
    
Hserout "TEST", CrLf
    
    Gosub init_HC12  'init radio

    Hserout "READY", CrLf  'Needs HC-12 configured to show on live PC
    
Call spi_init()  'initialise spi
WaitMs 1000

main:  '/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


Call spi_init()  'here SSPEN=1 sets the pin directions for SPI
altmtr_cs = 0
    SSPBUF = 0x0d  'address of ID
    WaitMs 1
    SSPBUF = 0
    WaitMs 1
    data = SSPBUF
altmtr_cs = 1

Toggle rled
WaitMs 100

Hserout "SSPBUF READ ID  ", #data, CrLf
'#########################################
Goto main
    
End                                               

'----------- PERIPHERAL UTILITIES  -------------
'HC-12 INITIALISATION
init_HC12:
                                    'PRESS BUTTON (OR RADSET)
    radset = RADIO_COMMAND_MODE  'SET HC-12 COMMAND ON
    Hserout "AT+C002", CrLf  '433.800
    radset = RADIO_DATA_MODE  'SET HC-12 RUN ON
    Return
'------------------
Proc spi_init()  'looks like straight from the datasheet

'4620
TRISC.3 = 0  'SCK to Slave
TRISC.4 = 1  'MISO
TRISC.5 = 0  'MOSI

'MODE 0,0
SSPSTAT.SMP = 0  'sample a mid data
SSPSTAT.CKE = 1
SSPSTAT.5 = 0  'I2C only
SSPSTAT.4 = 0  'I2C only
SSPSTAT.3 = 0  'I2C only
SSPSTAT.2 = 0  'I2C only
SSPSTAT.1 = 0  'I2C only
SSPSTAT.BF = 0  'If SSPBUF=1=Full

SSPCON1.WCOL = 0  'Collision detect
SSPCON1.SSPOV = 0  'Overflow
SSPCON1.SSPEN = 1  'Configure SCK,SD0,SDI,/SS
SSPCON1.CKP = 0  'Clock Idle Low, Active High
SSPCON1.SSPM3 = 0  '0010 = SPI Master mode, clock = FOSC/64
SSPCON1.SSPM2 = 0
SSPCON1.SSPM1 = 1
SSPCON1.SSPM0 = 0

End Proc
 

Attachments

jjw

Joined Dec 24, 2013
650
Do SPI init only once before main.
Maybe it is time to read bmp280 d/s again.
Changing the program randomly may not lead to anything.
 

Thread Starter

camerart

Joined Feb 25, 2013
2,600
Hi,
I've read the D/S until it is a blur, so instead I experimented, using the Data analyser.

After changing the timing and most of the lines I have arrived here:

As can be seen, the expected I.D. [0x58] is arriving into data analyser MISO, but not as DATA in the terminal view.
So nearly there, bt what next?
C
Code:
main:  '/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


altmtr_cs = 0
    SSPBUF = 0xd0  '0xd0 address of ID =0x58
    data = SSPBUF
    WaitUs 100
    SSPBUF = 0x00 
    WaitUs 100
altmtr_cs = 1

Toggle rled
WaitMs 100

Hserout "SSPBUF READ data ID  ", #data, CrLf

Goto main
 

Attachments

jjw

Joined Dec 24, 2013
650
Hi,
I've read the D/S until it is a blur, so instead I experimented, using the Data analyser.

After changing the timing and most of the lines I have arrived here:

As can be seen, the expected I.D. [0x58] is arriving into data analyser MISO, but not as DATA in the terminal view.
So nearly there, bt what next?
C
Code:
main:  '/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


altmtr_cs = 0
    SSPBUF = 0xd0  '0xd0 address of ID =0x58
    data = SSPBUF
    WaitUs 100
    SSPBUF = 0x00
    WaitUs 100
altmtr_cs = 1

Toggle rled
WaitMs 100

Hserout "SSPBUF READ data ID  ", #data, CrLf

Goto main
altmtr_cs = 0
SSPBUF = 0xd0 '0xd0 address of ID =0x58
data = SSPBUF
WaitUs 100
SSPBUF = 0x00
WaitUs 100
data= SSPBUF ' <---- add this
altmtr_cs = 1
 

Thread Starter

camerart

Joined Feb 25, 2013
2,600
altmtr_cs = 0
SSPBUF = 0xd0 '0xd0 address of ID =0x58
data = SSPBUF
WaitUs 100
SSPBUF = 0x00
WaitUs 100
data= SSPBUF ' <---- add this
altmtr_cs = 1
Hi J,
Here's one I tried earlier:
I tried it frst without the [1] then added it for clarity.
C
Code:
main:  '/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

altmtr_cs = 0
    SSPBUF = 0xd0  '0xd0 address of ID =0x58
    data = SSPBUF
    WaitUs 100
    SSPBUF = 0x00
    WaitUs 100
    data1 = SSPBUF
altmtr_cs = 1

Toggle rled
WaitMs 100

Hserout "SSPBUF READ data ID  ", #data, CrLf
Hserout "SSPBUF READ data1 ID  ", #data1, CrLf

Goto main


########################################
Result:
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
SSPBUF READ data ID  208
SSPBUF READ data1 ID  44
 

jjw

Joined Dec 24, 2013
650
At least it is consistent:)
The data1 should be the ID= 88 decimal.
44 means it is shifted right one place.
Check the clock settings.
 

jjw

Joined Dec 24, 2013
650
No, I mean that the data may be sampled at wrong phase, missing the bits.
Check the clock polarity, sampling on rising or falling edge, middle of the data bit...
 

Thread Starter

camerart

Joined Feb 25, 2013
2,600
No, I mean that the data may be sampled at wrong phase, missing the bits.
Check the clock polarity, sampling on rising or falling edge, middle of the data bit...
Hi J,
Does [SSBUF AN] tell you anything?
I can see that the 1st MOSI is before the clock.
Note: I changed [SSPCON1.CKP = 1 '[0] 'Clock Idle high, Active low], it worked better and I recalled the [OSH SPI INVERT CLOCK] also your comment.
I'll try upping the clock speed 1st.
C
 
Top