SEND 2x bytes in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
Using Oshonsoft is there a way to SEND 2x BYTEs with SPI?

In the attached jpg the bottom line suggests 'send 0x3C 0x0A' but Oshonsoft gives an error.
Camerart
 

ericgibbs

Joined Jan 29, 2010
18,766
hi C,
The only way with OSH Basic is,
SPISend 0x3C
SPISend 0x0A

The method I use is:
Problem you will have to use this method throughout the Code listing, major change.

''Code fragment to Send Data
lcd_mode = 1
ss = 0
spi8 = SSPBUF
SSPBUF = data.HB
While Not SSPSTAT.BF
Wend
spi8 = SSPBUF
SSPBUF = data.LB
While Not SSPSTAT.BF
Wend
ss = 1

'initialise the SPI Port at start up.
Proc spi_init()
TRISC.5 = 0 'sdo to 9225
TRISC.2 = 0 'TRISC.2 = 0 'ss
TRISC.4 = 1 'sdi not used on TFT
TRISC.3 = 0 'clk
ss = 1
SSPSTAT = 0
'SSPSTAT.CKE = 1 'clock edge unrem for trailing edge
'SSPCON1 = 0x10 ' at 8mhz 2mhz operation no SS control
SSPCON1 = %00100000
'00 = fosc/4.. 01 = fosc /16
'10 = fosc/64.. 11 controlled by timer2.
SSPCON1.CKP = 1 'clock polarity unrem for high polarity
End Proc
E
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi C,
The only way with OSH Basic is,
SPISend 0x3C
SPISend 0x0A

The method I use is:
Problem you will have to use this method throughout the Code listing, major change.

''Code fragment to Send Data
lcd_mode = 1
ss = 0
spi8 = SSPBUF
SSPBUF = data.HB
While Not SSPSTAT.BF
Wend
spi8 = SSPBUF
SSPBUF = data.LB
While Not SSPSTAT.BF
Wend
ss = 1

'initialise the SPI Port at start up.
Proc spi_init()
TRISC.5 = 0 'sdo to 9225
TRISC.2 = 0 'TRISC.2 = 0 'ss
TRISC.4 = 1 'sdi not used on TFT
TRISC.3 = 0 'clk
ss = 1
SSPSTAT = 0
'SSPSTAT.CKE = 1 'clock edge unrem for trailing edge
'SSPCON1 = 0x10 ' at 8mhz 2mhz operation no SS control
SSPCON1 = %00100000
'00 = fosc/4.. 01 = fosc /16
'10 = fosc/64.. 11 controlled by timer2.
SSPCON1.CKP = 1 'clock polarity unrem for high polarity
End Proc
E
Morning E,
Is 'ss' your chip select?

Is ' initialise the SPI Port at start up.' is before the LOOP?

First I'll see if there are other Compass modules, that don't need all this.
Thanks,
C.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi C,
Yes, ss = select chip
Init the SPI before starting the main loop, done just once at the start of the program.

What effect are you getting when you do a:
SPISend 0x3C
SPISend 0x0A

E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi E,
At first I got 255.
So far, trying to READ any of the Register addresses, has either been 255 or 0, part from 0x31 32 (See attachment) where I got a string of numbers.

