思考的大兵 发表于 2015-3-25 18:15:21

请教TTP229触摸芯片控制方式

在淘宝上买了一个TTP229 16路触摸式电容开管,如图所示,芯片是28脚的,但提供的资料是48脚芯片的资料,网上也没找到这种28脚的资料及程序,所以写程序时无法确定控制方式,希望有玩过的朋友帮助下


dsjsjf 发表于 2015-3-26 20:17:08

思考的大兵 发表于 2015-3-26 10:49
你这是用IIC的协议,但我看板子上没有预留IIC接口的SDA,只有串口的SDO,你这程序我也试了,不行,能否发 ...

程序是完整的,而且这个芯片已经用在产品上了。

wu1169668869 发表于 2015-3-26 00:13:15

同问,淘宝都给程序的啊

dsjsjf 发表于 2015-3-25 23:50:52

#define KeyShortTime 10
#define KeyLongTime 150

/************************************
**按键IO口初始化程序
************************************/
void Init_Keyboard(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

//触摸按键输入--TTP229(PB12--SDA, PB5--SCL)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

ErrorStatus I2C_Start(void)       //I2C开始位
{
SDA_H;
SCL_H;
delay_us(5);
if(!SDA_read)
    return ERROR;                            //SDA线为低电平则总线忙,退出
SDA_L;
delay_us(5);
if(SDA_read)
    return ERROR;                            //SDA线为高电平则总线出错,退出
SDA_L;                                                        //SCL为高电平时,SDA的下降沿表示停止位
delay_us(5);
return SUCCESS;
}

void I2C_Stop(void)                           //I2C停止位
{
SCL_L;
delay_us(5);
SDA_L;
delay_us(5);
SCL_H;
delay_us(5);
SDA_H;                                           //SCL为高电平时,SDA的上升沿表示停止位
delay_us(5);
}

static void I2C_Ack(void)                //I2C响应位
{       
SCL_L;
delay_us(5);
SDA_L;
delay_us(5);
SCL_H;
delay_us(5);
SCL_L;
delay_us(5);
}

static void I2C_NoAck(void)                //I2C非响应位
{       
SCL_L;
delay_us(5);
SDA_H;
delay_us(5);
SCL_H;
delay_us(5);
SCL_L;
delay_us(5);
}

ErrorStatus I2C_WaitAck(void)           //I2C等待应答位
{
SCL_L;
delay_us(5);
SDA_H;                       
delay_us(5);
SCL_H;
delay_us(5);
if(SDA_read)
{
    SCL_L;
    return ERROR;
}
SCL_L;
return SUCCESS;
}

/*******************************************************************************
* Function Name: I2C_SendByte
* Description    : 数据从高位到低位
* Input          : - SendByte: 发送的数据
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_SendByte(u8 SendByte)
{
u8 i;

for(i=0; i<8; i++)
{
    SCL_L;
    delay_us(5);
    if(SendByte & 0x80)
      SDA_H;                          //在SCL为低电平时,允许SDA数据改变
    else
      SDA_L;   
    SendByte <<= 1;
    delay_us(5);
    SCL_H;
    delay_us(5);
}
SCL_L;
}

/*******************************************************************************
* Function Name: I2C_ReceiveByte
* Description    : 数据从高位到低位
* Input          : None
* Output         : None
* Return         : I2C总线返回的数据
*******************************************************************************/
u8 I2C_ReceiveByte(void)
{
u8 i,ReceiveByte = 0;

SDA_H;                               
for(i=0; i<8; i++)
{
    ReceiveByte <<= 1;      
    SCL_L;
    delay_us(5);
    SCL_H;
    delay_us(5);       
    if(SDA_read)                                   //在SCL为高电平时,SDA上的数据保持不变,可以读回来
    {
      ReceiveByte |= 0x01;
    }
}
SCL_L;
return ReceiveByte;
}

/*******************************************************************************
* Function Name: I2C_WriteByte
* Description    : 写一字节数据
* Input          : - SendByte: 待写入数据
*                    - WriteAddress: 待写入地址
*                  - DeviceAddress: 器件类型
* Output         : None
* Return         : 返回为:=1成功写入,=0失败
*******************************************************************************/         
ErrorStatus I2C_WriteByte(u8 SendByte, u16 WriteAddress, u8 DeviceAddress)
{               
if(!I2C_Start())
    return ERROR;

I2C_SendByte(((WriteAddress & 0x0700) >>7) | DeviceAddress & 0xFFFE); //设置高起始地址+器件地址

if(!I2C_WaitAck())
{
    I2C_Stop();
    return ERROR;
}

I2C_SendByte((u8)(WriteAddress & 0x00FF));   //设置低起始地址
I2C_WaitAck();       
I2C_SendByte(SendByte);
I2C_WaitAck();                                                                       
I2C_Stop();
delay_ms(5);                   //注意:因为这里要等待EEPROM写完,可以采用查询或延时方式(10ms)

return SUCCESS;
}                                                                       

/*******************************************************************************
* Function Name: I2C_ReadByte
* Description    : 读取一串数据
* Input          : - pBuffer: 存放读出数据
*                    - length: 待读出长度
*                  - ReadAddress: 待读出地址
*                  - DeviceAddress: 器件类型
* Output         : None
* Return         : 返回为:=1成功读入,=0失败
*******************************************************************************/         
ErrorStatus I2C_ReadByte(u8* pBuffer, u16 length, u16 ReadAddress, u8 DeviceAddress)
{               
if(!I2C_Start())
    return ERROR;

I2C_SendByte(((ReadAddress & 0x0700) >> 7) | DeviceAddress & 0xFFFE); //设置高起始地址+器件地址

if(!I2C_WaitAck())
{
    I2C_Stop();
    return ERROR;
}

I2C_SendByte((u8)(ReadAddress & 0x00FF));   //设置低起始地址   
I2C_WaitAck();
I2C_Start();
I2C_SendByte(((ReadAddress & 0x0700) >>7) | DeviceAddress | 0x0001);
I2C_WaitAck();

while(length)
{
    *pBuffer = I2C_ReceiveByte();
    if(length == 1)
      I2C_NoAck();
    else
      I2C_Ack();
    pBuffer++;
    length--;
}

I2C_Stop();

return SUCCESS;
}


///************************************
//**按键检测函数
//**增加软件去抖,以降低灵敏度
//************************************/
u8 Key_Scan(void)
{
unsigned char Key = ' ';
unsigned short Tmp;
static unsigned short OldTmp;
static unsigned short LongTime = 0;

if(I2C_Start() == ERROR)
    return ' ';

I2C_SendByte(0xaf); //设置高起始地址+器件地址

if(I2C_WaitAck() == ERROR)
{
    I2C_Stop();
    return ' ';
}

Tmp = (u16)I2C_ReceiveByte()<<8;
I2C_Ack();
Tmp |= I2C_ReceiveByte();
I2C_NoAck();
I2C_Stop();

if(Tmp == OldTmp)
{
    if(Tmp == 0)
    {
      LongTime = 0;
    }
    else if(LongTime < 0xffff)
    {
      LongTime++;
    }

    switch(Tmp)
    {
    case 0x8000://TP0--NUMBER 3
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '3';
      }
      }break;
    case 0x4000://TP1--NUMBER 4
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '4';
      }
      }break;
    case 0x2000://TP2--NUMBER 5
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '5';
      }
      }break;
    case 0x1000://TP3--NUMBER 6
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '6';
      }
      }break;
    case 0x0800://TP4--NUMBER 7
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '7';
      }
      }break;
    case 0x0400://TP5--NUMBER 8
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '8';
      }
      }break;
    case 0x0200://TP6--NUMBER 9
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '9';
      }
      }break;
    case 0x0100://TP7--NUMBER 0
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '0';
      }
      }break;
    case 0x0080://TP8--ENTER
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = 'B';
      }
      }break;
    case 0x0040://TP9--DOWN
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = 'A';
      }
      }break;
    case 0x0020://TP10--RIGHT
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = 'C';
      }
      }break;
    case 0x0010://TP11--CANCEL
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = 'D';
      }
      else if(LongTime == KeyLongTime)
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(300);
          }
          Buzzer_OFF;
          Key = 'E';
      }
      }break;
    case 0x0008://TP12--NUMBER 1
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '1';
      }
      }break;
    case 0x0004://TP13--NUMBER 2;
      {
      if(LongTime == KeyShortTime)//短按
      {
          if(GetBuzzerSwitch() != RESET)
          {
            Buzzer_ON;
            delay_ms(50);
          }
          Buzzer_OFF;
          Key = '2';
      }
      }break;
    default:
      {
      Key = ' ';
      }break;
    }   
}
else
{
    OldTmp = Tmp;
    LongTime = 0;
}
return Key;
}

