Created
July 4, 2016 06:46
-
-
Save monpetit/222f60ecafeebff873edddea8b8dabf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <avr/sleep.h> | |
#include <avr/power.h> | |
#include <avr/interrupt.h> | |
#include <Wire.h> | |
#define PCF8563_ADDR 0x51 | |
#define RTCC_SQW_ADDR 0x0D | |
/* square wave contants */ | |
#define SQW_DISABLE B00000000 | |
#define SQW_32KHZ B10000000 | |
#define SQW_1024HZ B10000001 | |
#define SQW_32HZ B10000010 | |
#define SQW_1HZ B10000011 | |
const int led = 13; // LED | |
const int d2 = 2; // Interrupt Input | |
uint32_t count = 0; | |
/* PCF8563 RTC 통신 준비 */ | |
void pcf8563_init(void) | |
{ | |
Wire.begin(); | |
} | |
/* square wave pin 출력 설정 */ | |
void pcf8563_set_sqw(uint8_t frequency) | |
{ | |
Wire.beginTransmission(PCF8563_ADDR); | |
Wire.write(RTCC_SQW_ADDR); | |
Wire.write(frequency); | |
Wire.endTransmission(); | |
} | |
/* square wave pin 출력 중지 */ | |
void pcf8563_clear_sqw(void) | |
{ | |
pcf8563_set_sqw(SQW_DISABLE); | |
} | |
/* Interrupt Handler Callback 함수 */ | |
void blink(void) | |
{ | |
digitalWrite(led, digitalRead(led) ^ 1); | |
} | |
void enter_sleep(void) | |
{ | |
ADCSRA &= ~(1 << ADEN); // switch Analog to Digitalconverter OFF | |
/* Disable all peripherals but TIMER1. */ | |
power_all_disable(); | |
/* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */ | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
sleep_enable(); | |
/* Now enter sleep mode. */ | |
sleep_mode(); | |
/* The program will continue from here after the WDT timeout*/ | |
sleep_disable(); /* First thing to do is disable sleep. */ | |
/* Re-enable the peripherals. */ | |
power_all_enable(); | |
ADCSRA |= (1 << ADEN); // switch Analog to Digitalconverter ON | |
} | |
void setup(void) | |
{ | |
pcf8563_init(); | |
Serial.begin(9600); | |
pinMode(led, OUTPUT); | |
pinMode(d2, INPUT_PULLUP); | |
pcf8563_set_sqw(SQW_1HZ); // SQW 출력 주기 설정: 1 Hz | |
attachInterrupt(digitalPinToInterrupt(d2), blink, FALLING); | |
Serial.println("START..."); | |
} | |
void loop(void) | |
{ | |
Serial.print("count = "); | |
Serial.println(count++); | |
delay(2); | |
enter_sleep(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment