STM32F407discovery board'unun accelerometresini kullanamıyorum

Başlatan alper0017, 06 Aralık 2012, 23:44:24

LukeSkywalker

Yayınlarsanız kullanırım. STM32F4discovery kiti üzerindeki çipi okutamadım birtürlü.

fatih6761

Kütüphane için yeni konu açacaktım ama sanırsam yetkim yok. Bu yüzden kütüphaneyi burada yayınlıyorum.


MikroC for ARM derleyicisi STM32 MCU ile STM32F4-DISCOVERY kiti üzerinde bulunan LIS302DL ivme ölçeri kullanabilmeyi sağlayacak bir kütüphanedir.
Şu an için sadece Register bazında okuma-yazma yapılabilmekte. Zaten LIS302DL kolay bir çip. Temel fonksiyonları başlatacak şekilde bir ilklendirici (initializer) fonksiyonu içeriyor. Bu fonksiyonu örnek alarak kendi istediğiniz özelliklerle ivmeölçeri çalıştırabilirsiniz.
Bu kütüphanede Software SPI kullanılmıştır. Software SPI pin bağlantısı ana dosyada belirtilmelidir.
sbit Chip_Select at GPIOE_ODR.B3;
sbit SoftSpi_CLK at GPIOA_ODR.B5;
sbit SoftSpi_SDI at GPIOA_IDR.B6;
sbit SoftSpi_SDO at GPIOA_ODR.B7;

Chip_Select pini Software SPI modülü tarafından kullanılmamaktadır. Bu pin bağlantısını kütüphane kullanacaktır.
Bu kod kütüphane dahil edilmeden önce kullanılmalıdır. Yukarıdaki şekliyle bizim kitimizdeki ivmeölçerin bağlantılarına denk gelmektedir.
Sonrada include komutu ile kütüphane başlık dosyası dahil edilecek.
#include "lis302dl.h"

Aygıtın ilklendirici(initializer) fonksiyonu kullanılırken parametre olarak kullanılacak eksenleri girebilirsiniz:
lis302dl_init(LIS302DL_XYZ_ENABLE);

Sabitler ve tanımlamalar orijinal ST kütüphanesindeki isimleriyle kullanılmıştır. Zaten ST dökümantasyonu aynı haliyle başlık dosyasına aktarılmıştır.
Register'ların adresleri LIS302DL_"register adı"_ADDR olarak, sabitler ise işlevsel isimleriyle belirtilmiştir.
Her satırın açıklaması yanında verilmiştir.

lis302dl.c dosya içeriği:
/*  LIS302DL sürücü kütüphanesi  */
/*  Açık kaynak GNU GPLv3 lisans şartlarına tabidir.  */
/*  Yazar: Fatih YAZICI  */
/*  Sürüm: 0.0.1  */

/*  başlık dosyasını dahil et  */
#include "lis302dl.h"

/*  global değişkenler  */
LIS302DL_CommandTypeDef lis302dl_cmd;

/*  önerilmez, kullanımı nadir  */
void lis302dl_puts (unsigned char address, const char *s0) {
        const char *p = s0;
        lis302dl_cmd.RW = 0;        // yazma modu
        lis302dl_cmd.MS = 1;        // çok baytlı transfer
        lis302dl_cmd.AD = address;  // adres
        /*
         * Not : Çoklu veri transferi sırasında adresler otomatik arttırılır.
         *       Ayrılmış/rezerve adreslere dikkat ediniz...
         */
        
        Chip_Select = 0;
        
        Soft_SPI_Write(lis302dl_cmd.value); // komutu yolla

        do {
                Soft_SPI_Write(*p);       // bir bayt yolla
        } while(*(p++) != 0);             // *p=0 olana kadar devam et
        
        Chip_Select = 1;
}

// önerilmez
void lis302dl_gets(char * buff, unsigned int offset, unsigned int count) {
        char *p = buff;
        lis302dl_cmd.RW = 1;        // okuma modu
        lis302dl_cmd.MS = 1;        // çok baytlı veri aktarımı
        lis302dl_cmd.AD = offset;   // adres

        Chip_Select = 0;

        Soft_SPI_Write(lis302dl_cmd.value);   // komutu yolla

        while (count--) {
                *(p++) = Soft_SPI_Read(0);   // tek bayt oku
        }
        
        Chip_Select = 1;
}

