![Szczęśliwy :-)](./images/smilies/icon_e_smile.gif)
Code: Select all
root@ctdev:~/mpr121# i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- 5a -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@ctdev:~/mpr121#
Krótki program pozwoli sprawdzić działanie naszego modułu:
Code: Select all
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
from I2C import Device
import smbus
import os
import time
#Registers and Bits in the Control/Status register
MPR121_TOUCHSTATUS_L = 0x00
MPR121_TOUCHSTATUS_H = 0x01
MPR121_FILTDATA_0L = 0x04
MPR121_FILTDATA_0H = 0x05
MPR121_BASELINE_0 = 0x1E
MPR121_EPROXBV = 0x2A
MPR121_MHDR = 0x2B
MPR121_NHDR = 0x2C
MPR121_NCLR = 0x2D
MPR121_FDLR = 0x2E
MPR121_MHDF = 0x2F
MPR121_NHDF = 0x30
MPR121_NCLF = 0x31
MPR121_FDLF = 0x32
MPR121_NHDT = 0x33
MPR121_NCLT = 0x34
MPR121_FDLT = 0x35
MPR121_TOUCHTH_0 = 0x41
MPR121_RELEASETH_0 = 0x42
MPR121_DEBOUNCE = 0x5B
MPR121_CONFIG1 = 0x5C
MPR121_CONFIG2 = 0x5D
MPR121_CHARGECURR_0 = 0x5F
MPR121_CHARGETIME_1 = 0x6C
MPR121_ECR = 0x5E
MPR121_AUTOCONFIG0 = 0x7B
MPR121_AUTOCONFIG1 = 0x7C
MPR121_UPLIMIT = 0x7D
MPR121_LOWLIMIT = 0x7E
MPR121_TARGETLIMIT = 0x7F
MPR121_GPIODIR = 0x76
MPR121_GPIOEN = 0x77
MPR121_GPIOSET = 0x78
MPR121_GPIOCLR = 0x79
MPR121_GPIOTOGGLE = 0x7A
MPR121_SOFTRESET = 0x80
#The default I2C address
MPR121_I2CADDR_DEFAULT = 0x5A
# DS3231 Address:
ADDRESSmpr = MPR121_I2CADDR_DEFAULT
# I2C BUS:
BUS = 1
# initialize I2C BUS
mpr=Device(ADDRESSmpr, BUS)
# functions:
def mprSHOWFILTEREDFATA (sensor):
""" Function doc """
if sensor > 12:
return 0x00
else:
data = mpr.readU16(MPR121_FILTDATA_0L + sensor*2)
return data
def mprSHOWBASELINEDATA (sensor):
""" Function doc """
if sensor > 12:
return 0x00
else:
data = mpr.readU16( MPR121_BASELINE_0 + sensor)
return data
def mprTOUCHED ():
""" Function doc """
data = mpr.readU16(MPR121_TOUCHSTATUS_L)
return data
def mprSETTRESHOLDS (touch,release):
""" Function doc """
for i in range(0,12):
mpr.write8(MPR121_TOUCHTH_0 + 2*i, touch)
mpr.write8(MPR121_RELEASETH_0 + 2*i, release)
def mprINITIALIZATION ():
""" Function doc """
mpr.write8(MPR121_SOFTRESET,0x63)
time.sleep(1)
mpr.write8(MPR121_ECR, 0x0)
data = mpr.readU8(MPR121_CONFIG2)
if data <> 0x24:
exit()
mprSETTRESHOLDS(12, 6)
mpr.write8(MPR121_MHDR, 0x01)
mpr.write8(MPR121_NHDR, 0x01)
mpr.write8(MPR121_NCLR, 0x0E)
mpr.write8(MPR121_FDLR, 0x00)
mpr.write8(MPR121_MHDF, 0x01)
mpr.write8(MPR121_NHDF, 0x05)
mpr.write8(MPR121_NCLF, 0x01)
mpr.write8(MPR121_FDLF, 0x00)
mpr.write8(MPR121_NHDT, 0x00)
mpr.write8(MPR121_NCLT, 0x00)
mpr.write8(MPR121_FDLT, 0x00)
mpr.write8(MPR121_DEBOUNCE, 0)
mpr.write8(MPR121_CONFIG1, 0b00010000) #16uA charge current for each electrode
mpr.write8(MPR121_CONFIG2, 0x20) #0.5uS encoding, 1ms period
mpr.write8(MPR121_ECR, 0x8F) #start with first 5 bits of baseline tracking, alle electrodes are enabled
def readREGISTERSforTEST ():
""" Function doc """
for reg in range(0x00,0x80):
data = mpr.readU8(reg)
print hex(reg),' = ',hex(data)
# Main game:
mprINITIALIZATION()
#readREGISTERSforTEST() # only for test
print ""
while 1:
print 'Touched:', mprTOUCHED()
print ""
print "Raw data:"
for i in range(0,12):
print 'Sensor:', i ,hex(mprSHOWFILTEREDFATA(i)), hex(mprSHOWBASELINEDATA(i))
time.sleep(0.1)
os.system('clear')
Przykładowo na załączonym powyżej zdjęciu widać 'wduszony' sensor nr 8... Moduł można nabyć w cenie ok. 3,5 $.