STM32F4 discovery Accelerometer: Working code.

Limited support for STM32F4 Discovery, Nucleo and custom F4 boards
User avatar
mubase
Posts: 30
Joined: Wed Apr 29, 2015 5:10 pm
Contact:

STM32F4 discovery Accelerometer: Working code.

Postby mubase » Fri Jun 05, 2015 4:26 pm

STM32F4 discovery Accelerometer : Working code.

I've been working on getting the accelerometer (LIS203DL) working on the STM32F4 discovery board. Its actually quite simple. The code uses the HardwareSPI library for initialisation and then I've added a couple of functions to read and transfer data. Like 80% of my code, its hodgepodge but it works and the x y and z axes print to Serial port 2 (PA2 and PA3 -> RX & TX respectively) nicely. Its raw 16 bit data. signed. :)

Code: Select all

HardwareSPI acc(1);

#define CS 67

int x,y,xx,yy,z,zz;
int i;
byte ctrl[2]={0x20,0xc7};

int16 xg,yg,zg;
int16 x_array[100];
int16 y_array[100];
int16 z_array[100];
float x_avg,y_avg,z_avg;
float xyz_theta;

uint8 spiTransfer( uint8 data)
{

 SPI1_BASE->DR=data;
delay(10);
while(!(SPI1_BASE->SR & SPI_SR_RXNE));
return SPI1_BASE->DR;
}

uint8 spiRead(uint8 reg)
{
 uint8 in;
uint8 data = 0x80 | reg;
 gpio_write_bit(GPIOE,3,0);  // cs low
 delay(2);
spiTransfer(data);
delay(2);

in=spiTransfer(0xdead);
gpio_write_bit(GPIOE,3,1);  //cs high
delayMicroseconds(75);
 
return in ;
}

void setup() {
  // put your setup code here, to run once:
  Serial2.begin(9600);
  pinMode(CS,OUTPUT);
  gpio_set_af_mode(GPIOA,5,5);
gpio_set_af_mode(GPIOA,6,5);
gpio_set_af_mode(GPIOA,7,5);
acc.begin(SPI_1_125MHZ,MSBFIRST,SPI_MODE_3);
cs_low();
spiTransfer(0x20);
delay(1);
spiTransfer(0xc7);
cs_high();
x=spiRead(0x20);
delay(4);
Serial2.println(x,HEX);
delay(500);
}

void loop() {
  // put your main code here, to run repeatedly:
  cs_low();
 
x=spiTransfer(0x28 | 0x80);
//delayMicroseconds(500);
xx=spiTransfer((0x29 | 0x80));
//delayMicroseconds(500);

y=spiTransfer(0x2a | 0x80);
//delayMicroseconds(500);
yy=spiTransfer((0x2b | 0x80));
//delayMicroseconds(500);
x=spiTransfer(0x2c | 0x80);
//delayMicroseconds(500);
xx=spiTransfer((0x2d | 0x80));
//delayMicroseconds(500);
//xx=spiTransfer(0x28);
cs_high();
//for (i=0;i<100;i++)
// {
x=spiRead(0x28);
xx=spiRead(0x29);
y=spiRead(0x2a);
yy=spiRead(0x2b);
z=spiRead(0x2c);
zz=spiRead(0x2d);
xg=(x+(xx<<8));
yg=(y+(yy<<8));
zg=(z+(zz<<8));
//x_array[i]=xg;
//y_array[i]=yg;
//z_array[i]=zg;
// }
 Sort_Signed(x_array,100);
 Sort_Signed(y_array,100);
 Sort_Signed(z_array,100);
 x_avg=0;
  y_avg=0;
   z_avg=0;

Serial2.print("X: ");
Serial2.print(xg);

Serial2.print(" Y: ");
Serial2.print( yg);

Serial2.print(" Z: ");
Serial2.println(zg);
}

void cs_low()
{
 digitalWrite(CS,LOW);
delayMicroseconds(50);
}

void cs_high()
{
 digitalWrite(CS,HIGH);
delayMicroseconds(50);
}

// sorting of acc vals
void Sort_Signed(int16 A[], uint8 L)
{
  uint8 i = 0;
  uint8 status = 1;
 
  while(status == 1)
  {
    status = 0;
    for(i = 0; i < L-1; i++)
    {
      if (A[i] > A[i+1])
      {
        A[i]^=A[i+1];
        A[i+1]^=A[i];
        A[i]^=A[i+1];
        status = 1;   
      }
    }
  }
}


User avatar
RogerClark
Posts: 5461
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: STM32F4 discovery Accelerometer: Working code.

Postby RogerClark » Sat Jun 06, 2015 11:59 pm

Thanks Mubase

The more information we can amass about all these boards, the easier its going to be for everyone to use them.

piastri2
Posts: 6
Joined: Thu Oct 06, 2016 5:42 pm

Re: STM32F4 discovery Accelerometer: Working code.

Postby piastri2 » Mon Nov 07, 2016 11:18 pm

Hi, does anyone managed to get this sketch working with the new board that mounts the LIS3DSH instead of LIS203DL?
I just get some fixed value and don't know where to start with

edit: nevermind,i managed somehow

change your setup () into

Code: Select all

void setup() {
  // put your setup code here, to run once:
  Serial2.begin(4800);
  pinMode(CS,OUTPUT);
  gpio_set_af_mode(GPIOA,5,5);
gpio_set_af_mode(GPIOA,6,5);
gpio_set_af_mode(GPIOA,7,5);
acc.begin(SPI_1_125MHZ,MSBFIRST,SPI_MODE_3);
cs_low();
spiTransfer(0x23);
delay(1);
spiTransfer(0xc9);
cs_high();
delay (4);
cs_low();
spiTransfer(0x20);
delay(1);
spiTransfer(0x67);
cs_high();
delay(1);
x=spiRead(0x20);
delay(4);
Serial2.println(x,HEX);



delay(500);
}


what i did is just sending 0xc9 to the 0x23 register to reset the accelerometer and modified the byte(0x67) we sent to the 0x20 register

plus on my board serial2 is pd8-pd9.


Thanks mubase for your code


Return to “STM32F4 Boards”

Who is online

Users browsing this forum: No registered users and 1 guest