Cov txheej txheem:

Capacitive Touch With PIC16F886 Microcontroller: 3 Kauj Ruam
Capacitive Touch With PIC16F886 Microcontroller: 3 Kauj Ruam

Video: Capacitive Touch With PIC16F886 Microcontroller: 3 Kauj Ruam

Video: Capacitive Touch With PIC16F886 Microcontroller: 3 Kauj Ruam
Video: How to Implement Touch on a PIC® Microcontroller 2024, Hlis ntuj nqeg
Anonim
Capacitive Touch Nrog PIC16F886 Microcontroller
Capacitive Touch Nrog PIC16F886 Microcontroller

Hauv qhov kev qhia no peb yuav ua li cas koj tuaj yeem siv PIC16F886 microcontroller txhawm rau txheeb xyuas qhov sib txawv ntawm lub peev xwm, qhov no tuaj yeem siv tom qab los qhia yog tias kov lub ncoo raug nias. Nws yog qhov zoo kom paub nrog pic microcontrollers ua ntej ua qhov haujlwm no.

Kauj Ruam 1: Txhim Kho Koj Lub Tsev

Txhim Kho Koj Txoj Cai
Txhim Kho Koj Txoj Cai

Ua ntej, cia peb pib los ntawm kev txuas lub Circuit Court raws li cov txheej txheem saum toj no. Txhawm rau ua kom kov tau koj tuaj yeem quav aluminium ntawv ci rau hauv ib lub xwmfab thiab kab xev ntawm xaim. Koj tuaj yeem sim ib puag ncig nrog cov txiaj ntsig sib txawv rau 100k resistor, Kuv pom 100k ua haujlwm zoo rau kuv.

RC4 tus pin tau siv los pib them/tshem tawm lub peev xwm los ntsuas. C12IN0 txuas nrog rau - sab ntawm kev sib piv sab hauv thiab C1IN tus pin txuas nrog + sab ntawm tib tus neeg sib piv. Lub microcontroller pom lub peev xwm raws li them tag nrho thaum C12IN0 qhov hluav taws xob nce siab dua C1IN qhov hluav taws xob. Qhov sib faib hluav taws xob tiv thaiv ua kom ntseeg tau C1IN ze rau 5 volts.

Txij li kov lub ncoo nyob ntawm qhov muaj lub peev xwm tseem ceeb ntawm koj thiab thaj av hauv av muaj qhov ua tau tias lub roj teeb yuav tsis ua haujlwm.

Kauj ruam 2: Header File

Cov ntaub ntawv Header
Cov ntaub ntawv Header

Ua tiav nrog txhua qhov kev sib txuas? Zoo, peb yuav txuas ntxiv nrog cov ntawv header. Peb yuav siv XC8 tus neeg sau thiab raws li lub npe qhia koj tam sim no yuav tsum tsim cov ntaub ntawv header tshiab hauv koj qhov project thiab luam tawm-muab cov cai hauv qab no. Koj tuaj yeem ua tau zoo li theej-muab nws tso rau saum koj lub ntsiab cai yam tsis muaj cov ntawv header.

#define CALIBRATION_SAMPLE 20 #txhais TOUCH_SAMPLE 10 #define DISCHARGE_TIME 5

int suav;

int calibrationValue, maxCalibrationValue, minCalibrationValue;

