View previous topic :: View next topic |
Author |
Message |
priesty100
Joined: 16 Oct 2007 Posts: 6 Location: UK
|
Help with SPI |
Posted: Mon Mar 02, 2009 10:21 am |
|
|
Hi, I'm trying to communicate over SPI from an 18F8722 to an ADIS16209 (an inclinometer from Analog) but am not getting anywhere so any help would be most appreciated, thank you.
I'm simply trying to see if I can get a connection working by reading one of the registers from the Analog chip which returns the supply voltage. The code is as follows:
Code: |
#define ADIS_RST PIN_A3 //define the reset for the ADIS16209 chip
#define SUPPLY_OUT 0x02 //define the address of the supply voltage register
void main()
{
int data = 0;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
// ( Master |Polarity=1| Phase=1 |600KHz (aprox))
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_XMIT_L_TO_H|SPI_CLK_DIV_64);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
output_high(ADIS_RST);
while(true)
{
spi_write(SUPPLY_OUT);
data = spi_read(0);
printf("Supply = %i\r\n", data);
delay_ms(1000);
}
} |
I can see on a scope that the address gets sent correctly but no more clocks are sent for the spi_read function.
I've not used SPI before so this may be something obvious but any help you could give would be great. Thank you.
Regards
Sam _________________ Sam
Last edited by priesty100 on Mon Mar 02, 2009 10:48 am; edited 1 time in total |
|
|
priesty100
Joined: 16 Oct 2007 Posts: 6 Location: UK
|
Help with SPI |
Posted: Mon Mar 02, 2009 10:28 am |
|
|
I should say that the CS pin on the ADIS16209 is pulled low in hardware. _________________ Sam |
|
|
Ttelmah Guest
|
|
Posted: Mon Mar 02, 2009 11:06 am |
|
|
Start by not pulling the CS low in hardware. Connect it to a PIC pin, raise it when the PIC wakes up, and then lower it before trying to talk to the chip, and rise it afterwards.
SPI, without using CS, is _very_ unlikely to work. Problem is that depending on the sequence that the two chips actually 'start', and the levels that appear on the pins during wakeup, the slave device can easily see a spurious clock. Once this has happened, you will never get back into sync. It is possible to do it, (depending on the responses generated by the slave), by manually clocking the SPI clock line, till a particular bit is seen on the slave, and then starting the hardware SPI, but it is major work. The CS is there to be used to synchronise the data packets, and without it, you are making it very unlikely that the connection will work...
Though you connect to the reset, this is really unecessary. The unit takes at lest 30mSec to go 'ready' after you release the reset (probably a lot more, since power on reset takes up to 190mSec, and this will not have completed), so it is not ready on the first read, and then, the chip also requires _16bits_ to be read once you start a read access on a register. Note at the top of page 11 of the data sheet. The top two bits reflect system status information. You are only reading 8bits, so from this point the chips are out of sync,and without CS, can never recover...
Best Wishes |
|
|
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
|
Posted: Mon Mar 02, 2009 11:32 am |
|
|
Priest100
take a little look at this function:
Code: |
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_XMIT_L_TO_H|SPI_CLK_DIV_64);
|
in some devices SPI_XMIT_L_TO_H could not work, and check from ADIS16209 datasheet if it worry about clock active HIGH TO LOW or LOW TO HIGH it could cause some problems to... I suggest you to try
Code: |
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_64);
|
regards.
André _________________ Andre |
|
|
Guest
|
|
Posted: Mon Mar 02, 2009 5:11 pm |
|
|
Yo do need a CS line and I also noticed that your data variable is for 8bits. But the value you are reading is a 14 bit number so you need to change it to int16. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 02, 2009 5:41 pm |
|
|
Look at the SPI timing diagram on page 5 of the data sheet:
http://www.analog.com/static/imported-files/data_sheets/ADIS16209.pdf
The timing diagram shows that SCLK idles high and samples Din on the
rising edge. This is SPI mode 3. See the following web page:
http://www.totalphase.com/support/kb/10045/#modes
To implement this, add the following statements above main().
Code: | #define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) |
Then put SPI_MODE_3 in your setup_spi() statement.
The SCLK frequency may be limited to only 1 MHz maximum, depending
on the mode of the ADIS16209 chip. You did not post your PIC's clock
speed. Let's assume it's over 16 MHz. Then you need to use this divisor
in your setup_spi() statement:
Also note from the ADSI16209 data sheet that there is a Startup Time
of 190 ms. Don't attempt to read data from the chip before that time
has passed. Put a delay_ms(200) statement at the start of main(). |
|
|
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
|
Posted: Mon Mar 02, 2009 5:44 pm |
|
|
I dont think so Guest. _________________ Andre |
|
|
Guest
|
|
Posted: Tue Mar 03, 2009 2:22 am |
|
|
Have you remembered the required 10K pull-ups on both SDA and SCL?
P.S. is that you Sam?
Gerry |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 03, 2009 3:11 am |
|
|
Anonymous wrote: | Have you remembered the required 10K pull-ups on both SDA and SCL?
P.S. is that you Sam?
Gerry |
Not on _SPI_.
Pull ups are needed for I2C, which is a 'open collector' bi-directional bus, but SPI does not need these.
Best Wishes |
|
|
libellej8
Joined: 03 Mar 2009 Posts: 2 Location: Dorset UK
|
|
Posted: Tue Mar 03, 2009 7:18 am |
|
|
Ttelmah wrote: | Anonymous wrote: | Have you remembered the required 10K pull-ups on both SDA and SCL?
P.S. is that you Sam?
Gerry |
Not on _SPI_.
Pull ups are needed for I2C, which is a 'open collector' bi-directional bus, but SPI does not need these.
Best Wishes |
Thanks Ttelmah,
I stand corrected.
Sam,
I have some code that I used for an AD rate gyro, ADIS16255 same family, that works. I can't publish it here as it is proprietary, but you have my e-mail address.
Gerry |
|
|
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
|
Posted: Tue Mar 03, 2009 8:14 am |
|
|
Gerry, I would be glad if you send it to me too =)
thanks..
regards. _________________ Andre |
|
|
priesty100
Joined: 16 Oct 2007 Posts: 6 Location: UK
|
|
Posted: Tue Mar 03, 2009 5:28 pm |
|
|
Thank you all for your help,
I hadn't realised about the CS being required to sync the devices. Not sure how I managed to miss that I was only reading in 8bits.
Hey Gerry, yeah, its me, see what happens when I'm removed from your watchful eye, I've been trying to get that to work all day!
Thank you all again.
Regards _________________ Sam |
|
|
mazen82
Joined: 10 Sep 2009 Posts: 22
|
|
Posted: Mon Oct 26, 2009 7:08 am |
|
|
where you able to get it to work? can you post the code? |
|
|
|