CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

MPR121 For Proximity
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

MPR121 For Proximity
PostPosted: Tue Apr 07, 2020 12:25 am     Reply with quote

Hello everyone
I have been trying to detect proximity with the mpr121 module for a long time.
I finally succeeded. You can see the code below.
And I got the code from here and adapted it:
http://www.ccsinfo.com/forum/viewtopic.php?t=52320&highlight=mpr121

So thank you to sshahryiar.

But I realized that I could not operate the sensor steadily.
The value I measure changes on its own after a while.
Is there a deficiency in my code?
I think the sensor is of good quality and should work stable.
Thank you for your help


MPR121.h
Code:

#define   MPR121addr       0x5A

#define   Write_MPR121     0xB4
#define   Read_MPR121      0xB5

#define   ELE_L            0x00 //d
#define   ELE_H            0x01 //d
#define   OOR_STATU        0x02                           
#define   ELEPROX          0x03
#define   ELE0_L           0x04
#define   ELE0_H           0x05   
#define   ELE1_L           0x06
#define   ELE1_H           0x07
#define   ELE2_L           0x08
#define   ELE2_H           0x09
#define   ELE3_L           0x0A
#define   ELE3_H           0x0B
#define   ELE4_L           0x0C
#define   ELE4_H           0x0D                   
#define   ELE5_L           0x0E
#define   ELE5_H           0x0F
#define   ELE6_L           0x10
#define   ELE6_H           0x11
#define   ELE7_L           0x12
#define   ELE7_H           0x13
#define   ELE8_L           0x14
#define   ELE8_H           0x15
#define   ELE9_L           0x16
#define   ELE9_H           0x17
#define   ELE10_L          0x18
#define   ELE10_H          0x19
#define   ELE11_L          0x1A
#define   ELE11_H          0x1B
#define   EFDPROX_L        0x1C //denendi calisti.
#define   EFDPROX_H        0x1D //denendi
#define   E0BV             0x1E
#define   E1BV             0x1F
#define   E2BV             0x20
#define   E3BV             0x21
#define   E4BV             0x22
#define   E5BV             0x23   
#define   E6BV             0x24
#define   E7BV             0x25
#define   E8BV             0x26
#define   E9BV             0x27
#define   E10BV            0x28
#define   E11BV            0x29
#define   EPROXBV          0x2A  // deneNdi
#define   MHD_R            0x2B  //
#define   NHD_R            0x2C                   
#define   NCL_R            0x2D             
#define   FDL_R            0x2E   
#define   MHD_F            0x2F
#define   NHD_F            0x30
#define   NCL_F            0x31
#define   FDL_F            0x32
#define   NHD_T            0x33
#define   NCL_T            0x34
#define   FDL_T            0x35
#define   MHDPROXR         0x36 //
#define   NHDPROXR         0x37
#define   NCLPROXR         0x38
#define   FDLPROXR         0x39
#define   MHDPROXF         0x3A
#define   NHDPROXF         0x3B
#define   NCLPROXF         0x3C
#define   FDLPROXF         0x3D
#define   NHDPROXT         0x3E
#define   NCLPROXT         0x3F
#define   FDLPROXT         0x40
#define   ELE0_T           0x41
#define   ELE0_R           0x42
#define   ELE1_T           0x43
#define   ELE1_R           0x44
#define   ELE2_T           0x45
#define   ELE2_R           0x46
#define   ELE3_T           0x47
#define   ELE3_R           0x48
#define   ELE4_T           0x49
#define   ELE4_R           0x4A
#define   ELE5_T           0x4B                     
#define   ELE5_R           0x4C
#define   ELE6_T           0x4D
#define   ELE6_R           0x4E
#define   ELE7_T           0x4F
#define   ELE7_R           0x50
#define   ELE8_T           0x51
#define   ELE8_R           0x52
#define   ELE9_T           0x53
#define   ELE9_R           0x54
#define   ELE10_T          0x55
#define   ELE10_R          0x56
#define   ELE11_T          0x57
#define   ELE11_R          0x58
#define   EPROXTTH         0x59 //denendi
#define   EPROXRTH         0x5A //denendi
#define   DEBOUNCE         0x5B
#define   FIL_CFG1         0x5C
#define   FIL_CFG2         0x5D
#define   ELE_CFG          0x5E
#define   CDC0             0x5F
#define   CDC1             0x60
#define   CDC2             0x61
#define   CDC3             0x62
#define   CDC4             0x63
#define   CDC5             0x64
#define   CDC6             0x65
#define   CDC7             0x66
#define   CDC8             0x67
#define   CDC9             0x68
#define   CDC10            0x69
#define   CDC11            0x6A
#define   CDCPROX          0x6B
#define   CDT0_1           0x6C
#define   CDT2_3           0x6D
#define   CDT4_5           0x6E
#define   CDT6_7           0x6F
#define   CDT8_9           0x70
#define   CDT10_11         0x71
#define   CDTPROX          0x72
#define   GPIO_CTRL0       0x73
#define   GPIO_CTRL1       0x74
#define   GPIO_DATA        0x75
#define   GPIO_DIR         0x76
#define   GPIO_EN          0x77
#define   GPIO_SET         0x78
#define   GPIO_CLEAR       0x79
#define   GPIO_TOGGLE      0x7A
#define   AUTO_CFG0        0x7B   
#define   AUTO_CFG_CTRL    0x7C
#define   AUTO_CFGU        0x7D   
#define   AUTO_CFGL        0x7E
#define   AUTO_CFGT        0x7F                 
#define   SRST             0x80   
#define   TOU_THRESH       0x0F
#define   REL_THRESH       0x09
                                                                       
