51 microcontroller uses the high-impedance state of the I/O port to realize touch control of LED lights

51 microcontroller uses the high-impedance state of the I/O port to realize touch control of LED lights

1 Overview

This article introduces an experiment that uses the high-impedance state of the I/O port to realize a touch-controlled LED light. In this experiment, the hand touches the P3.7 pin and changes the level signal to control the light on and off.

2. Experimental process

2.1.Experimental materials

name model quantity
Microcontroller STC12C2052 1
LED lanterns none 1
crystal oscillator 12MHZ 1
capacitance 30pf 2
resistance 100 ohms 1
Breadboard none 1
Connecting line none 5

2.2. Circuit principle

Connect the light of the P3.7 interface to the P1.7 interface, and connect the others according to the circuit diagram.
Insert image description here

Insert image description here

2.3. Experimental procedures

/*************************************************************
* 程序名: IO口高阻模式实现触控控制LED灯
* 编写人: bruce
* 硬件支持:STC12C2052
* 日  期: 2023-11-23
* 修改日志: 
/*************************************************************/

#include<STC12C2052AD.H>

// 定义P1.3 和 P1.2接口
sbit LED1 = P1^7;
sbit KEY = P3^7;


void main(void){
    
    
	//设置P1接口为准双口模式
	P1M0 = 0X00;
	P1M1 = 0X00;
	//设置P3.7接口为高阻模式
	P3M0 = 0X80;
	P3M1 = 0X00;
	// 定义接口高电平
	LED1 = 1;

	while(1){
    
    
		LED1 = ~KEY;
	}

}

The light will turn on when you put your finger on the P3.7 interface, and will go off when you leave it. But in actual use, the light turns on before the finger touches the P3.7 interface. This is caused by interference. This is optimized below.

  • First, set P3.7 to quasi-dual port + delay to prevent the low-level control light from turning on without touching P3.7. When the hand starts to touch P3.7, the interface is in a quasi-dual port, and has an output function that can pull the interference current to GND, eliminating part of the interference.
  • When setting P3.7 to a high-impedance state, add a delay to eliminate some interference again.
  • After optimization, it is much more stable than before. The LED light will light up only when your finger touches the P3.7 and P1.0 pins.
/*************************************************************
* 程序名: IO口高阻模式实现触控控制LED灯
* 编写人: bruce
* 硬件支持:STC12C2052
* 日  期: 2023-11-23
* 修改日志: 
/*************************************************************/

#include<STC12C2052AD.H>

// 定义P1.3 和 P1.2接口
sbit LED1 = P1^7;
sbit KEY = P3^7;

/*********************************************************************************************
函数名:毫秒级CPU延时函数
调  用:DELAY_MS (?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
/*********************************************************************************************/
void DELAY_MS (unsigned int a){
    
    
	unsigned int i;
	while( --a != 0){
    
    
		for(i = 0; i < 600; i++);
	}
}

void main(void){
    
    
	//设置P1接口为准双口模式
	P1M0 = 0X00;
	P1M1 = 0X00;
	//设置P3.7接口为高阻模式
	P3M0 = 0X80;
	P3M1 = 0X00;
	//初始化高低电平
	LED1 = 1;
	KEY = 0;

	while(1){
    
    
		//当手靠近还未触摸到P3.7引脚,感应到微电流后就会改变状态使控制灯的开关不稳定。
		//设置为准双口,+ 延迟 过滤微电流影响触摸不稳定现象。
		P3M0 = 0X00;
		DELAY_MS(2);
		P3M0 = 0X80;
		DELAY_MS(2);
		LED1 = ~KEY;
	}

}
Touch light effect

Insert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/134585185