int lis302dl_init (unsigned char axes) {
        unsigned char cmd;
        unsigned char who_am_i;
        
        Soft_SPI_Init();
        
        who_am_i = lis302dl_read(LIS302DL_WHO_AM_I_ADDR);

        // çipin doğruluğunu test et
        if(who_am_i != 0x3B)
            return -1;

        // 1. denetleme kayıtşısını ayarla
        cmd = LIS302DL_DATARATE_400   | LIS302DL_LOWPOWERMODE_ACTIVE |
              LIS302DL_FULLSCALE_9_2  | LIS302DL_SELFTEST_NORMAL |
              axes;

        lis302dl_write(LIS302DL_CTRL_REG1_ADDR, cmd);

        // 2. denetleme kayıtçısını ayarla
        cmd = LIS302DL_SERIALINTERFACE_4WIRE | LIS302DL_BOOT_REBOOTMEMORY;

        lis302dl_write(LIS302DL_CTRL_REG2_ADDR, cmd);

        // kesmeler, filtre ve tıklama olayları sonraki
        // sürümlerde eklenebilir.
        // biraz veri sayfası karıştırılarak bu olaylar ayrıca ayarlanabilir.

        return 0; //  ilklendirme başarılı
}

// X ekseni bilgisini al
unsigned char lis302dl_getX () {
        return lis302dl_read(LIS302DL_OUT_X_ADDR);
}

// Y ekseni bilgisini al
unsigned char lis302dl_getY () {
        return lis302dl_read(LIS302DL_OUT_Y_ADDR);
}

// Z ekseni bilgisini al
unsigned char lis302dl_getZ () {
        return lis302dl_read(LIS302DL_OUT_Z_ADDR);
}

// Tüm eksenleri oku ( önerilmez )
void lis302dl_getAxes(
        unsigned char *axis_x,
        unsigned char *axis_y,
        unsigned char *axis_z) {

        unsigned char buffer[5];
        lis302dl_gets(buffer, LIS302DL_OUT_X_ADDR, 6); // 6 bayt veri oku

        *axis_x = buffer[0];
        *axis_y = buffer[2];
        *axis_z = buffer[4];
}

// eksenlere göre ivme bilgisini alır.
// out parametresi en az 3 tane int32 nesnesine işaret etmelidir.
void lis302dl_getACC(unsigned int *out) {
        unsigned char buffer[5];
        unsigned char ctrl;
        
        ctrl = lis302dl_read(LIS302DL_CTRL_REG1_ADDR);
        lis302dl_gets(buffer, LIS302DL_OUT_X_ADDR, 5);
        
        switch (ctrl & LIS302DL_FULLSCALE_9_2) {
               case 0x00:
                    // FS biti 0 ise tam ölçek 2.3g olacak.
                    // buna göre hız hesaplanacak.
                    // x ekseninde hız
                    *(out++) = (unsigned int)(LIS302DL_SENSITIVITY_2_3G * (uint8_t)buffer[0]);
                    // y ekseninde hız
                    *(out++) = (unsigned int)(LIS302DL_SENSITIVITY_2_3G * (uint8_t)buffer[2]);
                    // z ekseninde hız
                    *out = (unsigned int)(LIS302DL_SENSITIVITY_2_3G * (uint8_t)buffer[4]);
                    break;
               case 0x20:
                    // FS biti 1 ise tam ölçek 9.2g olacak.
                    // buna göre hız hesaplanacak.
                    // x ekseninde hız
                    *(out++) = (unsigned int)(LIS302DL_SENSITIVITY_9_2G * (uint8_t)buffer[0]);
                    // y ekseninde hız
                    *(out++) = (unsigned int)(LIS302DL_SENSITIVITY_9_2G * (uint8_t)buffer[2]);
                    // z ekseninde hız
                    *out = (unsigned int)(LIS302DL_SENSITIVITY_9_2G * (uint8_t)buffer[4]);
                    break;
        }
}

// 1 byte veri yaz ( address [0x0F .. 0x3F] )
void lis302dl_write (unsigned char address,  unsigned char mems_data) {
        lis302dl_cmd.RW = 0;        // yazma modu
        lis302dl_cmd.MS = 0;        // tek bayt aktarımı
        lis302dl_cmd.AD = address;  // 6-bit adres

        Chip_Select = 0;

        Soft_SPI_Write(lis302dl_cmd.value); // komutu yolla
        Soft_SPI_Write(mems_data);    // veriyi yolla
        
        Chip_Select = 1;
}

// 1 byte veri oku ( address [0x0F .. 0x3F] )
unsigned char lis302dl_read (unsigned char address) {
        // voltile anahtar kelimesi derleyici iyileştirmelerini engeller
        volatile unsigned char result;  // okuma sonucu

        lis302dl_cmd.RW = 1;        // okuma modu
        lis302dl_cmd.MS = 0;        // tek bayt aktarımı
        lis302dl_cmd.AD = address;  // 6-bit adres

        Chip_Select = 0;

        Soft_SPI_Write(lis302dl_cmd.value);  // komutu yolla
        // sahte bayt olarak 0 yollanabilir, değeri önemli değil
        // burada TX tamponuna parametre olarak alınan değer
        // yerleştirilir. SPI protokolü gereği MISO okunurken MOSI ye
        // veri yazılmalıdır. Shift Register Principal
        result = Soft_SPI_Read(0);     // 8-saat darbesi işle, MISO hattını oku
        
        Chip_Select = 1;

        return result;
}


