Surat 41.1: Arduino-ga DS1307 va WH1602
modullarini ulash sxemasi
133
print2digits(tm.Day,0,1);
lcd.print(“/”);
print2digits(tm.Month,3,1);
lcd.print(“/”);
lcd.print(tmYearToCalendar(tm.Year));
}
else
{
if (RTC.chipPresent())
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“DS1307 is stopped”);
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“DS1307 read error”);
}
delay(9000);
}
delay(1000);
}
// Ikkita raqam qo’shilgan holda ko’rsatiladigan protsedura
void print2digits(int number,int col, int str)
{
lcd.setCursor(col, str);
if (number >= 0 && number < 10)
{lcd.print(“0”);}
lcd.print(number);
}
Ulanish tartibi:
1. Biz DS1307 modulini va LCD-ni Arduino platasiga
41.1 suratdagi sxema bo’yicha ulaymiz.
2. 41 darsdan sketchni Arduino platasiga yuklang.
3. LCD ekranda har soniyada o’zgarib turadigan vaqt
va sanani ko’ring. Biroq, displey ekranida noto’g’ri vaqt
va noto’g’ri sanani ko’ramiz. Haqiqat shundaki, elektr
quvvati yo’q bo’lganda, DS1307 mikrosxemasidagi vaqt
134
26.2 ro’yxati
// RTC uchun kutubxonalarni ulash
#include
#include
#include
// LCD uchun kutubxonani ulash
#include
// ulanish pimlarini ko’rsatgan holda boshlash
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
// ketma-ket portga tushadigan
ma’lumotlardan yig’ilgan satr
String inputString = “”;
boolean stringComplete = false; // chiziq to’liqligi bayrog’i
void setup()
{
Serial.begin(9600); // ketma-ket portni ishga tushiring
lcd.begin(16, 2); //
ekran o’lchamini o’rnating
}
void loop()
{
qiymati 01.01.2000 soat 00:00:00 gacha tiklanadi. Quvvat
o’chirilganda vaqt tiklanmasligini ta’minlash uchun 3
V batareyadan modulning favqulodda quvvat manbai
ta’minlanadi.LEDlarning holatini ma’lum bir kod olganda
o’zgartiradigan sketchni yozamiz. Sketchning tarkibi
39.2-ro’yxatda keltirilgan. K2 - K9 konstantalarining ma’nosi
(2-9 tugmachalarining kodlari) siz uchun boshqacha bo’ladi.
Vaqtni belgilash uchun kutubxonada RTC.
write (tmElements_t tm) funktsiyasi mavjud.
Sketchga “dd / mm / YYYY hh: mm: ss” kabi
qatorni yuborish orqali ketma-ket port orqali RTC
ma’lumotlarini o’rnatish qobiliyatini qo’shaylik.
135
tmElements_t tm;
// kiruvchi so’rovni tahlil qilish uchun satr oxirini kutish:
if (stringComplete)
{
tm.Day=(int(inputString[0])-48)*10+(int(inputString[1])-48);
tm.Month=(int(inputString[3])-48)*10+(int(inputString[4])-48);
tm.Year=CalendarYrToTm((int(inputString[6])-
48)*1000+(int(inputString[7])-48)*100+
(int(inputString[8])-48)*10+(int(inputString[9])-48));
tm.Hour=(int(inputString[11])-48)*10+(int(inputString[12])-48);
tm.Minute=(int(inputString[14])-
48)*10+(int(inputString[15])-48);
tm.Second=(int(inputString[17])-
48)*10+(int(inputString[18])-48);
RTC.write(tm); // RTC-da yozish vaqti
// chiziqni tozalang
inputString = “”;
stringComplete = false;
}
if (RTC.read(tm))
{
print2digits(tm.Hour,0,0);
lcd.print(“:”);
print2digits(tm.Minute,3,0);
lcd.print(“:”);
print2digits(tm.Second,6,0);
print2digits(tm.Day,0,1);
lcd.print(“/”);
print2digits(tm.Month,3,1);
lcd.print(“/”);
lcd.print(tmYearToCalendar(tm.Year));
}
else
{
if (RTC.chipPresent())
136
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“DS1307 is stopped”);
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“DS1307 read error”);
}
delay(9000);
}
delay(1000);
}
// ikkita raqam qo’shilgan holda namoyish protsedurasi
void print2digits(int number,int col, int str)
{
lcd.setCursor(col, str);
if (number >= 0 && number < 10)
{lcd.print(“0”);}
lcd.print(number);
}
// ketma-ket port orqali ma’lumotlarni qabul qilish
void serialEvent()
{
while (Serial.available())
{
// keyingi baytni oling:
char inChar = (char)Serial.read();
// qatorga qo’shish
inputString += inChar;
// /n - uzatish tugashi
if (inChar == ‘\n’)
137
{stringComplete = true;}
}
}
Endi ketma-ket port monitoridan “dd / mm / YYYY hh:
mm: ss” qatorini yuborib vaqtni belgilaymiz va displey
ekranida to’g’ri sana va vaqt ko’rsatilishini ko’ramiz.
Do'stlaringiz bilan baham: |