wh854997179 发表于 2015-3-25 20:46:00

淘宝商家没给资料吗??

wu1169668869 发表于 2015-3-26 00:20:08

一般51程序,修改下通信接口就可以了orz

zfz0122 发表于 2015-3-26 00:43:38

不懂帮顶的路过

数码小叶 发表于 2015-3-26 09:09:48

同样不懂帮顶的路过 {:3_50:}

思考的大兵 发表于 2015-3-26 10:49:39

dsjsjf 发表于 2015-3-25 23:50
#define KeyShortTime 10
#define KeyLongTime 150



你这是用IIC的协议,但我看板子上没有预留IIC接口的SDA,只有串口的SDO,你这程序我也试了,不行,能否发个完整的程序了?或者你有这芯片28脚的资料吗?店家给的是48脚的

思考的大兵 发表于 2015-3-26 10:50:13

wh854997179 发表于 2015-3-25 20:46
淘宝商家没给资料吗??

给了,没程序,资料芯片和实物芯片引脚不一致

思考的大兵 发表于 2015-3-26 10:51:14

wu1169668869 发表于 2015-3-26 00:13
同问,淘宝都给程序的啊

没给程序,芯片资料也不是很符合
页: [1] 2
查看完整版本: 请教TTP229触摸芯片控制方式