|
樓主 |
發表於 2015-7-26 22:44:20
|
顯示全部樓層
程式碼的部份 & Arduino函式庫
#include <Wire.h>
#include <PT6961.h>
#define DS1307_ADDRESS 0x68
#define TIMEOUT 10000
PT6961 LED(8, 7, 6);
void SetTime();
void GetTime();
byte readByteq();
byte bcdToDec(byte val);
byte decToBcd(byte val);
byte seconds;
byte hours;
byte minutes;
int weekdays;
int days;
int months;
int years;
int DISP[4];
long time=0;
void setup()
{
Serial.begin(9600);
Wire.begin();
LED.initDisplay();
}
void loop()
{
if(Serial.available()) SetTime();
GetTime();
DISP[0] = (hours/10)%10;
DISP[1] = hours%10;
DISP[2] = (minutes/10)%10;
DISP[3] = minutes%10;
LED.sendDigits(DISP[0],DISP[1],DISP[2],DISP[3],seconds%2);
delay(500);
}
void GetTime()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
seconds = bcdToDec(Wire.read());
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0b111111); //24 hour time
weekdays = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
days = bcdToDec(Wire.read());
months = bcdToDec(Wire.read());
years = bcdToDec(Wire.read());
Serial.print(months);
Serial.print("/");
Serial.print(days);
Serial.print("/");
Serial.print(years);
Serial.print(" ");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.println(seconds);
}
void SetTime()
{
byte yearset = readByteq();
byte monthset = readByteq();
byte dayset = readByteq();
byte weekdayset = readByteq();
byte hourset = readByteq();
byte minset = readByteq();
byte secset = readByteq();
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(byte(0));
Wire.write(decToBcd(secset)); //seconds
Wire.write(decToBcd(minset)); //minutes
Wire.write(decToBcd(hourset)); //hours
Wire.write(decToBcd(weekdayset)); //weekdays
Wire.write(decToBcd(dayset)); //days
Wire.write(decToBcd(monthset)); //months
Wire.write(decToBcd(yearset)); //years
Wire.write(byte(0));
Wire.endTransmission();
delay(100);
}
byte readByteq()
{
while (!Serial.available()) delay(10);
byte reading = 0;
byte incomingByte = Serial.read();
while (incomingByte != '\n')
{
if (incomingByte >= '0' && incomingByte <= '9')
{reading = reading * 10 + (incomingByte - '0');}
incomingByte = Serial.read();
}
Serial.flush();
return reading;
}
byte bcdToDec(byte val)
{
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
byte decToBcd(byte val)
{
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
} |
評分
-
2
查看全部評分
-
|