#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // the I2C address of Tiny RTC
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val) );
}
///////////////////////////////////////////////////////////
//Pin connected to ST_CP of 74HC595
int lacthPin = 5;
//Pin connected to SH_CP of 74HC595
int clockPin = 7;
//Pin connected to DS of 74HC595
int dataPin = 6;
int SW1=15;
//int SW2=2;
int SW3=3;
int SW4=4;
int com1=8;
int com2=9;
int com3=10;
int com4=11;
int com5=0;
int com6=0;
int buzzer=2;
//int B=0;
//int C=0;
int t=1;
byte num[] ={0b11111101, //0
0b01100000, //1
0b11011011, //2
0b11110011, //3
0b01100110, //4
0b10110111, //5
0b10111111, //6
0b11100000, //7
0b11111111, //8
0b11110111, //9
};
void setDateDs1307()
{
second =50;
minute = 59;
hour = 11;
dayOfWeek = 2;
dayOfMonth =22;
month =6;
year= 60;
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(decToBcd(00));
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
//bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
// Function to gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(decToBcd(0));
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year,DEC);
Serial.print(" ");
Serial.println();
Serial.println(13);
//Serial.print("Day of week:");
}
void setup()
{
pinMode(lacthPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(SW1,INPUT);
// pinMode(SW2,INPUT);
pinMode(SW3,INPUT);
pinMode(SW4,INPUT);
pinMode(2,OUTPUT);
digitalWrite(lacthPin,HIGH);
Wire.begin();
Serial.begin(19200);
setDateDs1307(); //Set current time;
}
int i=0;
int m= 0;
void loop()
{
// if(digitalRead(SW1)== HIGH)
// { (digitalRead(SW1)== LOW);
// {
// i+=1;
// delay(t);
// if(i>99)
// {
// i=0;
// }
// }
// }
// if(digitalRead(SW2)== HIGH)
// { (digitalRead(SW2)== LOW);
// {
// i-=1;
// delay(t);
// if(i<0)
// {
// i=99;
// }
// }
// }
//////////////////////////////////////////////////// SW2
//
// if(digitalRead(SW3)== HIGH)
// { (digitalRead(SW3)== LOW);
// {
// m+=1;
// delay(t);
// if(m>99)
// {
// m=0;
// }
// }
// }
// if(digitalRead(SW4)== HIGH)
// { (digitalRead(SW4)== LOW);
// {
// m-=1;
// delay(t);
// if(m<0)
// {
// m=99;
// }
// }
// }
com1=hour/10;
// B=i00;
com2=hour;
com3=minute/10;
com4=minute;
com5=second/10;
com6=second;
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com1]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
// digitalWrite(9,HIGH);
// digitalWrite(10,HIGH);
// digitalWrite(11,HIGH);
// digitalWrite(16,HIGH);
// digitalWrite(17,HIGH)
delay (t);
////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com2]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
//////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com3]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
//////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com4]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////)//////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);;
delay (t);
//////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com5]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
delay (t);
////////////)//////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
//////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com6]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
delay (t);
////////////)//////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
// delay(1000);
getDateDs1307();//get the time data from tiny RTC
}
อยากให้ Buzzer ดังขึ้น ทุกๆ ชั่วโมงทำยังไงได้บ้างครับ
#define DS1307_I2C_ADDRESS 0x68 // the I2C address of Tiny RTC
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val) );
}
///////////////////////////////////////////////////////////
//Pin connected to ST_CP of 74HC595
int lacthPin = 5;
//Pin connected to SH_CP of 74HC595
int clockPin = 7;
//Pin connected to DS of 74HC595
int dataPin = 6;
int SW1=15;
//int SW2=2;
int SW3=3;
int SW4=4;
int com1=8;
int com2=9;
int com3=10;
int com4=11;
int com5=0;
int com6=0;
int buzzer=2;
//int B=0;
//int C=0;
int t=1;
byte num[] ={0b11111101, //0
0b01100000, //1
0b11011011, //2
0b11110011, //3
0b01100110, //4
0b10110111, //5
0b10111111, //6
0b11100000, //7
0b11111111, //8
0b11110111, //9
};
void setDateDs1307()
{
second =50;
minute = 59;
hour = 11;
dayOfWeek = 2;
dayOfMonth =22;
month =6;
year= 60;
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(decToBcd(00));
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
//bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
// Function to gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(decToBcd(0));
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year,DEC);
Serial.print(" ");
Serial.println();
Serial.println(13);
//Serial.print("Day of week:");
}
void setup()
{
pinMode(lacthPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(SW1,INPUT);
// pinMode(SW2,INPUT);
pinMode(SW3,INPUT);
pinMode(SW4,INPUT);
pinMode(2,OUTPUT);
digitalWrite(lacthPin,HIGH);
Wire.begin();
Serial.begin(19200);
setDateDs1307(); //Set current time;
}
int i=0;
int m= 0;
void loop()
{
// if(digitalRead(SW1)== HIGH)
// { (digitalRead(SW1)== LOW);
// {
// i+=1;
// delay(t);
// if(i>99)
// {
// i=0;
// }
// }
// }
// if(digitalRead(SW2)== HIGH)
// { (digitalRead(SW2)== LOW);
// {
// i-=1;
// delay(t);
// if(i<0)
// {
// i=99;
// }
// }
// }
//////////////////////////////////////////////////// SW2
//
// if(digitalRead(SW3)== HIGH)
// { (digitalRead(SW3)== LOW);
// {
// m+=1;
// delay(t);
// if(m>99)
// {
// m=0;
// }
// }
// }
// if(digitalRead(SW4)== HIGH)
// { (digitalRead(SW4)== LOW);
// {
// m-=1;
// delay(t);
// if(m<0)
// {
// m=99;
// }
// }
// }
com1=hour/10;
// B=i00;
com2=hour;
com3=minute/10;
com4=minute;
com5=second/10;
com6=second;
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com1]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
// digitalWrite(9,HIGH);
// digitalWrite(10,HIGH);
// digitalWrite(11,HIGH);
// digitalWrite(16,HIGH);
// digitalWrite(17,HIGH)
delay (t);
////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com2]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
//////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com3]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
//////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com4]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
////////////)//////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);;
delay (t);
//////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com5]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
delay (t);
////////////)//////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
//////////////////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[com6]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
delay (t);
////////////)//////////////////////////
digitalWrite(lacthPin,LOW);
shiftOut(dataPin,clockPin,LSBFIRST,num[0b11111111]);
digitalWrite(lacthPin,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay (t);
// delay(1000);
getDateDs1307();//get the time data from tiny RTC
}