#define dir_flow_set DDRC
#define out_flow_pin PORTC
#define in_flow_pin PINC
#define data_out_set (dir_flow_set=0xFF, out_flow_pin=0xFB)
#define data_in_set (dir_flow_set=0xFD)
#define ds1302_reg_second (0x80)
#define ds1302_reg_minute (0x82)
#define ds1302_reg_hour (0x84)
#define ds1302_reg_date (0x86)
#define ds1302_reg_month (0x88)
#define ds1302_reg_day (0x8a)
#define ds1302_reg_year (0x8c)
#define ds1302_reg_ctrl (0x8e)
#define ds1302_reg_trickle_charger (0x90)
#define ds1302_reg_clock_burst (0xbe)
unsigned char check_am_pm=0;
unsigned char hour_data[3]={0}, minute_data[3]={0}, second_data[3]={0};
unsigned char date_data[3]={0}, month_data[3]={0};
unsigned char day_data[3]={0}, year_data[3]={0};
unsigned char get_hour=0, get_minute=0, get_second=0;
unsigned char get_date=0, get_month=0;
unsigned char get_day=0, get_year=0;
/* ----- Pin Assignment : PORT0 = /RST, PORT1 = I/O, PORT2 = SCLK ----- */
void ds1302_write(unsigned char Instruction, unsigned char Data) {
unsigned char i;
data_out_set; _delay_us(2);
for(i=0; i<8; i++) {
if((Instruction & 0x01)) out_flow_pin|=0x02;
else out_flow_pin&=0xFD;
out_flow_pin|=0x04; Instruction>>=1; out_flow_pin&=0xFB;
}
for(i=0; i<8; i++) {
if((Data & 0x01)) out_flow_pin|=0x02;
else out_flow_pin&=0xFD;
out_flow_pin|=0x04; Data>>=1; out_flow_pin&=0xFB;
}
_delay_us(1); out_flow_pin = 0xFA;
}
unsigned char ds1302_read(unsigned char Instruction) {
unsigned char i, Data = 0;
data_out_set; Instruction |= 0x01; _delay_us(2);
for(i=0; i<8; i++) {
if((Instruction & 0x01)) out_flow_pin|=0x02;
else out_flow_pin&=0xFD; out_flow_pin|=0x04;
Instruction>>=1; out_flow_pin&=0xFB;
}
data_in_set;
for(i=0; i<8; i++) {
Data>>=1;
if((in_flow_pin & 0x02) != 0) Data|=0x80;
out_flow_pin|=0x04; out_flow_pin&=0xFB;
}
_delay_us(1); out_flow_pin=0xFA; return Data;
}
/* -------------------- 사용자 정의 함수 -------------------- */
void ds1302_get_time(void) {
get_second=ds1302_read(ds1302_reg_second);
/* if((get_second&0x80)==0x80) {
get_second&=0x7F;
ds1302_write(ds1302_reg_second, get_second);
}*/
get_minute=ds1302_read(ds1302_reg_minute);
get_hour=ds1302_read(ds1302_reg_hour);
if(!(get_hour & 0x20)) check_am_pm=0;
else check_am_pm=1;
second_data[0]=((get_second>>4)&0x0F)+'0';
second_data[1]=(get_second & 0x0F)+'0';
minute_data[0]=((get_minute>>4)&0x0F)+'0';
minute_data[1]=(get_minute & 0x0F)+'0';
hour_data[0]=(((get_hour&0x1F)>>4)&0x0F)+'0';
hour_data[1]=((get_hour&0x1F)&0x0F)+'0';
}
void ds1302_get_calendar(void){
get_date=ds1302_read(ds1302_reg_date);
get_month=ds1302_read(ds1302_reg_month);
get_day=ds1302_read(ds1302_reg_day);
get_year=ds1302_read(ds1302_reg_year);
date_data[0]=((get_date>>4)&0x0F)+'0';
date_data[1]=(get_date & 0x0F)+'0';
month_data[0]=((get_month>>4)&0x0F)+'0';
month_data[1]=(get_month & 0x0F)+'0';
day_data[1]=(get_day & 0x0F)+'0';
year_data[0]=((get_year>>4)&0x0F)+'0';
year_data[1]=(get_year & 0x0F)+'0';
}