int getChargeTime () {

int timerCount = 0; int overflowCount = 0; // tawm lub peev xwm los ntsuas RC4 = 0; _delay_ms (DISCHARGE_TIME); // muab ncua ncua kom txaus (yuav luag tag nrho) tso tawm "lub capacitor" // tshem lub cim timer dhau los chij T0IF = 0; // tos rau timer kom dhau, pib suav los ntawm 0 thaum (! T0IF); T0IF = 0; // pib them lub peev xwm los ntsuas RC4 = 1; // tos rau lub peev xwm kom them mus txog qhov ntsuas hluav taws xob thaum (C1OUT) {timerCount = TMR0; yog (T0IF) {overflowCount ++; T0IF = 0; }} suav = (256 * overflowCount) + timerCount; // pib dua timerCount timerCount = 0; overflowCount = 0; suav rov qab; }

int isTouching (ua siab ntev) {

// nruab nrab ntawm ntau qhov piv txwv ob npaug nruab nrab = 0; rau (int kuv = 0; kuv calibrationValue+kam rau ua) nruab nrab ++; } nruab nrab /= TOUCH_SAMPLE; // nruab nrab yuav yog tus lej nruab nrab ntawm 0 thiab 1 yog (nruab nrab> 0.2) rov qab 1; rov 0; }

tsis muaj calibrate () {

int nruab nrab = 0; int cov qauv [CALIBRATION_SAMPLE]; // tau txais tus nqi nruab nrab rau (int i = 0; i <CALIBRATION_SAMPLE; i ++) {kuaj = getChargeTime (); nruab nrab += kuaj ; } nruab nrab /= CALIBRATION_SAMPLE; calibrationValue = nruab nrab; // tau txais max/min qhov tseem ceeb maxCalibrationValue = kuaj [0]; minCalibrationValue = kuaj [0]; rau (int i = 0; i maxCalibrationValue) maxCalibrationValue = kuaj ; yog (kuaj <minCalibrationValue) minCalibrationValue = kuaj ; }}

void setupCapacitiveTouch () {

// teeb tsa tus nqi/tawm tus pin raws li qhov tso tawm, hauv qhov no nws yog RC4 TRISCbits. TRISC4 = 0; // teeb tsa timer0 T0CS = 0; PSA = 1; // teeb tsa kev sib piv C1CH0 = 0; C1CH1 = 0; C1R = 0; C1ON = 1; C1POL = 0; // tshem tawm qhov suav suav suav = 0; // clearing calibration values calibrationValue = 0; maxCalibrationValue = 0; minCalibrationValue = 0; // khiav calibration thaum pib calibrate (); }

Kauj Ruam 3: Sau Txoj Cai Tseem Ceeb

Pib nrog cov cai tseem ceeb, koj yuav tsum suav nrog cov ntaub ntawv header tsim hauv qib dhau los. Cov cai hauv qab no yog piv txwv ntawm qhov koj tuaj yeem siv isTouching ua haujlwm li cas. Hauv kuv qhov xwm txheej kuv tau muab lub taub hau lub npe capacitiveTouch.h.

#suav nrog

#suav nrog "capacitiveTouch.h"

// qhov sib txawv no qhia yog tias lub pob yog lossis tseem tsis tau nias

int lastState = 0;

void main () {

// teeb RC5 raws li tso tawm TRISCbits. TRISC5 = 0; // koj yuav tsum tau hu txoj haujlwm no thaum pib ntawm qhov program setupCapacitiveTouch (); _delay_ms (1000); // ntsuas tom qab koj teeb tsa qhov tseeb calibrate (); thaum (1) {// txheeb xyuas yog tias lub pob tau raug nias yog (isTouching (15) && lastState == 0) {yog (RC5) RC5 = 0; lwm RC5 = 1; lastState = 1; } // xyuas yog tias khawm tau tso tawm lwm qhov yog (lastState == 1 &&! isTouching (15)) lastState = 0; _delay_ms (20); }}

ntsuas:

Thaum txoj haujlwm no hu ua qhov hloov pauv calibrationValue, maxCalibrationValue thiab minCalibrationValue yuav raug hloov kho. calibrationValue yog siv los ntawm isTouching muaj nuj nqi. Nco ntsoov tias lub ncoo kov yuav tsum nyob ib leeg thaum lub sijhawm ntsuas.

setupCapacitiveTouch:

Yuav tsum tau hu ua thaum pib ntawm koj qhov haujlwm. Nws teeb tsa cov khoom tsim nyog siv los ntawm lwm cov haujlwm. Nws tseem ua haujlwm hu xov tooj. Txawm li cas los kuv tau txais cov txiaj ntsig zoo dua los ntawm kev tos ib pliag thiab khiav qhov ntsuas dua ib leeg cais.

yog kov:

Qhov haujlwm no rov qab 1 yog tias nws pom qhov nce ntawm lub peev xwm ntawm C12IN0 thiab rov qab 0 yog tias lub peev xwm nyob ze rau qhov nws tau ua thaum ntsuas. Yooj yim hais, yog tias ib tus neeg kov lub ncoo lub isTouching muaj nuj nqi yuav rov qab los 1. Lub luag haujlwm tseem xav tau qhov ntsuas ntsuas raws li kev nkag, qhov no yog qhov ua siab ntev rau nws kom ua rau. Qhov siab dua tus nqi ua rau tsis nkag siab nws tau txais. Hauv kuv teeb kuv pom 15 ua haujlwm tau zoo, tab sis vim qhov no nyob ntawm ocsillator zaus thiab ntau npaum li cas lub peev xwm ntxiv thaum koj nias nws koj yuav tsum sim ib puag ncig nrog tus nqi no kom txog thaum koj pom qee yam uas ua haujlwm rau koj.

getChargeTime:

Thaum koj xav paub ntev npaum li cas nws yuav siv sijhawm los them lub peev xwm rau CVREF qhov hluav taws xob, qhov haujlwm no yuav ntsuas nws thiab rov ua tus lej. Txhawm rau kom tau sijhawm nyob hauv vib nas this koj siv cov mis no: (4 * getChargeTime) / oscillatorFrequency = chargeTimeInSeconds Cov mis no tseem tuaj yeem siv tau qhov ua siab ntev nkag los ntawm isTouching muaj nuj nqi rau vib nas this.

Pom zoo: