اطلاعیه

Collapse
No announcement yet.

سورس کد شتابسنج ADXL345 با رابط I2C (تست شده با میکرو AVR)

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    سورس کد شتابسنج ADXL345 با رابط I2C (تست شده با میکرو AVR)

    [code=c]#ifndef _ADXL345_INCLUDED_
    #define _ADXL345_INCLUDED_
    /// Slave add+write=0xA6 , Slaveadd+Read=0xA7
    // I2C Bus functions
    #asm
    .equ __i2c_port=0x15 //PORTC
    .equ __sda_bit=4
    .equ __scl_bit=5
    #endasm
    #include <i2c.h>
    //////// Read ADXL345 on address add
    unsigned char read345reg(unsigned char add)
    {
    unsigned char regvalue=0;
    while(!i2c_start()) //wait here to free bus
    {#asm("wdr&quot}
    i2c_write(0xA6); //slave adress+write
    i2c_write(add); //set register address
    i2c_stop(); //restart (stoP followed by Start)
    i2c_start();
    i2c_write(0xA7); //slave adress+Read
    regvalue=i2c_read(0x00);
    i2c_stop();
    return(regvalue);
    }



    //////// Read ADXL345 data X=0x32L,0x33H Y=0x34L,0x35H Z=0x36L,0x37H
    int read345data(unsigned char add)
    {
    unsigned char low=0,high=0;
    int acc;

    low= read345reg(add);
    high=read345reg(add+1);
    acc=(int)( (high<<8) | low);

    return(acc);
    }


    ///////// if we can read ADXL345 DEVID=0xE5
    char is345ok(void)
    {
    if(read345reg(0x00)!=0xE5) // read DEVID register on address 0x00
    return (0);
    return (1);
    }

    /////// initilize ADXL345
    void init_345(void)
    {
    while(!i2c_start()) //wait here to free bus
    {#asm("wdr&quot}
    i2c_write(0xA6); //slave adress+write
    i2c_write(0x31); //DATA_FORMAT register
    i2c_write(0x0B); //±16g, 13-BIT MODE
    i2c_stop();
    delay_ms(5);

    while(!i2c_start()) //wait here to free bus
    {#asm("wdr&quot}
    i2c_write(0xA6); //slave adress+write
    i2c_write(0x2C); //BW_RATE register
    i2c_write(0x09); //output rate=50Hz(BW=25Hz)
    i2c_stop();
    delay_ms(5);

    while(!i2c_start()) //wait here to free bus
    {#asm("wdr&quot}
    i2c_write(0xA6); //slave adress+write
    i2c_write(0x2D); //POWER_CTL register
    i2c_write(0x08); //START MEASUREMENT
    i2c_stop();
    delay_ms(5);
    #asm("wdr"// Reset WDT
    }


    #endif[/code]
لطفا صبر کنید...
X