lis302dl.h dosya içeriği:
/*  LIS302DL sürücü kütüphanesi başlık dosyası  */
/*  Açık kaynak GNU GPLv3 lisans şartlarına tabidir.  */
/*  Yazar: Fatih YAZICI  */
/*  Sürüm: 0.0.1  */

#ifndef __LIS302DL_H
#define __LIS302DL_H

typedef unsigned char uint8_t;        // ST kütüphanesi uyumluluğu için
// bunun yerine <stdint.h> kütüphanesi dahil edilebilirdi ama
// pratik olması açısından bu kod yeterlidir.

// Discovery kiti için CS pini
extern sfr sbit Chip_Select;
// bu kısımda CS pininin bulundupu port ve 
// bu pine ait pin mask belirlenmelidir
#define LIS302DL_ChipSelect_BASE  &GPIOE_BASE
#define LIS302DL_ChipSelect_MASK  _GPIO_PINMASK_3
// Pin atama sonu

/*  LIS302DL için bir baytlık komut yapısı
 *  dikkat: veri yazpısı big-endian'a göre düzenlenmiş olmalı
 *  SPI aktarımı MSB-first olarak belirlenmiş ( ST tarafondan )
 *  bu yüzden ilk gönderilecek RW biti en sona yazılacak  */
typedef union {
      unsigned char value;
      struct {
             /* 6-bit adres */
             unsigned char AD : 6;
             /*  Tek veya çok bayt aktarımı (0:tek, 1:çok)  */
             unsigned char MS : 1;
             /*  Oku/Yaz veri akış yönü (0:yaz, 1:oku)  */
             unsigned char RW : 1;
      };
} LIS302DL_CommandTypeDef;

/*  public define
 *  Bu kısım ST kütüphanesinden alınmıştır.
 *  Böylece kütüphaneler arası uyum ve isimlendirmede iyileştirme
 * yapılmıştır.
 * STMicroelectronics'in belirlediği lisans şartlarına tâbidir.
 * (karma lisans)  */

/******************************************************************************/
/*************************** START REGISTER MAPPING  **************************/
/******************************************************************************/

/*******************************************************************************
*  WHO_AM_I Register: Device Identification Register
*  Read only register
*  Default value: 0x3B
*******************************************************************************/
#define LIS302DL_WHO_AM_I_ADDR                  0x0F

/*******************************************************************************
*  CTRL_REG1 Register: Control Register 1
*  Read Write register
*  Default value: 0x07
*  7 DR: Data Rate selection.
*        0 - 100 Hz output data rate
*        1 - 400 Hz output data rate
*  6 PD: Power Down control.
*        0 - power down mode
*        1 - active mode
*  5 FS: Full Scale selection.
*        0 - Typical measurement range 2.3
*        1 - Typical measurement range 9.2
*  4:3 STP-STM Self Test Enable:
*              STP |  STM |   mode
*            ----------------------------
*               0  |  0   |   Normal mode
*               0  |  1   |   Self Test M
*               1  |  0   |   Self Test P
*  2 Zen: Z axis enable.
*         0 - Z axis disabled
*         1- Z axis enabled
*  1 Yen: Y axis enable.
*         0 - Y axis disabled
*         1- Y axis enabled
*  0 Xen: X axis enable.
*         0 - X axis disabled
*         1- X axis enabled
********************************************************************************/
#define LIS302DL_CTRL_REG1_ADDR                 0x20

/*******************************************************************************
*  CTRL_REG2 Regsiter: Control Register 2
*  Read Write register
*  Default value: 0x00
*  7 SIM: SPI Serial Interface Mode Selection.
*         0 - 4 wire interface
*         1 - 3 wire interface
*  6 BOOT: Reboot memory content
*          0 - normal mode
*          1 - reboot memory content
*  5 Reserved
*  4 FDS: Filtered data selection.
*         0 - internal filter bypassed
*         1 - data from internal filter sent to output register
*  3 HP FF_WU2: High pass filter enabled for FreeFall/WakeUp#2.
*               0 - filter bypassed
*               1 - filter enabled
*  2 HP FF_WU1: High pass filter enabled for FreeFall/WakeUp#1.
*               0 - filter bypassed
*               1 - filter enabled
*  1:0 HP coeff2-HP coeff1 High pass filter cut-off frequency (ft) configuration.
*                 ft= ODR[hz]/6*HP coeff
*            HP coeff2 | HP coeff1 |   HP coeff
*            -------------------------------------------
*                 0     |     0     |   8
*                 0     |     1     |   16
*                 1     |     0     |   32
*                 1     |     1     |   64
*            HP coeff |  ft[hz]   |  ft[hz]   |
*                     |ODR 100Hz | ODR 400Hz  |
*            --------------------------------------------
*              00     |    2      |     8     |
*              01     |    1      |     4     |
*              10     |    0.5    |     2     |
*              11     |    0.25   |     1     |
*******************************************************************************/
#define LIS302DL_CTRL_REG2_ADDR              0x21

