Biblioteka ta sama, 99 nie ustawia opcji w timerze.
Baterię możesz zostawić, wyjmowałem do testu jaki jest pobór prądu. Wraz z rozładowaniem baterii pobór prądu powinien spadać, do 400nA przy 2V, krzywa V vs I robi się tym bardziej stroma im bardziej powyżej 3V.
Nie mam jakiegoś specjalistycznego miernika nA, to dość dobry multimetr, ale dla setek nA pomiary już w okolicach ostatniej cyfry wyświetlacza to może być i +/- 100%, ale ciągle to byłoby <1uA. Nie chciałem tego mierzyć przez rezystor 10k by nie wprowadzać dużego zakłócenia.
Sprawdź też jak jest po drugim uśpieniu, bo gdy sie bawiłem przy laptopie było OK, jak przeniosłem na stół z zasilaczem było OK, tylko jak zostawiłem na parę minut i wróciłem to żarło 350uA, bo uC się nie usypiał po wybudzeniu prawidłowo.
Schemat to podłączony moduł RTC do Atmegi 328, Atmega z kwarcem 4MHz by można ją było zasilać od 1.8V. Nawet rezystor podciągających I2C nie ma, bo biblioteka robi to wewnętrznymi. Całość zasilana jest jest przez rezystor R100, to też trochę ogranicza prąd. Pod pin LED mam jeszcze podłączony LED z R1000 i buzer aktywny przez R20, by słyszeć te mignięcia led co 2minuty.
Kod:
/* Demonstration of Rtc_Pcf8563 Alarms.
*
* The Pcf8563 has an interrupt output, Pin3.
* Pull Pin3 HIGH with a resistor, I used a 10kohm to 5v.
* I used a RBBB with Arduino IDE, the pins are mapped a
* bit differently. Change for your hw.
* SCK - A5, SDA - A4, INT - D3/INT1
*
* After loading and starting the sketch, use the serial monitor
* to see the clock output.
*
* setup: see Pcf8563 data sheet.
* 1x 10Kohm pullup on Pin3 INT
* No pullups on Pin5 or Pin6 (I2C internals used)
* 1x 0.1pf on power
* 1x 32khz chrystal
*
* Joe Robertson, jmr
* orbitalair@gmail.com
*/
#include <Wire.h>
#include <Rtc_Pcf8563.h>
#define debug 1
uint32_t czasTeraz,czasPoprzedni,tik=10; //tik musi byc mniejszy niz 1000 i dzilic 1000ms na rowne czesci
uint8_t nTik,sekundy,minuty,godziny,dni; //liczniki tikow, sekund, itd.
bool fnTik,fsekundy,fminuty,fgodziny,fdni; //flagi zdarzen nowy tik, nowa sekunda,minuta, godzina, dzien
//20221109 21:00 3.9065
//20221110 21:19 3.919 3.9112
/*
Vcc I
V uA
5 7
4,5 5,1
4,2 4,1
3,5 2,2
3,3 1,7
3 1
2,8 0,8
2,5 0,6
2,3 0,5
2,1 0,4
*/
bool flaga1;
/* get a real time clock object */
Rtc_Pcf8563 rtc;
/* a flag for the interrupt */
volatile int alarm_flag=0;
const byte led=13;
byte year;
byte month;
byte date;
byte dOW;
byte hour;
byte minute;
byte second;
uint8_t minutki;
void goSleep()
{
// Wylaczenie ADC
ADCSRA = 0;
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
noInterrupts ();
//Wybudzenie INT0 na pinie 3
attachInterrupt (0, wakeUp, FALLING);
EIFR = bit (INTF0); // clear flag for interrupt 0
//Wylaczenie BOD
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
//uspienie
interrupts (); // one cycle
sleep_cpu (); // one cycle
}
/* the interrupt service routine */
void wakeUp()
{
alarm_flag=1;
sleep_disable();
detachInterrupt (0);
}
void setup()
{
pinMode(2, INPUT); // set pin to input
digitalWrite(2, HIGH); // turn on pullup resistors
pinMode(led,OUTPUT);
#if debug
Serial.begin(250000);
#endif
// noInterrupts ();
// CLKPR = bit (CLKPCE);
// CLKPR = clock_div_8;
// interrupts ();
/* clear out all the registers */
// rtc.initClock();
/* set a time to start with.
* day, weekday, month, century, year */
// rtc.setDate(14, 6, 3, 0, 10);
/* hr, min, sec */
// rtc.setTime(1, 15, 40);
/* set an alarm for 20 secs later...
* alarm pin goes low when match occurs
* this triggers the interrupt routine
* min, hr, day, weekday
* 99 = no alarm value to be set
*/
minutki=rtc.getMinute();
// rtc.setAlarm(99, 12, 99, 99);
rtc.setAlarm((minutki+=2)%60, 99, 99, 99);
/* setup int on pin 3 of arduino */
attachInterrupt(0, wakeUp, FALLING);
alarm_flag=0;
goSleep();
}
void loop()
{
czas();
#if debug
if (Serial.available()) {
getDateStuff(year, month, date, dOW, hour, minute, second);
rtc.setDate(date, dOW, month, 0, year);
rtc.setTime(hour, minute, second);
}
/* each sec update the display */
// if (fsekundy and (sekundy%10==0))
// {
Serial.print(rtc.formatTime());
Serial.print(" ");
Serial.print(rtc.formatDate());
Serial.print(" 0x");
Serial.print(rtc.getStatus2(), HEX);
Serial.print("\r\n");
// }
#endif
if (alarm_flag==1){
clr_alarm();
flaga1=!flaga1;
}
}
void clr_alarm()
{
minutki=rtc.getMinute();
#if debug
Serial.print("blink!\r\n");
#endif
rtc.clearAlarm();
rtc.setAlarm((minutki+2)%60, 99, 99, 99);
// rtc.setAlarm(99, 12, 99, 99);
for (int i=0;i<6;i++)
{
digitalWrite(led,! digitalRead(led));
delay(50);
}
alarm_flag=0;
#if debug
Serial.println("Ide spac");
Serial.flush();
#endif
goSleep();
}
void getDateStuff(byte& year, byte& month, byte& date, byte& dOW,
byte& hour, byte& minute, byte& second) {
// Call this if you notice something coming in on
// the serial port. The stuff coming in should be in
// the order YYMMDDwHHMMSS, with an 'x' at the end.
// 2210311032200x - bez znakow konca linii i NL
// 2211067214500x
boolean gotString = false;
char inChar;
byte temp1, temp2;
char inString[20];
byte j=0;
while (!gotString) {
if (Serial.available()) {
inChar = Serial.read();
inString[j] = inChar;
j += 1;
if (inChar == 'x') {
gotString = true;
}
}
}
Serial.println(inString);
// Read year first
temp1 = (byte)inString[0] -48;
temp2 = (byte)inString[1] -48;
year = temp1*10 + temp2;
// now month
temp1 = (byte)inString[2] -48;
temp2 = (byte)inString[3] -48;
month = temp1*10 + temp2;
// now date
temp1 = (byte)inString[4] -48;
temp2 = (byte)inString[5] -48;
date = temp1*10 + temp2;
// now Day of Week
dOW = (byte)inString[6] - 48;
// now hour
temp1 = (byte)inString[7] -48;
temp2 = (byte)inString[8] -48;
hour = temp1*10 + temp2;
// now minute
temp1 = (byte)inString[9] -48;
temp2 = (byte)inString[10] -48;
minute = temp1*10 + temp2;
// now second
temp1 = (byte)inString[11] -48;
temp2 = (byte)inString[12] -48;
second = temp1*10 + temp2;
}
void czas()
{
czasTeraz=millis();
fnTik=fsekundy=fminuty=fgodziny=fdni=0;
if((uint32_t)(czasTeraz-czasPoprzedni)>=tik) //tan napisany warunek jest odporny na "klątwe 50 dni millis()"
{
czasPoprzedni=czasTeraz;
fnTik=1;
nTik++;
if(nTik>=(1000/tik))
{
nTik=0;
sekundy++;
fsekundy=1;
if (sekundy>=60)
{
sekundy=0;
minuty++;
fminuty=1;
if (minuty>=60)
{
minuty=0;
godziny++;
fgodziny=1;
if (godziny>=24)
{
godziny=0;
fdni=1;
dni++;
}
}
}
}
}
}