ST sensor LIS3LV02DL

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

}
#1294273
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
#1300245
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
Attached files:
Guest #1406459
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.

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now