電路圖
程式列表
Python - ADXL345.py
from micropython import const
from machine import Pin,I2C
import math
TO_READ = 6 #Byte N
buff = bytearray(6)
# ADXL345 constants
device = const(0x53)
EARTH_GRAVITY_MS2 = 9.80665
SCALE_MULTIPLIER = 0.004
THRESH_TAP = const(0x1D) # Tap threshold
OFSX = const(0x1E) # X-axis offset
OFSY = const(0x1F) # Y-axis offset
OFSZ = const(0x20) # Z-axis offset
BW_RATE = const(0x2C) # Data rate and power mode control (Read/Write)
BW_RATE_25HZ = const(0x09)
BW_RATE_50HZ = const(0x0A)
BW_RATE_100HZ = const(0x0B)
BW_RATE_200HZ = const(0x0C)
BW_RATE_400HZ = const(0x0D)
BW_RATE_800HZ = const(0x0E)
BW_RATE_1600HZ = const(0x0F)
POWER_CTL = const(0x2D) # Power-saving features control (Read/Write)
Wakeup_8Hz = const(0x00)
Wakeup_4Hz = const(0x01)
Wakeup_2Hz = const(0x02)
Wakeup_1Hz = const(0x03)
Sleep = const(0x04)
MEASURE = const(0x08)
AUTO_SLEEP = const(0x10)
INT_ENABLE = const(0x2E) # Interrupt enable control
INT_MAP = const(0x2F) # Interrupt mapping control
INT_SOURCE = const(0x30) # Source of interrupts
DATA_FORMAT = const(0x31) # Data format control (Read/Write)
RANGE_2G = const(0x00)
RANGE_4G = const(0x01)
RANGE_8G = const(0x02)
RANGE_16G = const(0x03)
Justify_Right = const(0x00)
Justify_Left = const(0x04)
FULL_RES = const(0x08)
AXES_DATA = const(0x32) # Register 0x32 to Register 0x37—DATAX0, DATAX1,
# DATAY0, DATAY1, DATAZ0, DATAZ1 (Read Only)
FIFO_CTL = const(0x38) # FIFO control (Read/Write)
FIFO_STATUS = const(0x39) # FIFO status (Read Only)
class ADXL345:
def __init__(self, i2c, slvAddr=device):
value = bytearray(1)
self.slvAddr = slvAddr
self.i2c = i2c
self.writeByte(POWER_CTL,0) #sleep
self.writeByte(BW_RATE,BW_RATE_100HZ)
value[0] = self.readByte(DATA_FORMAT)
value[0] &= ~0x0F;
value[0] |= FULL_RES | RANGE_2G;
self.writeByte(DATA_FORMAT,value[0])
self.writeByte(POWER_CTL,AUTO_SLEEP)
self.writeByte(POWER_CTL,MEASURE)
# @property
def ReadValue(self):
buff = bytearray(TO_READ)
self.i2c.send(bytearray([AXES_DATA]),self.slvAddr)
self.i2c.recv(buff,self.slvAddr)
x = (int(buff[1]) << 8) | buff[0]
if x > 32767:
x -= 65536
y = (int(buff[3]) << 8) | buff[2]
if y > 32767:
y -= 65536
z = (int(buff[5]) << 8) | buff[4]
if z > 32767:
z -= 65536
return x,y,z
def RP_calculate(self,x,y,z):
roll = math.atan2(y , z) * 57.3
pitch = math.atan2((- x) , math.sqrt(y * y + z * z)) * 57.3
return roll,pitch
# returns the current reading from the sensor for each axis
# parameter gforce:
# False (default): result is returned in m/s^2
# True : result is returned in gs
def getAxes(self,x,y,z, gforce = False):
ax = x * SCALE_MULTIPLIER
ay = y * SCALE_MULTIPLIER
az = z * SCALE_MULTIPLIER
if gforce == False:
ax = ax * EARTH_GRAVITY_MS2
ay = ay * EARTH_GRAVITY_MS2
az = az * EARTH_GRAVITY_MS2
ax = round(ax, 4)
ay = round(ay, 4)
az = round(az, 4)
return {"x": ax, "y": ay, "z": az}
def writeByte(self, addr, data):
self.i2c.send(bytearray([addr,data]),self.slvAddr)
def readByte(self, addr):
data = bytearray(1)
self.i2c.send(bytearray([addr]),self.slvAddr)
self.i2c.recv(data,self.slvAddr)
return data[0]
Python - ePy-Lite_ADXL345.py
"""
ePy-Lite_ADXL345.py
Reference:
ePy-Lite | ADXL345
----------|----------
GND | GND
3V3 | VCC
| CS
| INT1
| INT2
| SDO
P18_SDA0 | SDA
P17_SCL0 | SCL
"""
from micropython import const
from machine import I2C, Pin
from machine import Switch #Get button KEY library
import utime
import ADXL345
addrADXL345 = const(0x53)
if __name__ == '__main__':
KeyA = Switch('keya') #Create button A
i2c = I2C(0,I2C.MASTER,baudrate=100000) #創建頻率為100kHz的I2C外設,選擇要使用的外設I2C 0
adx = ADXL345.ADXL345(i2c)
utime.sleep_ms(1000)
while True:
x,y,z=adx.ReadValue()
roll,pitch = adx.RP_calculate(x,y,z)
axes = adx.getAxes(x,y,z,True)
print('The acceleration info of x, y, z are:%d,%d,%d'%(x,y,z))
print("x = %.3fG, y = %.3fG, z = %.3fG" % ( axes['x'], axes['y'], axes['z']))
print('roll=',roll)
print('pitch=',pitch)
if KeyA.value() == True: #Press A Key
break
utime.sleep_ms(500)
執行結果