ST sensor LIS3LV02DL

OP #1292728
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);
  }

}
OP
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
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
OP
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
Attached files:
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
Attached files:
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
OP
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
Guest
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.

Reply

A question of your own? This thread is over 17 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.