#include <16F877A.h> //Initialization
#include <string.h>
#fuses HS, NOWDT, NOPROTECT, NOLVP, PUT
#use delay (clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(master, sda=PIN_C4, scl=PIN_C3, FAST=100000)
unsigned short read_ds1307(unsigned short address );
void write_ds1307(unsigned short address,unsigned short w_data);
unsigned int sec;
unsigned int min;
unsigned int hrs;
unsigned int day;
unsigned int month;
unsigned int yr;
unsigned int dow;
unsigned int data;
unsigned int test_sec;
unsigned int test_minute;
unsigned int test_hour;
unsigned int test_day;
unsigned int test_date;
unsigned int test_month;
unsigned int test_year;
void write_ds1307(unsigned int day, unsigned int mth, unsigned int year, unsigned int dow, unsigned int hr, unsigned int min, unsigned int sec)
{
sec &= 0x7F;
hr &= 0x3F;
i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(sec); // REG 0
i2c_write(min); // REG 1
i2c_write(hr); // REG 2
i2c_write(dow); // REG 3
i2c_write(day); // REG 4
i2c_write(mth); // REG 5
i2c_write(year); // REG 6
i2c_write(0x80); // REG 7 - Disable squarewave output pin
i2c_stop();
i2c_start();
i2c_write(0xD0); // address DS1307
i2c_write(0); // start from word at address 0
i2c_write(0); // write 0 to REG0 (enable counting + 0 sec)
i2c_stop();
}
void ds1307_get_time()
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_start();
i2c_write(0xD1);
sec = i2c_read(1);
min = i2c_read(1);
hrs = i2c_read(1);
dow = i2c_read(1);
day = i2c_read(1);
month = i2c_read(1);
yr = i2c_read(0);
i2c_stop();
}
void init_ds1307()
{
int seconds = 0;
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_start();
i2c_write(0xD1); // RD from RTC
seconds = i2c_read(0);
i2c_stop();
seconds &= 0x7F;
delay_us(3);
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_write(seconds);
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x07); // Control Register
i2c_write(0x80); // Disable squarewave output pin
i2c_stop();
}
void main()
{ //Start of Main Program
//set_tris_b(0x00);
set_tris_c(0x18);
Delay_ms(1000);
// Set date for -> 15 June 2005 Tuesday
// Set time for -> 15:20:55
write_ds1307(15,6,5,2,15,20,55); //day,mth,yr,dow,hr,min,sec
while(1)
{
Delay_ms(1000);
ds1307_get_time();
printf("\%02d:\%02d:\%02d\n", hrs,min,sec);
}
} //End of program
hi ivalex,
You can check it from 2 ways :
1 ) By hardware : If the output pin of the RTC is configured to generate a clock, detecting a signal there, you can assume that the IC was already started to work.
2 ) By firmware : Just checking if time inside it is compatible to GMT-n ( where n is an integer number ). It means that a valid timezone is programmed, and not spurious data.
+++
...what should I check/bit test to see if there is a time already placed inside the RTC...
...What I want to do is to prevent the MCU to overwrite the current time placed in the RTC...
void main()
{ //Start of Main Program
int sec;
int min;
int hrs;
int day;
int month;
int yr;
int dow;
int test_sec = 0;
int test_minute = 0;
int test_hour = 5;
int test_day = 1;
int test_date = 4;
int test_month = 6;
int test_year = 11;
set_tris_c(0x18);
init_ds1307();
Delay_ms(1000);
/////Next 2 lines prevent overwriting the current time placed in
/////DS1307 by reading the stored time in the DS1307 itself
ds1307_read_store(sec,min,hrs,day,month,yr,dow);
write_ds1307(day,month,yr,dow,hrs,min,sec);
/*
/////For First time writing time in the DS1307
write_ds1307(3,6,11,6,11,25,10);
/////Comment line above to prevent overwriting the current time placed
*/
while(1)
{
Delay_ms(1000);
ds1307_get_time(hrs,min,sec);
ds1307_get_date(day,month,yr,dow);
if(test_sec == sec && test_minute == min && test_hour == hrs && test_day == day && test_date == dow && test_month == month && test_year == yr)
{
output_d(0xff);
}
ds1307_store_time(sec,min,hrs,day,month,yr,dow);
printf("Day:%02d, Month:%02d, Year:%02d\n",day,month,yr);
printf("\%02d:\%02d:\%02d\n", hrs,min,sec);
}
} //End of program
time_t curtime;
struct_tm *t;
curtime = time(NULL);
t=localtime(&curtime);
unsigned int8 Time[] = [B] __time__[/B] ;
ivalex,
I´m not sure if you are using same compiler I worked ( CCS ).
If the case, there are some directives that allow we to get the PC current time.
Follows a piece of code I used to cath it to load DS1307 :
Code:unsigned int8 Time[] = [B] __time__[/B] ;
Check if compiler you´re working have a kind of similar built in macro in a #pragma directive.
+++
unsigned int8 Time[] = __time__ ;
HourDecimal = ( ( ( Time[0]-'0' ) * 10 ) + ( Time[1]-'0' ) ) ;
MinuteDecimal = ( ( ( Time[3]-'0' ) * 10 ) + ( Time[4]-'0' ) ) ;
SecondDecimal = ( ( ( Time[6]-'0' ) * 10 ) + ( Time[7]-'0' ) ) ;
unsigned int8 Data[] = __date__ ;
switch ( Data[3] )
{
case 'J' :
case 'j' :
if ( ( Data[4] == 'A' ) || ( Data[4] == 'a' ) )
{
MesDezena = 0 ;
MesUnidade = 1 ;
}
else
{
if ( ( Data[5] == 'N' ) || ( Data[5] == 'n' ) )
{
MesDezena = 0 ;
MesUnidade = 6 ;
}
else
{
MesDezena = 0 ;
MesUnidade = 7 ;
}
}
break ;
///////////////////
case 'F' :
case 'f' :
MesDezena = 0 ;
MesUnidade = 2 ;
break ;
///////////////////
case 'M' :
case 'm' :
switch ( Data[5] )
{
case 'R' :
case 'r' :
MesDezena = 0 ;
MesUnidade = 3 ;
break ;
case 'I' :
case 'i' :
MesDezena = 0 ;
MesUnidade = 5 ;
break ;
}
break ;
///////////////////
case 'A' :
case 'a' :
switch ( Data[4] )
{
case 'B' :
case 'b' :
MesDezena = 0 ;
MesUnidade = 4 ;
break ;
case 'G' :
case 'g' :
MesDezena = 0 ;
MesUnidade = 8 ;
break ;
}
break ;
///////////////////
case 'S' :
case 's' :
MesDezena = 0 ;
MesUnidade = 9 ;
break ;
///////////////////
case 'O' :
case 'o' :
MesDezena = 1 ;
MesUnidade = 0 ;
break ;
///////////////////
case 'N' :
case 'n' :
MesDezena = 1 ;
MesUnidade = 1 ;
break ;
///////////////////
case 'D' :
case 'd' :
MesDezena = 1 ;
MesUnidade = 2 ;
break ;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?