CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

No SPI data from MPL115A1 Pressure Sensor

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
keplerforever



Joined: 23 Dec 2005
Posts: 8

View user's profile Send private message

No SPI data from MPL115A1 Pressure Sensor
PostPosted: Sun Dec 26, 2010 12:29 pm     Reply with quote

Hi all,

Although a frequent reader of this forum, I try as much as I can to find myself answers to questions....but this time I'm really stuck.

My goal is to interface my pic 16f877A with the MPL115A1 Pressure Sensor sold by Sparkfun:
http://www.sparkfun.com/products/9721

SPI protocol is used, and I connected the pins as follows:

MPL115A1 SDN - +5V (always power on)
MPL115A1 CSN - PIN_C2
MPL115A1 SDO - PIN_C4 (twisted?)
MPL115A1 SDI - PIN_C5 (twisted?)
MPL115A1 SCK - PIN_C3
MPL115A1 GND - GND
MPL115A1 VDD - +5V

and wrote the following program based on the following example:
http://www.sparkfun.com/datasheets/BreakoutBoards/MPL115A1-ATmega328Code-8-9-10.zip
and on the application note from freesacle:
http://cache.freescale.com/files/sensors/doc/app_note/AN3785.pdf


Code:
#include <16f877A.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7)

#define CSN_SPI PIN_C2
int8 address, uiTadc, data ;

 
void main()
{
printf("Start\n\r");

setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64);
output_low(CSN_SPI);


while(1){
   data = 5; //just to be sure that the value is updated

   //Start temperature and pressure conversion
   address = 0x24;
   address &= 0x7F;
   
   output_low(CSN_SPI);
   delay_ms(1);
   spi_write(address);
   delay_ms(1);
   spi_write(0x00);
   delay_ms(1);
   output_high(CSN_SPI);
   delay_ms(2);
   
   
   // get MSB for Pressure
   address = 0x00;
   address |= 0x80;
   
   output_low(CSN_SPI);
   delay_ms(1);
   spi_write(address);
   delay_ms(1);
   data = spi_read(0x00);
   delay_ms(1);
   output_high(CSN_SPI);

   printf("%u\n\r", data);

   //uiTadc = (unsigned int) data << 8;
   //printf("%u\n\r", uiTadc);
      
   
   delay_ms(100);
  }

}


Currently the only value I'm able to see on my RS232 terminal...since 2...weeks is 0.
I've been checking on the scope the signals CSN, SDI and SCK, and they look OK: I can see clock and data transfer. I don't get any answers from SDO line, which explains the 0 on the terminal.

So far I tried the following:
- twisted DO and DI cable
- change the setup values of SPI: div_4, div_16, L_TO_H
- put delays everywhere in the code
- tried different ways for read and write data

I'm out of ideas, so if someone could give me some hints...I would be really thankful.

Regards

Olivier
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Dec 26, 2010 1:34 pm     Reply with quote

Can you post a graphic/screen shot showing us all the signals?

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 26, 2010 2:33 pm     Reply with quote

Quote:

void main()
{
printf("Start\n\r");

setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64);

output_low(CSN_SPI);

I didn't look too closely at your code but I noticed two things that
are wrong.

You are using SPI mode 2, but the AN3785 says the sensor uses Mode 0.
Figure 11 (on page 13) in the appnote shows that the SPI clock idles at a
low level and samples on the rising edge. This is mode 0. See this diagram:
http://www.totalphase.com/support/kb/10045/#modes

In addition to that, you are initializing the Chip Select line to a low level,
which is the active state. It should be initialized to the In-Active state
which is a high level.

It's much easier to set the correct SPI mode if you use these definitions.
Just add these lines above main() and edit your setup_spi() statement to
use the appropriate mode constant.
Code:

// SPI mode definitions.
#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)


Example of revised code:
Code:

// SPI mode definitions.
#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)


void main()
{
printf("Start\n\r");

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);

output_high(CSN_SPI);

delay_ms(100);

Also, I can't find a spec for it, but it's possible that the sensor requires
a small amount of time for internal initialization. I added a 100 ms
delay above, to allow for this.
keplerforever



Joined: 23 Dec 2005
Posts: 8

View user's profile Send private message

PostPosted: Sun Dec 26, 2010 4:54 pm     Reply with quote

Yeahhh!!
Wohohoho!!

You made it PCM, it rocks perfectly now.
Many thanks for your tremendous support.

Olivier
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group