• Pic® Basic


  • Using the RTC on a 18F65K22

    I have a routine for RTC PIC18F24J11. I think the internal RTC is the same in 65K22.

    For Write RTC

    '************************************************* ************************
    '** SUBRUTINA: EscribirFechaHora
    '** Escribe la fecha y hora del RTC interno
    '**
    '************************************************* ************************

    EscribirFechaHora:
    RTCPTR1 = 1 'Ajusto puntero al valor más alto para leer el año
    RTCPTR0 = 1 'Cada vez que leo RTCVALH, el puntero decrementa en 1 hasta que llega a 00

    'Ajusto RTCWREN a 1 para permitir escribir en los registros
    MOVLB 0x0f
    MOVLW 0x55
    MOVWF EECON2,0
    MOVLW 0xAA
    MOVWF EECON2,0
    BSF RTCCFG,5,1

    RTCEN = 1 'RTCC Enable bit

    Tempo = Anno
    GOSUB DECaBCD
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    RTCVALL = Tempo 'escribo año
    RTCVALH = Tempo 'Simplemente decrementa el puntero a 10
    Tempo = Dia
    GOSUB DECaBCD
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    RTCVALL = Tempo 'escribo dia
    Tempo = Mes
    GOSUB DECaBCD
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    RTCVALH = Tempo 'escribo mes y decremanta puntero a 01
    Tempo = Hora
    GOSUB DECaBCD
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    RTCVALL = Tempo 'escribo hora
    Tempo = DiaSemana
    GOSUB DECaBCD
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    RTCVALH = Tempo 'escribo dia de la semana y decremanta puntero a 00
    Tempo = Segundos
    GOSUB DECaBCD
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    RTCVALL = Tempo 'escribo segundos
    Tempo = Minutos
    GOSUB DECaBCD
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    RTCVALH = Tempo 'escribo minutos

    'Ajusto RTCWREN a 0 para permitir escribir en los registros
    RTCWREN = 0


    RETURN

    '** FIN SUBRUTINA: EscribirFechaHora
    For read RTC:

    '************************************************* ************************
    '** SUBRUTINA: LeerFechaHora
    '** Lee la fecha y hora del RTC interno
    '**
    '************************************************* ************************

    LeerFechaHora:
    RTCPTR1 = 1 'Ajusto puntero al valor más alto para leer el año
    RTCPTR0 = 1 'Cada vez que leo RTCVALH, el puntero decrementa en 1 hasta que llega a 00
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    Tempo = RTCVALL 'Leo año
    GOSUB BCDaDEC
    Anno = Tempo
    Tempo = RTCVALH 'Simplemente decrementa el puntero a 10
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    Tempo = RTCVALL 'Leo día
    GOSUB BCDaDEC
    Dia = Tempo
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    Tempo = RTCVALH 'Leo mes y decrementa puntero a 01
    GOSUB BCDaDEC
    Mes = Tempo
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    Tempo = RTCVALL 'Leo hora
    GOSUB BCDaDEC
    Hora = Tempo
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    Tempo = RTCVALH 'Leo dia de la semana y decrementa puntero a 00
    GOSUB BCDaDEC
    DiaSemana = Tempo
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    Tempo = RTCVALL 'Leo segundos
    GOSUB BCDaDEC
    Segundos = Tempo
    WHILE RTCSYNC = 1
    'Espero una ventana segura para evitar leer un valor malo. Cuando RTCSYNC = 0 es seguro
    WEND
    Tempo = RTCVALH 'Leo minutos
    GOSUB BCDaDEC
    Minutos = Tempo

    RETURN

    '** FIN SUBRUTINA: LeerFechaHora
    BCD to DEC:
    '************************************************* ************************
    '** SUBRUTINA: BCDaDEC
    '** Convierte un byte codificado en BCD en un byte codificado en binario
    '**
    '************************************************* ************************

    BCDaDEC:

    Tempo2=Tempo & $0F
    Tempo3=Tempo & $F0
    Tempo3=Tempo3>>4
    Tempo3=Tempo3 * 10
    Tempo=Tempo2 + Tempo3


    RETURN

    '** FIN SUBRUTINA: BCDaDEC
    DEC to BCD:
    '************************************************* ************************
    '** SUBRUTINA: DECaBCD
    '** Convierte un byte codificado en binario en un byte codificado en BCD
    '** Variable de entrada: Tempo. Variable de salida:Tempo
    '************************************************* ************************

    DECaBCD:

    Tempo2=DIG Tempo,0
    Tempo3=DIG Tempo,1
    Tempo3=Tempo3<<4
    Tempo=Tempo2 ^ Tempo3


    RETURN

    '** FIN SUBRUTINA: DECaBCD
    Variables:

    DIM Hora AS BYTE 'autoexplicativo
    DIM Minutos AS BYTE
    DIM Segundos AS BYTE
    DIM DiaSemana AS BYTE
    DIM Dia AS BYTE
    DIM Mes AS BYTE
    DIM Anno AS BYTE
    Register initialization:
    'Registro RTCCFG: RTCC CONFIGURATION REGISTER (BANKED F3Fh)
    RTCEN = 1 'RTCC Enable bit; 1 = RTCC module is enabled;0 = RTCC module is disabled
    'symbol bit 6 Unimplemented: Read as ‘ 0’
    RTCWREN = 0 'RTCC Value Registers Write Enable bit; 1 = RTCVALH and RTCVALL registers can be written to by the user; 0 = RTCVALH and RTCVALL registers are locked out from being written to by the user
    RTCSYNC = 0 'RTCC Value Registers Read Synchronization bit; 1 = RTCVALH, RTCVALL and ALRMRPT registers can change while reading due to a rollover ripple resulting in an invalid data read; If the register is read twice and results in the same data, the data can be assumed to be valid. 0 = RTCVALH, RTCVALL or ALCFGRPT registers can be read without concern over a rollover ripple
    HALFSEC = 0 'Half-Second Status bit; 1 = Second half period of a second; 0 = First half period of a second
    RTCOE = 0 'RTCC Output Enable bit; 1 = RTCC clock OUTPUT enabled; 0 = RTCC clock OUTPUT disabled
    RTCPTR1 = 0 ' <1:0>: RTCC Value Register Window Pointer bits; Points TO the corresponding RTCC Value registers when reading the RTCVALH<7:0> AND RTCVALL<7:0> registers; the RTCPTR<1:0> value decrements on every read or write of
    RTCPTR0 = 0 'RTCVALH<7:0> UNTIL it reaches ‘00’.
    'RTCVALH<7:0> :
    '00 = Minutes
    '01 = Weekday
    '10 = Month
    '11 = Reserved
    'RTCVAL L<7 : 0>:
    '00 = Seconds
    '01 = Hours
    '10 = Day
    '11 = Yea

    'Registro RTCCAL: RTCC CALIBRATION REGISTER (BANKED F3Eh)
    CAL7 = 0 'RTC Drift Calibration bits
    CAL6 = 0 '01111111 = Maximum positive adjustment; adds 508 RTC clock pulses every minute
    CAL5 = 0 '...
    CAL4 = 0 '00000001 = Minimum positive adjustment; adds four RTC clock pulses every minute
    CAL3 = 0 '00000000 = No adjustment
    CAL2 = 0 '11111111 = Minimum negative adjustment; subtracts four RTC clock pulses every minute
    CAL1 = 0 '...
    CAL0 = 0 '10000000 = Maximum negative adjustment; subtracts 512 RTC clock pulses every minute

    'Registro PADCFG1: PAD CONFIGURATION REGISTER (BANKED F3Ch)
    'symbol bit 7-3 Unimplemented: Read as ‘ 0’
    RTSECSEL1 = 0 'RTCC Seconds Clock Output Select bits
    RTSECSEL0 = 0 '11 = Reserved, do not use; 10 = RTCC source clock is selected for the RTCC pin (pin can be INTRC or T1OSC, depending on the RTCOSC (CONFIG3L<1>) setting)
    '01 = RTCC seconds clock is selected for the RTCC pin; 00 = RTCC alarm pulse is selected for the RTCC pin
    PMPTTL = 0 'PMP Module TTL Input Buffer Select bit; 1 = PMP module uses TTL input buffers; 0 = PMP module uses Schmitt input buffer

    'Registro ALRMCFG: ALARM CONFIGURATION RE GISTER (ACCESS F91h)
    ALRMEN = 0 'Alarm Enable bit; 1 = Alarm is enabled (cleared automatically after an alarm event whenever ARPT<7:0> = 0000 0000; and CHIME =0); 0 = Alarm is disabled
    CHIME = 0 'Chime Enable bit; 1 = Chime is enabled; ALRMRPT<7:0> bits are allowed to roll over from 00h to FFh; 0 = Chime is disabled; ALRMRPT<7:0> bits stop once they reach 00h
    AMASK3 = 0 'Alarm Mask Configuration bits
    AMASK2 = 0 '0000 = Every half second
    AMASK1 = 0 '0001 = Every second
    AMASK0 = 0 '0010 = Every 10 seconds
    '0011 = Every minute
    '0100 = Every 10 minutes
    '0101 = Every hour
    '0110 = Once a day
    '0111 = Once a week
    '1000 = Once a month
    '1001 = Once a year (except when configured for February 29th, once every four years)
    '101x = Reserved – do not use
    '11xx = Reserved – do not use
    ALRMPTR1 = 0 'Alarm Value Register Window Pointer bits
    ALRMPTR0 = 0 'Points to the corresponding Alarm Value registers when reading the ALRMVALH and ALRMVALL
    'registers. The ALRMPTR<1:0> value decrements on every read or write of ALRMVALH until it reaches ‘00’.
    'ALRMVALH<1 5: 8>:
    '00 = ALRMMIN
    '01 =ALRMWD
    '10 =ALRMMNTH
    '11 = Unimplemented
    'ALRMVALL<7 : 0>:
    '00 = ALRMSEC
    '01 =ALRMHR
    '10 =ALRMDAY
    '11 = Unimplemented

    'Registro ALRMRPT: ALARM CA LIBRATION REGISTER (ACCESS F90h)
    ARPT7 = 0 'Alarm Repeat Counter Value bits
    ARPT6 = 0 '11111111 = Alarm will repeat 255 more times
    ARPT5 = 0 ' .....
    ARPT4 = 0 '00000000 = Alarm will not repeat
    ARPT3 = 0 'The counter decrements on any alarm event. The counter is prevented from rolling over from 00h to
    ARPT2 = 0 'FFh unless CHIME =1.
    ARPT1 = 0
    ARPT0 = 0

    Regards,
    Gamboa
    This article was originally published in forum thread: Using the RTC on a 18F65K22 started by SimonJA View original post