一、前期准备
单片机: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