TS12芯片驱动
使用模拟I2C驱动TS12完成触摸按键检测功能:
#include "stm8s.h"
#include "global.h"
#include "drv_key.h"
uint8_t KeyByte;
/*******************************************************************************
// Function: I2C_Int
// Description: 模拟I2C 与ds1307端口初始化
// Param:
// Return:
// Author: Huangzhigang 2014-0410
*******************************************************************************/
static void I2C_Int(void)
{
IIC_SDA_OUTPUT;
IIC_SCL_OUTPUT;
}
/*******************************************************************************
// Function: Delay_5us
// Description: 微妙级延时函数 延时时间约为16us
// Param:
// Return: fcpu 16MHz 时
// Author: Huangzhigang 2014-0410
*******************************************************************************/
static void Delay_5us(void)
{
uint8_t i;
for (i=5; i>0; i--);
}
/*******************************************************************************
// Function: I2C_Start
// Description: I2C 开始传输信号 当SCL 为高时 SDA由高变低
// Param:
// Return:
// Author: Huangzhigang 2014-0410
*******************************************************************************/
static void I2C_Start(void)
{
// SDA 1->0 while SCL High
SDA_HIGH;
SCL_HIGH;
Delay_5us();
SDA_LOW;
Delay_5us();
SCL_LOW;
}
/*******************************************************************************
// Function: I2C_Stop
// Description: I2C 停止传输信号 当SCL 为高时 SDA由低变高
// Param:
// Return:
// Author: Huangzhigang 2014-0410
*******************************************************************************/
static void I2C_Stop(void)
{
// SDA 0->1 while SCL High
SDA_LOW;
SCL_HIGH;
Delay_5us();
SDA_HIGH;
Delay_5us();
}
/*******************************************************************************
// Function: I2C_SendACK
// Description: 主机向从机发送应答信号
// Param: 应答信号 1:表示SDA高电平(无应答) 0:SDA低电平(有应答)
// Return:
// Author: Huangzhigang 2014-0410
*******************************************************************************/
static void I2C_SendACK(uint8_t ack)
{
if(ack == 0)
{
SDA_LOW;
}
else
{
SDA_HIGH;
}
SCL_HIGH;
Delay_5us();
SCL_LOW;
Delay_5us();
}
/*******************************************************************************
// Function: I2C_SendByte
// Description: 模拟I2C通信 发送8位数据
// Param: 发送的8为数据值
// Return: 返回应答信号 0表示有应答 1表示无应答
// Author: Huangzhigang 2014-0410
*******************************************************************************/
static uint8_t I2C_SendByte(uint8_t SendByte)
{
static uint8_t i,RevAckTS;
SDA_LOW;
for (i= 0 ; i< 8; i++)
{
SCL_LOW;
if (SendByte & 0x80) // write data
{
SDA_HIGH;
}
else
{
SDA_LOW;
}
Delay_5us();
SendByte <<= 1;
SCL_HIGH;
Delay_5us();
}
SCL_LOW;
SDA_HIGH;
Delay_5us();
IIC_SDA_INPUT;
SCL_HIGH;
asm("nop");
asm("nop");
RevAckTS = (uint8_t)READ_SDA;
Delay_5us();
SCL_LOW;
IIC_SDA_OUTPUT;
Delay_5us();
return RevAckTS;
}
/*******************************************************************************
// Function: I2C_RecvByte
// Description: 模拟I2C通信 从从机读取8位数据
// Param:
// Return: 返回读取的8为数据值
// Author: Huangzhigang 2014-0410
*******************************************************************************/
static uint8_t I2C_RecvByte()
{
uint8_t i;
uint8_t RecvData = 0;
SDA_HIGH; // latch the Data port befor reading
IIC_SDA_INPUT;
for (i=0; i<8; i++)
{
RecvData <<= 1;
SCL_HIGH;
asm("nop");
asm("nop");
if (READ_SDA)
{
RecvData |= 0x01;
}
else
{
RecvData &= 0xfe;
}
Delay_5us();
SCL_LOW;
Delay_5us();
}
IIC_SDA_OUTPUT;
return RecvData;
}
/*******************************************************************************
// Function: Ts12_WriteByte
// Description: 模拟I2C通信 写入1字节数据到指定地址
// Param: WriteAddr:待写入数据 WriteData;写入的地址
// Return: 1: 成功写入 0: 写入出错
// Author: Huangzhigang 2014-0410
*******************************************************************************/
uint8_t Ts12_WriteByte(uint8_t WriteAddr,uint8_t WriteData)
{
I2C_Start();
if(I2C_SendByte(0xF0)) // Device Addr + Write (operation)
{
return 0;
}
if(I2C_SendByte(WriteAddr))
{
return 0;
}
if(I2C_SendByte(WriteData))
{
return 0;
}
I2C_Stop();
return 1;
}
/*******************************************************************************
// Function: Ts12_ReadByte
// Description: 模拟I2C通信 从指定地址读取1字节数据
// Param: ReadAddr:读取的地址
// Return: RevData:读取的8位数据
// Author: Huangzhigang 2014-0410
*******************************************************************************/
uint8_t Ts12_ReadByte(uint8_t ReadAddr)
{
uint8_t RevData;
I2C_Start();
I2C_SendByte(0xF0); // Device Addr + Write (operation)
I2C_SendByte(ReadAddr);
I2C_Start();
I2C_SendByte(0xF1); // Device Addr + Read (operation)
RevData = I2C_RecvByte();
I2C_SendACK(1);
I2C_Stop();
return RevData;
}
void TS12_InitAgain()
{
I2C_Start();
I2C_SendByte(0xf0);
I2C_SendByte(0x02);
I2C_SendByte(0xAA); //Sensitivity1
I2C_SendByte(0xAA);
I2C_SendByte(0xAA);
I2C_SendByte(0xAA);
I2C_SendByte(0xAA);
I2C_SendByte(0xAA);
I2C_SendByte(0x33); //CTRL1
I2C_Stop();
I2C_Start();
I2C_SendByte(0xF0);
I2C_SendByte(0x0A);
I2C_SendByte(0x00); //Ref_rst1
I2C_SendByte(0x00); //Ref_rst2
I2C_SendByte(0x00); //Ch_hold1
I2C_SendByte(0x00); //Ch_hold2
I2C_SendByte(0x00); //Cal_hold1
I2C_SendByte(0x00); //Cal_hold2
I2C_Stop();
}
/*******************************************************************************
// Function: Ts12_Init
// Description: 模拟I2C通信 写入8字节数据 从0x02~0x0f
// Param: pWriteData: 指针指向待写入的数组的地址
// Return:
// Author: Huangzhigang 2014-0410
*******************************************************************************/
void Ts12_Init()
{
I2C_Int();
I2C_Start();
I2C_SendByte(0xF0); // Device Addr + Write (operation)
I2C_SendByte(0x09);
I2C_SendByte(0x0F);
I2C_Stop();
TS12_InitAgain();
I2C_Start();
I2C_SendByte(0xF0); // Device Addr + Write (operation)
I2C_SendByte(0x09);
I2C_SendByte(0x07);
I2C_Stop();
}
/*******************************************************************************
// Function: Ts12_ReadKey
// Description: 模拟I2C通信 读取8字节数据 从0x10~0x12
// Param: pReadData: 指针指向保存数据的数组
// Return:
// Author: Huangzhigang 2014-0410
*******************************************************************************/
void Ts12_ReadKey()
{
static uint8_t KeyValue1;
static uint8_t KeyValue2;
static uint8_t KeyValue3;
I2C_Start();
I2C_SendByte(0xF0); // Device Addr + Write (operation)
I2C_SendByte(0x10);
// I2C_Stop(); //此步骤需要确认 -》可加可不加
I2C_Start();
I2C_SendByte(0xF1); // Device Addr + Read (operation)
KeyValue1 = I2C_RecvByte();
I2C_SendACK(0); //DIO低电平 表示ACK
KeyByte = KEY_EVENT_NULL_CLICK; //每次都清楚按键
if(((KeyValue1 >> 6) & 0x03 ) >= KeyStandard) //i-Sense
{
KeyByte += KEY_EVENT_SMART_CLICK;
}
if(((KeyValue1 >> 4) & 0x03) >= KeyStandard) //Crisper
{
KeyByte += KEY_EVENT_CRISPER_CLICK;
}
if(((KeyValue1 >> 2) & 0x03) >= KeyStandard) //MODE
{
KeyByte += KEY_EVENT_MODE_CLICK;
}
if((KeyValue1) & 0x03 >= KeyStandard) //DOWN
{
KeyByte += KEY_EVENT_DOWN_CLICK;
}
KeyValue2 = I2C_RecvByte();
I2C_SendACK(0); //DIO低电平 表示ACK
if(((KeyValue2 >> 6) & 0x03) >= KeyStandard) //Auto Defrost
{
KeyByte += KEY_EVENT_AD_CLICK;
}
if(((KeyValue2 >> 4) & 0x03) >= KeyStandard) //UP
{
KeyByte += KEY_EVENT_ICE_CLICK;
}
if(((KeyValue2 >> 2) & 0x03) >= KeyStandard) //Fast ICE
{
KeyByte += KEY_EVENT_UP_CLICK;
}
if((KeyValue2 & 0x03) >= KeyStandard) //Energy Saving
{
KeyByte += KEY_EVENT_SAVE_CLICK;
}
KeyValue3 = I2C_RecvByte();
I2C_SendACK(1);
if((KeyValue3 & 0x03) >= KeyStandard) //Manual Defrost
{
KeyByte += KEY_EVENT_MD_CLICK;
}
I2C_Stop();
}
触摸按键--模拟I2C驱动TS12芯片,布布扣,bubuko.com
原文地址:http://blog.csdn.net/a656343072/article/details/38704489