Code :
program pcf; var Sec, Min, Hr, Day, Mn, Year : byte; txt : string[15]; procedure Zero_fill(var value: array[3] of char); begin if value[1]=' ' then value[1] := 48; value[0] := ':'; end; procedure Read_time(var Sec, Min, Hr, Day, Mn, Year : byte); begin I2C_start; I2C_Wr($A0); I2C_Wr(2); I2C_Repeated_Start; I2C_Wr($A1); sec :=I2C_Rd(1); while I2C_Is_Idle = 0 do nop; min :=I2C_Rd(1); while I2C_Is_Idle = 0 do nop; hr :=I2C_Rd(1); while I2C_Is_Idle = 0 do nop; day :=I2C_Rd(1); while I2C_Is_Idle = 0 do nop; mn :=I2C_Rd(0); while I2C_Is_Idle = 0 do nop; I2C_Stop; end; procedure Transform_time(var Sec, Min, Hr, Day, Mn, Year : byte); begin sec := ((sec and $F0) shr 4)*10 + (sec and $0F); min := ((min and $F0) shr 4)*10 + (min and $0F); hr := ((hr and $F0) shr 4)*10 + (hr and $0F); year := ((day and $C0)) shr 6; day := ((day and $30) shr 4)*10 + (day and $0F); mn := ((mn and $10) shr 4)*10 + (mn and $0F); end;
procedure Display_time(Sec, Min, Hr, Day, Mn, Year : byte); begin ByteToStr(day,txt); Zero_fill(txt); LCD_Out(1,5,txt); ByteToStr(mn,txt); Zero_fill(txt); LCD_Out(1,8,txt); LCD_Chr(1,15,52+year); ByteToStr(hr,txt); Zero_fill(txt); LCD_Out(2,5,txt); ByteToStr(min,txt); Zero_fill(txt); LCD_Out(2,8,txt); ByteToStr(sec,txt); Zero_fill(txt); LCD_Out(2,11,txt); end;
procedure Init; begin OPTION_REG := $7F; LCD_Config(portb,0,2,1,6,5,4,3); I2C_init(100000); txt := 'Date:'; LCD_Out(1,1,txt); Lcd_Chr(1,8,':'); Lcd_Chr(1,11,':'); txt := 'Time:'; LCD_Out(2,1,txt); Lcd_Chr(2,8,':'); Lcd_Chr(2,11,':'); txt := '200'; LCD_Out(1,12,txt); Lcd_Cmd(LCD_CURSOR_OFF); end;
begin Init; while true do begin Read_Time(Sec, Min, Hr, Day, Mn, Year); Transform_Time(Sec, Min, Hr, Day, Mn, Year); Display_Time(Sec, Min, Hr, Day, Mn, Year); Delay_ms(10); end;
end. |