module Real_time_clock
' Compiler mikroBasic for dsPIC
' dim tempVal as word
dim hours, minutes, seconds as byte
dim days, months, years,flag_timer as byte
dim tempVal as word
dim SOUND_Alarm as bit
SOUND as sbit at LATB.9
sub procedure Display_Time()
sub procedure PIC24_RTU_SetTime(dim hours, minutes, seconds as byte)
sub procedure PIC24_RTU_SetDate(dim days, months, years as byte)
sub procedure PIC24_RTU_GetTime() 'dim hours, minutes, seconds as byte)
sub procedure PIC24_RTU_GetDate() 'dim days, months, years as byte)
sub procedure PIC24_RTC_Init ()
sub procedure PIC24_RTC_Unlock()
sub function PIC24_RTC_GetWeekdays () as byte
'sub Procedure PIC24_RTC_Alarm_Int ()
sub Procedure PIC24_RTC_ActivateAlarm(dim alarm_mask, alarm_count as byte,dim alarm_int as boolean)
sub Procedure PIC24_RTC_SetAlarm(dim workdays, days, months, hours, minutes, seconds as byte)
sub Procedure PIC24_RTC_DeactivateAlarm ()
implements
'вывод на экран
sub procedure Display_Time()
Lcd_Out(1,1,chr($E0)+chr($BF)+"_")
Lcd_Chr(1, 4, (days div 10) + 48) ' Print tens digit of day variable
Lcd_Chr(1, 5, (days mod 10) + 48) ' Print oness digit of day variable
Lcd_Chr(1, 6, ".")
Lcd_Chr(1, 7, (months div 10) + 48)
Lcd_Chr(1,8, (months mod 10) + 48)
' Lcd_Chr(1,15, year_ + 56) ' Print year vaiable + 8 (start from year 2008)
Lcd_Out(1,10,"Bp_")
Lcd_Chr(1, 13, (hours div 10) + 48)
Lcd_Chr(1, 14, (hours mod 10) + 48)
Lcd_Chr(1, 15, ":")
Lcd_Chr(1, 16, (minutes div 10) + 48)
Lcd_Chr(1,17, (minutes mod 10) + 48)
Lcd_Chr(1, 18, ":")
Lcd_Chr(1,19, (seconds div 10) + 48)
Lcd_Chr(1,20, (seconds mod 10) + 48)
end sub
'************************************************* *****************************
' запись RTC
'************************************************* *****************************
sub procedure PIC24_RTU_SetTime(dim hours, minutes, seconds as byte)
dim tempVal as word
minutes = ((minutes DIV 10) ‹‹ 4) or (minutes MOD 10)
seconds = ((seconds DIV 10) ‹‹ 4) or (seconds MOD 10)
SetBit(RCFGCAL, 8) 'ClearBit (RCFGCAL,8)
ClearBit (RCFGCAL, 9)
tempVal = RTCVAL AND 0x0700
hours = ((hours DIV 10) ‹‹ 4) or (hours MOD 10)
SetBit(RCFGCAL, 8)
ClearBit (RCFGCAL, 9)
RTCVAL = tempVal or hours
RTCVAL = (minutes ‹‹ 8 ) or seconds
PIC24_RTC_Unlock()
end sub
'************************************************* *****************************
' запись RTC
'************************************************* *****************************
sub procedure PIC24_RTU_SetDate(dim days, months, years as byte)
days = ((days DIV 10) ‹‹ 4) or (days MOD 10)
months = ((months DIV 10) ‹‹ 4) or (months MOD 10)
years = ((years DIV 10) ‹‹ 4) or (years MOD 10)
SetBit(RCFGCAL, 8)
SetBit(RCFGCAL, 9)
RTCVAL = years AND 0x00FF
' ClearBit(RCFGCAL, 8)
' SetBit(RCFGCAL, 9)
RTCVAL = (months ‹‹ 8) or days
PIC24_RTC_Unlock()
end sub
'************************************************* *****************************
' чтение RTC
'************************************************* *****************************
sub procedure PIC24_RTU_GetTime ()
SetBit(RCFGCAL, 8)
ClearBit (RCFGCAL, 9)
hours = lo(RTCVAL)
seconds = lo(RTCVAL)
minutes = hi(RTCVAL)
hours = (((hours AND 0x30) ›› 4) * 10) + (hours AND 0x0F) '
minutes = (((minutes AND 0x70) ›› 4) * 10) + (minutes AND 0x0F) '
seconds = (((seconds AND 0x70) ›› 4) * 10) + (seconds AND 0x0F) '
End sub
'************************************************* *****************************
' чтение RTC
'************************************************* *****************************
sub procedure PIC24_RTU_GetDate() ' dim days, months, years as byte)
dim tempVal as word
SetBit(RCFGCAL, 8) '
SetBit(RCFGCAL, 9)
years = lo(RTCVAL)
months = hi(RTCVAL)
SetBit(RCFGCAL, 9)
ClearBit(RCFGCAL, 8)
days = lo(RTCVAL) '
' '
years = (((years AND 0xF0) ›› 4) * 10) + (years AND 0x0F) '
months = (((months AND 0x10) ›› 4) * 10) + (months AND 0x0F) '
days = (((days AND 0x30) ›› 4) * 10) + (days AND 0x0F) '
End sub'
'************************************************* *****************************
' инициализация the RTC on a PIC24 microcontroller
' Function: RTCCInit
' Preconditions: RTCCInit must be called before.
' Overview: Enable the oscillator for the RTCC
' Input: None.
' Output: None.
'************************************************* ******************/
sub procedure PIC24_RTC_Init ()
' Enables the LP OSC for RTCC operation
asm
' move address of OSCCON to W1
mov #0x0742, W1
' move 8-bit literal to W0, 16-bit.
mov.b #0x02, W0
'unlock byte 1 for OSCCONL(low byte)
mov.b #0x46, W2
' unlock byte 2 for OSCCONL(low byte)
mov.b #0x57, W3
' move 8-bit of Wn to OSCCON register
' write unlock byte 1
mov.b W2, [W1]
' write unlock byte 2
mov.b W3, [W1]
' enable SOSCEN
mov.b W0, [W1]
end asm
' Unlock sequence must take place for RTCEN to be written
RCFGCAL = 0
PIC24_RTC_Unlock()
RCFGCAL.RTCEN = 1 ' bit15
'RTCC pin pad conected to RTCC second clock
PADCFG1.RTSECSEL = 1
'RCFGCAL.RTCOE = 1 'RTCC Output Enable bit
'* Enable the RTCC interrupt*/
IFS3.RTCIF = 0 'clear the RTCC interrupt flag
IEC3.RTCIE = 1 ' enable the RTCC interrupt
End sub
'************************************************* ********************
' Function: RTCCUnlock
' Preconditions: None.
' Overview: The function allows a writing into the clock registers.
' Input: None.
' Output: None.
'************************************************* ******************/
sub procedure PIC24_RTC_Unlock()
asm
'move the address of NVMKEY into W1
MOV #0x0766, W1
'start 55/AA sequence
MOV #0x55, W2
MOV W2, [W1]
MOV #0xAA, W3
MOV W3, [W1]
'set the RTCWREN bit
BSET RCFGCAL, #13
end asm
end sub
'************************************************* *****************************
' дни недели (0 - 6) îò RTC
'************************************************* *****************************
sub function PIC24_RTC_GetWeekdays () as byte
SetBit(RCFGCAL, 8)
ClearBit(RCFGCAL, 9)
result = (RTCVAL ›› 8) AND 0x07
End sub
''************************************************ ******************************
'' Interrupt procedure for the RTC Alarm
''************************************************ ******************************
'sub Procedure PIC24_RTC_Alarm_Int () org $90
' 'PLACE YOUR CODE HERE TO EXECUTE WHEN THE ALARM IS TRIGERED
' flag_timer = 1 'ôëàã âðàùåíèÿ ëîòêà
' inc(tempDat)
' if tempDat › 288 then
' inc(DW)
' end if
' IFS3 = IFS3 AND 0xBFFF ' Clear RTC flag
'End sub
'************************************************* *****************************
' Activates the alarm with given Alarm Mask Configuration
' %00000000 = Every half second
' %00000001 = Every second
' %00000010 = Every 10 seconds
' %00000011 = Every minute
' %00000100 = Every 10 minutes
' %00000101 = Every hour
' %00000110 = Once a day
' %00000111 = Once a week
' %00001000 = Once a month
' %00001001 = Once a year (except when configured for February 29th, once every 4 years)
'************************************************* *****************************
sub Procedure PIC24_RTC_ActivateAlarm(dim alarm_mask, alarm_count as byte,dim alarm_int as boolean)
ALCFGRPT = 0xC000 OR ((alarm_mask AND 0x0F) ‹‹ 10)
if alarm_int then '' If true, we activate the interrupt
IFS3 = IFS3 AND 0xBFFF ' Clear RTC flag
IEC3 = IEC3 OR 0x4000 ' Enable RTC Int
End if
End sub
'************************************************* *****************************
' Set the time and date the alarm should interrupt. Please consult the datasheet
' for the parameters that is required by the alarm mask specified
'************************************************* *****************************
sub Procedure PIC24_RTC_SetAlarm(dim workdays, days, months, hours, minutes, seconds as byte)
dim tempALCF as word
tempALCF = ALCFGRPT
ALCFGRPT = 0 ' Deactivate the alarm to set time
SetBit(ALCFGRPT, 9)
ClearBit(ALCFGRPT, 8)
hours = ((hours DIV 10) ‹‹ 4) or (hours MOD 10)
minutes = ((minutes DIV 10) ‹‹ 4) or (minutes MOD 10)
seconds= ((seconds DIV 10) ‹‹ 4) or (seconds MOD 10)
workdays= (((workdays AND 0xF0) ›› 4) * 10) + (workdays AND 0x0F)
months = (((months AND 0x10) ›› 4) * 10) + (months AND 0x0F)
days = (((days AND 0x30) ›› 4) * 10) + (days AND 0x0F)
ALRMVAL = (days ‹‹ 8) or months
ALRMVAL = (hours ‹‹ 8) or workdays
ALRMVAL = (seconds ‹‹ 8) or minutes
ALCFGRPT = tempALCF ' Activate the alarm again
End sub
'************************************************* *****************************
' Deactivates the alarm and it's interrupt
'************************************************* *****************************
sub Procedure PIC24_RTC_DeactivateAlarm ()
ALCFGRPT = 0x0000
IEC3 = IEC3 AND 0xBFFF ' Disable RTC Int
IFS3 = IFS3 AND 0xBFFF ' Clear RTC flag
End sub
end. |