/*******************************************************************************
*  CTRL_REG3 Register: Interrupt Control Register
*  Read Write register
*  Default value: 0x00
*  7 IHL active: Interrupt active high/low.
*                0 - active high
*                1 - active low
*  6 PP_OD: push-pull/open-drain.
*           0 - push-pull
*           1 - open-drain
*  5:3 I2_CFG2 - I2_CFG0 Data signal on INT2 pad control bits
*  2:0 I1_CFG2 - I1_CFG0 Data signal on INT1 pad control bits
*        I1(2)_CFG2  |  I1(2)_CFG1  |  I1(2)_CFG0  | INT1(2) Pad
*        ----------------------------------------------------------
*              0     |      0       |       0      | GND
*              0     |      0       |       1      | FreeFall/WakeUp#1
*              0     |      1       |       0      | FreeFall/WakeUp#2
*              0     |      1       |       1      | FreeFall/WakeUp#1 or FreeFall/WakeUp#2
*              1     |      0       |       0      | Data ready
*              1     |      1       |       1      | Click interrupt
*******************************************************************************/
#define LIS302DL_CTRL_REG3_ADDR              0x22

/*******************************************************************************
*  HP_FILTER_RESET Register: Dummy register. Reading at this address zeroes
*  instantaneously the content of the internal high pass filter. If the high pass
*  filter is enabled all three axes are instantaneously set to 0g.
*  This allows to overcome the settling time of the high pass filter.
*  Read only register
*  Default value: Dummy
*******************************************************************************/
#define LIS302DL_HP_FILTER_RESET_REG_ADDR     0x23

/*******************************************************************************
*  STATUS_REG Register: Status Register
*  Default value: 0x00
*  7 ZYXOR: X, Y and Z axis data overrun.
*           0: no overrun has occurred
*           1: new data has overwritten the previous one before it was read
*  6 ZOR: Z axis data overrun.
*         0: no overrun has occurred
*         1: new data for Z-axis has overwritten the previous one before it was read
*  5 yOR: y axis data overrun.
*         0: no overrun has occurred
*         1: new data for y-axis has overwritten the previous one before it was read
*  4 XOR: X axis data overrun.
*         0: no overrun has occurred
*         1: new data for X-axis has overwritten the previous one before it was read
*  3 ZYXDA: X, Y and Z axis new data available
*           0: a new set of data is not yet available
*           1: a new set of data is available
*  2 ZDA: Z axis new data available.
*         0: a new set of data is not yet available
*         1: a new data for Z axis is available
*  1 YDA: Y axis new data available
*         0: a new set of data is not yet available
*         1: a new data for Y axis is available
*  0 XDA: X axis new data available
*         0: a new set of data is not yet available
*         1: a new data for X axis is available
*******************************************************************************/
#define LIS302DL_STATUS_REG_ADDR             0x27

/*******************************************************************************
*  OUT_X Register: X-axis output Data
*  Read only register
*  Default value: output
*  7:0 XD7-XD0: X-axis output Data
*******************************************************************************/
#define LIS302DL_OUT_X_ADDR                  0x29

/*******************************************************************************
*  OUT_Y Register: Y-axis output Data
*  Read only register
*  Default value: output
*  7:0 YD7-YD0: Y-axis output Data
*******************************************************************************/
#define LIS302DL_OUT_Y_ADDR                  0x2B

/*******************************************************************************
*  OUT_Z Register: Z-axis output Data
*  Read only register
*  Default value: output
*  7:0 ZD7-ZD0: Z-axis output Data
*******************************************************************************/
#define LIS302DL_OUT_Z_ADDR                  0x2D

/*******************************************************************************
*  FF_WW_CFG_1 Register: Configuration register for Interrupt 1 source.
*  Read write register
*  Default value: 0x00
*  7 AOI: AND/OR combination of Interrupt events.
*         0: OR combination of interrupt events
*         1: AND combination of interrupt events
*  6 LIR: Latch/not latch interrupt request
*         0: interrupt request not latched
*         1: interrupt request latched
*  5 ZHIE: Enable interrupt generation on Z high event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value higher than preset threshold
*  4 ZLIE: Enable interrupt generation on Z low event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value lower than preset threshold
*  3 YHIE: Enable interrupt generation on Y high event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value higher than preset threshold
*  2 YLIE: Enable interrupt generation on Y low event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value lower than preset threshold
*  1 XHIE: Enable interrupt generation on X high event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value higher than preset threshold
*  0 XLIE: Enable interrupt generation on X low event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value lower than preset threshold
*******************************************************************************/
#define LIS302DL_FF_WU_CFG1_REG_ADDR         0x30

