Moderadores: andre_luis, 51, guest2003, Renie
//**** VARIABLE DEFINITIONS *********
unsigned char sec, min1, hr, day, dow, mn, year;
char *txt, *txt0, *txt1, *txt2, *txt3, *txt4, *txt5, *txt6, *txt8, tnum[4];
char tnum2[4], tnum3[4], tnum4[4], tnum5[4], tnum6[4];
//**** FUNCTION DECLARATIONs ********
void start1307s(); //starting your ds1307 to caunt
void ds1307_init(); //initialization of ds1307
void ds1307_set_date_time(char day,char mth,char year,char dow,char hr,char min, int sec);
//setting date and time
void ds1307_get_time_date(char *day, char *mth, char *year, char *dow, char *hr, char *min, char *sec); //getting date and time from ds1307 to pic
void dateclock();
void Display_Time(char sec, char min, char hr, char day, char mn, char year) ;
void Zero_Fill(char *value);
//**** FUNCTION DEFINITIONs ********
void start1307s(){
//START SIGNAL FOR DS1307***********
Soft_I2C_Start(); // issue start signal
Soft_I2C_Write(0xD0); // address of ds1307
Soft_I2C_Write(0); // start from word at address 0
Soft_I2C_Write(0); // write 0 to config word (enable counting)
Soft_I2C_Stop(); // issue stop signal
//**********************************
}//~end of function
void ds1307_init(){
int seconds=0;
//Soft_I2C_Config(&PORTC, 5, 2); //veja os pinos e o port que vc esta usando
Soft_I2C_Start();
Soft_I2C_Write(0xD0); // WR to RTC
Soft_I2C_Write(0x00); // REG 0
Soft_I2C_Start();
Soft_I2C_Write(0xD1); // RD from RTC
seconds = Bcd2Dec(Soft_I2C_Read(0)); // Read current "seconds" in DS1307
Soft_I2C_Stop();
seconds = (seconds & 0x7F);
Delay_ms(50);
Soft_I2C_Start();
Soft_I2C_Write(0xD0); // WR to RTC
Soft_I2C_Write(0x00); // REG 0
Soft_I2C_Write(Dec2Bcd(seconds)); // Start oscillator with current "seconds value
Soft_I2C_Start();
Soft_I2C_Write(0xD0); // WR to RTC
Soft_I2C_Write(0x07); // Control Register
Soft_I2C_Write(0x80); // Disable squarewave output pin
Soft_I2C_Stop();
}//~end of function
void ds1307_set_date_time(char day,char mth,char year,char dow,char hr,char min, int sec){
sec =(sec & 0x7F);
hr =(hr & 0x3F);
Soft_I2C_Start();
Soft_I2C_Write(0xD0); // I2C write address
Soft_I2C_Write(0x00); // Start at REG 0 - Seconds
Soft_I2C_Write(Dec2Bcd(sec)); // REG 0
Soft_I2C_Write(Dec2Bcd(min)); // REG 1
Soft_I2C_Write(Dec2Bcd(hr)); // REG 2
Soft_I2C_Write(Dec2Bcd(dow)); // REG 3
Soft_I2C_Write(Dec2Bcd(day)); // REG 4
Soft_I2C_Write(Dec2Bcd(mth)); // REG 5
Soft_I2C_Write(Dec2Bcd(year)); // REG 6
Soft_I2C_Write(0x80); // REG 7 - Disable squarewave output pin
Soft_I2C_Stop();
}//~end of function
void ds1307_get_time_date(char *day, char *mth, char *year, char *dow, char *hr, char *min, char *sec){
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(0x00); // Start at REG 3 - Day of week
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
*sec=Bcd2Dec(Soft_I2C_Read(1) & 0x7F);
*min=Bcd2Dec(Soft_I2C_Read(1) & 0x7F);
*hr=Bcd2Dec(Soft_I2C_Read(1) & 0x3F);
*dow=Bcd2Dec(Soft_I2C_Read(1) & 0x7F); // REG 3
*day=Bcd2Dec(Soft_I2C_Read(1) & 0x3F); // REG 4
*mth=Bcd2Dec(Soft_I2C_Read(1) & 0x1F); // REG 5
*year=Bcd2Dec(Soft_I2C_Read(0)); // REG 6}
Soft_I2C_Stop();
}//~end of function
void dateclock(){
ds1307_get_time_date(&day, &mn, &year, &dow, &hr, &min1, &sec);
Display_Time(sec, min1, hr, day, mn, year); // prepare and display on LCD
}//~
void Display_Time(char sec, char min, char hr, char day, char mn, char year) {
T6963C_waitReady();
ByteToStr(day,tnum);
txt0 = rtrim(tnum);
Zero_Fill(txt0);
T6963C_waitReady();
ByteToStr(day,tnum);
txt1 = rtrim(tnum);
Zero_Fill(txt1);
T6963C_write_text(txt1 , 0, 5, 1) ;
T6963C_waitReady();
ByteToStr(mn,tnum);
txt2 = rtrim(tnum);
Zero_Fill(txt2);
T6963C_write_text(txt2 , 3, 5, 1) ;
T6963C_waitReady();
ByteToStr(year,tnum);
txt3 = rtrim(tnum);
Zero_Fill(txt3);
T6963C_write_text(txt3 , 6, 5, 1) ;
T6963C_waitReady();
ByteToStr(hr,tnum);
txt4 = rtrim(tnum);
Zero_Fill(txt4);
T6963C_write_text(txt4 , 0, 6, 1) ;
T6963C_waitReady();
ByteToStr(min,tnum);
txt5 = rtrim(tnum);
Zero_Fill(txt5);
T6963C_write_text(txt5 , 3, 6, 1) ;
T6963C_waitReady();
ByteToStr(sec,tnum);
txt6 = rtrim(tnum);
Zero_Fill(txt6);
T6963C_write_text(txt6 , 6, 6, 1) ;
T6963C_waitReady();
}//~
void Zero_Fill(char *value) { // fill text repesentation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}//~
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante