Code: Select all
DB0 VAR BYTE [8]
I2CREAD SDA,SCL,$D1,$00,[STR DB0\8]
Code: Select all
lcdout $FE, $C0,"Vrijeme:",hex2 DB0[2],":",hex2 DB0[1]
Hvala unaprijed
Moderators: pedja089, stojke369, [eDo], trax
Code: Select all
DB0 VAR BYTE [8]
I2CREAD SDA,SCL,$D1,$00,[STR DB0\8]
Code: Select all
lcdout $FE, $C0,"Vrijeme:",hex2 DB0[2],":",hex2 DB0[1]
Code: Select all
program RTC;
var Tsec,GSec,Gmin,Ghr,Gday,Gdt,Gmn,Gye,add0 : byte;
text : string[3];
txday: string[10];
Procedure Init;
Begin
TrisD:=0; // LCD 4x20 PortD output
TrisB:=0; // LED row, show secoonds PortB output
LCD_Init(PortD);
Lcd_Cmd(LCD_CURSOR_OFF);
Lcd_Cmd(LCD_Clear);
Lcd_Out(1,1,'RTC DS1307-Time/Date');
Lcd_Out(2,1,'Time:');
Lcd_Out(3,1,'Date:');
Lcd_Out(4,1,'Day:');
add0:=0;
I2C_init(100000);
End;
Procedure Show_time;
begin
if GSec<10 then add0:=1;
ByteToStr(Gsec,text);
Lcd_Out(2,12,text);
if add0=1 then lcd_chr(2,13,'0');
add0:=0;
if Gmin<10 then add0:=1;
ByteToStr(Gmin,text);
Lcd_Out(2,9,text);
Lcd_chr(2,12,':');
if add0=1 then lcd_chr(2,10,'0');
add0:=0;
if Ghr<10 then add0:=1;
ByteToStr(Ghr,text);
Lcd_Out(2,6,text);
Lcd_chr(2,9,':');
if add0=1 then lcd_chr(2,7,'0');
add0:=0;
end;
Procedure Show_date;
begin
if Gye<10 then add0:=1;
ByteToStr(Gye,text);
Lcd_Out(3,14,text);
Lcd_Out(3,13,'20');
Lcd_chr(3,17,'.');
if add0=1 then lcd_chr(3,15,'0');
add0:=0;
if Gmn<10 then add0:=1;
ByteToStr(Gmn,text);
Lcd_Out(3,9,text);
Lcd_chr(3,12,'.');
if add0=1 then lcd_chr(3,10,'0');
add0:=0;
if Gdt<10 then add0:=1;
ByteToStr(Gdt,text);
Lcd_Out(3,6,text);
Lcd_chr(3,9,'.');
if add0=1 then lcd_chr(3,7,'0');
add0:=0;
end;
Procedure Show_day;
begin
If Gday=1 then txday:='Ponedeljak';
If Gday=2 then txday:='Utorak ';
If Gday=3 then txday:='Srijeda ';
If Gday=4 then txday:='Cetvrtak ';
If Gday=5 then txday:='Petak ';
If Gday=6 then txday:='Subota ';
If Gday=7 then txday:='Nedelja ';
Lcd_Out(4,7,txday);
end;
Procedure Read_time;
begin
I2C_Start;
I2C_Wr($D0); // Set I2c Unit to write mode
I2C_Wr($00); // Set pointer on I2c Unit
I2C_Repeated_Start;
I2C_Wr($D1); // Set I2c Unit to Read mode
Gsec :=I2C_Rd(1); // Read byte
Gmin :=I2C_Rd(1); // Read next byte
Ghr :=I2C_Rd(1); // Read next byte
Gday :=I2C_Rd(1); // Read next byte
Gdt :=I2C_Rd(1); // Read next byte
Gmn :=I2C_Rd(1); // Read next byte
Gye :=I2C_Rd(0); // Read next byte
I2C_Stop;
PortB:=Gsec;
GSec := Bcd2Dec(Gsec);
Gmin := Bcd2Dec(Gmin);
Ghr := Bcd2Dec(Ghr);
Gdt := Bcd2Dec(Gdt);
Gmn := Bcd2Dec(Gmn);
Gye := Bcd2Dec(Gye);
Show_time;
Show_date;
Show_day;
end;
Procedure Write_time;
begin
I2C_start;
I2C_wr($D0); // write to RTC
I2C_wr(0); // set pointer to 00
I2C_wr(128); // toggle crystal 32.768 on/off bit
I2C_stop;
I2C_start;
I2C_wr($D0); // write to RTC
I2C_wr($01); //
I2C_wr($18); // set min 0..59
I2C_wr($23); // set hour 0..23
I2C_wr($03); // set day 1..7
I2C_wr($14); // set date 1..31
I2C_wr($12); // set month 1..12
I2C_wr($11); // set year 1..99
I2C_Stop(); // Stop I2c BUS
I2C_start;
I2C_wr($D0); // write to RTC
I2C_wr(0); // set pointer to 00
I2C_wr(0); // toggle crystal 32.768 on/off bit
I2C_Stop(); // Stop I2c BUS
end;
begin
Init;
Write_time; // rem this line after set DS1307
Read_time;
while true do
begin
I2C_Start;
I2C_Wr($D0); // Set I2c Unit to write mode
I2C_Wr($00); // Set pointer on I2c Unit
I2C_Repeated_Start;
I2C_Wr($D1); // Set I2c Unit to Read mode
Tsec :=I2C_Rd(0); // Read byte
I2C_Stop;
Tsec := Bcd2Dec(Tsec);
if Tsec<>Gsec then // Novi prikaz na ekranu, samo ako se sec promjenila.
Begin
Tsec:=Gsec;
Read_time;
End;
end;
end.
Code: Select all
I2CWrite SDA, SCL, RTC, SecReg,[sec,mins,hr,day,date,mon,yr,cntrl]
Pause 10
I2CWrite SDA, SCL, RTC, ContReg,[%00000110]
Pause 10
I2CWrite SDA, SCL, RTC, StatusReg,[%00000000]
PAUSE 10
Code: Select all
I2C_Start;
I2C_Wr($D0); // Set I2c Unit to write mode
I2C_Wr($00); // Set pointer on I2c Unit
I2C_Repeated_Start;
I2C_Wr($D1); // Set I2c Unit to Read mode
Gsec :=I2C_Rd(1); // Read byte
Gmin :=I2C_Rd(1); // Read next byte
Ghr :=I2C_Rd(1); // Read next
Gday :=I2C_Rd(1); // Read next
Gdt :=I2C_Rd(1); // Read next
Gmn :=I2C_Rd(1); // Read next
Gye :=I2C_Rd(0); // Read last
I2C_Stop;
Code: Select all
I2C_start;
I2C_wr($D0); // write to RTC
I2C_wr($00); // set pointer to 00
I2C_wr(Gsec); // set sec 0..59
I2C_wr(Gmin); // set min 0..59
I2C_wr(Ghr); // set hour 0..23
I2C_wr(Gday); // set day 1..7
I2C_wr(Gdt); // set date 1..31
I2C_wr(Gmn); // set month 1..12
I2C_wr(Gye); // set year 0..99
I2C_Stop(); // Stop I2c BUS