/*******************************************************************************
*  FF_WU_SRC_1 Register: Interrupt 1 source register.
*  Reading at this address clears FF_WU_SRC_1 register and the FF, WU 1 interrupt
*  and allow the refreshment of data in the FF_WU_SRC_1 register if the latched option
*  was chosen.
*  Read only register
*  Default value: 0x00
*  7 Reserved
*  6 IA: Interrupt active.
*        0: no interrupt has been generated
*        1: one or more interrupts have been generated
*  5 ZH: Z high.
*        0: no interrupt
*        1: ZH event has occurred
*  4 ZL: Z low.
*        0: no interrupt
*        1: ZL event has occurred
*  3 YH: Y high.
*        0: no interrupt
*        1: YH event has occurred
*  2 YL: Y low.
*        0: no interrupt
*        1: YL event has occurred
*  1 YH: X high.
*        0: no interrupt
*        1: XH event has occurred
*  0 YL: X low.
*        0: no interrupt
*        1: XL event has occurred
*******************************************************************************/
#define LIS302DL_FF_WU_SRC1_REG_ADDR           0x31

/*******************************************************************************
*  FF_WU_THS_1 Register: Threshold register
*  Read Write register
*  Default value: 0x00
*  7 DCRM: Reset mode selection.
*          0 - counter resetted
*          1 - counter decremented
*  6 THS6-THS0: Free-fall/wake-up threshold value.
*******************************************************************************/
#define LIS302DL_FF_WU_THS1_REG_ADDR          0x32

/*******************************************************************************
*  FF_WU_DURATION_1 Register: duration Register
*  Read Write register
*  Default value: 0x00
*  7:0 D7-D0 Duration value. (Duration steps and maximum values depend on the ODR chosen)
 ******************************************************************************/
#define LIS302DL_FF_WU_DURATION1_REG_ADDR     0x33

/*******************************************************************************
*  FF_WW_CFG_2 Register: Configuration register for Interrupt 2 source.
*  Read write register
*  Default value: 0x00
*  7 AOI: AND/OR combination of Interrupt events.
*         0: OR combination of interrupt events
*         1: AND combination of interrupt events
*  6 LIR: Latch/not latch interrupt request
*         0: interrupt request not latched
*         1: interrupt request latched
*  5 ZHIE: Enable interrupt generation on Z high event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value higher than preset threshold
*  4 ZLIE: Enable interrupt generation on Z low event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value lower than preset threshold
*  3 YHIE: Enable interrupt generation on Y high event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value higher than preset threshold
*  2 YLIE: Enable interrupt generation on Y low event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value lower than preset threshold
*  1 XHIE: Enable interrupt generation on X high event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value higher than preset threshold
*  0 XLIE: Enable interrupt generation on X low event.
*          0: disable interrupt request
*          1: enable interrupt request on measured accel. value lower than preset threshold
*******************************************************************************/
#define LIS302DL_FF_WU_CFG2_REG_ADDR         0x34

/*******************************************************************************
*  FF_WU_SRC_2 Register: Interrupt 2 source register.
*  Reading at this address clears FF_WU_SRC_2 register and the FF, WU 2 interrupt
*  and allow the refreshment of data in the FF_WU_SRC_2 register if the latched option
*  was chosen.
*  Read only register
*  Default value: 0x00
*  7 Reserved
*  6 IA: Interrupt active.
*        0: no interrupt has been generated
*        1: one or more interrupts have been generated
*  5 ZH: Z high.
*        0: no interrupt
*        1: ZH event has occurred
*  4 ZL: Z low.
*        0: no interrupt
*        1: ZL event has occurred
*  3 YH: Y high.
*        0: no interrupt
*        1: YH event has occurred
*  2 YL: Y low.
*        0: no interrupt
*        1: YL event has occurred
*  1 YH: X high.
*        0: no interrupt
*        1: XH event has occurred
*  0 YL: X low.
*        0: no interrupt
*        1: XL event has occurred
*******************************************************************************/
#define LIS302DL_FF_WU_SRC2_REG_ADDR           0x35

/*******************************************************************************
*  FF_WU_THS_2 Register: Threshold register
*  Read Write register
*  Default value: 0x00
*  7 DCRM: Reset mode selection.
*          0 - counter resetted
*          1 - counter decremented
*  6 THS6-THS0: Free-fall/wake-up threshold value.
*******************************************************************************/
#define LIS302DL_FF_WU_THS2_REG_ADDR          0x36

