Cov txheej txheem:

Arduino Sijhawm Txoj Kev Nrog Millis (): 4 Cov Kauj Ruam
Arduino Sijhawm Txoj Kev Nrog Millis (): 4 Cov Kauj Ruam

Video: Arduino Sijhawm Txoj Kev Nrog Millis (): 4 Cov Kauj Ruam

Video: Arduino Sijhawm Txoj Kev Nrog Millis (): 4 Cov Kauj Ruam
Video: 🤞8 YAM COV NEEG TXAWJ NTSE ZAM TSIS UA YOG DAB TSI?🚫 2024, Hlis ntuj nqeg
Anonim
Arduino Sijhawm Txoj Kev Nrog Millis ()
Arduino Sijhawm Txoj Kev Nrog Millis ()

Hauv kab lus no peb qhia cov millis (); muaj nuj nqi thiab muab nws siv los tsim ntau yam piv txwv piv txwv.

Millis? Tsis muaj dab tsi ua nrog di ncauj-syncers … vam tias koj tau lees paub milli ua tus lej ua ntej rau ib txhiab uas yog sib npaug chav ntsuas ntawm 0.001 (lossis kaum rau lub zog ntawm qhov tsis zoo 3).

Qhov txaus siab peb cov kab ke Arduino yuav suav tus naj npawb ntawm milliseconds (ntau txhiab tus thib ob) los ntawm qhov pib ntawm kev kos duab kom txog thaum suav mus txog tus lej siab kawg uas muaj peev xwm khaws cia hauv hom sib txawv tsis tau kos npe ntev (32-ntsis [plaub byte] integer -uas yog los ntawm xoom mus rau (2^32) -1. (2^32) -1, lossis 4294967295 milliseconds hloov mus rau 49.71027-hnub txawv.

Lub txee rov pib dua thaum Arduino rov pib dua, nws mus txog tus nqi siab tshaj lossis daim duab tshiab tau muab tso rau. Txhawm rau kom tau txais tus nqi ntawm lub txee ntawm ib qho kev sib tshuam tshwj xeeb, tsuas yog hu rau txoj haujlwm - piv txwv li:

pib = millis ();

Qhov pib yog qhov tsis sib xws ntev tsis tau kos npe. Nov yog qhov piv txwv yooj yim heev los qhia koj millis () hauv kev nqis tes ua:

/ * millis () ua qauv qhia */

tsis kos npe ntev pib, ua tiav, dhau mus;

void teeb tsa ()

{Serial.begin (9600); }

void lub voj ()

{Serial.println ("Pib …"); pib = millis (); ncua (1000); tiav = millis (); Serial.println ("Tiav lawm"); elapsed = tiav-pib; Serial.print (dhau mus); Serial.println ("milliseconds elapsed"); Serial.println (); ncua (500); }

Cov duab kos khaws cov millis suav tam sim no hauv kev pib, tom qab ntawd tos ib pliag, tom qab ntawd khaws cov nqi ntawm millis dua thaum ua tiav. Thaum kawg nws suav lub sijhawm dhau los ntawm kev ncua. Hauv cov vijtsam hauv qab no pov tseg ntawm tus saib xov tooj, koj tuaj yeem pom tias lub sijhawm tsis tas yuav raws nraim 1000 milliseconds, raws li qhia hauv daim duab.

Kauj ruam 1:

Duab
Duab

Txhawm rau muab nws yooj yim, millis ua haujlwm siv lub txee sab hauv hauv ATmega microcontroller ntawm lub plawv ntawm koj Arduino. Lub txee no nce ntxiv txhua lub voj voog moos - uas tshwm sim (hauv tus qauv Arduino thiab sib xws) ntawm lub moos ceev ntawm 16 Mhz. Qhov nrawm no yog tswj los ntawm cov siv lead ua ntawm Arduino pawg thawj coj saib (cov khoom siv nyiaj nrog T16.000 stamped rau nws).

Kauj ruam 2:

Duab
Duab

Crystal qhov tseeb tuaj yeem sib txawv nyob ntawm qhov ntsuas kub sab nraud, thiab kev ua siab ntev ntawm cov siv lead ua nws tus kheej. Qhov no nyeg yuav cuam tshuam qhov tseeb ntawm koj cov txiaj ntsig millis. Cov kev paub dhau los tau tshaj tawm qhov kev ncua hauv lub sijhawm raug tuaj yeem nyob ib puag ncig peb lossis plaub vib nas this rau nees nkaum plaub teev sijhawm.

Yog tias koj tab tom siv lub rooj tsavxwm lossis koj tus kheej cov ntawv uas tab tom siv cov nplaim hluavtaws resonator siv cov siv lead ua, nco ntsoov tias lawv tsis yog qhov tseeb thiab yuav qhia txog qhov muaj peev xwm ua kom siab dua. Yog tias koj xav tau qib siab ntau ntawm lub sijhawm raug, xav txog lub sijhawm IC tshwj xeeb xws li Maxim DS3231.

Tam sim no peb tuaj yeem siv cov millis rau ntau lub sijhawm ua haujlwm. Raws li tau qhia hauv qhov piv txwv yav dhau los kos duab, peb tuaj yeem suav lub sijhawm dhau mus. Txhawm rau coj lub tswv yim no mus tom ntej, cia ua lub moos nres yooj yim. Ua li ntawd tuaj yeem yooj yim lossis nyuaj raws li qhov tsim nyog, tab sis rau qhov xwm txheej no peb yuav hloov mus rau qhov yooj yim.

Ntawm qhov kho vajtse pom, peb yuav muaj ob lub khawm-Pib thiab Nres-nrog 10k ohm rub-down resistors txuas nrog digital pins 2 thiab 3 feem. Thaum tus neeg siv nias pib kos duab yuav sau tus nqi rau millis - tom qab ntawd tom qab nres nres, daim duab yuav rov nco dua tus nqi rau millis, suav thiab tso saib lub sijhawm dhau los. Tus neeg siv tuaj yeem nias pib rov ua cov txheej txheem, lossis nres rau cov ntaub ntawv hloov tshiab. Ntawm no yog daim duab:

/* Super-basic stopwatch siv millis (); */

tsis kos npe ntev pib, ua tiav, dhau mus;

void teeb tsa ()

{Serial.begin (9600); pinMode (2, INPUT); // pib khawm pinMode (3, INPUT); // nres khawm Serial.println ("Nias 1 rau Pib/pib dua, 2 rau lub sijhawm dhau los"); }

void displayResult ()

{ntab h, m, s, ms; tsis kos npe ntev dua; elapsed = tiav-pib; h = int (elapsed/3600000); dhau = elapsed%3600000; m = int (tshaj/60000); tshaj = tshaj%60000; s = int (tshaj/1000); ms = tshaj%1000; Serial.print ("Lub sij hawm dhau los:"); Serial.println (dhau mus); Serial.print ("Lub sijhawm dhau mus:"); Serial.print (h, 0); Serial.print ("h"); Serial.print (m, 0); Serial.print ("m"); Serial.print (s, 0); Serial.print ("s"); Serial.print (ms, 0); Serial.println ("ms"); Serial.println (); }

void lub voj ()

{yog (digitalRead (2) == HIGH) {pib = millis (); ncua (200); // rau debounce Serial.println ("Pib …"); } yog (digitalRead (3) == HIGH) {tiav = millis (); ncua (200); // rau debounce displayResult (); }}

Kev hu kom ncua () tau siv los nthuav tawm cov hloov pauv - cov no yog nyob ntawm yeem thiab lawv siv yuav nyob ntawm koj cov cuab yeej siv. Cov duab yog piv txwv ntawm daim duab kos duab saib ua ntej tso tawm - lub tswb moos tau pib, thiab tom qab ntawd khawm ob nias rau rau zaug nyob rau lub sijhawm.

Kauj ruam 3: Speedometer…

Image
Image

Yog tias koj muaj lub ntsuas hluav taws xob thaum pib thiab xaus ntawm qhov nrug deb, qhov nrawm tuaj yeem suav tau: nrawm = nrug ÷ sijhawm.

Koj kuj tseem tuaj yeem ua lub ntsuas cua nrawm rau lub log uas txav tau, piv txwv li tsheb kauj vab. Tam sim no peb tsis muaj lub tsheb kauj vab los cuam tshuam nrog, txawm li cas los xij peb tuaj yeem piav qhia tus txheej txheem kom ua li ntawd - nws yooj yim heev. (Kev tsis lees paub - ua li ntawm koj tus kheej txoj kev pheej hmoo thiab lwm yam.)

Ua ntej tshaj plaws, cia rov tshuaj xyuas cov lej uas tsim nyog. Koj yuav tsum paub qhov ncig ntawm lub log. Kho vajtse - koj yuav xav tau lub sensor. Piv txwv li - tus pas nrig hloov thiab hlau nplaum. Xav txog qhov hloov pauv mus rau qhov ib txwm qhib-khawm, thiab txuas ib txwm muaj nrog 10k ohm rub-down resistor.

Lwm tus tuaj yeem siv lub ntsuas qhov ntsuas qhov cuam tshuam-txhua tus rau lawv tus kheej). Nco qab los ntawm chav kawm lej, txhawm rau xam ib puag ncig - siv cov qauv: ib puag ncig = 2πr qhov twg r yog lub vojvoog ntawm lub voj voog.

Tam sim no koj muaj lub log ib puag ncig, tus nqi no tuaj yeem suav tias yog peb qhov 'nrug deb', thiab yog li qhov nrawm tuaj yeem suav los ntawm kev ntsuas lub sijhawm dhau los ntawm kev sib hloov tag nrho.

Koj lub ntsuas pa-ib zaug haum-yuav tsum ua tib txoj hauv kev ib yam li ib txwm qhib-khawm uas raug thawb txhua qhov kev sib hloov. Peb cov duab kos yuav ntsuas lub sijhawm dhau los ntawm txhua qhov mem tes los ntawm lub ntsuas.

Txhawm rau ua qhov no, peb tus piv txwv yuav muaj lub ntsuas hluav taws xob txuas nrog tus lej digital 2 - vim nws yuav ua rau cuam tshuam los laij lub nrawm. Cov duab kos yuav txwv tsis pub nthuav tawm qhov nrawm ntawm I2C-interface LCD module ib txwm muaj. I2C interface tau pom zoo vim qhov no xav tau tsuas yog 4 xov ntawm Arduino pawg thawj coj saib mus rau LCD - cov xov hlau tsawg dua qhov zoo dua.

Nov yog daim duab kos rau koj qhov kev thov:

/*Kev ntsuas nrawm siv millis (); */

#suav nrog "Wire.h" // rau I2C npav LCD

#include "LiquidCrystal_I2C.h" // rau I2C npav LCD module - https://bit.ly/m7K5wt LiquidCrystal_I2C lcd (0x27, 16, 2); // teeb tsa qhov chaw nyob LCD rau 0x27 rau 16 tus cim thiab 2 kab lus tso tawm

ntab pib, ua tiav;

ntab dhau sijhawm, sijhawm; ntab circMetric = 1.2; // log ncig ib tus txheeb ze rau qhov chaw sensor (hauv metres) ntab circImperial; // siv 1 kilometer = 0.621371192 mais ntab speedk, speedm; // tuav xam qhov nrawm nrawm hauv metric thiab imperial

void teeb tsa ()

{attachInterrupt (0, speedCalc, RISING); // cuam tshuam hu thaum sensors xa digital 2 siab (txhua lub log tig) pib = millis (); // teeb tsa LCD lcd.init (); // pib lub lcd lcd.backlight (); // tig rau LCD backlight lcd.clear (); lcd.println ("Hnav ib lub kaus mom hlau!"); ncua (3000); lcd.clear (); Serial.begin (115200); circImperial = circMetric*.62137; // hloov cov ntsuas mus rau huab tais rau MPH suav nrog}

void speedCalc ()

{elapsed = millis ()-pib; pib = millis (); speedk = (3600*circMetric)/dhau mus; // km/h speedm = (3600*circImperial)/dhau mus; // mais ib teev}

void lub voj ()

{lcd.setCursor (0, 0); lcd.print (int (speedk)); lcd.print ("km/h"); lcd.print (int (speedm)); lcd.print ("MPH"); lcd.setCursor (0, 1); lcd.print (int (dhau los))); lcd.print ("ms/rev"); ncua (1000); // kho rau tus kheej nyiam kom txo qis qhov muag}

Tsis muaj ntau qhov mus ntxiv - txhua zaus lub log ua tiav ib qho kev hloov pauv lub teeb liab los ntawm lub ntsuas cua yuav los ntawm qis mus rau siab - ua rau muaj kev cuam tshuam uas hu ua kev ua haujlwm speedCalc ().

Qhov no yuav siv kev nyeem ntawv ntawm millis () thiab tom qab ntawd suav qhov sib txawv ntawm kev nyeem tam sim no thiab kev nyeem yav dhau los - tus nqi no dhau los ua lub sijhawm los npog qhov nrug (uas yog ib puag ncig ntawm lub log piv rau lub ntsuas cua - khaws cia hauv

ntab circMetric = 1.2;

thiab ntsuas hauv meters). Thaum kawg nws suav qhov nrawm hauv km/h thiab MPH. Nruab nrab ntawm kev cuam tshuam cov duab kos qhia cov ntaub ntawv hloov tshiab nrawm ntawm LCD nrog rau lub sijhawm nyoos rau txhua qhov kev hloov pauv rau kev xav paub. Hauv lub neej tiag tiag kuv tsis xav tias ib tus neeg yuav nce lub LCD ntawm lub tsheb kauj vab, tej zaum cov zaub LED yuav cuam tshuam ntau dua.

Lub sijhawm no, koj tuaj yeem pom qhov piv txwv no ua haujlwm li cas hauv cov vis dis aus luv luv hauv qab no. Hloov chaw ntawm lub log tsheb kauj vab thiab hloov pauv ntoo/sib nqus sib txuas, Kuv tau txuas nrog lub xwmfab nthwv dej tso tawm los ntawm lub tshuab hluav taws xob ua haujlwm rau tus pin cuam tshuam los ua kom pom cov mem tes los ntawm lub ntsuas, yog li koj tuaj yeem tau txais lub tswv yim ntawm nws ua haujlwm li cas.

Kauj ruam 4:

Qhov ntawd tsuas yog hais txog qhov suav nrog kev siv millis () rau lub sijhawm tam sim no. Kuj tseem muaj lub micros (); ua haujlwm uas suav microseconds.

Yog li ntawd koj muaj nws - lwm qhov kev ua haujlwm uas tuaj yeem tso cai daws teeb meem ntau dhau ntawm lub ntiaj teb ntawm Arduino. Raws li ib txwm muaj, tam sim no nws yog nyob ntawm koj thiab koj lub tswv yim txhawm rau nrhiav qee yam los tswj lossis nce mus rau lwm tus shenanigans.

Cov ntawv tshaj tawm no tau coj tuaj rau koj los ntawm pmdway.com - txhua yam rau cov kws tsim khoom thiab cov khoom siv hluav taws xob, nrog kev xa khoom dawb thoob ntiaj teb.

Pom zoo: