ds1302 pic16f877
Hi all,
DS-1302 is successfully interfaced,and here is the code.
void display_write(void)
{
unsigned char yr1, mn1, date1, dy1, hr1, min1, sec1,line1[16];
reset_3w();
wbyte_3w(0xBF); /* clock burst */
sec1 = rbyte_3w();
min1 = rbyte_3w();
hr1 = rbyte_3w();
dy1 = rbyte_3w();
date1 = rbyte_3w();
mn1 = rbyte_3w();
yr1 = rbyte_3w();
reset_3w();
line1[0]=((date1/10) % 10)+0x30;
line1[1]=((date1) % 10)+0x30;
line1[2]=0x2F; //symbol "/"
line1[3]=((mn1/10) % 10)+0x30;
line1[4]=((mn1) % 10)+0x30;
line1[5]=0x2F;
line1[6]=((yr1/10) % 10)+0x30;
line1[7]=((yr1) % 10)+0x30;
line1[8]=((hr1/10) % 10)+0x30;
line1[9]=((hr1) % 10)+0x30;
line1[10]=0x3A; //symbol ":"
line1[11]=((min1/10) % 10)+0x30;
line1[12]=((min1) % 10)+0x30;
line1[13]=0x3A;
line1[14]=((sec1/10) % 10)+0x30;
line1[15]=((sec1) % 10)+0x30;
lcd_goto(0x00);
lcd_puts(line1);
}
void initialize_DS1302()
{
reset_3w();
wbyte_3w(0x8e); /* control register */
wbyte_3w(0); /* disable write protect */
reset_3w();
wbyte_3w(0x90); /* trickle charger register */
wbyte_3w(0xab); /* enable, 2 diodes, 8K resistor */
reset_3w();
wbyte_3w(0xbe); /* clock burst write (eight registers) */
wbyte_3w(sec);
wbyte_3w(min);
wbyte_3w(hr);
wbyte_3w(dy);
wbyte_3w(date);
wbyte_3w(mn);
wbyte_3w(yr);
wbyte_3w(0); /* must write control register in burst mode */
reset_3w();
}
void reset_3w()
{
RTC_SCLK = 0;
RTC_RS = 0;
RTC_RS = 1;
}
void wbyte_3w(unsigned char W_Byte) //write into DS1302
{
unsigned char i;
TRISB = 0b00000011; //RB3 as o/p
for(i = 0; i < 8; ++i)
{
RTC_IO = 0;
if(W_Byte & 0x01)
{
RTC_IO = 1; /* set port pin high to read data */
}
RTC_SCLK = 0;
RTC_SCLK = 1;
W_Byte >>= 1;
}
}
unsigned char rbyte_3w() //read from DS1302
{
unsigned char i;
unsigned char R_Byte;
unsigned char TmpByte;
TRISB = 0b00001011; //RB3 as i/p
R_Byte = 0x00;
RTC_IO = 1;
for(i=0; i<8; ++i)
{
RTC_SCLK = 1;
RTC_SCLK = 0;
TmpByte = (uchar)RTC_IO;
TmpByte <<= 7;
R_Byte >>= 1;
R_Byte |= TmpByte;
}
TRISB = 0b00000011; //RB3 as o/p
return R_Byte;
}
Thanks to all.
-IMP