stc15f104 realizes touch switch

Publisher:chaxue1987Latest update time:2018-08-04 Source: eefocusKeywords:stc15f104 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I want to make a small controller that can be turned on with a touch and turned off with a delay


Using an ordinary touch switch seems ugly, and the switch is not convenient to fix. It is not a good idea to put the control panel outside, right?


I want to use a touch switch, but I haven't found a suitable one after searching online for a long time. There is a capacitive sensing switch in the official STC document.


It needs to occupy 2 IO, one to generate PWM signal and one for AD judgment. In addition, it also needs 2 resistors, 2 diodes and 1 capacitor.


It felt a bit complicated, so I tried it and finally got it done with just one IO port, one resistor and one capacitor.


The idea is this:


A 2M resistor connects the power supply and IO port (I used p3.3)


A 33p capacitor is connected between the IO port and ground.


Connect a wire from the IO port to a metal sheet or something for touch.


Set p3.3 to 0 to discharge the capacitor first, then set it to high impedance state. At this time, the power supply will charge the capacitor through the resistor.


The time constant 2M*33p is about 66μs. The timer is used to count the time when the io port becomes high level.


If you don't use a timer, you may be affected by interruptions and there may be errors.


This time may need to be smoothed to avoid interference.


When the device is turned on, no one touches it, so record the charging time as a benchmark.


If someone touches it, the charging time will be longer, so check the charging time regularly.


If you find that the charging time is much longer than the benchmark charging time (I used twice the benchmark time for comparison), you can assume that someone has touched it.




It has been completed and put into use, and its reliability and anti-interference capabilities can meet the requirements.


void mw(){//main working loop


unsigned int i;

static unsigned int cdsj=0,bzz; //Charging time, standard value

static unsigned char csh=10; //initialize count



T2H=T2L=0; //Counter 2 count value is initialized to 0

P3M1 |=0x8; //p3.3 is set to open drain state

P3M0 |= 0x8; //

P3_3=0; //p3.3 output cleared to 0

delay(100);

P3M1 |=0x8; //p3.3 is set to high impedance state

P3M0 &=0xf7;//

AUXR |= 0x10; //Start counter 2

P3_3=1; //p3.3 output is 1, input state

IE1=0; //Clear the status of external interrupt 1

EX1=1; //Open external interrupt 1



while(EX1); //External interrupt is not completed and waiting in loop



i=cdsj>>3;

cdsj=cdsj-i+T2L; //smoothing

if(csh){

csh--;

if(csh==0){//The initial 10 counts are completed, and the current value is taken as the standard value

bzz=i+i;

};

return;

};

if(i>bzz){

This condition is met to indicate that the switch is touched

};

}


Keywords:stc15f104 Reference address:stc15f104 realizes touch switch

Previous article:STM32 PCB touch button (RC detection method)
Next article:Special registers in ARM

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号