TRIS setting for the DS1302


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1


    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.

  2. #2
    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

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