EmbDev.net

Forum: µC & Digital Electronics ST sensor LIS3LV02DL


von Liang M. (palm_treo)


Rate this post
useful
not useful
Hi

I have been working with the sensor LIS3LV02DL form ST company over 1 
month, right now the sensor does not give me any response, when i am 
hitting the board, the sensor give the same signal, and using the uart 
to see the signal , it is not readable.

The MCU is atmega 32.

Using the SPI mode.

i make the connection between SCL(sensor) and SCK(MCU), SDI(sensor) and 
MOSI(MCU), SDO(sensor) and MISO(MCU),CS to the ground.

can anybody help me about this problem?

my email is: lm198699@msn.com

The code is here:

#define F_CPU 4000000UL

#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>

#define BAUD 9600
#define MYUBRR (F_CPU/16/BAUD-1)

void writeReg(unsigned char regName, unsigned char regValue);


void spiInit(void)
{
  DDRB |= (1<<PB7); //sck
  DDRB |= (1<<PB5); //mosi
  DDRB |= (1<<PB4); //cs

  PORTB |= (1<<PB6);

  SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0) | (1 << CPHA) | (1 << 
CPOL);//|(1<<DORD);
}

unsigned char spiTrans(unsigned char cData)
{
  PORTB &= ~(1<<PB5);
  SPDR = cData;
  while(!(SPSR & (1<<SPIF)));
     //_delay_ms(1);
  PORTB |= (1<<PB5);
  return SPDR;
}

unsigned char spiRece(void)
{
   //Wait for reception complete
  while(!(SPSR & (1<<SPIF)));
  //Return data register
  return SPDR;
}




void accInit(void)
{
  writeReg(0x20, 0xD4); //power on the device, and enable z axis
  writeReg(0x21, 0x05); //2g, DRDY, 16 bit left justifile
  writeReg(0x27, 0x04); //z are available
  //writeReg(0x22, 0x00);
  //writeReg(0x38, 0x00);
  }

void writeReg(unsigned char regName, unsigned char regValue)
{
  regName &= 0x7F;
  spiTrans(regName);
  spiTrans(regValue);
}

unsigned char readReg(unsigned char regName)
{
  regName |= 0x80; //  SPDR =regName + 0x80
  spiTrans(regName);
  return spiTrans(0x00);
}

void USART_Init()
{
  //set the baud rate
  //unsigned int baud = MYUBRR;
  UBRRH = (F_CPU/BAUD/16-1)/256;
  UBRRL = (F_CPU/BAUD/16-1)%256;
  UCSRC = (1<<URSEL) | (3<<UCSZ0);  //set frame format: 8 bit, 1 stop 
bit
  UCSRB = (1<<TXEN) | (1<<RXEN) | (1<<RXCIE);  //enable receiver and 
transmitter
  sei();

}

void USART_Transmit(unsigned char data)
{
  while(!(UCSRA & (1<<UDRE)));    //wait for empty transmit buffer;

  UDR = data;
}

void USART_Transmit_String( char *string ){
  while (string){
       USART_Transmit(*string);
        string++;
    }
}

unsigned char USART_Receive(void)
{
  while(!(UCSRA & (1<<RXC)));     //wait for data to be received

  return UDR;
}

/*
void USART_FLUSH(void)
{
  unsigned char dummy;
  while(UCSRA & (1<<RXC))
  dummy = UDR;
}
*/

int main(void)
{

  USART_Init();

  spiInit();


  accInit();
  //writeReg(0x20,128);


  while(1)
  {
    USART_Transmit(readReg(0x2B)); //readReg(0x0F)
    _delay_ms(50);
      //USART_Transmit('Z');
    //USART_Transmit(readReg(0x2C));
    //_delay_ms(10);
  }

}

von Michael K. (mmike)


Rate this post
useful
not useful
Dear Liang,

I'm also using the sensor but with TWI instead of SPI (works fine so 
far). If you want I could provide the source code.
Here's link to a source code that also uses SPI. Haven't testet it, but 
might be worth a try ...

http://www.mmi.rwth-aachen.de/wiki/mmi-mediawiki/images/8/85/Programmierung-Beschl-Sens.zip

greets,

Michael

von liang meng (Guest)


Rate this post
useful
not useful
OK, thanks

von Liang M. (palm_treo)


Rate this post
useful
not useful
Dear Michael,

what is the baud rate?

Thans

Liang Meng

von Michael K. (mmike)


Attached files:

Rate this post
useful
not useful
Dear Liang,

the baudrate should be defined by your init function (either SPI or 
TWI). For TWI <= 400kHz should be fine. SPI: nothing in the datasheet. 
Attached you'll find my TWI demo.

Greets,
Michael

Btw: Have you tested the code from rwth?

