Aqui os dejo OTRO ejemplo de usar el DS1305 rtc con un 18f4550 , esta vez nada de simulación de módulo SPI por software. Este va con su módulo MSSP por hardware y funciona 10 puntos. Probado.
;Short example assembler code
;Interfacing a DS1305 with a pic16f4550 via SPI hardware master mode
LIST P=18F4550
#include <P18F4550.INC>
CONFIG FOSC = HS ;Osc por XTAL 20mhz
CONFIG DEBUG = OFF
CONFIG PWRT = OFF
CONFIG BOR = OFF
CONFIG WDT = OFF
CONFIG MCLRE = ON
CONFIG PBADEN = OFF
CONFIG LVP = OFF
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
#define CSRTC LATA,2 ; OUT RA2 as CE
#define SDI LATB,0 ; IN to SDO of Ds1305
#define SCK LATB,1 ; OUT to SCLK ds1305
#define SDO LATC,7 ; OUT to SDI of DS1305
cblock 0x00
SEC
MIN
HOUR
DAY
DOW
MON
YRS
outbyte
rxdata
endc
CONTROL equ 0x8f ; (INT disabled, OSC on, WP disabled)
WRITESEC equ 0x80
;***********************************
;ACTUALICE THESE WITH ACTUAL TIME BEFORE BURNING PIC !!
;***********************************
ACTUALYRS equ 09h ;years
ACTUALMON equ 02h ;month
ACTUALDAY equ 10h ;day
ACTUALDOW equ 03h ;day of the week
ACTUALHOUR equ 15h ;hours
ACTUALMIN equ 34h ;minutes
ACTUALSEC equ 00h ;seconds
ORG 00h
goto Ini
Ini
CLRF BSR
CLRF LATA
CLRF LATB
CLRF LATC
CLRF LATD
CLRF LATE
MOVLW b'00001111' ;
MOVWF ADCON1 ;
MOVLW b'11111011' ; TRISA
MOVWF TRISA ; RA2 output
MOVLW b'11111101' ;
movwf TRISB ; PortB
MOVLW b'00110111' ;
movwf TRISC ; PortC
movlw b'00000000'
movwf TRISD ; PortD
movlw 0x00
movwf TRISE ; Puerto E
movlw 0x07
movwf CMCON ;
movlw 0
movwf INTCON ; disables all interrupts
movwf SPPCON
movwf SSPCON1 ; clear SSP control register
movwf PIE1 ; disable peripheral interrupts
movwf SSPSTAT ; clear SSP status register
movwf SPPCFG
movwf UCON
movwf ADCON0
movlw 0x31 ; SPI master, clk/16, ckp=1
movwf SSPCON1 ; SSPEN enabled
movlw 0x80 ; set up spi port, SPI master,
movwf SSPSTAT ; cke = 0 (mode 1,1)
bcf CSRTC
Main
;Write burst of data on the DS1305
call RTC_brst_wr
;Read burst of data from DS1305
;Data are stored on YRS,MON,DAY, etc variables
stop call RTC_brst_rd
;Example showing YEARS on PORTD
movf SEC,w
movwf LATD
goto stop
;*******************************************************************************************************************
;*******************************************************************************************************************
;Burst write
RTC_brst_wr
bsf CSRTC ;
movlw CONTROL ;
movwf outbyte
call output
movlw 0 ;
movwf outbyte
call output
bcf CSRTC ;
nop
nop
bSf CSRTC ; chip select active
movlw WRITESEC ; send seconds register write address
movwf outbyte
call output ; call the SPI output routine
movlw ACTUALSEC
movwf outbyte ;
call output ; call the SPI output routine
movlw ACTUALMIN
movwf outbyte ;
call output ; call the SPI output routine
movlw ACTUALHOUR
movwf outbyte ;
call output ; call the SPI output routine
movlw ACTUALDOW
movwf outbyte ;
call output ; call the SPI output routine
movlw ACTUALDAY
movwf outbyte ;
call output ; call the SPI output routine
movlw ACTUALMON
movwf outbyte ;
call output ; call the SPI output routine
movlw ACTUALYRS
movwf outbyte ;
call output ; call the SPI output routine
bcf CSRTC ; clear the chip select
return
;Burst read
RTC_brst_rd
bsf CSRTC ; clear the chip select line
movlw 0h ; seconds register read address
movwf outbyte ; store in RAM location outbyte
call output ; call the SPI output routine
call output ; call the SPI output routine
movff rxdata, SEC ; save it
call output ; call the SPI output routine
movff rxdata, MIN
call output ; call the SPI output routine
movff rxdata, HOUR
call output ; call the SPI output routine
movff rxdata, DOW
call output ; call the SPI output routine
movff rxdata, DAY
call output ; call the SPI output routine
movff rxdata, MON
call output ; call the SPI output routine
movff rxdata, YRS
bcf CSRTC ; de-assert CE
return
;*******************SPI output subroutine***********************
output
movf outbyte,W ; move outbyte into w
movwf SSPBUF ; place data in send buffer
loop1
btfss SSPSTAT,BF ; has data been received?
goto loop1 ; loop if not received yet
movf SSPBUF,W ; empty the receive buffer
movwf rxdata ; put received byte into rxdata
retlw 0 ; return from subroutine
end