2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

電子回路入門 3軸加速度センサー

Posted at

#はじめに
書籍に沿って引き続き進めていきます。
#3軸加速度センサー
X,Y,Zの3軸の加速度を計測することができるセンサーです。
マルチコプターには9軸センサをのせたりします。(加速度+ジャイロ+磁気)

加速度から傾きを求めることもできます。(計算すれば…今回はやめておきます)

#使用部品

  • PSoC 5LP
  • 抵抗(1k,2kΩ)
  • PCA9306 (J4,J5をはんだ付け)
  • LIS3DH (A,B,Cをはんだ付け)

#回路図
前回、加速度センサーはSPI通信がいいよーって言った気がしますが、I2Cで実行します。

LIS3DH_ブレッドボード.png

#プログラム
あまりキレイではないかも…いつか直そう。(;´・ω・)

仕様書にはスレーブアドレスが0x30,0x31と書いてありますが、一番下のbitはRead/Write用みたいなので、そこらへんはPSoCで勝手に後付するのかな?なので、右に1つシフトした(Read/Writeのbit無し)0x18を使用します。
Arduinoやラズベリーパイでも同様だったような気がします。

0x30, 0x31 を右に1つシフトすると、0x18
0x31(0b110001) → 0x18(0b11000)

#include "project.h"
#include <stdio.h>
#include <stdlib.h>

 
char imp[255];

 uint8 I2C_Write(uint8 SlaveAddress, uint8 *DataAddress, uint8 Byte_Count, uint8 I2C_Mode){
    uint8 temp;
    temp = I2C_1_MasterWriteBuf(SlaveAddress,DataAddress,Byte_Count,I2C_Mode);
    while (temp != I2C_1_MSTR_NO_ERROR);
    while(I2C_1_MasterStatus() & I2C_1_MSTAT_XFER_INP);
    temp = I2C_1_MasterClearStatus();
    return (temp);
}

 uint8 I2C_Read(uint8 SlaveAddress, uint8 *DataAddress, uint8 Byte_Count, uint8 I2C_Mode){
    uint8 temp;
    temp = I2C_1_MasterWriteBuf(SlaveAddress,DataAddress,1,I2C_Mode);
    while (temp != I2C_1_MSTR_NO_ERROR);
    while(I2C_1_MasterStatus() & I2C_1_MSTAT_XFER_INP);
    temp = I2C_1_MasterClearStatus();
   
    temp = I2C_1_MasterReadBuf(SlaveAddress,DataAddress,Byte_Count, I2C_Mode);
    while (temp != I2C_1_MSTR_NO_ERROR);
    while(I2C_1_MasterStatus() & I2C_1_MSTAT_XFER_INP);
    temp = I2C_1_MasterClearStatus();
    return (temp);
}

int main(void)
{
    CyGlobalIntEnable;
    
    UART_1_Start();
    I2C_1_Start();
    CyDelay(2000);
    uint8 wr_buff[6]={0,0,0,0,0,0};
    for(;;)
    {
        wr_buff[0] = 0x0F;
        I2C_Read(0x18,wr_buff,1,I2C_1_MODE_COMPLETE_XFER);
        if(wr_buff[0] == 0x33){break;}
    }
    wr_buff[0] = 0x20;
    wr_buff[1] = 0x77;
    I2C_Write(0x18,wr_buff,2,I2C_1_MODE_COMPLETE_XFER);
    I2C_Read(0x18,wr_buff,1,I2C_1_MODE_COMPLETE_XFER);
    UART_1_PutString("start \n");
    for(;;)
    {
        short x,y,z;
        uint8 wr_buffa[6]={0,0,0,0,0,0};
        wr_buff[0] = 0x28;
        I2C_Read(0x18,wr_buff,1,I2C_1_MODE_COMPLETE_XFER);
        wr_buffa[0] = 0x29;
        I2C_Read(0x18,wr_buffa,1,I2C_1_MODE_COMPLETE_XFER);
        int p = 0;
        if((wr_buffa[0] >> 7) == 1){p = 0xF;}else{p = 0;}
        x = (p << 12) | (wr_buffa[0] << 4) | (wr_buff[0] >> 4);
        wr_buff[0] = 0x2A;
        I2C_Read(0x18,wr_buff,1,I2C_1_MODE_COMPLETE_XFER);
        wr_buffa[0] = 0x2B;
        I2C_Read(0x18,wr_buffa,1,I2C_1_MODE_COMPLETE_XFER);
        if((wr_buffa[0] >> 7) == 1){p = 0xF;}else{p = 0;}
        y = (wr_buffa[0] << 4) | (wr_buff[0] >> 4);
        wr_buff[0] = 0x2C;
        I2C_Read(0x18,wr_buff,1,I2C_1_MODE_COMPLETE_XFER);
        wr_buffa[0] = 0x2D;
        I2C_Read(0x18,wr_buffa,1,I2C_1_MODE_COMPLETE_XFER);
        if((wr_buffa[0] >> 7) == 1){p = 0xF;}else{p = 0;}
        z = (wr_buffa[0] << 4) | (wr_buff[0] >> 4);
        sprintf(imp,"%hd,%hd,%hd\n",x,y,z);
        UART_1_PutString(imp);
        CyDelay(1000);
    }
}

#結果
結果.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?