Welcome! Log In Create A New Profile

Advanced

SPI mode

Posted by GaryJScott 
SPI mode
April 29, 2015 08:24PM
I've just started experimenting with the BP1 and currently developing my understanding of the SPI interface and trying to connect a thermocouple module (readily available on ebay) which uses a MAX6675. I now believe it uses mode 1 but the BPI uses mode 0 communication so my question is can the mode that the BP1 uses be changed?
Re: SPI mode
April 30, 2015 08:05AM
The PIC32 does not have a single register bit(s) that will control the 'mode' as this register can also supply audio and so is more complex than a normal SPI set up. I can't tell you what the set up is that would be needed for the particular mode you need however I will show an example of changing the output from 8 bit communication to 16 bit communication by witting to the registers directly. In this way you can determine what you need from the data sheet and then set the registers accordingly

The SPI hardware registers can be directly accessed, bit length mode is set in the control register, the register addresses are here:
http://www.bypic.co.uk/index.php/MX1_Full and the relevant SPI registers are:
constant SPI1CON         0xBF805800
constant SPI1CONCLR      0xBF805804
constant SPI1CONSET      0xBF805808

According to the data sheet http://www.byvac.com/downloads/datasheets/PIC32MX1XX_2XX%20Family%20Data%20Sheet_61168D.pdf section 16, bits 10 and 11 control the bit length mode so switching to mode 16 should be:

@SPI1CONCLR = 0xc00 // clear the bits8 bit mode
@SPI1CONSET = 0x400 // set 16 bit mode

In this way any bit of any register can be changed and as it is that which controls the hardware then the hardware can be made to behave as you want it. It may also be worth looking at the Rookie code in the SPI section (http://byvac.com/mBlib/flb/Library/rookie/MX170_rookie3_part1.bas

Hope that helps, when you do get it going I can put the code on the web site for others with the same device if you like.
Re: SPI mode
April 30, 2015 06:41PM
Thank's. A quick check on the data sheet and I can see the clock edge (bit 8) and polarity (bit 6), so I think I just need to configure these to set the mode. I will take a look at the rookie code to make sure I've not missed anything else and post back how I get on.
Re: SPI mode
May 08, 2015 09:34PM
So it was in part an assumption that the default mode the BP1 used was 0, yup that was wrong. The rookie code is clear and (long story short) I can see register (SPI1CON) is initialised in the spi_init(speed) function with 0x8120 which among other things sets mode 1 (bit6 bit8). The issue I had was more to do with the algorithm needed to get the 2 byte reads into a temperature value which I've now fixed.

You could consider adding a variable to (for example) spi_init(mode,speed) and set bit6 bit8 in SPI1CON accordingly, but this would just cause a compatibility issue. It may though help just to add some further information in the comments. e.g. SPI default mode 1, set bit6 bit8 to subsequently change this similar to your example for changing 8/16 bit mode.

Thanks for you good guidance, this is a great development platform & device.

If anyone is interested I now have two functions, one to initialise the hardware and another to return an integer value of the read temperature

Regards
Sorry, only registered users may post in this forum.

Click here to login