PCF8563 kezelése
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
Itt lenne az ominózus kiíratás...
Lát esetleg valaki valami hibát?
Köszi
- void loop()
{
Wire.beginTransmission(B1010001);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(B1010001, 0x10);
int i = 0;
while(Wire.available())
{
Serial.print(i,HEX);
Serial.print("\t");
// Serial.println(Wire.receive() ,BIN);
Serial.println(Wire.receive() ,DEC);
i++;
}
Serial.println("Waiting a little");
delay(1500);
}
Lát esetleg valaki valami hibát?
Köszi
Az RTC esetén pár típushiba, amit el lehet (én is szoktam
) követni:
- élő eszköz, azaz a belső adat folyamatosan változik (pl. kiolvasás alatt a mp lép)
Én 2x olvasok, és ha mind2x azonos adat jön ki, akkor nem léptetett közben az IC. biztos van elegánsabb megoldás is.
- a belső adatok (1-1 byte) kombinált. Azaz pl.a másodperc adatbyte-janak a legmagasabb helyiértéke az elemkimerülés jelzőbitje. De a évszádad is ilyen.
- a letárolt adatok nem DEC típusban vannak, hanem ún. BCD adatként (a 74xxx IC-k és 7szegmenses kijelzők hagyományai miatt). Kiíráskor+beíráskor a DEC->BCD illetve a BCD->DEC konverziókat el kell végezni. Erre Bascom-AVR-ben a MakeBCD utasítás szolgál.
- élő eszköz, azaz a belső adat folyamatosan változik (pl. kiolvasás alatt a mp lép)
Én 2x olvasok, és ha mind2x azonos adat jön ki, akkor nem léptetett közben az IC. biztos van elegánsabb megoldás is.
- a belső adatok (1-1 byte) kombinált. Azaz pl.a másodperc adatbyte-janak a legmagasabb helyiértéke az elemkimerülés jelzőbitje. De a évszádad is ilyen.
- a letárolt adatok nem DEC típusban vannak, hanem ún. BCD adatként (a 74xxx IC-k és 7szegmenses kijelzők hagyományai miatt). Kiíráskor+beíráskor a DEC->BCD illetve a BCD->DEC konverziókat el kell végezni. Erre Bascom-AVR-ben a MakeBCD utasítás szolgál.
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
A beállítás nagyjából rendben lévőnek tűnik...
Amit kellett binárisban adtam meg.
Ami érdekes, hogy a másodperc tovább megy és az óra értéke is hülyeségre vált.
Amit kellett binárisban adtam meg.
Ami érdekes, hogy a másodperc tovább megy és az óra értéke is hülyeségre vált.
- Wire.begin();
Serial.begin(9600);
Wire.beginTransmission(B1010001);
Wire.send(B00000000);
Wire.send(B00000000); // 00H Test mode
Wire.send(B00000000); // 01H Alarm mode
Wire.send(B00000011); // 02H Seconds
// Wire.send(B01000101); // 03H Minutes:p
Wire.send(03); // 03H Minutes:p
Wire.send(18); // 04H Hours
Wire.send(B00000010); // 05H Date
Wire.send(B00000011); // 06H Day of the week
Wire.send(B00001011); // 07H Century bit 19XX = 1 | 20XX = 0
Wire.send(B00001011); // 08H year
Wire.send(B10000000); // 09H Minute alarm
Wire.send(B10000000); // 0AH Hour alarm
Wire.send(B10000000); // 0BH Day alarm
Wire.send(B10000000); // 0CH Weekday alarm
Wire.send(B10000011); // 0DH CLKOUT control (00: 32.768Khz 11: 1Hz)
Wire.send(B00000000); // 0EH Timer control
Wire.send(B00000000); // 0FH Timer
Wire.endTransmission();
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
Szerintem a BCD konverzióba futsz bele.
De tipp:
http://www.arduino.cc/playground/Main/R ... 3#Download
Illetve lépésenként, fórumban kitárgyalva:
http://www.arduino.cc/cgi-bin/yabb2/YaB ... 573932/all
BCD-vel fűszerezve:
http://variecose.wordpress.com/2011/09/ ... h-arduino/
De tipp:
http://www.arduino.cc/playground/Main/R ... 3#Download
Illetve lépésenként, fórumban kitárgyalva:
http://www.arduino.cc/cgi-bin/yabb2/YaB ... 573932/all
BCD-vel fűszerezve:
http://variecose.wordpress.com/2011/09/ ... h-arduino/
Kód: Egész kijelölése
#include <Wire.h>//I2C header file
// Defines
//#define DEBUG // Uncomment to turn on verbose mode
#define I2C_RTC 0x51 // 7 bit address (without last bit - look at the datasheet)
#define ERROR_LED 13
// Errors
#define ERROR_RTC_SET 1 // Unable to set RTC time and date
#define ERROR_RTC_GET 2 // Unable to get RTC time and date
#define ERROR_CLOCK_INTEGRITY 3 // RTC clock integrity not guaranteed
// Global variables
byte result;
byte second;
byte second_old; // The code ask the RTC for data only when the previous value has changed
byte minute;
byte minute_old; // The code ask the RTC for data only when the previous value has changed
byte hour;
byte hour_old; // The code ask the RTC for data only when the previous value has changed
byte weekday;
byte day;
byte month;
byte year;
byte century;
char* weekdayname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Function prototypes
byte BcdToDec(byte);
byte DecToBcd(byte);
void SetError(int);
void setup()
{
pinMode(ERROR_LED, OUTPUT); // Set error LED
Wire.begin(); // Initiate the Wire library and join the I2C bus as a master
Serial.begin(9600); // Initiate serial communication
// Set initial date and time
second_old = second = 53; // Second (0-59)
minute_old = minute = 59; // Minute (0-59)
hour_old = hour = 23; // Hour (0-23)
weekday = 0; // Day of the week (0-6)
day = 31; // Day (1-31)
month = 12; // Month (1-12)
year = 11; // Year (0-99)
Wire.beginTransmission(I2C_RTC); // Select RTC
Wire.send(0); // Start address
Wire.send(0); // Control and status 1
Wire.send(0); // Control and status 2
Wire.send(DecToBcd(second)); // Second
Wire.send(DecToBcd(minute)); // Minute
Wire.send(DecToBcd(hour)); // Hour
Wire.send(DecToBcd(day)); // Day
Wire.send(DecToBcd(weekday)); // Weekday
Wire.send(DecToBcd(month)); // Month (with century bit = 0)
Wire.send(DecToBcd(year)); // Year
Wire.send(0b10000000); // Minute alarm (and alarm disabled)
Wire.send(0b10000000); // Hour alarm (and alarm disabled)
Wire.send(0b10000000); // Day alarm (and alarm disabled)
Wire.send(0b10000000); // Weekday alarm (and alarm disabled)
Wire.send(0b10000011); // Output clock frequency enabled (1 Hz)
Wire.send(0); // Timer (countdown) disabled
Wire.send(0); // Timer value
Wire.endTransmission();
result = Wire.endTransmission();
#ifdef DEBUG
Serial.print("Result of setting date and time: ");
Serial.println(result, DEC);
#endif
if (result) SetError(ERROR_RTC_SET);
}
void loop()
{
Wire.beginTransmission(I2C_RTC);
Wire.send(0x02); // Start address
result = Wire.endTransmission();
#ifdef DEBUG
Serial.print("Result of asking for date and time: ");
Serial.println(result, DEC);
#endif
if (result) SetError(ERROR_RTC_GET);
Wire.requestFrom(I2C_RTC, 1);
second = Wire.receive();
if (second & 0x80) SetError(ERROR_CLOCK_INTEGRITY);
second = BcdToDec(second & 0b01111111);
if (second != second_old) // Cycle begins only when it has changed
{
second_old = second;
if (second == 0) // If second is zero I need to ask for the minute
{
Wire.requestFrom(I2C_RTC, 1);
minute = BcdToDec(Wire.receive());
if (minute != minute_old) // Cycle begins only when it has changed
{
minute_old = minute;
if (minute == 0) // If minute is zero I need to ask for the hour
{
Wire.requestFrom(I2C_RTC, 1);
hour = BcdToDec(Wire.receive());
if (hour != hour_old) // Cycle begins only when it has changed
{
hour_old = hour;
if (hour == 0) // If hour is zero I need to ask for other elements
{
Wire.requestFrom(I2C_RTC, 4);
day = BcdToDec(Wire.receive());
weekday = BcdToDec(Wire.receive());
month = Wire.receive();
century = (month & 0x80);
month = BcdToDec(month & 0b01111111);
year = BcdToDec(Wire.receive());
}
}
}
}
}
Serial.print(weekdayname[weekday]);
Serial.print("(");
Serial.print(weekday, DEC);
Serial.print(")");
if (century)
Serial.print(" 21");
else
Serial.print(" 20");
if (year < 10) Serial.print("0");
Serial.print(year, DEC);
Serial.print("-");
if (month < 10) Serial.print("0");
Serial.print(month,DEC);
Serial.print("-");
if (day < 10) Serial.print("0");
Serial.print(day, DEC);
Serial.print("");
if (hour < 10) Serial.print("0");
Serial.print(hour,DEC);
Serial.print(":");
if (minute < 10) Serial.print("0");
Serial.print(minute, DEC);
Serial.print(":");
if (second < 10) Serial.print("0");
Serial.println(second, DEC);
}
}
// Converts a BCD (binary coded decimal) to decimal
byte BcdToDec(byte value)
{
return ((value / 16) * 10 + value % 16);
}
// Converts a decimal to BCD (binary coded decimal)
byte DecToBcd(byte value){
return (value / 10 * 16 + value % 10);
}
void SetError(int error) // Blinks forever the error led a number of times corresponding to error number
{
while(1) // Forever
{
for (byte index = 0; index < error; index++)
{
digitalWrite(ERROR_LED, HIGH);
delay(500);
digitalWrite(ERROR_LED, LOW);
delay(500);
}
delay(1000);
}
}- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
Ebből a libből is lehet találni pár okosságot: http://www.arduino.cc/playground/Main/RTC-PCF8563
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00
- Moon_Walker
- DrótVégénSzéndarab
- Hozzászólások: 35
- Csatlakozott: 2011. szeptember 29. csütörtök, 6:00