#define   SDA_pin          pin_C1       
#define   SCL_pin          pin_C0                   
                                         
#use I2C(MASTER, SDA = SDA_pin, SCL = SCL_pin)

void MPR121_init();                                                   
unsigned char MPR121_read(unsigned char address);
void MPR121_write(unsigned char address, unsigned char value);


MPR121.c
Code:


#include "MPR121.h"


void MPR121_init()
{
   MPR121_write(GPIO_CTRL0, 0x00); //0x73
   MPR121_write(GPIO_CTRL1, 0x00); //0x74
   MPR121_write(GPIO_DIR, 0x00);   //0x76
   MPR121_write(GPIO_EN, 0x00);    //0x77
   MPR121_write(MHD_R, 0x01);      //0x2B
   MPR121_write(NHD_R, 0x01);      //0x2C  //3f li bi dene mesafe artıyomu               
   MPR121_write(NCL_R, 0x00);      //0x2D           
   MPR121_write(FDL_R, 0x00);      //0x2E
   MPR121_write(MHD_F, 0x01);      //0x2F
   MPR121_write(NHD_F, 0x01);      //0x30     
   MPR121_write(NCL_F, 0x7F);      //0x31
   MPR121_write(FDL_F, 0x09);      //0x32
   /*
   MPR121_write(ELE0_T, TOU_THRESH);  //0x41
   MPR121_write(ELE0_R, REL_THRESH);
   MPR121_write(ELE1_T, TOU_THRESH);
   MPR121_write(ELE1_R, REL_THRESH);
   MPR121_write(ELE2_T, TOU_THRESH);                   
   MPR121_write(ELE2_R, REL_THRESH);
   MPR121_write(ELE3_T, TOU_THRESH);                   
   MPR121_write(ELE3_R, REL_THRESH);
   MPR121_write(ELE4_T, TOU_THRESH);                   
   MPR121_write(ELE4_R, REL_THRESH);
   MPR121_write(ELE5_T, TOU_THRESH);                   
   MPR121_write(ELE5_R, REL_THRESH);
   MPR121_write(ELE6_T, TOU_THRESH);                   
   MPR121_write(ELE6_R, REL_THRESH);
   MPR121_write(ELE7_T, TOU_THRESH);                   
   MPR121_write(ELE7_R, REL_THRESH);
   MPR121_write(ELE8_T, TOU_THRESH);                   
   MPR121_write(ELE8_R, REL_THRESH);
   MPR121_write(ELE9_T, TOU_THRESH);                   
   MPR121_write(ELE9_R, REL_THRESH);
   MPR121_write(ELE10_T, TOU_THRESH);                   
   MPR121_write(ELE10_R, REL_THRESH);
   MPR121_write(ELE11_T, TOU_THRESH);       
   MPR121_write(ELE11_R, REL_THRESH); //0x58
  */

   MPR121_write(AUTO_CFG0, 0x09);
   
   MPR121_write(AUTO_CFGU, 0xC9);     //0x7D
   MPR121_write(AUTO_CFGL, 0x82);     //0x7E
   MPR121_write(AUTO_CFGT,0xb5);      //0x7F
   //MPR121_write(ELE_CFG, 0x84);
   //MPR121_write(ELE_CFG, 0x00);
   MPR121_write(ELE_CFG, 0x30); //çalışan //0x5E
   //////////////////////////////////////////////////
 // 
   MPR121_write(DEBOUNCE,0x71);
   
   //MPR121_write(FIL_CFG1,0x0f);   // ff  FIL_CFG1         0x5C  //1.3
   //Touch Threshold = 0x08, Release Threshold = 0x05. TOUCH>release
   
   MPR121_write(EPROXTTH,0x06);   // TOUCH relase dokun bırak 0x59
   MPR121_write(EPROXRTH,0x05);   // RELEASE                  0x5A
   

}

                 
unsigned int8 MPR121_read(unsigned int8 addr)                 
{                                       
   unsigned int8 value = 0;               
   
   I2C_Start();             
   I2C_Write(Write_MPR121);
   I2C_Write(addr);                             
   I2C_Start();
   I2C_Write(Read_MPR121);
   value = I2C_Read(0);                       
   I2C_Stop();
   
   return value;
}           
           
       
void MPR121_write(unsigned char addr, unsigned char value)
{
   I2C_Start();
   I2C_Write(Write_MPR121);
   I2C_Write(addr);
   I2C_Write(value);
   I2C_Stop();
}


