Cov txheej txheem:

Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino: 3 Kauj Ruam
Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino: 3 Kauj Ruam

Video: Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino: 3 Kauj Ruam

Video: Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino: 3 Kauj Ruam
Video: Qhia txuas xaim hluav taws xob daim: 02 2024, Hlis ntuj nqeg
Anonim
Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino
Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino
Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino
Siv DS1307 thiab DS3231 Lub Sijhawm Teev Sijhawm Sijhawm Nrog Arduino

Peb tseem tau txais kev thov yuav siv DS1307 thiab DS3231 lub sijhawm teev sijhawm nrog Arduino los ntawm ntau qhov chaw-yog li qhov no yog thawj ntawm ob ntu kev qhia yuav siv lawv li cas. Txog qhov kev qhia Arduino no peb muaj ob lub sijhawm teev sijhawm ua haujlwm siv, ib qho raws li Maxim DS1307 (tus qauv xwm txheej) thiab DS3231 (tus qauv duab plaub).

Muaj ob qhov sib txawv tseem ceeb ntawm ICs ntawm lub sijhawm teev sijhawm tiag, uas yog qhov raug ntawm lub sijhawm khaws cia. DS1307 siv hauv thawj tus qauv ua haujlwm tau zoo heev, txawm li cas los xij qhov ntsuas kub sab nraud tuaj yeem cuam tshuam rau zaus ntawm oscillator Circuit uas ua rau DS1307 lub txee sab hauv.

Qhov no yuav zoo li muaj teeb meem, txawm li cas los feem ntau yuav ua rau lub moos raug kaw los ntawm tsib lossis li feeb hauv ib hlis. DS3231 yog qhov muaj tseeb ntau dua, vim nws muaj cov oscillator sab hauv uas tsis cuam tshuam los ntawm lwm yam sab nraud - thiab yog li qhov tseeb yog ob peb feeb hauv ib xyoos twg tshaj plaws. Yog tias koj muaj tus qauv DS1307- tsis xav tias tsis zoo, nws tseem yog pawg thawj coj muaj txiaj ntsig zoo thiab yuav ua haujlwm zoo rau koj. Nrog ob qho ntawm cov qauv, yuav tsum muaj roj teeb thaub qab.

Nws yog lub tswv yim zoo los yuav lub roj teeb CR2032 tshiab thiab haum rau tus qauv. Nrog rau taug qab lub sijhawm thiab hnub tim, cov qauv no tseem muaj EEPROM me me, lub tswb nrov (tsuas yog DS3231 nkaus xwb) thiab muaj peev xwm los tsim lub xwmfab nthwv dej ntawm ntau zaus-txhua yam uas yuav yog qhov kev kawm thib ob.

Kauj Ruam 1: Txuas Koj Cov Qauv rau Arduino

Ob qho qauv siv I2C npav, uas ua rau kev sib txuas tau yooj yim heev.

Ua ntej koj yuav tsum txheeb xyuas tus pin twg ntawm koj Arduino lossis cov laug cam tau siv rau I2C npav - cov no yuav paub zoo li SDA (lossis cov ntaub ntawv) thiab SCL (lossis moos). Ntawm Arduino Uno lossis cov laug cam sib xws, cov pins no yog A4 thiab A5 rau cov ntaub ntawv thiab moos; Ntawm Arduino Mega tus pin yog D20 thiab D21 rau cov ntaub ntawv thiab moos; Thiab yog tias koj siv Pro Mini-tau tshaj cov pins yog A4 thiab A5 rau cov ntaub ntawv thiab moos, uas zoo ib yam rau tus pin tseem ceeb.

DS1307 tus qauv

Yog tias koj muaj DS1307 tus qauv koj yuav tsum tau muab cov xov hlau txuas rau ntawm lub rooj tsavxwm, lossis txuas rau ntawm qee lub hauv paus kab hauv taub hau kom koj tuaj yeem siv cov xov hlau txuas. Tom qab ntawd txuas SCL thiab SDA tus pin rau koj Arduino, thiab Vcc tus pin rau 5V tus pin thiab GND rau GND.

DS3231 tus qauv

Kev txuas tus qauv no yog ib qho yooj yim raws li cov header pins tau teeb tsa ntawm lub rooj tsavxwm ntawm lub hoobkas. Koj tuaj yeem yooj yim khiav cov xov hlau txuas ntxiv los ntawm SCL thiab SDA mus rau Arduino thiab ntxiv los ntawm tus qauv Vcc thiab GND pins rau koj lub rooj tsav xwm 5V lossis 3.3. V thiab GND. Txawm li cas los xij cov no tau theej ntawm lwm sab rau kev siv koj tus kheej cov xov hlau. Ob qho ntawm cov qauv no muaj qhov xav tau rub tawm cov neeg tawm tsam, yog li koj tsis tas yuav ntxiv koj tus kheej. Zoo li txhua yam khoom siv txuas nrog I2C npav, sim thiab khaws qhov ntev ntawm SDA thiab SCL xov hlau kom tsawg kawg.

