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

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

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

  4. #4


    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

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

  6. #6


    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

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

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