example.c

Code:

#include <16f1824.h>   
#device ADC=10  // 10 bitlik ADC kullanılacağı belirtiliyor.


#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#fuses INTRC_IO,  NOWDT, NOMCLR, BROWNOUT//, PLL


#use delay(internal=32000000)
#use  rs232(baud=9600,parity=N,xmit=PIN_a0,rcv=PIN_a1,bits=8)
#use i2c(Master,sda=PIN_c1,scl=PIN_c0,slow=400000,FORCE_HW)

#include "MPR121.c"


void main ( )
{

  MPR121_init();
  INT16 cap_value=0;

   while(true)

      cap_value = MPR121_read(EFDPROX_L); //çalışan 0x1c
      printf("cap_value    : %ld \n",cap_value);
}
}


OUTPUT
Quote:

cap_value : 216
cap_value : 217
cap_value : 217
cap_value : 218
cap_value : 218
cap_value : 218
cap_value : 221
cap_value : 220
cap_value : 219
cap_value : 220
cap_value : 220


You can get the data more smoothly by using filters such as bubble sort.
But for example, while a metal object constantly gives the value 195, after a while there are no changes, it becomes 234.
I am trying to solve this problem

Best regards
Ttelmah



Joined: 11 Mar 2010
Posts: 15723

View user's profile Send private message

PostPosted: Tue Apr 07, 2020 7:52 am     Reply with quote

Capacitance measures like this will drift with humidity changes.
The unit has 'self adjustment' for this, controlled by registers 0x2B to 0x40.
You are only setting the first few of these registers, and the values you are
using may well be totally wrong. You need to look at AN3891, which
describes how to set the values in these registers for your hardware.
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

PostPosted: Tue Apr 07, 2020 9:50 am     Reply with quote

Thank you Ttelmah.
Yes, I read that application note. I also read the application notes AN3893, AN3889, AN3890, AN3891, AN3893.
Its change due to humidity can be met with understanding.
My question is;
I get the value of 224.
In just a few minutes it gets to 238. The new value I got is 238. However, moisture or object does not change.

There are really a lot of application notes for integrated.
I don't think this is an integrated error. I don't know what I did wrong.
Ttelmah



Joined: 11 Mar 2010
Posts: 15723

View user's profile Send private message

PostPosted: Tue Apr 07, 2020 10:17 am     Reply with quote