/*******************************************************************************
*  FF_WU_DURATION_2 Register: duration Register
*  Read Write register
*  Default value: 0x00
*  7:0 D7-D0 Duration value. (Duration steps and maximum values depend on the ODR chosen)
 ******************************************************************************/
#define LIS302DL_FF_WU_DURATION2_REG_ADDR     0x37

/******************************************************************************
*  CLICK_CFG Register: click Register
*  Read Write register
*  Default value: 0x00
*  7 Reserved
*  6 LIR: Latch Interrupt request.
*         0: interrupt request not latched
*         1: interrupt request latched
*  5 Double_Z: Enable interrupt generation on double click event on Z axis.
*              0: disable interrupt request
*              1: enable interrupt request
*  4 Single_Z: Enable interrupt generation on single click event on Z axis.
*              0: disable interrupt request
*              1: enable interrupt request
*  3 Double_Y: Enable interrupt generation on double click event on Y axis.
*              0: disable interrupt request
*              1: enable interrupt request
*  2 Single_Y: Enable interrupt generation on single click event on Y axis.
*              0: disable interrupt request
*              1: enable interrupt request
*  1 Double_X: Enable interrupt generation on double click event on X axis.
*              0: disable interrupt request
*              1: enable interrupt request
*  0 Single_y: Enable interrupt generation on single click event on X axis.
*              0: disable interrupt request
*              1: enable interrupt request
 ******************************************************************************/
#define LIS302DL_CLICK_CFG_REG_ADDR     0x38

/******************************************************************************
*  CLICK_SRC Register: click status Register
*  Read only register
*  Default value: 0x00
*  7 Reserved
*  6 IA: Interrupt active.
*        0: no interrupt has been generated
*        1: one or more interrupts have been generated
*  5 Double_Z: Double click on Z axis event.
*        0: no interrupt
*        1: Double Z event has occurred
*  4 Single_Z: Z low.
*        0: no interrupt
*        1: Single Z event has occurred
*  3 Double_Y: Y high.
*        0: no interrupt
*        1: Double Y event has occurred
*  2 Single_Y: Y low.
*        0: no interrupt
*        1: Single Y event has occurred
*  1 Double_X: X high.
*        0: no interrupt
*        1: Double X event has occurred
*  0 Single_X: X low.
*        0: no interrupt
*        1: Single X event has occurred
*******************************************************************************/
#define LIS302DL_CLICK_SRC_REG_ADDR        0x39

/*******************************************************************************
*  CLICK_THSY_X Register: Click threshold Y and X register
*  Read Write register
*  Default value: 0x00
*  7:4 THSy3-THSy0: Click threshold on Y axis, step 0.5g
*  3:0 THSx3-THSx0: Click threshold on X axis, step 0.5g
*******************************************************************************/
#define LIS302DL_CLICK_THSY_X_REG_ADDR        0x3B

/*******************************************************************************
*  CLICK_THSZ Register: Click threshold Z register
*  Read Write register
*  Default value: 0x00
*  7:4 Reserved
*  3:0 THSz3-THSz0: Click threshold on Z axis, step 0.5g
*******************************************************************************/
#define LIS302DL_CLICK_THSZ_REG_ADDR         0x3C

/*******************************************************************************
*  CLICK_TimeLimit Register: Time Limit register
*  Read Write register
*  Default value: 0x00
*  7:0 Dur7-Dur0: Time Limit value, step 0.5g
*******************************************************************************/
#define LIS302DL_CLICK_TIMELIMIT_REG_ADDR        0x3D

/*******************************************************************************
*  CLICK_Latency Register: Latency register
*  Read Write register
*  Default value: 0x00
*  7:0 Lat7-Lat0: Latency value, step 1msec
*******************************************************************************/
#define LIS302DL_CLICK_LATENCY_REG_ADDR        0x3E

/*******************************************************************************
*  CLICK_Window Register: Window register
*  Read Write register
*  Default value: 0x00
*  7:0 Win7-Win0: Window value, step 1msec
*******************************************************************************/
#define LIS302DL_CLICK_WINDOW_REG_ADDR        0x3F

/******************************************************************************/
/**************************** END REGISTER MAPPING  ***************************/
/******************************************************************************/

#define LIS302DL_SENSITIVITY_2_3G                         18  /* 18 mg/digit*/
#define LIS302DL_SENSITIVITY_9_2G                         72  /* 72 mg/digit*/

/** @defgroup Data_Rate_selection
  * @{
  */
#define LIS302DL_DATARATE_100                             ((uint8_t)0x00)
#define LIS302DL_DATARATE_400                             ((uint8_t)0x80)
/**
  * @}
  */

