View previous topic :: View next topic |
Author |
Message |
AlisonP
Joined: 13 Oct 2011 Posts: 4
|
SPI on the PIC24FJ256 |
Posted: Thu Oct 13, 2011 8:56 am |
|
|
I am trying to use the SPI bus on the PIC24FJ256. I need to use it to communicate with an ADC and a DAC on the same lines.
To setup the SPI up for the ADC I am using the following:
Code: | #define SPI_PIN_SCL PIN_G8 //o
#define SPI_PIN_SDI PIN_G6 //i
#define SPI_PIN_SDO PIN_G7 //o
#use spi(SPI1, MASTER, DI=SPI_PIN_SDI, DO=SPI_PIN_SDO, CLK=SPI_PIN_SCL, BITS=8, MSB_FIRST, SAMPLE_RISE, IDLE=0, BAUD=2000000, STREAM=SPI_DAC) |
However, the DAC needs SAMPLE_RISE. How can I change this on the fly?
Thanks, _________________ Ali |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Oct 13, 2011 9:43 am |
|
|
To change SPI formats on the fly use spi_setup(), spi_read() and spi_write() instead of #use spi. |
|
|
AlisonP
Joined: 13 Oct 2011 Posts: 4
|
|
Posted: Fri Oct 14, 2011 1:36 am |
|
|
Quote: | To change SPI formats on the fly use spi_setup(), spi_read() and spi_write() instead of #use spi. |
Thanks for the reply, but how do I select the correct pins for the SPI using the spi_setup() command? The PIC24 has mapable pins but the CCS documentation does not mention it.
Anyone know? _________________ Ali |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Oct 14, 2011 1:59 am |
|
|
PCD has the #pin_select statement for peripheral pin selection. You can expect that it works correctly in a recent version. |
|
|
AlisonP
Joined: 13 Oct 2011 Posts: 4
|
|
Posted: Fri Oct 14, 2011 2:30 am |
|
|
I have added my pin setups like this:
Code: | #pin_select SDO1=PIN_G7
#pin_select SDI1=PIN_G6
#pin_select SCK1OUT=PIN_G8 |
... and it is working, I can see SPI data and clock on the correct pins. So thanks for that.
I have tried to change the hi-low option by adding these lines at the top of functions that write / read to the ADC and DAC, but it still changes the data on the rising edge for both.
ADC
Code: | setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4); |
DAC
Code: | setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4); |
There are only 4 functions that talk to these devices so I am confident that I have added it to them all. Any more ideas?
Many thanks. _________________ Ali |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Oct 14, 2011 3:19 am |
|
|
Assuming you are using a recent PCD version >= 4.109, both setups refer to the same active clock edge.
In the CCS PIC24 syntax, SPI_XMIT_L_TO_H has to be added to change active edge. SPI_H_TO_L has been renamed SPI_SCK_IDLE_HIGH, review the processor *.h files.
I previously posted some comments about inconsistencies and silent changes of PCD SPI functionalilty and am somewhat tired of the topic. In case of doubt, you'll need to check which SPI mode bits are actually set by your code and possibly correct it.
As an additional point. You possibly need to set setup_spi(false) before applying the new mode setting. |
|
|
AlisonP
Joined: 13 Oct 2011 Posts: 4
|
|
Posted: Fri Oct 14, 2011 4:35 am |
|
|
Quote: | In the CCS PIC24 syntax, SPI_XMIT_L_TO_H has to be added to change active edge |
Thank you for this advice, it has fixed my problem. I was looking in the help for the correct parameter labels *silly me*. I find it quite annoying that they have changed the compiler but not in the help. I suppose the lesson for me is always look in the 24*.h file.
I spent the best part of yesterday trying to solve this. I was trying to write directly to the correct SFR bit to change the SPI edge and it would not accept it (I am an old school assembler programmer - hehe). If I didn't have my '#use spi' line in it worked but otherwise I wrote to the bit and nothing happened. :rollseyes: I had tried setup_spi first (I found the #pin_select's when I went back to an earlier version) but because I had the wrong parameter name I was doomed and I gave up on it.
Anyway, I understand you are bored with the topic if you have been talking about it for a while, so thank you very very much, your help is greatly appreciated. _________________ Ali |
|
|
|