Moisture will be changing.
Just being absorbed into the structure of the PCB used to make the sensor
or drying out from it (modern PCB's tend to be water washed in manufacture). I use a similar sensor to read fluids in pipes. What is amazing is how much
it does drift. Things quite a long distance from the sensor will affect it. So
surfaces pick up or lose moisture as they heat and cool. The capacitance
does also shift with temperature. So you may simply be seeing the chip
itself warming up...
You have to get the self adjustment tuned to match the behaviour of
your system.
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

PostPosted: Tue Apr 07, 2020 11:56 am     Reply with quote

Which integrated are you using ?
Ttelmah



Joined: 11 Mar 2010
Posts: 15723

View user's profile Send private message

PostPosted: Tue Apr 07, 2020 1:04 pm     Reply with quote

I'm using a PIC, with built in capacitance sensing, and my own code
to perform the adjustments. I played with several chips when
prototyping, which is why I know this one...
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

PostPosted: Tue Apr 07, 2020 11:35 pm     Reply with quote

Formula used for capacity measurement; C=e*(A/d)
A; electrode surface does not change.
D; The distance between the body and the electrode does not change.
E; the object does not change.
But when the humidity changes, the E value will also change. That's right.

1 unit, 2 units, 3 units can be exchanges.
Idle measurement of my sensor: ex. 225.
Measurement when full: 215.
It can take values such as 216,214,217 when full.

However, it gives 215 for the first few minutes when it is full.
Then it starts to give 230 in 1 2 seconds.
When I measure the null value it shows 240.
I mean, the dielectric coefficient of air is 240 when it is 225.
The object's dielectric coefficient is 230 when it is 215.
(dielectric coefficient or integrated measurement value. you understand)

If the value changes step by step, we can say that it is heating up integrated.
If a few units change, we can say that it is changing humidity.

But I think the problem is somewhere else.

AN3889 page 9:
Autoconfig Control register: here we can choose how to change baseline. I used these settings, but I guess I failed.
Ttelmah



Joined: 11 Mar 2010
Posts: 15723

View user's profile Send private message

PostPosted: Tue Apr 07, 2020 11:46 pm     Reply with quote

Yes, as I said the PCB material itself tends to dry as the board heats up,
and pick up moisture when cold. Condensation is happening all the time.
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

PostPosted: Wed Apr 08, 2020 12:09 am     Reply with quote

OK. If you say so
What do you recommend hardware?
Ttelmah



Joined: 11 Mar 2010
Posts: 15723

View user's profile Send private message

PostPosted: Wed Apr 08, 2020 1:29 am     Reply with quote

Your hardware is potentially fine.
You need to spend the time to actually tune the filtration, so it correctly adjusts
the system as the conditions change. This is the hard part of using
capacitance based systems. I had problems because in manufacture
the boards were being wet washed. Though they were then dried, they
tended to settle over the first few weeks...
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

PostPosted: Wed Apr 08, 2020 1:43 am     Reply with quote

Wow. I now fully understand what you mean.
I am sorry, my English is not good.
Thank you. I learned something new Very Happy
temtronic



Joined: 01 Jul 2010
Posts: 7349
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 08, 2020 5:17 am     Reply with quote

re:
Quote:
I had problems because in manufacture
the boards were being wet washed. Though they were then dried,


The company in Toronto where I bought boards from, late 80s, used kitchen dishwasher to clean and dry the PCBs. Next day he'd test them. Any that didn't function 100%, went back to repair dept.

As the sensor is sensitive to humidity, could you add a local 'humidity sensor' to read local humidity, use the 'number' to adjust 'parameters' on your sensor ? More complicated but perhaps a more stable unit ??
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

PostPosted: Wed Apr 08, 2020 6:23 am     Reply with quote

It is interesting that there are no humidity statements in appnote and datasheets.
What you said is most likely true.



First, I will reread all appnotes.
If it doesn't work, I can add the humidity sensor. I don't have a humidity sensor right now.

Reminding: I don't measure liquid.
PCM programmer



Joined: 06 Sep 2003
Posts: 21064

View user's profile Send private message

PostPosted: Wed Apr 08, 2020 8:10 am     Reply with quote

ressas wrote:

It is interesting that there are no humidity statements in appnote and datasheets.

It's in the 2010 datasheet found on the Sparkfun website:
https://www.sparkfun.com/datasheets/Components/MPR121.pdf
Search the datasheet for 'moisture'. It's mentioned in these two sections:
2. Three levels of filtering
4. Environment Auto-Calibration

But they left it out of the 2013 datasheet:
https://www.nxp.com/docs/en/data-sheet/MPR121.pdf
ressas



Joined: 15 Nov 2019
Posts: 74

View user's profile Send private message

PostPosted: Wed Apr 08, 2020 1:25 pm     Reply with quote

Quote:

But they left it out of the 2013 datasheet:

Yes. I made the search on the 2013 datasheet.

In this case, is the moisture filter disabled in the new version?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group