• 方案介紹
    • 一、前言
    • 二、資料獲取
    • 三、設備使用
    • 四、代碼編寫
    • 五、參考
  • 附件下載
  • 推薦器件
  • 相關推薦
申請入駐 產(chǎn)業(yè)圖譜

嵌入式外設-4X4矩陣鍵盤(TTP229)驅動代碼編寫

2024/07/23
5420
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論

聯(lián)系方式.docx

共1個文件

一、前言

矩陣鍵盤 TTP229 是一種常見的電容觸摸式鍵盤芯片,通常用于電子設備中的鍵盤輸入控制。以下是關于 TTP229 的一些介紹:

1. **工作原理**:
- TTP229 是一種電容觸摸式鍵盤控制芯片,通過檢測觸摸電容變化來實現(xiàn)按鍵檢測。每個按鍵通常設置為一個電容觸摸面,當用戶觸摸按鍵時,會改變對應電容的值,TTP229 通過檢測這些值的變化來確定哪個按鍵被按下。

2. **特點**:
- **多通道**:TTP229 可以支持多達 16 個觸摸通道,因此適合設計多按鍵的鍵盤或面板。
- **低功耗**:通常情況下,TTP229 在工作時的功耗較低,適合于需要長時間運行的電池供電設備。
- **簡單接口**:通常通過串行接口(如 I2CSPI)與主控芯片通信,易于集成到各種嵌入式系統(tǒng)中。

3. **應用**:
- **電子產(chǎn)品**:常見用于各種小型家電、消費電子產(chǎn)品的按鍵控制。
- **工業(yè)設備**:用于控制面板或設備操作界面的按鍵輸入。
- **教育和DIY項目**:因其易用性和可靠性,經(jīng)常被用于教育實驗和DIY電子項目中。

4. **使用注意事項**:
- **環(huán)境干擾**:電容觸摸技術受到環(huán)境干擾的影響較大,應盡量避免在強電磁場或靜電干擾較多的環(huán)境中使用。
- **地線連接**:確保設備的地線連接良好,以保證按鍵觸摸的準確性和穩(wěn)定性。

總體來說,TTP229 是一種常用的電容觸摸式鍵盤芯片,適合于需要簡單而有效的觸摸按鍵輸入控制的各種應用場景。

二、資料獲取

關注微信公眾號--星之援工作室 發(fā)送關鍵字(TTP229

網(wǎng)上開源的項目資料,可自行移植
????452c0cf75b1d4e4895194df8a5022c34.png

三、設備使用

接線

PB6 -> SCL
PB7 -> SDI

四、代碼編寫

?main.c

實現(xiàn)函數(shù)調用



#include "sys.h"
#include "delay.h"
#include "usart.h" 

#include "usmart.h"
#include "string.h"		
#include "rtc.h"

#include "malloc.h" 
#include <stdio.h>

#include "key.h"
#include "led.h"
#include "common.h"
#include "TTP229.h"

struct stuIoT stuAliOSIoT;


u8 USART1_TX_BUF[USART_REC_LEN]; 							//串口1,發(fā)送緩存區(qū)
__align(4) u8 dtbuf[50];   									//打印緩存器


 int main(void)
 {
	u8 t=0; 
	u8 key=0XFF;
	int times = 0; 

	u16 nSecond = 0;
	unsigned int NowHour,NowMinute,NowSecond;
	 
	u8 dtbuf[50];   									//打印緩存器

	memset(dtbuf,'?',50);

	//初始化
	//延時函數(shù)初始化 	 
	delay_init();
		  
	uart_init(115200);	 		//串口1:Debug,初始化為115200
		 
	USART_RX_STA=0;  
	memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF)); 	
	 
	LED_Init();
    
  Touch_Configuration();
	
	while(RTC_Init())		//RTC初始化	,一定要初始化成功
	{ 
		printf("RTC ERROR!   nr");	
		delay_ms(800);
		printf("RTC Trying...nr");	
	}
	
	USART1_Send_String("System Init OK 20211201 rn");	
	
	//主循環(huán)	
	while(1) 
	{	
		times++;

		if(t!=calendar.sec)   
		{    
			t=calendar.sec;    
			
			sprintf((char *)dtbuf,"%02d:%02d:%02d rn",calendar.hour,calendar.min,calendar.sec);

			nSecond++;
		}

		key = Touch_KeyScan();        
        
        if(key!=0)
        {
            sprintf((char *)dtbuf,"TouchKey No.:%02d rn",key);
            USART1_Send_String((char *)dtbuf);
            //delay_ms(500);
        }
	}	
}

TTP229.c

#include "TTP229.h"
#include "delay.h"

void Touch_Configuration(void)
{
		GPIO_InitTypeDef GPIO_InitStructure;
		RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOB, ENABLE );	//使能GPIOB時鐘
	 
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;  //端口配置
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;       //推挽輸出
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       //50Mhz速度
		GPIO_Init(GPIOB, &GPIO_InitStructure);
    
		SDA_OUT();
		TTP_SDO = 0;
		TTP_SCL = 0;
		Touch_Read();
}