von Liang M. (palm_treo)


Rate this post
useful
not useful
Dear Michael

In my programming the baund rate is set as 9600.

I have test your code with SPI, it is not worked, so i want to know 
which

MCU you are using?

Because i am using the Atmega 32.

Liang Meng

von Michael K. (mmike)


Rate this post
useful
not useful
Dear Liang,

the baudrate of 9600 should comply with the serial (uart) connection and 
does not affect the SPI or TWI communication.

My processor is an AtMega16. So no difference concerning the code.

Please describe detailed your current problems and post your code again.

Greets,
Michael

von Liang M. (palm_treo)


Attached files:

Rate this post
useful
not useful
Dear Michael

My problem is the sensor is not working.

I connect the sensor to the atmage32, rs232 to the atmage32, when i 
runing

the programming, the sensor does not response any hitting, the 
hyperterminal

shows me the unreadable signal,and not changed.

Attached you will find my code

von Michael K. (mmike)


Rate this post
useful
not useful
Dear Liang,

I'll have a look at your code this week. Instead of using Hypertermin 
you can try to use HTerm (http://www.der-hammer.info/terminal/). This 
awesome software tool allows you to have a look at data from the serial 
port in either ASC, HEX, OCT and BIN !!!
Is the serial connection working? E.g. writing your name ?

Greets,
Michael

von Michael K. (mmike)


Rate this post
useful
not useful
Dear Liang,

I'll have a look at your code this week. Instead of using Hyperterminal 
you can try to use HTerm (http://www.der-hammer.info/terminal/). This 
awesome software tool allows you to have a look at data from the serial 
port in either ASC, HEX, DEC and BIN !!!
Is the serial connection working? E.g. writing your name ?

Greets,
Michael

von Michael K. (mmike)


Attached files:

Rate this post
useful
not useful
Dear Liang,

attached you'll find a changed version, which I adapted from my I2C code 
and the code from rwth. I also included my uart function of which I am 
sure that they are working. The code does nothing but enabling the LIS 
and query its WhoAmI register. This is done in the main loop. It should 
return 0x3A continously ...

First try using the hterm and if the string after µC startup is received 
correctly by your terminal. If not check your oszillator and the fuses! 
Get uart running first! After that check if 3A is returned. If so, then 
proceed and try to read the x, y, z values.

good luck ...

Greets,
Michael

von Liang M. (palm_treo)


Rate this post
useful
not useful
Dear Michael

Thanks a lot, but right now i do not have any sensor to test, they are

broken already. I am waiting for the sensor.

I will let you know the result.

Thanks

Liang Meng

von Liang M. (palm_treo)


Rate this post
useful
not useful
Dear Michael

I have test today, it does not transmit the 0x3A.

Can you give me you I2C code and tell me in the harware part, how you
connected?

Thanks

von Michael K. (mmike)


Rate this post
useful
not useful
Sure ... have a look at the fifth post ;-)

Greets,
Michael

von Michael K. (mmike)


Rate this post
useful
not useful
Connections:

CS  = VCC (I2C operation)
VCC = VCC
GND = GND
SDA = SDA
SCL = SCL

not very surprising ... Use short cables and enable the pullups for SDA 
and SCL ... otherwise use external pullups (4.7k)

regards,
Michael

von Liang M. (palm_treo)


Rate this post
useful
not useful
Dear Michael

The I2C is working, but the SPI is not.

Thanks

LM

von Michael K. (mmike)


Rate this post
useful
not useful
Your welcome ...

If your not forced to use SPI then use I2C ...

Greets,

Michael

von Liang M. (palm_treo)


Rate this post
useful
not useful
Hello, Michael

Right now i have four sensor working with the I2C on atmel32, i do not 
know how to set the interrupt, make 4 sensors sending the signal one by 
one.

Do you have some ideas about it?

Thanks

von Michael K. (mmike)


Rate this post
useful
not useful
No ideas to sync them ... sorry ...

Greets,
Michael

von Arnaud Taffanel (Guest)


Rate this post
useful
not useful
Dear Liang,

Your write that you force the CS pin at GND for the SPI mode, this can 
be the problem. This CS pin must be pulled to GND before each transfert 
and pushed to VCC after. By doing so the sensor can know which byte is 
actually the address (the byte just after the CS activation ...).

Your write function should so look like that:

void writeReg(unsigned char regName, unsigned char regValue)
{
  /* Here force CS at '0' */
  regName &= 0x7F;
  spiTrans(regName);
  spiTrans(regValue);
  /* Here force CS at '1' */
}

the same type of modification has to be applied in the read function.

I'm using this method with a STM32 and it works fine (I have a SPI clock 
around 200KHz).

Regards,
Arnaud.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.