遇到史上最奇怪最难解决的问题,关于电容触摸芯片MPR121,望有相关经验的朋友过来帮帮忙
2019-10-15 06:30发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
刚刚发错版块了,重新发
情况是这样的:最近由于一个项目需要用到电容触摸按键,需要用到12个触摸按键,本来准备用原子的方法,但是通道不够用,刚好在网上看到一款能满足要求的,飞思卡尔的MPR121,IIC通讯,于是买了块板子回来实验,这款芯片网上资料不是太多,STM32的驱动目前为止目前没找到,仅有Arduino的作为参考,项目使用了STM32F103RBT6单片机,于是直接拿原子的IIC底层驱动程序来修改,一切很顺利,能够成功驱动,但是读取按键值的时候却出现了问题,下面一边看代码一边来说:原子的IIC就不往上放了,一样的,主要看MPR121这个,首先是对芯片的一系列初始化,这个直接是参考的手册给所有需要配置的寄存器一个初值,这个暂时没改,也不知道什么意思,如下:
void Touchkey_Init(void)
{
IIC_Init();
IIC_Write(0x5E,0x00);
IIC_Write(0x80,0x63);
IIC_Write(0x73,0xFF);
IIC_Write(0x74,0xFF);
IIC_Write(0x76,0xFF);
IIC_Write(0x77,0xFF);
IIC_Write(0x75,0xFF);
IIC_Write(0x81,0x00);
IIC_Write(0x82,0x00);
IIC_Write(0x83,0x00);
IIC_Write(0x84,0x00);
IIC_Write(0x2B,0x01); //Max half delta Rising
IIC_Write(0x2C,0x01); //Noise half delta Rising
IIC_Write(0x2D,0x00); //Noise count limit Rising
IIC_Write(0x2E,0x00); //Delay limit Rising
IIC_Write(0x2F,0x01); //Max half delta Falling
IIC_Write(0x30,0x01); //Noise half delta Falling
IIC_Write(0x31,0xFF); //Noise count limit Falling
IIC_Write(0x32,0x02); //Delay limit Falling
IIC_Write(0x33,0x00); //Noise half delta Touched
IIC_Write(0x34,0x00); //Noise count Touched
IIC_Write(0x35,0x00); //Delay limit Touched
IIC_Write(0x36,0x0f); //Max half delta Rising
IIC_Write(0x37,0x0f); //Noise half delta Rising
IIC_Write(0x38,0x00); //Noise count Rising
IIC_Write(0x39,0x00); //Delay limit Rising
IIC_Write(0x3A,0x01); //Max half delta Falling
IIC_Write(0x3B,0x01); //Noise half delta Falling
IIC_Write(0x3C,0xff); //Noise count Falling
IIC_Write(0x3D,0xff); //Delay limit Falling
IIC_Write(0x3E,0x00);
IIC_Write(0x3F,0x00);
IIC_Write(0x40,0x00);
IIC_Write(0x41,TOU_THRESH); //ELE0 TOUCH THRESHOLD
IIC_Write(0x42,REL_THRESH); //ELE0 RELEASE THRESHOLD
IIC_Write(0x43,TOU_THRESH); //ELE1 TOUCH THRESHOLD
IIC_Write(0x44,REL_THRESH); //ELE1 RELEASE THRESHOLD
IIC_Write(0x45,TOU_THRESH); //ELE2 TOUCH THRESHOLD
IIC_Write(0x46,REL_THRESH); //ELE2 RELEASE THRESHOLD
IIC_Write(0x47,TOU_THRESH); //ELE3 TOUCH THRESHOLD
IIC_Write(0x48,REL_THRESH); //ELE3 RELEASE THRESHOLD
IIC_Write(0x49,TOU_THRESH); //ELE4 TOUCH THRESHOLD
IIC_Write(0x4A,REL_THRESH); //ELE4 RELEASE THRESHOLD
IIC_Write(0x4B,TOU_THRESH); //ELE5 TOUCH THRESHOLD
IIC_Write(0x4C,REL_THRESH); //ELE5 RELEASE THRESHOLD
IIC_Write(0x4D,TOU_THRESH); //ELE6 TOUCH THRESHOLD
IIC_Write(0x4E,REL_THRESH); //ELE6 RELEASE THRESHOLD
IIC_Write(0x4F,TOU_THRESH); //ELE7 TOUCH THRESHOLD
IIC_Write(0x50,REL_THRESH); //ELE7 RELEASE THRESHOLD
IIC_Write(0x51,TOU_THRESH); //ELE8 TOUCH THRESHOLD
IIC_Write(0x52,REL_THRESH); //ELE8 RELEASE THRESHOLD
IIC_Write(0x53,TOU_THRESH); //ELE9 TOUCH THRESHOLD
IIC_Write(0x54,REL_THRESH); //ELE9 RELEASE THRESHOLD
IIC_Write(0x55,TOU_THRESH); //ELE10 TOUCH THRESHOLD
IIC_Write(0x56,REL_THRESH); //ELE10 RELEASE THRESHOLD
IIC_Write(0x57,TOU_THRESH); //ELE11 TOUCH THRESHOLD
IIC_Write(0x58,REL_THRESH); //ELE11 RELEASE THRESHOLD
IIC_Write(0x59,PROX_TOU_THRESH); //ELE12 TOUCH THRESHOLD
IIC_Write(0x5A,PROX_REL_THRESH); //ELE12 RELEASE THRESHOLD
IIC_Write(0x5B,0x00);
IIC_Write(0x5C,0x10); //AFES=6 samples, same as AFES in 0x7B, Global CDC=16uA
IIC_Write(0x5D,0x24); //CT=0.5us, TDS=4samples, TDI=16ms
IIC_Write(0x7B,0x0B); //AFES=6 samples, same as AFES in 0x5C
IIC_Write(0x7D,0xC9); //AC up limit /C9/BD/C0/9C /3.3V/3.0V/2.8V/1.8V
IIC_Write(0x7E,0x82); //AC low limit /82/7A/7C/65 /3.3V/3.0V/2.8V/1.8V
IIC_Write(0x7F,0xB5); //AC target /B5/AA/AC/8C /3.3V/3.0V/2.8V/1.8V
IIC_Write(0x5E,0x0C);
}
上面完成了一些寄存器的配置,然后主函数很简单
int main(void)
{
SystemInit();
delay_init();
NVIC_Configuration();
uart_init(9600);
LED_Init();
Touchkey_Init();
while(1)
{
Touchkey_Scan();//在这个里面调用按键触摸扫描判断
}
}
下面是扫描函数
void Touchkey_Scan(void)
{
u16 read_data = 0;
read_data = Read_Touchkey_Data();//读取键值函数
if(read_data)//键值不为0,将键值打印出来
{
while(Read_Touchkey_Data());//等待手离开触摸面板
printf("x=%d
",read_data);//键值打印到串口
}
}
再进一步看一下Read_Touchkey_Data();这个函数,如下,问题就出在这个函数里面
键值是存放在0x00和0x01这两个寄存器里面的,如下
其中0x00寄存器里面存放的是触摸按键0-7的状态,0x01寄存器里面存放的是触摸按键8-11的状态,哪一个按键被触摸了,那么相应的位被置1,没有被触摸就为0,0x01寄存器的高三位不起作用,都为0,下面是我读寄存器的函数
u16 Read_Touchkey_Data(void)
{
u16 key_data=0;
key_data = IIC_Read(0x01);
key_data <<= 8;
key_data |=IIC_Read(0x00);
return key_data;
}
那么现在的问题是这样的,当我触摸的时候,是有反应的,比如我用手触摸按键0,那么从串口打印出来的值我截一段数据如下:
x=1
x=1
x=257
x=1
x=257
x=257
.
.
.其他按键的现象也是这样,理论上应该都是1才对,后来分析一下257实际上就是256+1,也就是高8位跟低8位一样时就会出现257
转换成二进制257=00000001 00000001 ,但是高8位是按键8-11,我没有触碰到任何一个按键,怎么会有值呢,而且触摸键1就会出现
x=2
x=2
x=514
x=514
x=2
x=514
.
.
另外触摸8-11按键,什么值都不返回,也就是没有任何反应,这个问题困扰我两天了,实在没办法,望有用过的或者有什么想法能够在这里提一提,多谢了
一周热门 更多>