//STK8312 Pseudo Code
static unsigned char AccSlaveAddr = 0x3D; //7-bit address
void Init_I2C_Setting()
{
unsigned char RegAddr, RegWriteValue;
//---------------------
//Software Reset
//---------------------
RegAddr = 0x20;
RegWriteValue = 0x00;
WriteAccRegister(AccSlaveAddr, RegAddr, RegWriteValue);
//---------------------
//Set to standby mode
//---------------------
//0x07—MODE/Features: 11000000
//D7 D6 D5 D4 D3 D2 D1 D0
//IAH IPP Reserved Reserved Reserved Reserved Reserved Mode
//
RegAddr = 0x07;
RegWriteValue = 0xC0; //Interrupt active high, push-pull, standby-mode
WriteAccRegister(AccSlaveAddr, RegAddr, RegWriteValue);
//---------------
//Set Measure Range, Resolution
//---------------
//0x13: Dynamic Range Setup and Shake Threshold Register (Read/Write)
//D7 D6 D5 D4 D3 D2 D1 D0
//RNG[1] RNG[0] - - - STH[2] STH[1] STH[0]
// *** RNG[1:0] MEASUREMENT RANGE Resolution ***
// 00 ±1.5 g 6
// 01 ±6 g 8
// 10 ±16 g 8
// 11 RESERVED
RegAddr = 0x13;
RegWriteValue = 0x42; //6g, 8bit
WriteAccRegister(AccSlaveAddr, RegAddr, RegWriteValue);
//---------------
//Output Data Rate Settings :
//---------------
//0x08: Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
//D7 D6 D5 D4 D3 D2 D1 D0
//FILT[2] FILT[1] FILT[0] AWSR[1] AWSR[0] AMSR[2] AMSR[1] AMSR[0]
RegAddr = 0x08;
RegWriteValue = 0x03; //ODR=50Hz
WriteAccRegister(AccSlaveAddr, RegAddr, RegWriteValue);
//---------------
//Set Mode as interrupt and measurement operation mode
//---------------
//0x06: Interrupt Setup Register (Read/Write)
//D7 D6 D5 D4 D3 D2 D1 D0
//SHINTX SHINTY SHINTZ GINT ASINT TINT PLINT FBINT
RegAddr = 0x06; //set Register Address
RegWriteValue = 0x10; //New Data Interrupt enabled
WriteAccRegister(AccSlaveAddr, RegAddr, RegWriteValue);
}
void Set_Enable(int en)
{
unsigned char RegAddr, RegWriteValue;
unsigned char readvalue = 0;
int count = 10; //time out setting
if(en)
{
//---------------
//Set to Active mode
//---------------
//0x07: Mode Register (Read/Write)
//D7 D6 D5 D4 D3 D2 D1 D0
//IAH IPP Reserved Reserved Reserved Reserved Reserved Mode
//
RegAddr = 0x07; //set Register Address
RegWriteValue = 0xC1; //INT# active-high, push-pull, Active-Mode
WriteAccRegister(AccSlaveAddr, RegAddr, RegWriteValue);
mSleep(1);
//---------------
//Engineering Setting
//---------------
WriteAccRegister(AccSlaveAddr, 0x3D, 0x70);
WriteAccRegister(AccSlaveAddr, 0x3D, 0x70);
WriteAccRegister(AccSlaveAddr, 0x3F, 0x02);
mSleep(1);
while((readvalue>>7)!=1 && count!=0)
{
mSleep(5);
ReadAccRegister(AccSlaveAddr, 0x3F, &readvalue); // Read Engineering Register 0x3F
--count;
}
ReadAccRegister(AccSlaveAddr, 0x3E, &readvalue);
if(readvalue != 0x00)
WriteAccRegister(AccSlaveAddr, 0x24, readvalue);
mSleep(300); //Waiting for data ready
}
else
{
//---------------------
//Set to standby mode
//---------------------
//0x07—MODE/Features: 11000000
//D7 D6 D5 D4 D3 D2 D1 D0
//IAH IPP Reserved Reserved Reserved Reserved Reserved Mode
//
RegAddr = 0x07;
RegWriteValue = 0xC0; //Interrupt active high, push-pull, standby-mode
WriteAccRegister(AccSlaveAddr, RegAddr, RegWriteValue);
}
}
void Polling_xyz_data(int data_out[]) //X = data_out[0], Y = data_out[1], Z = data_out[2]
{
unsigned char buffer[3];
unsigned int DataOutRegAddr = 0x00;
MCU_I2CDataOutRead(AccSlaveAddr, DataOutRegAddr, buffer, 3);
//XYZ Raw data = -128 ~ +127
if (buffer[0] & 0x80)
data_out[0] = buffer[0] - 256;
else
data_out[0] = buffer[0];
if (buffer[1] & 0x80)
data_out[1] = buffer[1] - 256;
else
data_out[1] = buffer[1];
if (buffer[2] & 0x80)
data_out[2] = buffer[2] - 256;
else
data_out[2] = buffer[2];
}
void main()
{
int data_out[3];
Init_I2C_Setting(); //Initialized registers
Set_Enable(1); //Set to Active mode
while(1)
{
Polling_xyz_data(data_out); //Polling data
printk("X = %d, Y = %d, Z = %d\n", data_out[0], data_out[1], data_out[2]);
mSleep(20); //ODR=50Hz
}
}