/** @defgroup Power_Mode_selection
  * @{
  */
#define LIS302DL_LOWPOWERMODE_POWERDOWN                   ((uint8_t)0x00)
#define LIS302DL_LOWPOWERMODE_ACTIVE                      ((uint8_t)0x40)
/**
  * @}
  */

/** @defgroup Full_Scale_selection
  * @{
  */
#define LIS302DL_FULLSCALE_2_3                            ((uint8_t)0x00)
#define LIS302DL_FULLSCALE_9_2                            ((uint8_t)0x20)
/**
  * @}
  */

/** @defgroup Self_Test_selection
  * @{
  */
#define LIS302DL_SELFTEST_NORMAL                          ((uint8_t)0x00)
#define LIS302DL_SELFTEST_P                               ((uint8_t)0x10)
#define LIS302DL_SELFTEST_M                               ((uint8_t)0x08)
/**
  * @}
  */

/** @defgroup Direction_XYZ_selection
  * @{
  */
#define LIS302DL_X_ENABLE                                 ((uint8_t)0x01)
#define LIS302DL_Y_ENABLE                                 ((uint8_t)0x02)
#define LIS302DL_Z_ENABLE                                 ((uint8_t)0x04)
#define LIS302DL_XYZ_ENABLE                               ((uint8_t)0x07)
/**
  * @}
  */

 /** @defgroup SPI_Serial_Interface_Mode_selection
  * @{
  */
#define LIS302DL_SERIALINTERFACE_4WIRE                    ((uint8_t)0x00)
#define LIS302DL_SERIALINTERFACE_3WIRE                    ((uint8_t)0x80)
/**
  * @}
  */

 /** @defgroup Boot_Mode_selection
  * @{
  */
#define LIS302DL_BOOT_NORMALMODE                          ((uint8_t)0x00)
#define LIS302DL_BOOT_REBOOTMEMORY                        ((uint8_t)0x40)
/**
  * @}
  */

 /** @defgroup Filtered_Data_Selection_Mode_selection
  * @{
  */
#define LIS302DL_FILTEREDDATASELECTION_BYPASSED           ((uint8_t)0x00)
#define LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER     ((uint8_t)0x20)
/**
  * @}
  */

 /** @defgroup High_Pass_Filter_Interrupt_selection
  * @{
  */
#define LIS302DL_HIGHPASSFILTERINTERRUPT_OFF              ((uint8_t)0x00)
#define LIS302DL_HIGHPASSFILTERINTERRUPT_1                ((uint8_t)0x04)
#define LIS302DL_HIGHPASSFILTERINTERRUPT_2                ((uint8_t)0x08)
#define LIS302DL_HIGHPASSFILTERINTERRUPT_1_2              ((uint8_t)0x0C)
/**
  * @}
  */

 /** @defgroup High_Pass_Filter_selection
  * @{
  */
#define LIS302DL_HIGHPASSFILTER_LEVEL_0                   ((uint8_t)0x00)
#define LIS302DL_HIGHPASSFILTER_LEVEL_1                   ((uint8_t)0x01)
#define LIS302DL_HIGHPASSFILTER_LEVEL_2                   ((uint8_t)0x02)
#define LIS302DL_HIGHPASSFILTER_LEVEL_3                   ((uint8_t)0x03)
/**
  * @}
  */


/** @defgroup latch_Interrupt_Request_selection
  * @{
  */
#define LIS302DL_INTERRUPTREQUEST_NOTLATCHED              ((uint8_t)0x00)
#define LIS302DL_INTERRUPTREQUEST_LATCHED                 ((uint8_t)0x40)
/**
  * @}
  */

/** @defgroup Click_Interrupt_XYZ_selection
  * @{
  */
#define LIS302DL_CLICKINTERRUPT_XYZ_DISABLE               ((uint8_t)0x00)
#define LIS302DL_CLICKINTERRUPT_X_ENABLE                  ((uint8_t)0x01)
#define LIS302DL_CLICKINTERRUPT_Y_ENABLE                  ((uint8_t)0x04)
#define LIS302DL_CLICKINTERRUPT_Z_ENABLE                  ((uint8_t)0x10)
#define LIS302DL_CLICKINTERRUPT_XYZ_ENABLE                ((uint8_t)0x15)
/**
  * @}
  */

/** @defgroup Double_Click_Interrupt_XYZ_selection
  * @{
  */
#define LIS302DL_DOUBLECLICKINTERRUPT_XYZ_DISABLE         ((uint8_t)0x00)
#define LIS302DL_DOUBLECLICKINTERRUPT_X_ENABLE            ((uint8_t)0x02)
#define LIS302DL_DOUBLECLICKINTERRUPT_Y_ENABLE            ((uint8_t)0x08)
#define LIS302DL_DOUBLECLICKINTERRUPT_Z_ENABLE            ((uint8_t)0x20)
#define LIS302DL_DOUBLECLICKINTERRUPT_XYZ_ENABLE          ((uint8_t)0x2A)

/******************************************************************************/
/*************************** END REGISTER MAPPING  ****************************/
/******************************************************************************/

/*  public functions  */
int lis302dl_init(unsigned char axes);

void lis302dl_write (unsigned char address, unsigned char mems_data);
unsigned char lis302dl_read (unsigned char address);

void lis302dl_puts(unsigned char address, const char *s0);
void lis302dl_gets(char * buff, unsigned int offset, unsigned int count);

unsigned char lis302dl_getX (void);
unsigned char lis302dl_getY (void);
unsigned char lis302dl_getZ (void);

void lis302dl_getAxes(
        unsigned char *axis_x,
        unsigned char *axis_y,
        unsigned char *axis_z);
        
void lis302dl_getACC(unsigned int *out);

#endif


Örnek kullanım:
// Module connections
sbit Chip_Select at GPIOE_ODR.B3;
sbit SoftSpi_CLK at GPIOA_ODR.B5;
sbit SoftSpi_SDI at GPIOA_IDR.B6;
sbit SoftSpi_SDO at GPIOA_ODR.B7;
// End module connections

#include "lis302dl.h"

unsigned int x, y, z;

void main() {
        GPIO_Digital_Output(&GPIOD_BASE,
                _GPIO_PINMASK_12 | _GPIO_PINMASK_13 | 
                _GPIO_PINMASK_14 | _GPIO_PINMASK_15);

        lis302dl_init(LIS302DL_XYZ_ENABLE);

        while(1) {
                x = lis302dl_getX();
                y = lis302dl_getY();

                if (x > 130) {
                        GPIOD_ODR.B13 = 1;
                        GPIOD_ODR.B15 = 0;
                } else {
                        GPIOD_ODR.B13 = 0;
                        GPIOD_ODR.B15 = 1;
                }

                if (y > 130) {
                        GPIOD_ODR.B12 = 1;
                        GPIOD_ODR.B14 = 0;
                } else {
                        GPIOD_ODR.B12 = 0;
                        GPIOD_ODR.B14 = 1;
                }

                Delay_Ms(30);
        }
}

Buraya dosya ekleyemiyorum ( en başta belirttiğim sorun ) o yüzden kodları GitHub'a yükledim:
https://github.com/yazici67/mikroc_lis302dl

LukeSkywalker

Hocam teşekkürler çalışıyor gayet güzel bir şekilde.
Peki neden soft_spi kullandınız? Özel bir sebebi var mı?
Doğrudan SPI modülünü neden kullanmadınız işlemcinin?

fatih6761

Donanımsal SPI modülü olduğunda belirli pinlere bağlı kalıyorsunuz. Elinde bizim kitten bulunmayan ve başka STM32 işlemci ile kütüphaneden faydalanmak isteyen arkadaşlar olursa onlara kolaylık olsun diye yazılımdal SPI kullandım. Donanımsal SPI için değiştirilecek yerler belli aslına. Tüm fonksiyonlarda Soft_SPI_Init, Soft_SPI_Write, Soft_SPI_Read yerine SPI1_Init, SPI1_Write, SPI1_Read kullanılacak. Bizim kartta ivmeölçer çipi doğrudan SPI1 pinlerine bağlı. Donanımsal SPI de dikkat edilmesi gereken MSB First olacak. SPI1_Init_Advanced kullanılarak bu ayarlanabilir örneğin:
SPI1_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER | _SPI_8_BIT |
                     _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                     _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                     &_GPIO_MODULE_SPI1_PA567);

Bundan sonra donanımsal SPI kullanılabilir.
Not : initial release üstüne daha yarım gün geçmeden güncelleme eklendi :) . lis302dl_getX,Y,Z fonksiyonlarını ben unsigned tanımlamıştım, meğer çip signed çalışıyormuş. Buna göre kodları düzelttim ayrıca daha güzel bir deneme uygulaması koydum. Yeni kodları kullanarak daha doğru sonuca ulaşırsınız.
Edit : son dakika gelişmesi : eksenlere göre hızı okuma fonksiyonu da eklendi.

LukeSkywalker

:S
Ben de aslında okutuyormuşum acclerometerı fakat
_SPI_CLK_IDLE_LOW değil _SPI_CLK_IDLE_HIGH yapıyordum o yüzden olmuyormuş.
LIS302'nin datasheetinde clock boştayken high gösteriyor. Sebebi nedir?

LukeSkywalker


alper0017

Keil ile de Mikroc ile de okuyabiliyorum.Kütüphanelerini yazdım.Elimde mevcut isteyenlerle paylaşabilirim.