I tried this:
'READ COMPASS
SPICSOn
For i = 0 To 2
compss = 0 'CHIP SELECT COMPASS ON
addr = 0x0a + i 'TEMP ADDRESS TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
SPISend 0x3c
SPISend addr
SPIReceive data
SPICSOff
b(i) = data
compss = 1 'CHIP SELECT COMPASS OFF
Next i
SPICSOff
id_raw.HB = b(1)
id_raw.LB = b(2)
id_raw.3B = b(3)
'''adc_ct = ct_raw????????????????????
strid = #id_raw

And it produced ID= 2204434431, which looks encouraging, although I don't have a number to verify it with.
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
After failing to get sensible results, I decided to try it with a logic analyser, and notice something that wasn't immediately obvious.
See the attached jpg, there are some solder links.
I connected across the SPI jumper, and guess that the C/S link is for holding the Compass 'on' once it has been programmed.
The logic analyser showed one REG default setting ok, so I'll go over some of the old settings tomorrow. Should prove positive.
C
 

Attachments

spinnaker

Joined Oct 29, 2009
7,830
Hi,
After failing to get sensible results, I decided to try it with a logic analyser, and notice something that wasn't immediately obvious.
See the attached jpg, there are some solder links.
I connected across the SPI jumper, and guess that the C/S link is for holding the Compass 'on' once it has been programmed.
The logic analyser showed one REG default setting ok, so I'll go over some of the old settings tomorrow. Should prove positive.
C
The full image is as tiny as the thumbnail.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
Failure!
I've tried many combinations of READ. Below are some examples.
The first one with [[THIS WORKS]] READs the REG B and shows 0x20 which is the default, so that seems ok to me.

READing the X,Z and Y registers [with logic analyser], either individually or singly, shows only fixed READing results where changing numbers are expected.
C.

'READ COMPASS 0X01 REGB [[[[THIS WORKS]]]]
''compss = 0 'CHIP SELECT COMPASS ON
''addr = 0x81
''SPICSOn
''For i = 0 To 1
''SPISend addr 'READ 0X81
''SPIReceive data
''SPICSOff
''b(i) = data
''addr = addr + 1 '####################################
''Next i
''SPICSOff
''compss = 1 'CHIP SELECT COMPASS OFF
''regb_raw.HB = b(1)
''regb_raw.LB = b(2)
'''adc_ct = ct_raw????????????????????
''strreghb = #regb_raw.HB
''strreglb = #regb_raw.LB
'''''''''''''''''''''''''''''TEST''''''''''''''''''''''''''''''''''''''''''''
'READ X REGISTER
SPICSOn
compss = 0 'CHIP SELECT COMPASS ON
WaitMs 10
SPISend 0x3d 'READ IDENTIFIER
SPISend 0x04 'READ X 0X03
SPIReceive x_raw.LB
compss = 1 'CHIP SELECT COMPASS OFF
WaitMs 10
SPICSOff
'x_raw.HB
'x_raw.LB
'strx = #x_raw.HB
strxhb = #x_raw.HB
strxlb = #x_raw.LB

'READ Z REGISTER
'compss = 0 'CHIP SELECT COMPASS ON
'addr = 0x85 'ZDATA ADDRESS XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'SPICSOn
'For i = 0 To 1
'SPISend addr 'READ Z 0X05
'SPIReceive data
'SPICSOff
'b(i) = data
'addr = addr + 1 '####################################
'Next i
'compss = 1 'CHIP SELECT COMPASS OFF
'SPICSOff
'z_raw.HB = b(1)
'z_raw.LB = b(2)
''strZ = #Z_raw.HB
'strzhb = #z_raw.HB
'strzlb = #z_raw.LB

'READ Y REGISTER
'compss = 0 'CHIP SELECT COMPASS ON
'addr = 0x87 'YDATA ADDRESS XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'SPICSOn
'For i = 0 To 1
'SPISend addr 'READ Y 0X07
'SPIReceive data
'SPICSOff
'b(i) = data
'addr = addr + 1 '####################################
'Next i
'compss = 1 'CHIP SELECT COMPASS OFF
'SPICSOff
'y_raw.HB = b(1)
'y_raw.LB = b(2)
'strY = #Y_raw.HB
'stryhb = #y_raw.HB
'strylb = #y_raw.LB
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi C,
The only way with OSH Basic is,
SPISend 0x3C
SPISend 0x0A

The method I use is:
Problem you will have to use this method throughout the Code listing, major change.

''Code fragment to Send Data
lcd_mode = 1
ss = 0
spi8 = SSPBUF
SSPBUF = data.HB
While Not SSPSTAT.BF
Wend
spi8 = SSPBUF
SSPBUF = data.LB
While Not SSPSTAT.BF
Wend
ss = 1

'initialise the SPI Port at start up.
Proc spi_init()
TRISC.5 = 0 'sdo to 9225
TRISC.2 = 0 'TRISC.2 = 0 'ss
TRISC.4 = 1 'sdi not used on TFT
TRISC.3 = 0 'clk
ss = 1
SSPSTAT = 0
'SSPSTAT.CKE = 1 'clock edge unrem for trailing edge
'SSPCON1 = 0x10 ' at 8mhz 2mhz operation no SS control
SSPCON1 = %00100000
'00 = fosc/4.. 01 = fosc /16
'10 = fosc/64.. 11 controlled by timer2.
SSPCON1.CKP = 1 'clock polarity unrem for high polarity
End Proc
E
Hi E,
I tried to implement your CODE snippet, but it was just doo difficult to add into my program.
I've ordered a some different compass modules [LSM303D], that are hopefully easier to implement. I already have the LSM303C type but they also have a problem. They need to have SPI 4 wire to match the rest of my modules.
Thanks,
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
hi C,
It is this one LSM303 D as per your post.
E
Hi E,
I made sure they supported 4-wire SPI, before ordering. I have to admit, I haven't been the most efficient parts order clerk:) Still, each time I have to read a new D/S, I learn something.

EDIT: I've been looking through 'your' D/S and see that the only reference to SPI is the setting you pointed to. I must have misslooked when I ordered them, I was sure they fully catered for SPI, bit I appear to be mistaken.
Perhaps, I am going to have to use both SPI and i2C, although I have been told, they have shared attributes, so back to the drawing board:(
C.
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
Hurray:)
I've just SEND and RECEIVED a BYTE to the GY-282 HMC5983 module, so now should be able to proceed to get some results.
Now when I read the D/S it looks obvious, whereas before it was a puzzle.
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
Here's the first results, showing a BYTE for each of the XZ and Y registers.

The D/S mentions '2’s complement form' As far as I understand this means that the 2x BYTEs need to have a calculation between them.
Can someone enlighten me please?
C
 

Attachments

Top