触摸键盘驱动(STM32,TTP229-B)

一、前期准备
单片机:STM32F407VET6
开发环境:MDK5.27
库函数:STM32Cube FW_F4 V1.25.0
触摸键模块:淘宝有售
在这里插入图片描述

二、实验效果
在这里插入图片描述

三、驱动原理
参考TTP229-BSF数据手册。注意功能选择可能对程序的适配有直接的影响。
驱动参考:触摸按键参考博客,在此感谢这位博主,给了我很大的启发

需要完整工程或者有问题的请加QQ:1002521871

四、驱动代码
touch.h

#ifndef __TOUCH_H__
#define	__TOUCH_H__
#include "stm32f4xx_hal.h"
#include "gpio.h"
#include "delay.h"

#define	SDA_IN()	{GPIOC->MODER &= ~(3 << (10*2)); GPIOC->MODER |= (0 << (10*2));}
#define	SDA_OUT()	{GPIOC->MODER &= ~(3 << (10*2)); GPIOC->MODER |= (1 << (10*2));}

#define	TTP_SCL		PCout(11)
#define	TTP_SDO		PCout(10)
#define	TTP_SDI		PCin(10)

extern uint16_t Touch_Read(void);
extern void Touch_Configuration(void);
extern uint8_t Touch_KeyScan(void);
#endif

touch.c

#include "touch.h"

void Touch_Configuration(void)
{
	SDA_OUT();
	TTP_SDO = 1;
	TTP_SCL = 1;
	Touch_Read();
}


uint16_t Touch_Read(void)
{
	uint8_t i = 0; 
	uint16_t real_Value = 0;
	SDA_OUT();
	TTP_SDO = 0;
	DelayUs(93);
	TTP_SDO = 1;
	DelayUs(20);
	SDA_IN();
	//DelayUs(20);
	for (i = 0; i < 16; i ++)
	{
		TTP_SCL = 1;
		DelayUs(100);
		TTP_SCL = 0;
		
		if (TTP_SDI == 1)
		{
			real_Value |= (1 << i);
		}
		
		DelayUs(100);
	}
	
	HAL_Delay(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;
	}
	
	if ((Current & 0x0002) && !(Previous & 0x0002))
	{
		key = 2;
	}
	
	if ((Current & 0x0004) && !(Previous & 0x0004))
	{
		key = 3;
	}
	
	if ((Current & 0x0008) && !(Previous & 0x0008))
	{
		key = 4;
	}
	
	if ((Current & 0x0010) && !(Previous & 0x0010))
	{
		key = 5;
	}
	
	if ((Current & 0x0020) && !(Previous & 0x0020))
	{
		key = 6;
	}
	
	if ((Current & 0x0040) && !(Previous & 0x0040))
	{
		key = 7;
	}
	
	if ((Current & 0x0080) && !(Previous & 0x0080))
	{
		key = 8;
	}
	
	if ((Current & 0x0100) && !(Previous & 0x0100))
	{
		key = 9;
	}
	
	if ((Current & 0x0200) && !(Previous & 0x0200))
	{
		key = 10;
	}
	
	if ((Current & 0x0400) && !(Previous & 0x0400))
	{
		key = 11;
	}
	
	if ((Current & 0x0800) && !(Previous & 0x0800))
	{
		key = 12;
	}
	
	if ((Current & 0x1000) && !(Previous & 0x1000))
	{
		key = 13;
	}
	
	if ((Current & 0x2000) && !(Previous & 0x2000))
	{
		key = 14;
	}
	
	if ((Current & 0x4000) && !(Previous & 0x4000))
	{
		key = 15;
	}
	
	if ((Current & 0x8000) && !(Previous & 0x8000))
	{
		key = 16;
	}
	
	Previous = Current;
	
	return key;
}

由于作者能力有限,有不妥之处欢迎指正,邮箱alex_hua@foxmail.com

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值