Autor Tema: DS1305 RTC con PIC18f4550 usando SPI por hardware  (Leído 5002 veces)

0 Usuarios y 2 Visitantes están viendo este tema.

Desconectado pibe

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 635
DS1305 RTC con PIC18f4550 usando SPI por hardware
« en: 11 de Febrero de 2009, 13:39:07 »
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
Mi jefe mirando el prototipo que estoy creando: "Y eso va a funcionar?"

Desconectado groundman

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1870
    • www.ingeniopic.com
Re: DS1305 RTC con PIC18f4550 usando SPI por hardware
« Respuesta #1 en: 09 de Agosto de 2010, 11:15:11 »
hola pibe.he querido usar tu codigo.
pero he visto un par de errores.

 movlw 0
 y
bcf  CSRTC   

son un par de lineas que no estan acabadas del todo.
Montando mi primera impresora 3D (Raprep Prusa i3)

Desconectado jim_17

  • Colaborador
  • PIC18
  • *****
  • Mensajes: 309
    • Blog personal
Re: DS1305 RTC con PIC18f4550 usando SPI por hardware
« Respuesta #2 en: 09 de Agosto de 2010, 16:15:43 »
Groundman fijate que CSRTC esta definido arriba como LATA,2 (#DEFINE CSRTC LATA,2) entonces si que esta bien empleado el 'bcf CSRTC'.
Share the world !

Blog's

http://www.area0x33.com

Desconectado groundman

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1870
    • www.ingeniopic.com
Re: DS1305 RTC con PIC18f4550 usando SPI por hardware
« Respuesta #3 en: 12 de Agosto de 2010, 10:04:19 »
 :tongue: no me habia dado cuenta.

ademas parece que movlw 0  es igual que un movlw 0x00.se ve que el ensamblador coje los valores como exagesimales por defecto.

aunque no se porque las lineas de abajo ubican los registros tan desordenados.

;***********************************
;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


supongo que ha querido dar unos valores a los registros.y en su lugar lo que ha pasado es que los valores han cogido posiciones de la memoria.
Montando mi primera impresora 3D (Raprep Prusa i3)