TRIS setting for the DS1302


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1

    Default TRIS setting for the DS1302

    Hi all

    I wonder if someone could please tell me what the TRIS settings should be to work with the DS1302.
    I have been checking out code examples through the forums and have even tried a few. I am still not sure of what the TRIS settings should be for my PIC to interact with the DS1302.

    So given the following for example :
    Code:
    RST     var     PORTA.2
    IO      var     PORTC.1
    SCLK    var     PORTC.3
    Kind regards

    Dennis

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I2CREAD/WRITE takes care of that for you.
    Dave
    Always wear safety glasses while programming.

  3. #3


    Did you find this post helpful? Yes | No

    Default Oh....

    @Dave thanks a million for that reply, it has sent me hurtling off to understand I2Cread and I2Cwrite.

    Can anyone confirm the use of a DS1302 with I2Cread/WRITE or should I rather be using the SHIFTIN/OUT displayed in most of the forum threads regarding the DS1302.

    I really would appreciate some help regarding the DS1302 and a kickstart of sorts.

    Kind regards

    Dennis

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    I suspect you use serial (SHIFTOUT) with the 1302.
    I know you use I2C with the 1307.

  5. #5
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    From CocaColaKid:

    Code:
    '-------------------------- Clock Variables ---------------------------------
    
    rst var portb.2 ' DS1302 Reset Pin
    clk var portb.1 ' DS1302 Clock Pin
    dq var portb.0 ' DS1302 Data Pin
    
    '----------------------- Write Commands For DS1302 --------------------------
    
    writectrl con $8E ' Control byte
    writeram con $80 ' Write to RAM
    writeprotect con $00 ' Write-protect DS1302
    writesec con $80 ' Write seconds
    writemin con $82 ' Write minutes
    writehour con $84 ' Write hour
    writedate con $86 ' Write date
    writemonth con $88 ' Write month
    writeyear con $8C ' Write year
    
    '------------------------- Read Commands For DS1302 -------------------------
    
    readsec con $81 ' Read seconds from DS1302
    readmin con $83 ' Read minutes from DS1302
    readhour con $85 ' Read hours from DS1302
    readdate con $87 ' Read date from DS1302
    readyear con $8D ' Read year from DS1302
    readmonth con $89 ' Read month from DS1302
    
    '------------------------------ Time Variables ------------------------------
    
    mem var byte ' Temporary data holder
    outbyte var byte ' Second byte to ds1302
    reg_adr var byte ' First byte to DS1302
    date var byte ' Date variable
    ss var byte ' Seconds variable
    mm var byte ' Minutes varibale
    hh var byte ' Hours variable
    mo var byte ' Month variable
    yr var byte ' Year variable
    
    '------------------------ Initial Settings For Ports ------------------------
    
    low rst ' Set reset pin low
    low clk ' Set clock pin low
    
    trisb = 0
    trisa = 0
    
    '----------------------- Set Initial Time Variables -------------------------
    
    if portb.4 = 1 then START ' Set inital clock start up if jumper is present
    reg_adr = writectrl ' Set to control byte
    outbyte = writeprotect ' Set turn off protection
    gosub w_out ' Send both bytes
    reg_adr = writesec ' Set to write seconds register
    outbyte = $00 ' Set to write 00 to seconds register
    gosub w_out
    reg_adr = writemin
    outbyte = $30
    gosub w_out
    reg_adr = writehour
    outbyte = $00
    gosub w_out
    reg_adr = writedate
    outbyte = $01
    gosub w_out
    reg_adr = writemonth
    outbyte = $01
    gosub w_out
    reg_adr = writeyear
    outbyte = $00
    gosub w_out
    reg_adr = writectrl
    outbyte = writeprotect
    gosub w_out
    
    start:
    reg_adr = readsec ' Read seconds
    gosub w_in
    ss = mem
    reg_adr = readmin ' Read minutes
    gosub w_in
    mm = mem
    reg_adr = readhour ' Read Hours
    gosub w_in
    hh = mem
    reg_adr = readyear ' Read Year
    gosub w_in
    yr = mem
    reg_adr = readdate ' Read Date
    gosub w_in
    date = mem
    reg_adr = readmonth ' Read Month
    gosub w_in
    mo = mem
    lcdout $fe,1,"TIME:",HEX2 hh,":",HEX2 mm,":",HEX2 ss
    pause 500
    goto start
    
    '----------------------- Time Commands Subroutines --------------------------
    
    w_in:
    mem = reg_adr ' Set mem variable to reg_adr contents
    high rst ' Activate the DS1302
    shiftout dq,clk,0, [mem] ' Send control byte
    shiftin dq,clk,1, [mem] ' Retrieve data in from the DS1302
    low rst ' Deactivate DS1302
    return
    
    w_out:
    mem = reg_adr ' Set mem variable to reg_adr contents
    high rst ' Activate the DS1302
    shiftout dq,clk,0, [mem] ' Send control byte
    mem = outbyte ' Set mem variable to outbyte contents
    shiftout dq,clk,0, [mem] ' Send data stored in mem variable to DS1302
    low rst ' Deactivate DS1302
    return
    I have used an assembler routine with the 1302 (in a basic program) and it works:

    Code:
    #define      SPI_CLK       PORTA,1       ; spi bus clock line
    #define      SPI_MOSI      PORTA,2       ; spi master out data
    #define      SPI_MISO      PORTA,3       ; spi slave input data
    #define      SPI_CE        PORTA,4       ; chip enable for SPI device
    
    SCRATCH      equ 0x40                    ; 1 by general purpose scratchpad
    TMP          equ 0x41                    ; temp register
    TMP2         equ 0x42                    ; temp register
    COUNT        equ 0x43
    ;YRS          equ 0x44
    ;MON          equ 0x45
    ;DOW          equ 0x46
    ;DAYS         equ 0x47
    ;HRS          equ 0x48
    ;MINS         equ 0x49
    ;SECS         equ 0x4a
    
    ;user_bits    equ 0x2C                    ;
    ;save_w       equ 0x38
    ;save_status  equ 0x39
    
    
    
    
    
    ;---- Read RTC into W (assume address already sent) ----
    ;----           assumes CE is asserted              ----
    read_RTC:
          movlw  08h                         ;Send 8 bits
          movwf  COUNT
    
    SPI_read_loop:
          rlf    TMP, 1
    
          bcf    SPI_CLK                     ; clock data out
    
          bcf    TMP, 0                      ; assume data out is low
          btfsc  SPI_MISO
          bsf    TMP, 0                      ; if data out=1, set bit
    
          bsf    SPI_CLK
          decfsz COUNT, 1
          goto   SPI_read_loop
    
          movf   TMP, W
    
          return
    
    ;--- Write the byte in W to RTC ---
    ;---- assumes CE is asserted ----
    write_RTC:
          movwf  TMP                         ;Save the data
    ;
    ;--- Do a SPI bus write of byte in 'TMP' ---
    ;
    SPI_write:
          movlw  08h                         ;Send 8 bits
          movwf COUNT
    
    SPI_w_loop:
          bcf    SPI_CLK
    
          bcf    SPI_MOSI                    ; assume data out is low
          btfsc  TMP, 7
          bsf    SPI_MOSI                    ; if data out=1, set bit
    
    SPI_w_cont:
          rlf    TMP, 1
          bsf    SPI_CLK                     ; clock it in
          decfsz COUNT, 1
          goto   SPI_w_loop
    
          return
    Cheers, Art.
    Last edited by Art; - 21st February 2010 at 21:06.

  6. #6


    Did you find this post helpful? Yes | No

    Default starting to make more sense (I think !)

    @ Art
    Thanks a million for the reply.
    I had already checked out code by Cocacolakid as well as Sayzer and quite a few others, as well as Melanie's example MN1307.txt, with little or no success.
    The code from Cocacolakid uses the shiftin/out method not so ?
    This is the reason why I asked about the TRIS statement.
    Check the first few lines of Cocolakid's code:
    Code:
    '-------------------------- Clock Variables ---------------------------------
    
    rst var portb.2 ' DS1302 Reset Pin
    clk var portb.1 ' DS1302 Clock Pin
    dq var portb.0 ' DS1302 Data Pin
    What should the TRIS statement be ? Or is this handled by the SHIFTIN/OUT statement ?
    Later on in his code there is :
    Code:
    '------------------------ Initial Settings For Ports ------------------------
    
    low rst ' Set reset pin low
    low clk ' Set clock pin low
    
    trisb = 0
    trisa = 0
    The forum is scattered with many examples for DS1302 and DS1307 and various timers, clocks elapsed timers, olympic clocks and there are even examples for the various LABX boards,even some methods to use a PIC as a timer with accuracy much like that of a DS1302/7 depending on the crystal you choose.

    With the wealth of information here it starts becoming rather difficult to figure out which clock chip is a good selection and why.

    From what I can see (and I may well be wrong),
    if you're using the DS1302 then you should use a shiftin/out method and
    if you're using a DS1307 you should use the I2C method.

    I am trying to build a very simple alarm clock which triggers on a matching date and time. It will also be used as a datalogger for logging the time and duration of power failures for my generator since we have many power failures in our area at some really odd times.

    Your info has been most useful in starting to figure out some parts of this puzzle.

    Any more info you or anyone else would also be most welcome.

    Kind regards

    Dennis
    Last edited by Dennis; - 21st February 2010 at 22:06.

Similar Threads

  1. real time clock
    By maria in forum Code Examples
    Replies: 44
    Last Post: - 1st March 2022, 12:13
  2. PICKit2 - warning about configuration words
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 26
    Last Post: - 4th August 2009, 14:01
  3. DS1302 16F628 Woes
    By idtat in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 15th January 2009, 14:15
  4. TRIS E setting for 18F2550
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd May 2007, 07:47
  5. Is LCDOUT setting always al TRIS ?
    By BigWumpus in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 19th June 2005, 17:02

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts