您的位置:首页 > 运维架构 > Linux

[Linux项目实践] 物联网单板测试之任务六:Accelerometer重力加速计

2012-04-05 21:36 423 查看
完成单板代码(部分IIC基代码共用之前博文)

/*任务6:Accelerometer重力加速计*/



Axis3_Test();

/*******************************************************************************

* Function Name : Axis3_Test

* Description : Accelerometer Test.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

void Axis3_Test(void)

{

char buf[24];

int32_t xoff = 0;

int32_t yoff = 0;

int32_t zoff = 0;

int8_t x = 0;

int8_t y = 0;

int8_t z = 0;

OLED_ClearScreen();

OLED_DisStrLine(0, 0, "Axis-3");

printf("\r\nAxis-3\r\n");



I2CInit(I2CMASTER, 0);

acc_init();

/* Assume base board in zero-g position when reading first value. */

acc_read(&x, &y, &z);

xoff = 0-x;

yoff = 0-y;

zoff = 0-z;

while(1)

{

/* Accelerometer */

acc_read(&x, &y, &z);

x = x+xoff;

y = y+yoff;

z = z+zoff;

snprintf(buf, 20, "Acc x: %d ", x);

OLED_DisStrLine(2, 0, (uint8_t *)buf);

printf("\r\nAcc x: %d, ", x);

snprintf(buf, 20, "Acc y: %d ", y);

OLED_DisStrLine(3, 0, (uint8_t *)buf);

printf("Acc y: %d, ", y);

snprintf(buf, 20, "Acc z: %d ", z);

OLED_DisStrLine(4, 0, (uint8_t *)buf);

printf("Acc z: %d. ", z);

delay_ms(250);

if(KEY_Read() == KEY_ESC)

break;

}

}

1、I2CInit(I2CMASTER, 0);

/*****************************************************************************

** Function name: I2CInit

**

** Descriptions: Initialize I2C controller

**

** parameters: I2c mode is either MASTER or SL***E

** Returned value: true or false, return false if the I2C

** interrupt handler was not installed correctly

**

*****************************************************************************/

uint32_t I2CInit( uint32_t I2cMode, uint32_t slaveAddr )

{

/* It seems to be bit0 is for I2C, different from

UM. To be retested along with SSP reset. SSP and I2C

reset are overlapped, a known bug, for now, both SSP

and I2C use bit 0 for reset enable. Once the problem

is fixed, change to "#if 1". */

#if 1

LPC_SYSCON->PRESETCTRL |= (0x1<<1);

#else

LPC_SYSCON->PRESETCTRL |= (0x1<<0);

#endif

LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5);

LPC_IOCON->PIO0_4 &= ~0x3F; /* I2C I/O config */

LPC_IOCON->PIO0_4 |= 0x01; /* I2C SCL */

LPC_IOCON->PIO0_5 &= ~0x3F;

LPC_IOCON->PIO0_5 |= 0x01; /* I2C SDA */

/*--- Clear flags ---*/

LPC_I2C->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;

/*--- Reset registers ---*/

#if FAST_MODE_PLUS

LPC_IOCON->PIO0_4 |= (0x1<<9);

LPC_IOCON->PIO0_5 |= (0x1<<9);

LPC_I2C->SCLL = I2SCLL_HS_SCLL;

LPC_I2C->SCLH = I2SCLH_HS_SCLH;

#else

LPC_I2C->SCLL = I2SCLL_SCLL;

LPC_I2C->SCLH = I2SCLH_SCLH;

#endif

if ( I2cMode == I2CSL***E )

{

LPC_I2C->ADR0 = slaveAddr;

}

/* Enable the I2C Interrupt */

NVIC_EnableIRQ(I2C_IRQn);

LPC_I2C->CONSET = I2CONSET_I2EN;

return( TRUE );

}

2、acc_init();

void acc_init (void)

{

/* set to measurement mode by default */

setModeControl( (ACC_MCTL_MODE(ACC_MODE_MEASURE) | ACC_MCTL_GLVL(ACC_RANGE_2G) ));

}

2.1、setModeControl()

static void setModeControl(uint8_t mctl)

{

uint8_t buf[2];

buf[0] = ACC_ADDR_MCTL;

buf[1] = mctl;

I2CWrite(ACC_I2C_ADDR, buf, 2);

}

2.1.1、ACC_ADDR_MCTL

#define ACC_ADDR_MCTL 0x16

2.1.2、ACC_I2C_ADDR

#define ACC_I2C_ADDR (0x1D << 1)

2.1.3、I2CWrite()

void I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len)

{

I2CAddr = addr;

I2CMasterBuffer = buf;

I2CWriteLength = len;

I2CReadLength = 0;

I2CEngine();

I2CWriteLength = I2CWriteLength;

}

2.2、ACC_MCTL_MODE()

#define ACC_MCTL_MODE(m) ((m) << 0)

2.3、ACC_MCTL_GLVL()

#define ACC_MCTL_GLVL(g) ((g) << 2)

3、acc_read(&x, &y, &z);

/******************************************************************************

*

* Description:

* Read accelerometer data

*

* Params:

* [out] x - read x value

* [out] y - read y value

* [out] z - read z value

*

*****************************************************************************/

void acc_read (int8_t *x, int8_t *y, int8_t *z)

{

uint8_t buf[1];

/* wait for ready flag */

while ((getStatus() & ACC_STATUS_DRDY) == 0);

/*

* Have experienced problems reading all registers

* at once. Change to reading them one-by-one.

*/

buf[0] = ACC_ADDR_XOUT8;

I2CWrite(ACC_I2C_ADDR, buf, 1);

I2CRead(ACC_I2C_ADDR, buf, 1);

*x = (int8_t)buf[0];

buf[0] = ACC_ADDR_YOUT8;

I2CWrite(ACC_I2C_ADDR, buf, 1);

I2CRead(ACC_I2C_ADDR, buf, 1);

*y = (int8_t)buf[0];

buf[0] = ACC_ADDR_ZOUT8;

I2CWrite(ACC_I2C_ADDR, buf, 1);

I2CRead(ACC_I2C_ADDR, buf, 1);

*z = (int8_t)buf[0];

}

3.1、getStatus()

static uint8_t getStatus(void)

{

uint8_t buf[1];

buf[0] = ACC_ADDR_STATUS;

I2CWrite(ACC_I2C_ADDR, buf, 1);

I2CRead(ACC_I2C_ADDR, buf, 1);

return buf[0];

}

3.2、I2CWrite()

3.3、I2CRead()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: