Pitx-sp software guide – Kontron pITX-SP User Manual
Page 38
KTD-S0003-C
Page 34
Tri-Axis Accelerometer Example
p
ITX-SP Software Guide
byte ReadReg (byte reg)
{
union REGS regs;
regs.h.ah = SMBUS_ENTRY;
regs.h.al = SMBUS_READ_BYTE;
regs.h.bh = DEVICE_ADDR;
regs.h.bl = reg;
int86 (INT_SERVICE, ®s, ®s);
return
regs.h.cl;
}
double TransformValue (word val, word *sg)
{
*sg = FALSE;
if (val & 0x8000)
{
*sg = TRUE;
val ^= 0xFFFF;
val &= 0x1FFF;
val
++;
}
return ((double) val / (double) 1024);
}
void main (void)
{
word
acc,
sign;
byte
acc_lo,
acc_hi;
double resX, resY, resZ, resAll;
int
ch;
char
str
[64];
clrscr
();
gotoxy
(1,1);
printf ("Accelerometer Test Program");
while
(TRUE)
{
WriteReg (PAGE_REG, PAGE0);
// Set page 0
WriteReg (OP_MODE_REG, START_MEASURE);
// Start measurement
delay
(1);
WriteReg (PAGE_REG, PAGE1);
// Set page 1
while
(TRUE)
{
if (! ReadReg (STATUS_REG))
// Read status
break;
}
acc_lo = ReadReg (XAXIS_REG);
// Read X value
acc_hi = ReadReg (XAXIS_REG+1);
acc = ((word) acc_hi * 256) + (word) acc_lo;
resX = TransformValue (acc, &sign);