Un mensaje que subí hace tiempo al foro de Mikropascal, donde se muestra cómo usar un chip reloj-calendario (RTC) con un microcontrolador PIC, y adjunto el código fuente en Pascal que escribí. Espero que le sirva a alguien.
Saludos !
----------------------------------------------------------
Schmatic:
HT1380 Descripcion and datasheet:
http://www.holtek.com.tw/english/doc...mer/1380_1.htm
Code for ht1380.ppas unit:
Código Delphi
[-]
Unit HT1380;
Implementation
Var
BitNum: Byte;
Const DaysNames: Array [1..7] Of String[3] =('Sat','Sun','Mon','Tue','Wed','Thu','Fri');
Function Copy_bits(Source, Start, Bits_to_copy: Byte): Byte;
Begin
Result:=0; For BitNum:=Start To Bits_to_copy - 1 Do Result.BitNum := Source.BitNum;
End;
Procedure SCLK_Pulse;
Begin
ClearBit(PORTE,0); SetBit(PORTE,0); Delay_10us; ClearBit(PORTE,0); Delay_10us; End;
Procedure Send_Byte(Value: Byte);
Begin
ClearBit(TRISE,1); For BitNum:=0 To 7 Do Begin
If Value.BitNum = 1 Then SetBit(PORTE,1)
Else
ClearBit(PORTE,1); SCLK_Pulse; End;
End;
Procedure Send_Command_and_Data(Command, Data: Byte);
Begin
ClearBit(PORTE,0); SetBit(PORTE,2); Send_Byte(Command); Send_Byte(Data); ClearBit(PORTE,2); End;
Function Read_Data(Command: Byte): Byte;
Begin
ClearBit(PORTE,0); SetBit(PORTE,2); Send_Byte(Command); SetBit(TRISE,1); For BitNum:=0 To 7 Do Begin
If TestBit(PORTE,1) = 1 Then
Result.BitNum := 1
Else
Result.BitNum := 0;
SCLK_Pulse; End;
ClearBit(PORTE,2); End;
Function Get_Seconds: Byte;
Begin
Get_Seconds:=Bcd2Dec( Copy_Bits(Read_Data($81), 0, 7));
End;
Function Get_Minutes: Byte;
Begin
Get_Minutes:= Bcd2Dec( Copy_Bits(Read_Data($83), 0, 7));
End;
Function Get_Hours: Byte;
Begin
Get_Hours:= Bcd2Dec( Copy_Bits(Read_Data($85), 0, 5));
End;
Function Get_Day: Byte;
Begin
Get_Day:= Bcd2Dec( Copy_Bits(Read_Data($87), 0, 6));
End;
Function Get_Month: Byte;
Begin
Get_Month:= Bcd2Dec( Copy_bits( Read_Data($89) ,0,5));
End;
Function Get_Day_Of_Week: Byte;
Begin
Get_Day_Of_Week:= Bcd2Dec( Copy_bits( Read_Data($8B) ,0,4));
End;
Function Get_Year: Byte;
Begin
Get_Year:= Bcd2Dec( Read_Data($8D));
End;
Procedure ByteToStr_2(Number: Byte ; Var Destination: String[2]);
Var
TmpString: String[3]; Begin
ByteToStr(Number, TmpString); For BitNum:=0 To 2 Do If TmpString[BitNum]=' ' Then
TmpString[BitNum]:='0';
Destination:=' '; For BitNum:=1 To 2 Do Destination[BitNum - 1]:= TmpString[BitNum];
End;
Procedure Get_Time(Var TimeString: String[8]);
Var
Hour, Minute, Second: String[2];
segundos: string[3];
Begin
TimeString:='';
ByteToStr_2(Get_Hours, Hour); ByteToStr_2(Get_Minutes, Minute);
ByteToStr_2(Get_Seconds, Second);
StrCat(TimeString,Hour); StrCat(TimeString,':');
StrCat(TimeString,Minute);
StrCat(TimeString,':');
StrCat(TimeString,Second);
End;
Procedure Get_Date(Var DateString: String[8]);
Var
Year, Month, Day: String[2];
Begin
DateString:='';
ByteToStr_2(Get_Year, Year); ByteToStr_2(Get_Month, Month);
ByteToStr_2(Get_Day, Day);
StrCat(DateString,Day); StrCat(DateString,'/');
StrCat(DateString,Month);
StrCat(DateString,'/');
StrCat(DateString,Year);
End;
Procedure Set_Time(Hour, Min, Sec: Byte);
Begin
Send_Command_and_Data($80, Dec2Bcd(Sec)); Send_Command_and_Data($82, Dec2Bcd(Min)); Send_Command_and_Data($84, Dec2Bcd(Hour)); End;
Procedure Set_Date(Day, Month, Year: Byte);
Begin
Send_Command_and_Data($86, Dec2Bcd(Day)); Send_Command_and_Data($88, Dec2BCD(Month)); Send_Command_and_Data($8C, Dec2BCD(Year)); End;
Function RTC_Not_Set: Byte;
Begin
If Get_Year = 00 Then Result:=1
Else
Result:=0;
End;
Procedure Rtc_Write_Protect;
Begin
Send_Command_and_Data($8E,$40);
End;
Procedure Rtc_Write_Enable;
Begin
Send_Command_and_Data($8E,$00);
End;
Procedure Initialize_RTC;
Begin
Send_Command_and_Data($8E,$00);
Send_Command_and_Data($80,$00);
Delay_ms(3000);
End;
End.