uint16_t Touch_Read2(void)
{
	uint8_t i = 0; 
	uint16_t real_Value = 0;
	
	SDA_OUT();
	TTP_SDO = 1;
	delay_us(100);
	TTP_SDO = 0;
	delay_us(20);
	
    SDA_IN();

	for (i = 0; i < 16; i ++)
	{
		TTP_SCL = 1;
		delay_us(200);
		TTP_SCL = 0;        
		
		if (TTP_SDI == 1)
		{
			real_Value |= (1 << i);
		}   
        
        delay_us(200);
	}
	
	delay_ms(2);
	
	return real_Value;
}


uint16_t Touch_Read(void)
{
	uint8_t i = 0; 
	uint16_t real_Value = 0;
	
    SDA_OUT();
	TTP_SDO = 1;
	delay_us(100);
	TTP_SDO = 0;
	delay_us(20);
	
    SDA_IN();

	for (i = 0; i < 16; i ++)
	{
		TTP_SCL = 1;
		delay_us(100);
		TTP_SCL = 0;
        delay_us(1);
		
		if (TTP_SDI == 1)
		{
			real_Value |= (1 << i);
		}   
        
	}
	
	delay_ms(2);
	
	return real_Value;
}

uint16_t Previous = 0;
uint16_t Current = 0;

uint8_t Touch_KeyScan(void)
{
	uint8_t key = 0;
	
	Current = Touch_Read();
	
	if ((Current & 0x0001) && !(Previous & 0x0001))
	{
		key = 1;
	}	
	else if ((Current & 0x0002) && !(Previous & 0x0002))
	{
		key = 2;
	}	
	else if ((Current & 0x0004) && !(Previous & 0x0004))
	{
		key = 3;
	}	
	else if ((Current & 0x0008) && !(Previous & 0x0008))
	{
		key = 4;
	}	
	else if ((Current & 0x0010) && !(Previous & 0x0010))
	{
		key = 5;
	}	
	else if ((Current & 0x0020) && !(Previous & 0x0020))
	{
		key = 6;
	}	
	else if ((Current & 0x0040) && !(Previous & 0x0040))
	{
		key = 7;
	}	
	else if ((Current & 0x0080) && !(Previous & 0x0080))
	{
		key = 8;
	}	
	else if ((Current & 0x0100) && !(Previous & 0x0100))
	{
		key = 9;
	}	
	else if ((Current & 0x0200) && !(Previous & 0x0200))
	{
		key = 10;
	}	
	else if ((Current & 0x0400) && !(Previous & 0x0400))
	{
		key = 11;
	}	
	else if ((Current & 0x0800) && !(Previous & 0x0800))
	{
		key = 12;
	}	
	else if ((Current & 0x1000) && !(Previous & 0x1000))
	{
		key = 13;
	}	
	else if ((Current & 0x2000) && !(Previous & 0x2000))
	{
		key = 14;
	}	
	else if ((Current & 0x4000) && !(Previous & 0x4000))
	{
		key = 15;
	}	
	else if ((Current & 0x8000) && !(Previous & 0x8000))
	{
		key = 16;
	}
	
	Previous = Current;
	
	return key;
}

TTP229.h

#ifndef __TTP229_H__
#define	__TTP229_H__

#include "sys.h"

//#define	SDA_IN()	{GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=8<<8;}
//#define	SDA_OUT()	{GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=3<<8;}

#define	SDA_IN()	{GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=0X80000000;}
#define	SDA_OUT()	{GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=0X30000000;}

#define	TTP_SCL		PBout(6)
#define	TTP_SDO		PBout(7)
#define	TTP_SDI		PBin(7)

extern uint16_t Touch_Read(void);
extern uint16_t Touch_Read2(void);
extern void Touch_Configuration(void);
extern uint8_t Touch_KeyScan(void);

#endif

五、參考

物聯(lián)網(wǎng)畢設 -- 智能門禁系統(tǒng)(STM32+人臉+RFID+密碼+APP+WIFI)icon-default.png?t=N7T8https://blog.csdn.net/herui_2/article/details/139135120?spm=1001.2014.3001.5501


聯(lián)系方式 微信號:13648103287

  • 聯(lián)系方式.docx
    下載

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風險等級 參考價格 更多信息
MC9S12A128CPVE 1 Rochester Electronics LLC 16-BIT, FLASH, 25MHz, MICROCONTROLLER, PQFP112, LEAD FREE, LQFP-112
$22.47 查看
MCF5282CVM66 1 Freescale Semiconductor MCF5282 V2CORE 512KFLASH
$40.57 查看
STM32F405RGT6 1 STMicroelectronics High-performance foundation line, Arm Cortex-M4 core with DSP and FPU, 1 Mbyte of Flash memory, 168 MHz CPU, ART Accelerator

ECAD模型

下載ECAD模型
$16.63 查看

相關推薦

方案定制

去合作
方案開發(fā)定制化,2000+方案商即時響應!

方案定制,程序設計方案、單片機程序設計與講解、APP定制開發(fā)。本公眾號致力于向讀者傳遞關于程序設計和開發(fā)的相關知識,并分享一些關于軟件開發(fā)的最佳實踐。如果您有什么問題或建議,請隨時聯(lián)系我們。我們將竭誠為您服務