Your Knowledge Your Future
Tiếng Việt English
Hotline: 0902561426

Bài 8: Đồng hồ điện tử chính xác

Bài 8: Đồng hồ điện tử chính xác

Bài 8: Đồng hồ điện tử chính xác

Bài 8: Đồng hồ điện tử chính xác

Bài 8: Đồng hồ điện tử chính xác
Bài 8: Đồng hồ điện tử chính xác

Tài liệu

Bài 8: Đồng hồ điện tử chính xác

Yêu cầu:

  • Mạch hiển thị giờ phút giây chính xác.
  • Co nút nhấn chỉnh thời gian

linh kiện sử dụng

  • Arduino Uno
  • 6 led 7 đoạn
  • 6 IC 74HC595
  • 2 nút nhấn
  • 2 điện trở 220

Mạch đi dây

chương trình điều khiển

#define DATA 8 //DS
#define LATCH 7 //ST_CP
#define CLOCK 9 //SH_CP
#define settingbutton 12
#define upbutton 13
int settingState = 0;
int upState = 0;
int thoigianlaymau = 0;
int thoigiannhapnhay = 0;
int giaima7doan[]={252,96,218,242,102,182,190,224,254,246};
                              // 0    1    2     3     4     5     6     7     8     9
int i=0,j=0,k=0;
int phase = 0;
int tt1=0, tt2=0, tt3=0;
void setting_button() //Nút nhấn chuyển giữa chế độ chạy và chế độ chỉnh sửa
{
  settingState = digitalRead(settingbutton); //đọc trạng thái nút nhấn
  if (settingState == 0) tt2 = 0;
  if ((settingState == 1)&&(tt2 == 0))
  {
    delay(20);
    if (settingState == 1)
    {
      tt2 = 1;
      phase = phase +1 ; // chuyển chế độ 
      if (phase == 4) phase = 0;
    }
  }
}
void up_button() // tăng giá trị giờ phút giây
{
  upState = digitalRead(upbutton); //đọc trạng thái nút nhấn
  if (upState == 0) tt1 = 0;
  if ((upState == 1)&&(tt1 == 0))
  {
    delay(20);
    if (upState == 1)
    {
      tt1 = 1;
      if (phase ==1) 
      {
        k = k+1;
        if (k>59) k =0;
      }
      else if (phase ==2)
      {
        j = j+1;
        if (j>59) j =0;
      }
      else if (phase ==3) 
      {
        i = i+1;
        if (i>23) i =0;
      }
    }
  }

}
void giay(int sogiay) //ép số giây vô ic 74HC959
{
  int a = sogiay/10;
  int b = sogiay%10;
  shiftOut(DATA, CLOCK, LSBFIRST, giaima7doan[b]);
  shiftOut(DATA, CLOCK, LSBFIRST, giaima7doan[a]);
}
void phut(int sophut) //ép số phút vô ic 74HC959
{
  int a = sophut/10;
  int b = sophut%10;
  shiftOut(DATA, CLOCK, LSBFIRST, giaima7doan[b]);
  shiftOut(DATA, CLOCK, LSBFIRST, giaima7doan[a]);
}
void gio(int sogio) //ép số giờ vô ic 74HC959
{
  int a = sogio/10;
  int b = sogio%10;
  shiftOut(DATA, CLOCK, LSBFIRST, giaima7doan[b]);
  shiftOut(DATA, CLOCK, LSBFIRST, giaima7doan[a]);
}
void tatled() // tắt 2 led 7 đoạn 
{
  shiftOut(DATA, CLOCK, LSBFIRST, 0);
  shiftOut(DATA, CLOCK, LSBFIRST, 0);
}
void setup() 
{
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void loop() 
{
  setting_button();
  if (phase == 0) // phase0 chạy đồng hồ bình thường
  {
     if ( (unsigned long) (millis() - thoigianlaymau) > 1000)
     {
        if (k<60)
        {
          k =k+1;
        }
        if (k== 60)
        {
          k = 0;
          j = j+1;
        }
        if (j==60)
        {
          j =0;
          i = i+1;
        }
        if (i==24)
        {
          i =0;
        }
        digitalWrite(LATCH, LOW);
        giay(k);
        phut(j);
        gio(i);
        digitalWrite(LATCH, HIGH);
        thoigianlaymau = millis();
     }
  }  
  if (phase == 1) //chế độ chỉnh sửa giây, led hiển thị giây sẽ nhấp nháy với 300ms
  {
    if ( (unsigned long) (millis() - thoigiannhapnhay) < 300)
    {
    up_button();
    digitalWrite(LATCH, LOW);
    tatled();
    phut(j);
    gio(i);
    digitalWrite(LATCH, HIGH);
    }
    if ( (unsigned long) (millis() - thoigiannhapnhay) > 300)
    {
    up_button();
    digitalWrite(LATCH, LOW);
    giay(k);
    phut(j);
    gio(i);
    digitalWrite(LATCH, HIGH);
    }
    if ( (unsigned long) (millis() - thoigiannhapnhay) > 600)
    {
    up_button();
    thoigiannhapnhay= millis();
    }
  }
   if (phase == 2) //chế độ chỉnh sửa phút, led hiển thị phút sẽ nhấp nháy với 300ms
  {
    if ( (unsigned long) (millis() - thoigiannhapnhay) < 300)
    {
    up_button();
    digitalWrite(LATCH, LOW);
    giay(k);
    tatled();
    gio(i);
    digitalWrite(LATCH, HIGH);
    }
    if ( (unsigned long) (millis() - thoigiannhapnhay) > 300)
    {
    up_button();
    digitalWrite(LATCH, LOW);
    giay(k);
    phut(j);
    gio(i);
    digitalWrite(LATCH, HIGH);
    }
    if ( (unsigned long) (millis() - thoigiannhapnhay) > 600)
    {
    up_button();
    thoigiannhapnhay= millis();
    }
  }
  if (phase == 3) //chế độ chỉnh sửa giờ, led hiển thị giờ sẽ nhấp nháy với 300ms
  {
    if ( (unsigned long) (millis() - thoigiannhapnhay) < 300)
    {
    up_button();
    digitalWrite(LATCH, LOW);
    giay(k);
    phut(j);
    tatled();
    digitalWrite(LATCH, HIGH);
    }
    if ( (unsigned long) (millis() - thoigiannhapnhay) > 300)
    {
    up_button();
    digitalWrite(LATCH, LOW);
    giay(k);
    phut(j);
    gio(i);
    digitalWrite(LATCH, HIGH);
    }
    if ( (unsigned long) (millis() - thoigiannhapnhay) > 600)
    {
    up_button();
    thoigiannhapnhay= millis();
    }
  }
 }

Tin khác

Thông báo mới

Fanpage facebook

Liên kết website