TRIS setting for the DS1302


Closed Thread
Results 1 to 29 of 29
  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.

  7. #7


    Did you find this post helpful? Yes | No

    Default is it the code ?

    @Art

    I decided to give Cocacolakid's code a whirl,sadly time stand still :-( display is stuck at 00:00:80
    I think it's because of the if statement here:
    Code:
    if portb.4 = 1 then START ' Set initial clock start up if jumper is present
    So after removing it since I have no jumper there and re-programming the PIC.
    The display shows:
    00:30:00 and time stands still :-(
    Here's the code I am using :
    Code:
    '
    'using pic18f4520
    '
    'Ocsillator selections here
            OSCCON = $70            'Int CLK 8MHz
            OSCTUNE.6 = 1           'PLL 4x
            ADCON1= %00001111       '$0F = disable A/D converter
            cmcon   =   7 
            INTCON2.7 = 0     'switch pull-ups ON
    'END of oscillator selections
      'timer/oscillator defines 
            DEFINE OSC 32            '4x 8MHz
    'END of timer/oscillator defines
    
    'Port IO directions and presets for port pins begin here
    'TRISX = %76543210   << tris bit order numbering
    'TRISA = %11111111       'All pins are outputs
    'Define port pins as inputs and outputs ...
    'example only - TRISB = %00001111 ;Make 'B4-B7 outputs, B0-B3 inputs 
    TRISA  = %00000000 'assigned to LCD      
    TRISB = %11111111  'for 4x4 keypad all input
            TRISC = %10000000
            TRISD = %00000000
            TRISE.0 = 0
            TRISE.1 = 0
            TRISE.2 = 0
    'End of Port IO directions and presets for port pins begin here
    
                       
    
    'LCD defines begin here   
            DEFINE LCD_BITS 4 	'defines the number of data interface lines (4 or 8) 
            DEFINE LCD_DREG PORTD 	'defines the port where data lines are connected to
            DEFINE LCD_DBIT 4 	'defines the position of data lines for 4-bit interface (0 or 4)
            DEFINE LCD_RSREG PORTD 	'defines the port where RS line is connected to
            DEFINE LCD_RSBIT 2 	'defines the pin where RS line is connected to 
            DEFINE LCD_EREG PORTD 	'defines the port where E line is connected to 
            DEFINE LCD_EBIT 3 	'defines the pin where E line is connected 
            'DEFINE LCD_RWREG 0 	'defines the port where R/W line is connected to (set to 0 if not used)
            'DEFINE LCD_RWBIT 0 	'defines the pin where R/W line is connected to (set to 0 if not used)
            DEFINE LCD_COMMANDUS 2000 	'defines the delay after LCDOUT statement 
            DEFINE LCD_DATAUS 200 		'delay in micro seconds
    'END of LCD DEFINES
    
    'includes begin here
            INCLUDE "modedefs.bas"
            include "c:\pbp\samples\keypad.bas"
    'end of includes
    
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 207 ' 2400 Baud @ 32MHz, 0.17%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    
    '-------------------------- Clock Variables ---------------------------------
    
    
    clk     var portC.3 ' DS1302 Clock Pin
    dq      var portC.4 ' DS1302 Data Pin
    rst     var portD.1 ' DS1302 Reset 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
    
    '----------------------- 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 attached my circuit diagram too ...perhaps that's where the mess-up is ?

    Will try asm code as well and feedback

    Also going to source a DS1307 and DS1337C tomorrow and later this week, will post an update asap.

    Kind regards

    Dennis
    Attached Images Attached Images  

  8. #8
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Dennis View Post

    Also going to source a DS1307 and DS1337C tomorrow and later this week, will post an update asap.

    Kind regards

    Dennis
    Dennis,

    I'm using a DS1307, well one of MicroElektronica's RTC modules for the EasyPIC5 board and have the following example displaying time and date on a 16 x 2 LCD. I've only included the code for the RTC as there are loads of examples for hooking up an LCD.

    Code:
    SDA var PORTC.1                     ' RTC data 
    SCL var PORTC.0                     ' RTC clock
    
    TRISC= %00000011
    DB0 var byte[8]
    CMCON = %00000111                   ' Comparators = off
    gosub write_1307
    read_1307:                          ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
    I2CREAD SDA,SCL,$D1,$00,[STR DB0\8] ' Read 8 bytes from DS1307
    
    lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1],":",hex2 DB0[0]    'bit 0=sec, bit 1=min, bit 2=hrs
    lcdout $fe,$c0,"Date=",hex2 DB0[4],":",hex2 DB0[5],":",hex2 db0[6]  'bit 4=day, bit 5=month, bit 6=year
    goto read_1307
    end
    
    Write_1307:                          ' Set time & date to 19:00:00  14th Feb 201
    I2CWRITE SDA,SCL,$D0,$00,[$00,$00,$19,$7,$14,$2,$10,$90] ' Write to DS1307
    pause 10
    RETURN                               ' Sec Min Hr Day D M Y Control
    I've had this running for some time in testing, and seems quite stable. One thing that needs developing further is a means of entering the date and time via push buttons or some other means, but at least you should be able to see the basic operation

  9. #9
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    you don't need to read the registers of the DS1302 individually. You can grab the whole contents of the time register in one shot using "burst mode".

    Code:
    high rst         ' Ready for transfer
    Shiftout IO, SCLK, LSBFIRST, [$bf] ' Read all 8 RTC registers in burst mode     
    Shiftin IO, SCLK, LSBPRE, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol]
    low rst         ' Reset RTC
    That should load all the time info into your variables, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol


    steve

  10. #10


    Did you find this post helpful? Yes | No

    Default cool

    @Steve :-)

    Thanks for that info , actually saw your posts as I was sifting through the forum posts on DS1307 and DS1302 and I2Cread and I2CWrite , in fact I even tried it with my DS1302 (of course it didn't work since I2C is not what should be used for the DS1302) :-)
    Will try get something going for the DS1302 using that stub you posted and see what comes of it and will definitely give the DS1307 stubs a good bash when I get my DS1307.

    Regarding this line though ...
    Code:
    Write_1307:                          ' Set time & date to 19:00:00  14th Feb 201
    , do you think they made much of a fuss about Valentines day back then ?
    I also wonder if this isn't how the Rosetta stone came about, some ancients trying to scribe stuff into a rock...it's just like us really .. we are making rock (Silicon) do some really wild things.
    Even wirelessly rock to rock ...INSANE hey ?
    Now to explain those cave paintings !

    Chat soon

    Kind regards

    Dennis

  11. #11
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Dennis View Post

    Regarding this line though ...
    Code:
    Write_1307:                          ' Set time & date to 19:00:00  14th Feb 201
    , do you think they made much of a fuss about Valentines day back then ?
    I also wonder if this isn't how the Rosetta stone came about, some ancients trying to scribe stuff into a rock...it's just like us really .. we are making rock (Silicon) do some really wild things.
    Even wirelessly rock to rock ...INSANE hey ?
    Now to explain those cave paintings !

    Chat soon

    Kind regards

    Dennis
    LOL - must check my posts before uploading

  12. #12


    Did you find this post helpful? Yes | No

    Default ok :-) definitely gonna try this tonight!

    @Steve ..apologies .. was actually asking Malc-c about valentines day :-)
    @ Steve , regarding these lines :
    Code:
    high rst         ' Ready for transfer
    Shiftout IO, SCLK, LSBFIRST, [$bf] ' Read all 8 RTC registers in burst mode     
    Shiftin IO, SCLK, LSBPRE, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol]
    low rst         ' Reset RTC
    I understand these
    LSBFIRST ' MODE 0 - Shift data out lowest bit first. Clock idles low.
    LSBPRE ' MODE 0 - Shift data in lowest bit first,Read data before sending clock. Clock idles low.
    But what is the [$bf] ?

    @Malc-c --to err is human :-)

    Kind regards

    Dennis

  13. #13
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    $bf is the register address for clock burst mode read I believe.

    Do a search for $bf in the ds1302 data sheet.


    steve

  14. #14


    Did you find this post helpful? Yes | No

    Default Thanks Steve

    Yes had just checked data sheet just after I posted !
    $bf is for burst mode :-)

    Thanks to all who replied .. this is making a whole lot more sense now :-)

    Kind regards
    Dennis

  15. #15


    Did you find this post helpful? Yes | No

    Default ok read funstion understood !

    @ Steve ...
    Do you have a snippet for the write in burst mode so one can set the clock ?

    Kind regards

    Dennis

  16. #16
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Sure, this should do it:

    Code:
    settime:    ' Subroutine to write time to RTC
            RST = 1         ' Ready for transfer
            Shiftout IO, SCLK, LSBFIRST, [$8e, 0]  ' Enable write
            RST = 0         ' Reset RTC
            RST = 1         ' Ready for transfer
                ' Write all 8 RTC registers in burst mode
            Shiftout IO, SCLK, LSBFIRST, [$be, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, 0]
            rst = 0        
            Return
    steve

  17. #17


    Did you find this post helpful? Yes | No

    Default ok .. i was close then

    @Steve

    My code before you replied ...
    Code:
    setclk:
            high rst         ' Ready for transfer  RTC pin must go high
            Shiftout ds_data, ds_clk, LSBFIRST, [$bf] ' send burst mode control $bf to read all 8 RTC registers in burst mode     
            Shiftout ds_data, ds_clk, LSBFIRST, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol] send the data
            low rst         ' Reset RTC reset pin
    As you can see I was totally off the mark with the $bf , $be setting !

    Ok but I have read in a lot of posts regarding the DS1302 and BCD conversion ..does this still apply ?

    Kind regards
    Dennis
    Last edited by Dennis; - 22nd February 2010 at 21:03.

  18. #18


    Did you find this post helpful? Yes | No

    Default My clock never ticks :-(

    Hi all

    I have been battling for over a week now trying to get the DS1302 clock chip working.

    I think I finally have the format and code sorted , only problem is that my clock never ticks :-( in other words the time never changes!

    Please could somebody either take a look at my code or advise me what I should check next.
    I'm sure it is really something small or silly that I have overlooked.

    Kind regards

    Dennis
    Code:
    'device 18F4520 
    'DS1302  test
    '
    '
    
    'Ocsillator selections here
            OSCCON = $70            'Int CLK 8MHz
            OSCTUNE.6 = 1           'PLL 4x
            ADCON1= %00001111       '$0F = disable A/D converter
            cmcon   =   7 
            INTCON2.7 = 0     'switch pull-ups ON
    'END of oscillator selections
      'timer/oscillator defines 
            DEFINE OSC 32            '4x 8MHz
    'END of timer/oscillator defines
    '
    '
    'Port IO directions and presets for port pins begin here
    'TRISX = %76543210   << tris bit order numbering
    'TRISA = %11111111       'All pins are outputs
    '// Define port pins as inputs and outputs ...
            TRISA  = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs, 
            TRISB = %11111111  'for 4x4 keypad all input
            TRISC = %10000000
            TRISD = %00000000
            TRISE.0 = 0
            TRISE.1 = 0
            TRISE.2 = 0
    'End of Port IO directions and presets for port pins begin here
    '
    '                   
    '
    'LCD defines begin here   
            DEFINE LCD_BITS 4 	'defines the number of data interface lines (4 or 8) 
            DEFINE LCD_DREG PORTD 	'defines the port where data lines are connected to
            DEFINE LCD_DBIT 4 	'defines the position of data lines for 4-bit interface (0 or 4)
            DEFINE LCD_RSREG PORTD 	'defines the port where RS line is connected to
            DEFINE LCD_RSBIT 2 	'defines the pin where RS line is connected to 
            DEFINE LCD_EREG PORTD 	'defines the port where E line is connected to 
            DEFINE LCD_EBIT 3 	'defines the pin where E line is connected 
            'DEFINE LCD_RWREG 0 	'defines the port where R/W line is connected to (set to 0 if not used)
            'DEFINE LCD_RWBIT 0 	'defines the pin where R/W line is connected to (set to 0 if not used)
            DEFINE LCD_COMMANDUS 2000 	'defines the delay after LCDOUT statement 
            DEFINE LCD_DATAUS 200 		'delay in micro seconds
    'END of LCD DEFINES
    '
    'includes begin here
            INCLUDE "modedefs.bas"
           'end of includes
    '
    'HSER defines
            DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
            DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
            DEFINE HSER_SPBRG 207 ' 2400 Baud @ 32MHz, 0.17%
            DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    'HSER defines begin here
    
    '-------------------------- Clock Variables ---------------------------------
    
    
    'SCLK    var portC.3 ' DS1302 Clock Pin  7
    'IO     var portC.4 ' DS1302 Data Pin   6
    'rst     var portD.1 ' DS1302 Reset Pin 5
    
    
    'pins
    RST var PORTA.0 'DS1302 PIN 5
    IO var PORTA.1 'DS1302 PIN 6
    SCLK var PORTA.2  'DS1302 PIN 7
    
    'variables
    rtcyear var byte
    rtcday var byte
    rtcmonth var byte
    rtcdate var byte
    rtchr var byte
    rtcmin var byte
    rtcsec var byte
    rtccontrol var byte
    
    
    Main:
    Low RST ' Reset DS1302 RTC
    Low SCLK 'pull SCLK line LOW
    ' clock Set 16:45:00 04/14/08
    rtcyear = $08
    rtcday = $06
    rtcmonth = $04
    rtcdate = $14
    rtchr = $16
    rtcmin = $45
    rtcsec = $00
    Gosub SetTime   'fetch the time 
    Goto loopy  'goto loopy
    
    loopy:
    Gosub gettime ' go fetch the time
    Lcdout $fe, 1 'display date on top row on 16x2 lcd
    lcdout hex2 rtcmonth, "/", hex2 rtcdate, "/" , hex2 rtcyear
    lcdout $fe,$c0   'display time output on bottom line 16x2 lcd
    lcdout hex2 rtchr, ":", hex2 rtcmin, ":", hex2 rtcsec
    Pause 200 ' pause 0.2
    Goto loopy  'return to main loop 
    
    
    
    
    SetTime:  'this module sets the time using shiftout
    RST = 1 ' bring the DS1302 RST line high and now ready for transfer
    
    Shiftout IO, SCLK, LSBFIRST, [$8e, 0]   ' Enable write
    RST = 0 ' Reset DS1302 RTC
    RST = 1 ' bring reset line high and now ready for transfer
    Shiftout IO, SCLK, LSBFIRST, [$be, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, 0] ' Write all 8 RTC registers in burst mode
    RST = 0 ' reset DS1302 RTC
    Return 'return control to calling program
    
    
    GetTime: ' Subroutine to read time from RTC
    RST = 1 ' reset line high now ready for transfer
    Shiftout IO, SCLK, LSBFIRST, [$bf] ' Read all 8 DS1302 RTC registers in burst mode using shiftout/shiftin
    Shiftin IO, SCLK, LSBPRE, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol]
    RST = 0 ' Reset RTC
    Return  'return control to calling module
    
    End
    Last edited by Dennis; - 24th February 2010 at 21:32.

  19. #19
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Hmmm. I don't see any code to initialize the RTC. Add something like this to your code, so it runs once before you enter the main loop....
    You'll have to look in the data sheet to set your preferred trickle charge rates and such if you are going to use the charger.


    Code:
    'initialize the RTC and set the trickle charge rate
    Low SCLK
    low rst        ' Reset RTC
    high rst       ' RTC Ready for transfer        
    Shiftout IO, SCLK, LSBFIRST, [$8e, 0]  ' Enable write
    low rst        
    high rst
    Shiftout IO, SCLK, LSBFIRST, [$90, %10101011]  ' Set charger on and to "2 diodes, 8Kohm"              
    Low RST         ' Reset RTC

    steve

  20. #20
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Dennis View Post
    Ok but I have read in a lot of posts regarding the DS1302 and BCD conversion ..does this still apply ?
    If you use a push button to increment the minutes (or hours, etc) you will run into a problem because of the BCD.

    This subroutine increments the minutes by 1 every time it's called.
    It does the BCD to decimal conversion, increments the minutes, then converts the new number back to BCD before setting the time.

    Code:
    increminute:  'subroutine to do BCD to decimal conversion, increment and convert back to BCD
            mathtemp =(rtcmin>>4)*10+(rtcmin & $0F)  'convert BCD minutes into Decimal minutes        
            mathtemp=mathtemp+1   'increment by 1
            If mathtemp>59 then      'If minutes exceeds 59
                  mathtemp=0            'reset to 0
                  endif  
            rtcmin=((mathtemp DIG 1)<<4)+mathtemp DIG 0 'convert Decimal back to BCD
            rstcount = 0         
            gosub settime       
            return

    steve

  21. #21


    Did you find this post helpful? Yes | No

    Default still no luck

    @Steve

    Thanks for the trickle charger tip :-) now that table makes sense too :-) YAY

    I added the code below (originally exactly as you gave it to me) see the updates, I haven't added all the possible options but I will so any line could be un-commented for any selection of the desired charger setup and will post the updates tomorrow sometime.
    Code:
    init:
    'initialize the RTC and set the trickle charge rate
    Low SCLK
    low rst        ' Reset RTC
    high rst       ' RTC Ready for transfer        
    Shiftout IO, SCLK, LSBFIRST, [$8e, 0]  ' Enable write
    low rst        
    high rst
    'Shiftout IO, SCLK, LSBFIRST, [$90, %10101011]  ' Set charger on and to "2 diodes, 8Kohm"              
    'Shiftout IO, SCLK, LSBFIRST, [$90, %01011100]  ' inititial power state     
    Shiftout IO, SCLK, LSBFIRST, [$90, %00000000]  ' disabled                       
    Low RST         ' Reset RTC
    return
    Sadly the clock is still not working. I still just get a static display.
    I have no buttons assigned to the clock as yet,so I didn't add any increment sub-routine..should I ?
    I was hoping to just preset the time and it would start ticking away.
    If I did add an increment sub-routine , would I need to add one for secs,mins,hours and day month and year ? Or just for mins and hours?

    Kind regards
    Dennis

    Are there any other things I could try ?

    Kind regards
    Dennis
    Last edited by Dennis; - 25th February 2010 at 00:45.

  22. #22
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Dennis View Post
    Are there any other things I could try ?
    I don't suppose you've got access to a 'scope?
    The first thing *I'd* do is probe the oscillator pins on the DS1302 and see if THAT part of the clock is ticking.

    steve

  23. #23


    Did you find this post helpful? Yes | No

    Default news

    @ Steve

    I think I may have found the problem ....
    It looks like I am using a 32.768 Mhz Crystal not a 32.768Khz one!
    Will get 32.768 Khz crystals tomorrow and confirm :-)

    Thanks a million for the info thus far :-)

    Kind regards

    Dennis

  24. #24
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Ahhh, yes... the wrong xtal would do it.

    No amount of fooling with code and registers will help if the hardware is broken.

    Last week I spent 2 hours looking over a data sheet to see what registers and settings were wrong when I couldn't get a new circuit to work.
    The problem turned out to be 1 unsoldered pin on a 44TQFP part...

    Let us know how it goes with the right xtal.


    steve

  25. #25


    Did you find this post helpful? Yes | No

    Default It's working....!

    YAY it works :-)

    @ Steve .... :-) so easy to miss a pin soldering sometimes or on a breadboard test even forget to put the PIC back into its socket after programming and then watching a blank serial debugger screen :-)
    I remember years back in a PC workshop two of the technicians ,on inspection of a 'DEAD PC' that just arrived in, meticulously tested and replaced every component and card (drives,motherboard ,etc) in the PC including the case, eventually out of sheer frustration they called me in to check why it was still dead and the customer had been waiting nearly two weeks for their PC.
    I checked the power cable first ... it was plugged into the PSU but it was on a long workbench so I followed the cable to its source... and there was the problem ...the mains switch for the wall plug was OFF :-)

    Sometimes even the obvious is not so obvious and often we are in a huge rush, excited to get to the end result as soon as we can.

    Thanks Steve and everyone else for all the help so far, I really appreciate it !

    Kind regards

    Dennis

  26. #26


    Did you find this post helpful? Yes | No

    Default Now to add an alarm...

    @Steve

    To add an alarm(s) , would I just store a date and time in flash and then use an IF ..THEN statement as a matching trigger ?

    Kind regards

    Dennis

  27. #27
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    352


    Did you find this post helpful? Yes | No

    Smile

    Hi Dennis,

    You could do that or you could use the DS1337 that has 2 alarms built-in.
    The DS1337 uses I2C communications and can be configured in several
    different ways. Several years ago, Tony Galea presented an entire program
    using the DS1337. Recently, JEllis posted his trials setting up this clock.
    It doesn't have a battery charger like the DS1302 but it does have two separate alarm outputs. If you only need one alarm and need a 1hz pulse, the DS1337 will fill your needs. Check out the data sheet. I use alot of them and have a backup battery circuit for them.

    HTH,

    BobK

  28. #28
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Or you could just use a 18F46J50 that has a RTCC built in.
    *None* of that serial communications junk that way, eh?

    I still continue to use the DS1302 for a lot of projects because I LIKE the "keep alive battery" and charger...

  29. #29
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Dennis View Post
    @Steve

    To add an alarm(s) , would I just store a date and time in flash and then use an IF ..THEN statement as a matching trigger ?

    Kind regards

    Dennis
    Sure.
    Store an alarm hour and and minute (and day if you want), and IF "current minute" >= "alarm minute" AND "current hour" >= "alarm hour" THEN gosub makeahelluvanoise.


    steve

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