Kauj Ruam 2: Nyeem thiab Sau Sijhawm Los Ntawm Koj Tus Qauv RTC

Thaum koj tau txuas koj li RTC module. nkag mus thiab rub cov duab kos hauv qab no. Txawm hais tias cov ntawv sau thiab cov haujlwm hauv daim duab kos tsuas yog siv rau DS3231, cov cai kuj tseem ua haujlwm nrog DS1307.

#include "Wire.h" #define DS3231_I2C_ADDRESS 0x68 // Hloov cov lej zauv zauv mus rau binary coded decimal byte decToBcd (byte val) {rov qab ((val/10*16) + (val%10)); } // Hloov cov lej zauv zauv zauv rau zauv zauv zauv byte bcdToDec (byte val) {rov qab ((val/16*10) + (val%16)); } void teeb () {Wire.begin (); Serial.begin (9600); // teeb tsa lub sijhawm pib ntawm no: // DS3231 vib nas this, feeb, teev, hnub, hnub, hli, xyoo // setDS3231time (30, 42, 21, 4, 26, 11, 14); } void setDS3231time (byte thib ob, byte feeb, byte teev, byte dayOfWeek, byte dayOfMonth, byte hli, byte xyoo) {// teev sijhawm thiab hnub tim cov ntaub ntawv rau DS3231 Wire.beginTransmission (DS3231_I2C_ADDRESS); Hlau.write (0); // teeb tsa cov lus qhia tom ntej kom pib ntawm vib nas this sau npe Wire.write (decToBcd (thib ob)); // teeb tsa vib nas this Wire.write (decToBcd (feeb)); // teem feeb Wire.write (decToBcd (teev)); // teev teev Wire.write (decToBcd (dayOfWeek)); // teem hnub ntawm lub lim tiam (1 = Hnub Sunday, 7 = Hnub Saturday) Wire.write (decToBcd (dayOfMonth)); // teem hnub tim (1 txog 31) Wire.write (decToBcd (hli)); // teem hli Wire.write (decToBcd (xyoo)); // teem xyoo (0 txog 99) Wire.endTransmission (); } void readDS3231time (byte *thib ob, byte *feeb, byte *teev, byte *dayOfWeek, byte *dayOfMonth, byte *hli, byte *xyoo) {Wire.beginTransmission (DS3231_I2C_ADDRESS); Hlau.write (0); // teeb tsa DS3231 sau tus taw tes rau 00h Wire.endTransmission (); Wire.requestFrom (DS3231_I2C_ADDRESS, 7); // thov xya bytes ntawm cov ntaub ntawv los ntawm DS3231 pib los ntawm kev sau npe 00h *thib ob = bcdToDec (Wire.read () & 0x7f); *feeb = bcdToDec (Wire.read ()); *teev = bcdToDec (Wire.read () & 0x3f); *dayOfWeek = bcdToDec (Wire.read ()); *dayOfMonth = bcdToDec (Wire.read ()); *hli = bcdToDec (Wire.read ()); *xyoo = bcdToDec (Wire.read ()); } void displayTime () {byte thib ob, feeb, teev, dayOfWeek, dayOfMonth, hli, xyoo; // khaws cov ntaub ntawv los ntawm DS3231 nyeem DS3231time (& thib ob, & feeb, & teev, & dayOfWeek, & dayOfMonth, & hli, & xyoo); // xa nws mus rau tus saib xyuas Serial.print (teev, DEC); // hloov qhov sib txawv byte mus rau tus lej lej thaum tso tawm Serial.print (":"); yog (feeb <10) {Serial.print ("0"); } Serial.print (feeb, DEC); Serial.print (":"); yog (thib ob <10) {Serial.print ("0"); } Serial.print (thib ob, DEC); Serial.print (""); Serial.print (dayOfMonth, DEC); Serial.print ("/"); Serial.print (hli, DEC); Serial.print ("/"); Serial.print (xyoo, DEC); Serial.print ("Hnub ntawm lub lim tiam:"); hloov (dayOfWeek) {rooj plaub 1: Serial.println ("Hnub Sunday"); tawg; rooj plaub 2: Serial.println ("Hnub Monday"); tawg; rooj plaub 3: Serial.println ("Tuesday"); tawg; rooj plaub 4: Serial.println ("Wednesday"); tawg; rooj plaub 5: Serial.println ("Hnub Thursday"); tawg; rooj plaub 6: Serial.println ("Friday"); tawg; rooj plaub 7: Serial.println ("Hnub Saturday"); tawg; }} tsis muaj dab tsi voj () {displayTime (); // tso saib cov ntaub ntawv ntawm lub sijhawm tiag tiag ntawm Serial Monitor, ncua (1000); // txhua thib ob}

Tej zaum yuav muaj ntau txoj cai, txawm li cas los xij nws tawg tau zoo rau hauv qhov tswj tau.

Nws xub suav nrog Cov tsev qiv ntawv Hlau, uas yog siv rau I2C kev sib txuas lus tsheb npav, ua raws los ntawm kev txheeb xyuas qhov chaw nyob npav rau RTC li 0x68. Cov no yog ua raws los ntawm ob txoj haujlwm uas hloov cov lej zauv rau BCD (binary-coded decimal) thiab hloov pauv. Cov no yog qhov tsim nyog raws li RTC ICs ua haujlwm hauv BCD tsis yog zauv.

Kev ua haujlwm setDS3231time () yog siv los teeb tsa lub moos. Siv nws yog qhov yooj yim heev, yooj yim ntxig qhov tseem ceeb los ntawm ib xyoos mus rau zaum ob, thiab RTC yuav pib txij lub sijhawm ntawd. Piv txwv li yog tias koj xav teem hnub thiab sijhawm hauv qab no - Wednesday Kaum Ib Hlis 26, 2014 thiab 9:42 teev tsaus ntuj thiab 30 vib nas this - koj yuav siv:

setDS3231 sij hawm (30, 42, 21, 4, 26, 11, 14);

Kauj ruam 3:

Nco ntsoov tias lub sijhawm tau teeb tsa siv sijhawm 24 teev, thiab tus lej thib plaub yog "hnub ntawm lub lim tiam". Qhov no ntog nruab nrab ntawm 1 thiab 7 uas yog Hnub Sunday txog Hnub Saturday. Cov ntsuas no yog byte qhov tseem ceeb yog tias koj tab tom tso koj tus kheej cov kev hloov pauv.

Thaum koj tau ua txoj haujlwm ib zaug nws yog qhov zoo ua ntej nrog // thiab xa koj cov cai ntxiv, yog li nws yuav tsis rov pib dua lub sijhawm thaum lub zog tau hloov pauv lossis micrcontroller rov pib dua. Nyeem lub sijhawm tsim koj li RTC Tsuas yog ib qho yooj yim, qhov tseeb cov txheej txheem tuaj yeem ua raws li ua tau zoo hauv kev ua haujlwm displayTime (). Koj yuav tsum tau txiav txim siab xya qhov piv txwv sib txawv los khaws cov ntaub ntawv los ntawm RTC, thiab cov no tau muab tso rau hauv kev ua haujlwm readDS3231time (). Piv txwv li yog tias koj qhov kev hloov pauv yog:

byte thib ob, feeb, teev, dayOfWeek, dayOfMonth, hli, xyoo;

… Koj yuav rov kho lawv nrog cov ntaub ntawv tam sim no los ntawm RTC los ntawm kev siv:

readDS3232time (& thib ob, & feeb, & teev, & dayOfWeek, & dayOfMonth, & hli, & xyoo);

Tom qab ntawd koj tuaj yeem siv qhov sib txawv raws li koj pom zoo, los ntawm kev xa lub sijhawm thiab hnub tim mus rau tus saib xyuas raws li qhov piv txwv piv txwv ua - kom hloov cov ntaub ntawv mus rau hauv daim ntawv tsim nyog rau txhua hom khoom siv tso tawm.

Tsuas yog txhawm rau txheeb xyuas txhua yam ua haujlwm, nkag mus rau lub sijhawm thiab hnub tsim nyog rau hauv daim duab ua qauv qhia, upload nws, tawm tswv yim tawm setDS3231time () muaj nuj nqi thiab rub nws dua. Tom qab ntawd qhib lub ntsuas saib, thiab koj yuav tsum tau muab nrog cov khoom siv khiav ntawm lub sijhawm tam sim no thiab hnub tim.

Txij ntawm no koj tam sim no muaj cov cuab yeej software los teeb tsa cov ntaub ntawv rau thiab khaws nws los ntawm koj lub sijhawm teev sijhawm tiag, thiab peb vam tias koj muaj kev nkag siab txog yuav siv cov qauv tsis kim li cas.

Koj tuaj yeem kawm paub ntau ntxiv txog lub sijhawm teev sijhawm ICs tshwj xeeb los ntawm cov chaw tsim khoom lub vev xaib-DS1307 thiab DS3231.

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: