DS1302 16F628 Woes


Closed Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    This is what I use. I cut-and-pasted from some other code, so everything might not be here, but this should give you a good idea.




    Code:
           RTC                  =  %11010000 ' RTC device address (byte addressing); should not be a constant
            ScReg               CON  $00   ' seconds address (00 - 59)  MSBit of SecReg must be set to a 0 to enable RTC
            MnReg               CON  $01   ' minutes address (00 - 59)
            HrReg               CON  $02   ' hours address (01 - 12) or (00 - 23)
            DaReg               CON  $03   ' day address (1 - 7)
            DtReg               CON  $04   ' date address (01 - 28/29, 30, 31)
            MoReg               CON  $05   ' month address (01 - 12)
            YrReg               CON  $06   ' year address (00 - 99)
            ContReg             CON  $07   ' control register
            RAMstart            CON  $08
            RTCflag             CON  0     ' RTC flag (location 0 of internal EEPROM)
            ClockControlByte    CON  %00010000 ' sets the SQW/OUT to 1Hz pulse, logic level low
            
            
            ' Clock Variables        
           
    
            Sc              VAR BYTE
            Mn              VAR BYTE
            Hr              VAR BYTE
            Da              VAR BYTE
            Dt              VAR BYTE
            Mo              vAR BYTE
            YR              var BYTE
    
    
    ;------------------------------------------------------------
    
    
    DoClock:
              HSEROUT [13,10,"To SET clock, enter 'S', to READ clock, enter 'R'",13,10]
               HSERIN [Dummy]
             
               
               IF Dummy = "S" OR Dummy = "s" THEN
             
                  HSEROUT ["Enter Time - all inputs require two digits, no editing is possible",13,10]
                  
                  HSEROUT [13,10,"Hours   "]
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[1] = Dummy
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[2] = Dummy
                             
                  HSEROUT [13,10,"Minutes "]
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[3] = Dummy
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[4] = Dummy 
                   
                  HSEROUT [13,10,"Date    "]
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[9] = Dummy
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[10] = Dummy 
                  
                  HSEROUT [13,10,"Month   "]
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[7] = Dummy
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[8] = Dummy
                  
                  HSEROUT [13,10,"Year    "]
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[11] = Dummy
                  HSERIN  [Dummy]
                  HSEROUT [Dummy]
                  TimBuff[12] = Dummy
                  GOSUB SETTime
               ENDIF
                  
               IF Dummy = "R" or Dummy = "r" THEN 
                  GOSUB ShowTime
                  
                  HSEROUT [TimBuff[1],TimBuff[2],":",TimBuff[3],TimBuff[4],":00,",TimBuff[7],TimBuff[8],"/",TimBuff[9],TimBuff[10],"/",TimBuff[11],TimBuff[12],13,10]
                  
                  PAUSE 10000
                  
               ENDIF              
              
               RETURN
                
                
              
    HexASCII2BCD:
               BCD = (Number1 - 48)<<4 + (Number2 - 48)
               RETURN
          
    BCD2HexASCII:
               Number1 = (BCD >> 4) + 48
               Number2 = (BCD & %00001111) + 48
               RETURN  
               
              
                                
    
    SetTime:
         
          Number1 = TimBuff[5]
          Number2 = TimBuff[6]
          
          Gosub HexAscii2BCD
          
          Sc = BCD
          
          Number1 = TimBuff[3]
          Number2 = TimBuff[4]   
    
          
          Gosub HexAscii2BCD
                                      
          Mn = BCD
          
          Number1 = TimBuff[1]
          Number2 = TimBuff[2]
          
          Gosub HexAscii2BCD
          
          Hr = BCD
          
          Number1 = TimBuff[7]
          Number2 = TimBuff[8]
          
          Gosub HexAscii2BCD
          
          Mo = BCD
          
          Number1 = TimBuff[9]
          Number2 = TimBuff[10]
          
          Gosub HexAscii2BCD
         
          Da = 0                            ' We don't care what day of the week it is
          Dt = BCD
          
          Number1 = TimBuff[11]
          Number2 = TimBuff[12]
          
          Gosub HexAscii2BCD
         
          YR = BCD
          
          Cntrl = 0                         ' Must be zero to make clk run - NOT same as control reg!
          Gosub ClockWrite
          GOSUB ClockControl
          Spare1 = 55                       ' Doesn't have to be BCD
          GOSUB ClockRamWrite
          RETURN
    
    
    ShowTime:
         
          Gosub ClockRead
          BCD = sc
          GOSUB BCD2HexAscii
          
          TimBuff[5] = Number1
          TimBuff[6] = Number2
          
          BCD = mn
          GOSUB BCD2HexAscii
          TimBuff[3] = Number1
          TimBuff[4] = Number2
          
          BCD = hr
          GOSUB BCD2HexAscii
          TimBuff[1] = Number1
          TimBuff[2] = Number2
          
          BCD = mo
          GOSUB BCD2HexAscii
          TimBuff[7] = Number1
          TimBuff[8] = Number2
          
          BCD = dt
          GOSUB BCD2HexAscii
          TimBuff[9] = Number1
          TimBuff[10] = Number2
          
          BCD = yr
          GOSUB BCD2HexAscii
          TimBuff[11] = Number1
          TimBuff[12] = Number2
          
          RETURN
    
           
    ClockWrite:
          I2CWrite SDA1,SCL1,RTC,ScReg,[sc,mn,hr,da,dt,mo,yr,Cntrl]
          Return 
               
    ClockControl:
          I2CWRITE SDA1,SCL1,RTC,ContReg,[ClockControlByte]
          RETURN          
               
    ClockRAMRead:
          I2CRead SDA1,SCL1,RTC,RAMstart,[Spare1,Spare2,Spare3,Spare4,Spare5,Spare6]                                    
          RETURN
          
    ClockRAMWrite:
          I2CWRITE SDA1,SCL1,RTC,RAMstart,[Spare1,Spare2,Spare3,Spare4,Spare5,Spare6]
          RETURN          
    
    ClockRead:   
          I2CRead SDA1,SCL1,RTC,ScReg,[sc,mn,hr,da,dt,mo,yr]
          RETURN
    Charles Linquist

  2. #2
    Join Date
    Apr 2005
    Location
    Virginia
    Posts
    65


    Did you find this post helpful? Yes | No

    Default

    idtat,

    I'm attempting to use the DS1302 myself with luck as well. Now, I haven't read through your code to any great extend, but one thing jumped out at me:

    Code:
    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
    I'm assuming you're using burst mode? Regardless, in either burst mode or the non-burst mode, you have to use LSB. I see you did that for your shiftouts, but not for your shiftin above. In other words, try changing this:

    Code:
    shiftin dq,clk,1, [mem] ' Retrieve data in from the DS1302
    to this:

    Code:
    shiftin dq,clk,0, [mem] ' Retrieve data in from the DS1302
    Let me know if you have luck. I'm trying to read seconds and I'm getting junk.

  3. #3
    Join Date
    Apr 2005
    Location
    Virginia
    Posts
    65


    Did you find this post helpful? Yes | No

    Default

    Okay, I'm an idiot. I didn't realize a '1' in shiftin is for LSB, whereas a '1' in shiftout is MSB. I came across some other code (haven't tried it yet) on the forum that may help:

    http://www.picbasic.co.uk/forum/show...ghlight=ds1302


    Good luck.

Similar Threads

  1. real time clock
    By maria in forum Code Examples
    Replies: 44
    Last Post: - 1st March 2022, 13:13
  2. LCDOut Woes
    By LetTheSmokeOut in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 11th June 2008, 02:45
  3. Ds1302 Stops are runs slow
    By Kman in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 13th June 2006, 03:10
  4. SERIN2 not working with 16F628
    By d1camero in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th May 2004, 21:37
  5. 16F628 on-chip eeprom problem
    By atomski in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 4th March 2004, 08:43

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