Hai FvM,Thanks for replying me.So what about rtc_write function? that is correct or wrong?
- - - Updated - - -
Hai FvM,,I have done the modifications of my code.But still i got the junk or null values.
plz verify my code and modify that and send modification code.plz...............Its urgently required
set_time(12, 20, 40, HOUR_MODE_12, AM);
get_time();
void RTC_Init()
{
RESET_DIR=1;
SCLK_DIR=1;
DATA_IO_DIR=1;
RESET=0;
SCLK=0;
DATA_IO=0;
}
void delay(unsigned int l)
{
//unsigned char i;
for(i=0; i<l; i++);
{
}
}
void clock(void)
{
delay();
SCLK = 1; // Start clock
delay();
SCLK = 0; // Clear SCL
}
void rtc_write(unsigned far char reg, unsigned far char data)
{
unsigned far char i;
//unsigned char final_16b_data;
unsigned char final_16bit_data = ( ((data << 8) & 0xFF00) | (((reg<<1) | 0x80) & 0xFF) );
//Send_Char_Uart2(final_16bit_data);
//Send_Char_Uart2(data);
//Send_Char_Uart2(reg);
RESET = 1;
for(i=0;i<16;i++)
{
if(final_16bit_data & 0x01)
DATA_IO=1;
else
DATA_IO=0;
clock();
final_16bit_data >>=1;
}
RESET = 0;
Send_Char_Uart2(final_16bit_data);
delay();
}
unsigned char rtc_read(unsigned char reg)
{
unsigned char i, data;
//Send_Char_Uart2(reg);
reg = ((reg << 1) | 0x81);
// Send_Char_Uart2(reg);
data = 0;
RESET = 1;
for(i=0;i<8;i++)
{
if(reg & 0x01)
DATA_IO=1;
else
DATA_IO=0;
clock();
reg >>=1;
}
for(i=0;i<8;i++)
{
data <<=1;
data |= DATA_IO;
clock();
}
RESET = 0;
delay();
return reg;
}
void dis_prot_en_osc(void)
{
rtc_write(0x00, 0x00); //disable write protection
rtc_write(0x80, 0x00); //enable oscillator
}
void en_prot(void)
{
rtc_write(0x8E, 0x00); //enable write protection
}
void set_time(char hour, char min, char sec, char hour_mode, char am_or_pm) //takes decimal values...
{
char seconds=0, minutes=0, hours=0;
seconds = ( (((sec/10) & 0x7) << 4) | ((sec%10) & 0xF) );
// Send_Char_Uart2(seconds);
minutes = ( (((min/10) & 0x7) << 4) | ((min%10) & 0xF) );
//Send_Char_Uart2(minutes);
if(hour_mode == HOUR_MODE_12)
hours = ( (HOUR_MODE_12 << 7) |
(0x0 << 6) |
(am_or_pm << 5) |
(((hour/10) & 0x1) << 4) |
((hour%10) & 0xF) );
else //24 hour mode
hours = ( (HOUR_MODE_12 << 7) |
(0x0 << 6) |
(((hour/10) & 0x3) << 4) |
((hour%10) & 0xF) );
dis_prot_en_osc();
rtc_write(0x80, seconds);
rtc_write(0x82, minutes);
rtc_write(0x84, hours);
en_prot();
}
void get_time(void) //output will be in decimal
{
char seconds=0, minutes=0, hours=0;
char second_output, minute_output, hour_output, hour_mode_output, am_or_pm_output;
seconds = rtc_read(0x81);
minutes = rtc_read(0x83);
hours = rtc_read(0x85);
second_output = ( (((seconds >> 4) & 0x7)*10) + (seconds & 0xF) );
minute_output = ( (((minutes >> 4) & 0x7)*10) + (minutes & 0xF) );
Send_Char_Uart2(second_output); // printf statement
Send_Char_Uart2(minute_output); // printf statement
hour_mode_output = ((hours >> 7) & 0x1) ? HOUR_MODE_12 : HOUR_MODE_24;
if(hour_mode_output == HOUR_MODE_12)
{
hour_output = ( (((hours >> 4) & 0x1)*10) + (hours & 0xF) );
am_or_pm_output = ((hours >> 5) & 0x1) ? PM : AM;
}
else //24 hour mode
{
hour_output = ( (((hours >> 4) & 0x3)*10) + (hours & 0xF) );
Send_Char_Uart2(hour_output); // printf statement
}
}