anilvadnala
New Member
Dear Frends,
I am doing a project on accelerometer to find out motion acceleration so that I have to write a C program to PIC18f452.
But acctually I have a programme regarding my project in python language
which I donot understand but I know it is quite related to C.
I am requesting you give any suggesions or help. I am writing the programme in python....
#!/bin/env python
#==========================================================================
#
#
#==========================================================================
# IMPORTS
#==========================================================================
import sys
import time
from aardvark_py import *
#==========================================================================
# CONSTANTS
#==========================================================================
port = 0
bitrate = 100
# Device
DEVICE = 0x18
# Addresses
XOUT_H = 0x00
XOUT_L = 0x01
YOUT_H = 0x02
YOUT_L = 0x03
ZOUT_H = 0x04
ZOUT_L = 0x05
FF_INT = 0x06
FF_DELAY = 0x07
MOT_INT = 0x08
MOT_DELAY = 0x09
CTRL_REGC = 0x0A
CTRL_REGB = 0x0B
CTRL_REGA = 0x0C
# CTRL_REGA
Parity = 0x04
MOTI = 0x02
FFI = 0x01
# CTRL_REGB
CLKhld = 0x80
nEnable = 0x40
ST = 0x20
MOTDen = 0x10
FFDen = 0x08
MOTIen = 0x04
FFIen = 0x02
FFMOTI = 0x01
#==========================================================================
# MAIN PROGRAM
#==========================================================================
# Open the device
handle = aa_open(port)
if (handle <= 0):
print "Unable to open Aardvark device on port %d" % port
print "Error code = %d" % handle
sys.exit()
# User Configuration
Sensitivity = (5.0/(2**12)) # g/count
SampleRate = 250.0 # Hz
sys.stdout.write("Freefall Threshold (g): ")
FF_Threshold = float(sys.stdin.readline())
FF_Threshold = round(FF_Threshold / (16 * Sensitivity))
print "FF_INT byte: 0x%x" % FF_Threshold
print "Freefall Threshold set to %0.3fg" % (16 * FF_Threshold * Sensitivity)
sys.stdout.write("Freefall Interrupt Delay (ms): ")
FF_Delay = float(sys.stdin.readline())/1000
FF_Delay = int(round(FF_Delay * SampleRate))
if FF_Delay < 0x01
FF_Delay = 0x01
if FF_Delay > 0xFF:
FF_Delay = 0xFF
print "FF_DELAY byte: 0x%x" % FF_Delay
print "Freefall Interrupt Delay set to %dms" % ((FF_Delay / SampleRate) * 1000)
sys.stdout.write("Motion Threshold (g): ")
MOT_Threshold = float(sys.stdin.readline())
MOT_Threshold = round(MOT_Threshold / (16 * Sensitivity))
print "MOT_INT byte: 0x%x" % MOT_Threshold
print "Motion Threshold set to %0.3fg" % (16 * MOT_Threshold * Sensitivity)
sys.stdout.write("Motion Interrupt Delay (ms): ")
MOT_Delay = float(sys.stdin.readline())/1000
MOT_Delay = int(round(MOT_Delay * SampleRate))
if MOT_Delay < 0x01:
MOT_Delay = 0x01
if MOT_Delay > 0xFF:
MOT_Delay = 0xFF
print "MOT_DELAY byte: 0x%x" % MOT_Delay
print "Motion Interrupt Delay set to %dms" % ((MOT_Delay / SampleRate) * 1000)
# Ensure that the I2C subsystem is enabled
aa_configure(handle, AA_CONFIG_SPI_I2C)
# Power the EEPROM using the Aardvark adapter's power supply.
# This command is only effective on v2.0 hardware or greater.
# The power pins on the v1.02 hardware are not enabled by default.
aa_target_power(handle, AA_TARGET_POWER_BOTH)
# Activate the pull-up resistor(s)
aa_i2c_pullup(handle,AA_I2C_PULLUP_BOTH);
# Setup the clock phase
#aa_spi_configure(handle, mode >> 1, mode & 1, AA_SPI_BITORDER_MSB)
# Set the bitrate
bitrate = aa_i2c_bitrate(handle, bitrate)
print "Bitrate set to %d kHz" % bitrate
# Configure the device
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGC, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
aa_i2c_write(handle,DEVICE,0,array('B',[FF_DELAY, FF_Delay]))
aa_i2c_write(handle,DEVICE,0,array('B',[MOT_DELAY, MOT_Delay]))
aa_i2c_write(handle,DEVICE,0,array('B',[FF_INT, int(FF_Threshold)]))
aa_i2c_write(handle,DEVICE,0,array('B',[MOT_INT, int(MOT_Threshold)]))
print "Ready..."
while (1):
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGA]))
(ret, data, count) = aa_i2c_read_ext(handle,DEVICE,0,1)
if len(data) < 1:
sys.stdout.write("SENSOR READ ERROR. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
elif data[0] & FFI:
sys.stdout.write("Freefall started. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
elif data[0] & MOTI:
sys.stdout.write("High-g motion detected. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
aa_close(handle)
data sheet of the accelerometer which I am using is kionix company's
KXP84-1050 tri axis digital accelerometer.
Thank you with regards
Anil Vadnala
I am doing a project on accelerometer to find out motion acceleration so that I have to write a C program to PIC18f452.
But acctually I have a programme regarding my project in python language
which I donot understand but I know it is quite related to C.
I am requesting you give any suggesions or help. I am writing the programme in python....
#!/bin/env python
#==========================================================================
#
#
#==========================================================================
# IMPORTS
#==========================================================================
import sys
import time
from aardvark_py import *
#==========================================================================
# CONSTANTS
#==========================================================================
port = 0
bitrate = 100
# Device
DEVICE = 0x18
# Addresses
XOUT_H = 0x00
XOUT_L = 0x01
YOUT_H = 0x02
YOUT_L = 0x03
ZOUT_H = 0x04
ZOUT_L = 0x05
FF_INT = 0x06
FF_DELAY = 0x07
MOT_INT = 0x08
MOT_DELAY = 0x09
CTRL_REGC = 0x0A
CTRL_REGB = 0x0B
CTRL_REGA = 0x0C
# CTRL_REGA
Parity = 0x04
MOTI = 0x02
FFI = 0x01
# CTRL_REGB
CLKhld = 0x80
nEnable = 0x40
ST = 0x20
MOTDen = 0x10
FFDen = 0x08
MOTIen = 0x04
FFIen = 0x02
FFMOTI = 0x01
#==========================================================================
# MAIN PROGRAM
#==========================================================================
# Open the device
handle = aa_open(port)
if (handle <= 0):
print "Unable to open Aardvark device on port %d" % port
print "Error code = %d" % handle
sys.exit()
# User Configuration
Sensitivity = (5.0/(2**12)) # g/count
SampleRate = 250.0 # Hz
sys.stdout.write("Freefall Threshold (g): ")
FF_Threshold = float(sys.stdin.readline())
FF_Threshold = round(FF_Threshold / (16 * Sensitivity))
print "FF_INT byte: 0x%x" % FF_Threshold
print "Freefall Threshold set to %0.3fg" % (16 * FF_Threshold * Sensitivity)
sys.stdout.write("Freefall Interrupt Delay (ms): ")
FF_Delay = float(sys.stdin.readline())/1000
FF_Delay = int(round(FF_Delay * SampleRate))
if FF_Delay < 0x01
FF_Delay = 0x01
if FF_Delay > 0xFF:
FF_Delay = 0xFF
print "FF_DELAY byte: 0x%x" % FF_Delay
print "Freefall Interrupt Delay set to %dms" % ((FF_Delay / SampleRate) * 1000)
sys.stdout.write("Motion Threshold (g): ")
MOT_Threshold = float(sys.stdin.readline())
MOT_Threshold = round(MOT_Threshold / (16 * Sensitivity))
print "MOT_INT byte: 0x%x" % MOT_Threshold
print "Motion Threshold set to %0.3fg" % (16 * MOT_Threshold * Sensitivity)
sys.stdout.write("Motion Interrupt Delay (ms): ")
MOT_Delay = float(sys.stdin.readline())/1000
MOT_Delay = int(round(MOT_Delay * SampleRate))
if MOT_Delay < 0x01:
MOT_Delay = 0x01
if MOT_Delay > 0xFF:
MOT_Delay = 0xFF
print "MOT_DELAY byte: 0x%x" % MOT_Delay
print "Motion Interrupt Delay set to %dms" % ((MOT_Delay / SampleRate) * 1000)
# Ensure that the I2C subsystem is enabled
aa_configure(handle, AA_CONFIG_SPI_I2C)
# Power the EEPROM using the Aardvark adapter's power supply.
# This command is only effective on v2.0 hardware or greater.
# The power pins on the v1.02 hardware are not enabled by default.
aa_target_power(handle, AA_TARGET_POWER_BOTH)
# Activate the pull-up resistor(s)
aa_i2c_pullup(handle,AA_I2C_PULLUP_BOTH);
# Setup the clock phase
#aa_spi_configure(handle, mode >> 1, mode & 1, AA_SPI_BITORDER_MSB)
# Set the bitrate
bitrate = aa_i2c_bitrate(handle, bitrate)
print "Bitrate set to %d kHz" % bitrate
# Configure the device
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGC, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
aa_i2c_write(handle,DEVICE,0,array('B',[FF_DELAY, FF_Delay]))
aa_i2c_write(handle,DEVICE,0,array('B',[MOT_DELAY, MOT_Delay]))
aa_i2c_write(handle,DEVICE,0,array('B',[FF_INT, int(FF_Threshold)]))
aa_i2c_write(handle,DEVICE,0,array('B',[MOT_INT, int(MOT_Threshold)]))
print "Ready..."
while (1):
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGA]))
(ret, data, count) = aa_i2c_read_ext(handle,DEVICE,0,1)
if len(data) < 1:
sys.stdout.write("SENSOR READ ERROR. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
elif data[0] & FFI:
sys.stdout.write("Freefall started. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
elif data[0] & MOTI:
sys.stdout.write("High-g motion detected. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
aa_close(handle)
data sheet of the accelerometer which I am using is kionix company's
KXP84-1050 tri axis digital accelerometer.
Thank you with regards
Anil Vadnala