program Kapcsoloora;

type
  TTime = record
    year, month, day, hours, minutes, seconds : byte;
end;

var TimeRead : TTime;
    yearmod4 : byte;
    byteRead : byte;
    txt : string[3];
    old_sec: byte;


procedure settime();
var TimeToWrite:Ttime;
begin
   TimeToWrite.year    := 9;    // offset from 2000
   TimeToWrite.month   := 2;
   TimeToWrite.day     := 25;
   TimeToWrite.hours   := 20;
   TimeToWrite.minutes := 55;
   TimeToWrite.seconds := 00;


   Soft_I2C_Start;            // issue start signal
   Soft_I2C_Write($A0);          // address PCF8583
   Soft_I2C_Write(0);            // start from word at address 0 (configuration word)
   Soft_I2C_Write($80);          // write $80 to config. (pause counter...)
   Soft_I2C_Write(0);            // write 0 to cents word
   Soft_I2C_Write((TimeToWrite.seconds div 10) shl 4 + (TimeToWrite.seconds mod 10)); // write seconds word
   Soft_I2C_Write((TimeToWrite.minutes div 10) shl 4 + (TimeToWrite.minutes mod 10)); // write minutes word
   Soft_I2C_Write((TimeToWrite.hours div 10) shl 4 + (TimeToWrite.hours mod 10)); // write hours word
   Soft_I2C_Write((TimeToWrite.year mod 4) shl 6 + (TimeToWrite.day div 10) shl 4 +(TimeToWrite.day mod 10));
   Soft_I2C_Write((TimeToWrite.month div 10) shl 4 + (TimeToWrite.month mod 10));  // write weekday/month
   Soft_I2C_Stop;             // issue stop signal

   Soft_I2C_Start;            // issue start signal
   Soft_I2C_Write($A0);          // address PCF8530
   Soft_I2C_Write($10);          // start from word at address 16
   Soft_I2C_Write(TimeToWrite.year);         // write year to RAM
   Soft_I2C_Stop;             // issue stop signal


   Soft_I2C_Start;            // issue start signal
   Soft_I2C_Write($A0);          // address PCF8530
   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;

procedure readtime();
begin

   soft_I2C_start;                 // issue start signal
   soft_I2C_Write($A0);               // address PCF8583
   soft_I2C_Write(2);                 // first word address
   soft_I2C_Start;        // issue repeated start signal
   soft_I2C_Write($A1);               // address PCF8583 for reading R/W=1

   byteRead := soft_I2C_Read(1);     // read seconds byte
   TimeRead.seconds := (byteRead shr 4)*10 + byteRead and 0x0F; // transform seconds


   byteRead := soft_I2C_Read(1);     // read minutes byte
   TimeRead.minutes := (byteRead shr 4)*10 + byteRead and 0x0F; // transform minutes


   byteRead := soft_I2C_Read(1);     // read hours byte
   TimeRead.hours := (byteRead shr 4)*10 + byteRead and 0x0F; // transform hours

   byteRead := soft_I2C_Read(1);     // read year/day byte
   TimeRead.day := ((byteRead and %00110000) shr 4)*10 + byteRead and 0x0F; // transform day
   yearmod4 := (byteRead and %11000000) shr 6;   // get year mod 4 from RTC

   byteRead := soft_I2C_Read(0);     // read weekday/month byte
   TimeRead.month := ((byteRead and %00010000) shr 4)*10 + byteRead and 0x0F; // transform month

   soft_I2C_Stop;

   soft_I2C_start;                 // issue start signal
   soft_I2C_Write($A0);               // address PCF8583
   soft_I2C_Write($10);               // first word address
   soft_I2C_Start;        // issue repeated start signal
   soft_I2C_Write($A1);               // address PCF8583 for reading R/W=1

   byteRead := soft_I2C_Read(0);     // read year
   if yearmod4 <> (byteRead mod 4) then // check if year is incremented in RTC
       Inc(byteRead);  // in this case the new value should be written to RTC RAM at address 16(0x10)
                       // see rtc_write example
   TimeRead.year := byteRead;


   soft_I2C_Stop;


end;

procedure displaytime();
var st:string[10];
begin


if old_sec<>TimeRead.Seconds then
begin
     old_sec:=TimeRead.Seconds;
   st:='          ';
   txt:='';
    //Lcd_cmd(LCD_CLEAR);
//- év
   ByteToStr(TimeRead.Year, txt);
   if txt[1]=' ' then txt[1]:='0';
   st[0]:='2';
   st[1]:='0';
   st[2]:=txt[1];
   st[3]:=txt[2];
   st[4]:='-';
//- hónap
   ByteToStr(TimeRead.Month, txt);
   if txt[1]=' ' then txt[1]:='0';

   st[5]:=txt[1];
   st[6]:=txt[2];
   st[7]:='-';

//- nap
   ByteToStr(TimeRead.Day, txt);
   if txt[1]=' ' then txt[1]:='0';

   st[8]:=txt[1];
   st[9]:=txt[2];

   LCD_Out(1, 3, st);

   st:='          ';
   txt:='';
//- óra
   ByteToStr(TimeRead.Hours, txt);
   if txt[1]=' ' then txt[1]:='0';

   st[0]:=txt[1];
   st[1]:=txt[2];
   st[2]:=':';
//- perc
   ByteToStr(TimeRead.Minutes, txt);
   if txt[1]=' ' then txt[1]:='0';

   st[3]:=txt[1];
   st[4]:=txt[2];
   st[5]:=':';

//- másodperc
   ByteToStr(TimeRead.Seconds, txt);
   if txt[1]=' ' then txt[1]:='0';

   st[6]:=txt[1];
   st[7]:=txt[2];

   LCD_Out(2, 4, st);

end;

end;

procedure init();
begin

  CM1CON0:=0;
  CM2CON0:=0;
  CM2CON1:=0;
  ANSEL:=0;
  ANSELH:=0;
  TRISC:=0;
  PORTC:=0;
  TRISB:=0;
  PORTB:=0;

  Soft_I2C_Config(PortB,6,4);
  Lcd_Config(PORTC, 3, 2, 1, 0, PORTC, 4, 7, 5);
  Lcd_Cmd(LCD_CURSOR_OFF);

  //settime();

end;


begin

     init();

     while true do
     begin
         readtime();
         displaytime();
     end;


end.