2017年12月16日 星期六

Arduino + DS3231 可調整時間的電子鐘



Assembly List

LabelPart TypeProperties
Grove1Grove 4 Digit Display大小 1x1; interface digital; variant variant 4
R210kΩ Resistor電阻 10kΩ; 套件 THT; tolerance ±5%; bands 4; 接腳間隔 400 mil
R310kΩ Resistor電阻 10kΩ; 套件 THT; tolerance ±5%; bands 4; 接腳間隔 400 mil
S1Momentary Switch套件 ksa_sealed_tac_switch; variant ksa_sealed
S2Momentary Switch套件 ksa_sealed_tac_switch; variant ksa_sealed
ZS-042 RTC1ZS-042 RTC Modulechip DS3231; variant variant 4
元件1Arduino Nano (Rev3.0)類型 Arduino Nano (3.0)



程式碼

#include <TimerOne.h>
#include "TM1637.h"
#include <Wire.h>
#include "RTClib.h"

#define CLK 8//pins definitions for TM1637 and can be changed to other ports    
#define DIO 7
#define ON 1
#define OFF 0
#define SW1 2
#define SW2 3


int ADJhour=0 ;
int ADJminute = 0;

int8_t TimeDisp[] = {0x00,0x00,0x00,0x00};
unsigned char ClockPoint = 1;

volatile int UPDATE ;
TM1637 tm1637(CLK,DIO);
RTC_DS3231 rtc;




void setup() {
  //
  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);
  
  //
  Timer1.initialize(500000);//timing for 500ms
  Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR
  //
  
  Serial.begin(9600);
  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
  //
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  
  
  // put your setup code here, to run once:

  
  
}

void loop() {
  // put your main code here, to run repeatedly:
   
   
  if (UPDATE == ON) {
    DispTime();
  
  }
  
  //ADJ HOUR
  if (digitalRead(SW1)) {
    ADJhour ++ ;
    if (ADJhour > 23) {
      ADJhour = 0;
    }
    ADJminute = (TimeDisp[2]*10) + TimeDisp[3];
    //Serial.print(ADJhour);
    //Serial.print(":");
    //Serial.println(ADJminute);
    //
    TimeDisp[0] = ADJhour/10 ;
    TimeDisp[1] = ADJhour%10;
    //
    tm1637.display(TimeDisp);
    rtc.adjust(DateTime(2014, 1, 21, ADJhour, ADJminute, 0));
    delay(150);
    
  }

  //ADJ Minute
  if (digitalRead(SW2)) {
    ADJminute ++ ;
    if (ADJminute > 59) {
      ADJminute = 0;
    }
    ADJhour = (TimeDisp[0]*10) + TimeDisp[1];
    TimeDisp[2] = ADJminute/10;
    TimeDisp[3] = ADJminute%10;
    //
    //Serial.print(ADJhour);
    //Serial.print(":");
    //Serial.println(ADJminute);
    //
    tm1637.display(TimeDisp);
    rtc.adjust(DateTime(2014, 1, 21, ADJhour, ADJminute, 0));
    delay(150);
  }
  


}

void TimingISR() {
  UPDATE = ON;
  
  
}

void DispTime() {
    DateTime now = rtc.now();
    ClockPoint = (~ClockPoint) & 0x01;
    if(ClockPoint)tm1637.point(POINT_ON);
    else tm1637.point(POINT_OFF); 

    TimeDisp[0] = now.hour()/10;
    TimeDisp[1] = now.hour()%10;
    TimeDisp[2] = now.minute()/10;
    TimeDisp[3] = now.minute()%10;
    
    
    tm1637.display(TimeDisp);
    UPDATE = OFF ;
  
}



沒有留言:

張貼留言