Most of the PIC24 microcontrollers comes with an on chip RTC. Here is a library to interface the RTC making use of mikroPascal.
I'm busy with the alarm interrupt and will post the code as soon as it is ready.
Please scroll down for new version
Code: Select all
//******************************************************************************
// PIC24FJxx - Real time clock library
//
// Author: Anton Rieckert Email: anton@riecktron.co.za
// Date: 22 September 2008
// Webpage: www.riecktron.co.za
//
// Compiler: mikroPascal for dsPIC v 6.01
//
//******************************************************************************
unit PIC24_RTC;
implementation
//******************************************************************************
// Sets the time on the RTC
//******************************************************************************
Procedure PIC24_RTU_SetTime(hour, min, sec : byte);
var tempVal : word;
Begin
hour := ((hour DIV 10) shl 4) or (hour MOD 10);
min := ((min DIV 10) shl 4) or (min MOD 10);
sec := ((sec DIV 10) shl 4) or (sec MOD 10);
SetBit(RCFGCAL, 8);
ClearBit(RCFGCAL, 9);
tempVal := RTCVAL AND 0x0700;
SetBit(RCFGCAL, 8);
ClearBit(RCFGCAL, 9);
RTCVAL := tempVal or hour;
RTCVAL := (min shl 8) or sec;
End;
//******************************************************************************
// Read the date on the RTC
//******************************************************************************
Procedure PIC24_RTU_SetDate(day, month, year : byte);
Begin
day := ((day DIV 10) shl 4) or (day MOD 10);
month := ((month DIV 10) shl 4) or (month MOD 10);
year := ((year DIV 10) shl 4) or (year MOD 10);
SetBit(RCFGCAL, 8);
SetBit(RCFGCAL, 9);
RTCVAL := year AND 0x00FF;
RTCVAL := (month shl 8) or day;
End;
//******************************************************************************
// Read the time on the RTC
//******************************************************************************
Procedure PIC24_RTU_GetTime(var hour, min, sec : byte);
Begin
SetBit(RCFGCAL, 8);
ClearBit(RCFGCAL, 9);
hour := lo(RTCVAL);
min := hi(RTCVAL);
sec := lo(RTCVAL);
hour := (((hour AND 0x30) shr 4) * 10) + (hour AND 0x0F);
min := (((min AND 0x70) shr 4) * 10) + (min AND 0x0F);
sec := (((sec AND 0x70) shr 4) * 10) + (sec AND 0x0F);
End;
//******************************************************************************
// Read the date on the RTC
//******************************************************************************
Procedure PIC24_RTU_GetDate(var day, month, year : byte);
Begin
SetBit(RCFGCAL, 8);
SetBit(RCFGCAL, 9);
year := lo(RTCVAL);
month := hi(RTCVAL);
SetBit(RCFGCAL, 9);
ClearBit(RCFGCAL, 8);
day := lo(RTCVAL);
year := (((year AND 0xF0) shr 4) * 10) + (year AND 0x0F);
month := (((month AND 0x10) shr 4) * 10) + (month AND 0x0F);
day := (((day AND 0x30) shr 4) * 10) + (day AND 0x0F);
End;
//******************************************************************************
// Get the weekday number (0 - 6) from RTC
//******************************************************************************
Function PIC24_RTC_GetWeekDay : byte;
Begin
SetBit(RCFGCAL, 8);
ClearBit(RCFGCAL, 9);
result := (RTCVAL shr 8) AND 0x07;
End;
//******************************************************************************
// Init the RTC on a PIC24 microcontroller
//******************************************************************************
Procedure PIC24_RTC_Init;
var tempASM : word;
Begin
tempASM := OSCCON; // Only for linker to include the register
// Unlock the OSCCON register and then enable SOSC to enable the 32.xxxkHz
// crystal for the RTC
asm
disi #7
mov #@OSCCON, W1
mov.b #0x02, W0
mov.b #0x46, W2
mov.b #0x57, W3
mov.b W2, [W1]
mov.b W3, [W1]
mov.b W0, [W1]
end;
tempASM := NVMKEY; // Only for linker to include the register
tempASM := RCFGCAL; // Only for linker to include the register
RCFGCAL := 0x0000;
// Unlock the RCFGCAL register and enable the RTC
asm
PUSH W12
DISI #9
MOV #0x55, W12
MOV W12, NVMKEY
MOV #0xAA, W12
MOV W12, NVMKEY
BSET RCFGCAL, #13
NOP
NOP
BSET RCFGCAL, #15
POP W12
end;
End;
end.