JKaiser
- 15th October 2013, 19:09
I am trying to get my Amicus18 board to talk to a Sparkfun MPR121 Capacitive Touch Keypad over I2C. Could someone verify the maximum usable OSC value.
Right now I have it set to 64 but it looks like the PBP manual says the max usable is 20 with I2C.
Can I run it at 64 and use DEFINE I2C_SLOW 1?
Thanks John
JKaiser
- 15th October 2013, 20:16
Under Write_MPR121 - the D2 values never match the Register_val it writes just before. That is why I question if its working. I wanted to make sure this is working before moving forward.
This is the main code I am using,
'************************************************* *****************************
'* Name : MPR121.pbp *
'* Author : John Kaiser *
'* Date : 2013-10-11 *
'* Version : 1.0 *
'* Notes : This file contains register definitions for the *
'* MPR121 Proximity Capacitive Touch Sensor Controller *
'************************************************* *****************************
DEFINE OSC 64
DEFINE LOADER_USED 1
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1
DEFINE HSER_SPBRG 138
SPBRGH = 0
BAUDCON.3 = 1
TRISC.7 = 1
TRISB.6 = 0
TRISB.7 = 0
include "MPR121_defs.pbp"
DEFINE I2C_SLOW 1
Register_num var byte
Register_val var byte
Touch_0_7 var byte
Touch_0_7 = 0
d2 var byte
d2 = 5
symbol Cpin = PORTB.6
Symbol Dpin = PORTB.7
Symbol Touch_IRQ = PORTC.7
Gosub MPR121_Init ' Iniatialize MPR121 registers
'************************************************* *****************************
' xx
'------------------------------------------------------------------------------
Mainloop:
Pause 30
HSEROUT[" Touch_0_7 = ", bin Touch_0_7 ,10]
'if Touch_IRQ = 0 then goto Read_MPR121
Goto Mainloop
'************************************************* *****************************
' xx
'------------------------------------------------------------------------------
Write_MPR121:
I2CWRITE Dpin, Cpin, $5A, Register_num,[Register_val]
Pause 10
HSEROUT["Register_num = ", hex Register_num ," Register_val = ", hex Register_val ,10]
Pause 50
I2CREAD Dpin, Cpin, $5A, Register_num,[d2]
Pause 200
HSEROUT["Register_num-- = ", hex Register_num ," Register_val-- = ", hex d2 ,10]
Pause 200
return
'************************************************* *****************************
' xx
'------------------------------------------------------------------------------
'Read_MPR121:
' HSEROUT[" It has been touched !!"]
' I2CREAD Dpin, Cpin, $5A, $00,[Touch_0_7]
' Pause 2000
' HSEROUT[" Touch_0_7 = ", dec Touch_0_7 ,10]
' Pause 2000
'return
'************************************************* *****************************
' xx
'------------------------------------------------------------------------------
MPR121_Init:
Register_num = MPR121_ELE_CFG : Register_val = $00 : gosub Write_MPR121
'Section A - Controls filtering when data is > baseline.
Register_num = MPR121_MHD_R : Register_val = $01 : gosub Write_MPR121
Register_num = MPR121_NHD_R : Register_val = $01 : gosub Write_MPR121
Register_num = MPR121_NCL_R : Register_val = $00 : gosub Write_MPR121
Register_num = MPR121_FDL_R : Register_val = $00 : gosub Write_MPR121
'Section B - Controls filtering when data is < baseline.
Register_num = MPR121_MHD_F : Register_val = $01 : gosub Write_MPR121
Register_num = MPR121_NHD_F : Register_val = $01 : gosub Write_MPR121
Register_num = MPR121_NCL_F : Register_val = $FF : gosub Write_MPR121
Register_num = MPR121_FDL_F : Register_val = $02 : gosub Write_MPR121
'Section C - Sets touch and release thresholds for each electrode
Register_num = MPR121_ELE0_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE0_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE1_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE1_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE2_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE2_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE3_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE3_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE4_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE4_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE5_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE5_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE6_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE6_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE7_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE7_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE8_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE8_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE9_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE9_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE10_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE10_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE11_T : Register_val = MPR121_TOU_THRESH : gosub Write_MPR121
Register_num = MPR121_ELE11_R : Register_val = MPR121_REL_THRESH : gosub Write_MPR121
'Section D - Set the Filter Configuration - Set ESI2
Register_num = MPR121_FIL_CFG1 : Register_val = $04 : gosub Write_MPR121
'Section E - Electrode Configuration - Set ELE_CFG to 0x00 to return to standby mode
Register_num = MPR121_ELE_CFG : Register_val = $0C : gosub Write_MPR121 'Enables all 12 Electrodes
'Section F - Enable Auto Config and auto Reconfig
Register_num = MPR121_ATO_CFG0 : Register_val = $0B : gosub Write_MPR121
Register_num = MPR121_ATO_CFGU : Register_val = $C9 : gosub Write_MPR121 'USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V
Register_num = MPR121_ATO_CFGL : Register_val = $82 : gosub Write_MPR121 'LSL = 0.65*USL = 0x82 @3.3V
Register_num = MPR121_ATO_CFGT : Register_val = $B5 : gosub Write_MPR121 'Target = 0.9*USL = 0xB5 @3.3V
Register_num = MPR121_ELE_CFG : Register_val = $0C : gosub Write_MPR121
return
'************************************************* *****************************
end
And this is the Register defs,
'************************************************* *****************************
'* Name : MPR121_defs.pbp *
'* Author : John Kaiser *
'* Date : 2013-10-11 *
'* Version : 1.0 *
'* Notes : This file contains register definitions for the *
'* MPR121 Proximity Capacitive Touch Sensor Controller *
'************************************************* *****************************
'************************************************* *****************************
' MPR121 Note: After completing power on reset (POR) or soft reset by command,
' MPR121 all registers are in default reset initial value (see Table 1).
' All the registers are cleared, except AFE Configuration Registers 0x5C
' (Default 0x10) and 0x5D (Default 0x24).
'
' MPR121 reset default mode is Stop Mode without any capacitance measurement.
' Set MPR121 into Run Mode by configuring ECR register (0x5E) so that least
' one of the 13 measurement channels is enabled. Set MPR121 into Stop Mode
' by writing 2bXX000000 to ECR register at any time. For all the registers,
' the current value can be read at any time.
'
' Note: Registers write operation can only be done after MPR121 is set into
' Stop Mode, except the ECR and GPIO/LED related registers, which means
' changing the value of these registers while MPR121 is in Run Mode will not
' be effective.
'
' Registers from address 0x00 to 0x2A are the output registers which are
' updated periodically when MPR121 is in Run Mode. While Baseline Value
' Registers 0x1D to 0x2A are the internal output baseline registers, these
' registers are also writeable to user, this is useful if customer setting
' targets as the initial baseline tracking start points is desired.
'
' Registers from address 0x2B to 0x7F are control and configuration registers,
' which need to be correctly configured and initialized to start the
' capacitance measurement and touch detection.
'************************************************* *****************************
'************************************************* *****************************
' Global Constants
'------------------------------------------------------------------------------
MPR121_TOU_THRESH CON $06
MPR121_REL_THRESH CON $0A
'------------------------------------------------------------------------------
'************************************************* *****************************
' MPR121 Touch Status Registers (0x00~0x01)
' From Section #2 of data sheet
'------------------------------------------------------------------------------
MPR121_xxxx_2_1 CON $00
MPR121_xxxx_2_2 CON $01
'************************************************* *****************************
' MPR121 Out Of Range Status Registers ($02, $03)
' From Section #13 of data sheet
'------------------------------------------------------------------------------
MPR121_xxxx_13_1 CON $02
MPR121_xxxx_13_2 CON $03
'************************************************* *****************************
' MPR121 Electrode Data Register ($04~$1D)
' From Section #3 of data sheet
'------------------------------------------------------------------------------
MPR121_xxxx_3_1 CON $04
MPR121_xxxx_3_2 CON $05
MPR121_xxxx_3_3 CON $06
MPR121_xxxx_3_4 CON $07
MPR121_xxxx_3_5 CON $08
MPR121_xxxx_3_6 CON $09
MPR121_xxxx_3_7 CON $0A
MPR121_xxxx_3_8 CON $0B
MPR121_xxxx_3_9 CON $0C
MPR121_xxxx_3_10 CON $0D
MPR121_xxxx_3_11 CON $0E
MPR121_xxxx_3_12 CON $0F
MPR121_xxxx_3_13 CON $10
MPR121_xxxx_3_14 CON $11
MPR121_xxxx_3_15 CON $12
MPR121_xxxx_3_16 CON $13
MPR121_xxxx_3_17 CON $14
MPR121_xxxx_3_18 CON $15
MPR121_xxxx_3_19 CON $16
MPR121_xxxx_3_20 CON $17
MPR121_xxxx_3_21 CON $18
MPR121_xxxx_3_22 CON $19
MPR121_xxxx_3_23 CON $1A
MPR121_xxxx_3_24 CON $1B
MPR121_xxxx_3_25 CON $1C
MPR121_xxxx_3_26 CON $1D
'************************************************* *****************************
' MPR121 Electrode Data Register ($04~$1D)
' From Section #4 of data sheet
'------------------------------------------------------------------------------
MPR121_xxxx_4_1 CON $1E
MPR121_xxxx_4_2 CON $1F
MPR121_xxxx_4_3 CON $20
MPR121_xxxx_4_4 CON $21
MPR121_xxxx_4_5 CON $22
MPR121_xxxx_4_6 CON $23
MPR121_xxxx_4_7 CON $24
MPR121_xxxx_4_8 CON $25
MPR121_xxxx_4_9 CON $26
MPR121_xxxx_4_10 CON $27
MPR121_xxxx_4_11 CON $28
MPR121_xxxx_4_12 CON $29
MPR121_xxxx_4_13 CON $2A
'************************************************* *****************************
' MPR121 Baseline Filtering Control Register ($2B~$40)
' From Section #5 of data sheet
'------------------------------------------------------------------------------
MPR121_MHD_R CON $2B
MPR121_NHD_R CON $2C
MPR121_NCL_R CON $2D
MPR121_FDL_R CON $2E
MPR121_MHD_F CON $2F
MPR121_NHD_F CON $30
MPR121_NCL_F CON $31
MPR121_FDL_F CON $32
MPR121_NHD_T CON $33
MPR121_NCL_T CON $34
MPR121_FDL_T CON $35
MPR121_EPROX_MHD_R CON $36
MPR121_EPROX_NHD_R CON $37
MPR121_EPROX_NCL_R CON $38
MPR121_EPROX_FDL_R CON $39
MPR121_EPROX_MHD_F CON $3A
MPR121_EPROX_NHD_F CON $3B
MPR121_EPROX_NCL_F CON $3C
MPR121_EPROX_FDL_F CON $3D
MPR121_EPROX_NHD_T CON $3E
MPR121_EPROX_NCL_T CON $3F
MPR121_FEPROX_DL_T CON $40
'************************************************* *****************************
' MPR121 Touch and Release Threshold ($41~$5A)
' From Section #6 of data sheet
'------------------------------------------------------------------------------
MPR121_ELE0_T CON $41
MPR121_ELE0_R CON $42
MPR121_ELE1_T CON $43
MPR121_ELE1_R CON $44
MPR121_ELE2_T CON $45
MPR121_ELE2_R CON $46
MPR121_ELE3_T CON $47
MPR121_ELE3_R CON $48
MPR121_ELE4_T CON $49
MPR121_ELE4_R CON $4A
MPR121_ELE5_T CON $4B
MPR121_ELE5_R CON $4C
MPR121_ELE6_T CON $4D
MPR121_ELE6_R CON $4E
MPR121_ELE7_T CON $4F
MPR121_ELE7_R CON $50
MPR121_ELE8_T CON $51
MPR121_ELE8_R CON $52
MPR121_ELE9_T CON $53
MPR121_ELE9_R CON $54
MPR121_ELE10_T CON $55
MPR121_ELE10_R CON $56
MPR121_ELE11_T CON $57
MPR121_ELE11_R CON $58
MPR121_ELE12_T CON $59
MPR121_ELE12_R CON $5A
'************************************************* *****************************
' Debounce Register ($5B)
' From Section #7 of data sheet
'------------------------------------------------------------------------------
MPR121_DEBOUNCE CON $5B
'************************************************* *****************************
' AFE Configuration Register ($5C, $5D)
' From Section #8 of data sheet
'------------------------------------------------------------------------------
MPR121_FIL_CFG1 CON $5C
MPR121_FIL_CFG2 CON $5D
'************************************************* *****************************
' Electrode Configuration Register (ECR, $5E)
' From Section #11 of data sheet
'------------------------------------------------------------------------------
MPR121_ELE_CFG CON $5E
'************************************************* *****************************
' Individual Charge Current Register ($5F~$6B)
' From Section #9 of data sheet
'------------------------------------------------------------------------------
MPR121_xxxx_9_1 CON $5F
MPR121_xxxx_9_2 CON $60
MPR121_xxxx_9_3 CON $61
MPR121_xxxx_9_4 CON $62
MPR121_xxxx_9_5 CON $63
MPR121_xxxx_9_6 CON $64
MPR121_xxxx_9_7 CON $65
MPR121_xxxx_9_8 CON $66
MPR121_xxxx_9_9 CON $67
MPR121_xxxx_9_10 CON $68
MPR121_xxxx_9_11 CON $69
MPR121_xxxx_9_12 CON $6A
MPR121_xxxx_9_13 CON $6B
'************************************************* *****************************
' Individual Charge Time Register ($6C~$72)
' From Section #10 of data sheet
'------------------------------------------------------------------------------
MPR121_xxxx_10_1 CON $6C
MPR121_xxxx_10_2 CON $6D
MPR121_xxxx_10_3 CON $6E
MPR121_xxxx_10_4 CON $6F
MPR121_xxxx_10_5 CON $70
MPR121_xxxx_10_6 CON $71
MPR121_xxxx_10_7 CON $72
'************************************************* *****************************
' GPIO Registers ($73~$7A)
' From Section #14 of data sheet
'------------------------------------------------------------------------------
MPR121_GPIO_CTRL0 CON $73
MPR121_GPIO_CTRL1 CON $74
MPR121_GPIO_DATA CON $75
MPR121_GPIO_DIR CON $76
MPR121_GPIO_EN CON $77
MPR121_GPIO_SET CON $78
MPR121_GPIO_CLEAR CON $79
MPR121_GPIO_TOGGLE CON $7A
'************************************************* *****************************
' Auto Configuration Registers ($7B~$7F)
' From Section #12 of data sheet
'------------------------------------------------------------------------------
MPR121_ATO_CFG0 CON $7B
MPR121_ATO_CFG1 CON $7C
MPR121_ATO_CFGU CON $7D
MPR121_ATO_CFGL CON $7E
MPR121_ATO_CFGT CON $7F
'************************************************* *****************************
' Software Reset Register ($80)
' From Section #15 of data sheet
'------------------------------------------------------------------------------
MPR121_xxxx_15_1 CON $80
Darrel Taylor
- 15th October 2013, 23:29
Typically, datasheets will specify the device's "Slave Address" as a 7-bit number.
In the case of the MPR121, the datasheet says 5A (with the ADDR pin tied to ground).
In order to use that address in the control byte of the I2C commands, you have to shift it left 1-bit to make room for the Read/Write bit.
So the control byte should be ... $B4.
JKaiser
- 16th October 2013, 14:56
Thanks Darrel, I changed $5A to $B4 and it works great.
John
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.