PDA

View Full Version : DS1307 RTC - Advice please.



malc-c
- 22nd March 2010, 20:52
Hi Guys and Gals,

I'm trying to the the RTC2 module (see image below) working on my EasyPIC5 board.

http://www.mcustore.com/acatalog/rtc2_150.jpg

I've scoured this forum, and trawled the web for various examples but still can't get the date and time displayed on the LCD. My guess is that I'm either missing something in the port config (can't say I can understand the datasheet but is seems that you don't need to set any fancy register) or the module has developed a fault.

The following code compiles fine and loads on the target chip (16F877a). It runs but displays 00/00/00 00:00:00 on the LCD.



@ __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF

DEFINE OSC 20
CLEAR

;----[LCD definitions]------------------------------------------------------
DEFINE LCD_DREG PORTB ' LCD Data port
DEFINE LCD_DBIT 0 ' starting Data bit (0 or 4)
DEFINE LCD_EREG PORTB ' LCD Enable port
DEFINE LCD_EBIT 5 ' Enable bit (on EasyPIC 5 LCD)
DEFINE LCD_RSREG PORTB ' LCD Register Select port
DEFINE LCD_RSBIT 4 ' Register Select bit (on EasyPIC 5 LCD)
DEFINE LCD_BITS 4 ' LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 4 ' number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Command delay time in us
DEFINE LCD_DATAUS 50 ' Data delay time in us

'RTC pins on 877a
SDA Var PORTC.4
SCL Var PORTC.3

' Allocate variables
RTCYear Var Byte
RTCMonth Var Byte
RTCDate Var Byte
RTCDay Var Byte
RTCHour Var Byte
RTCMin Var Byte
RTCSec Var Byte
RTCCtrl Var Byte

' Initialize LCD
LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250

' Set initial time to 8:00:00AM 06/21/05
RTCYear = $10
RTCMonth = $03
RTCDate = $22
RTCDay = $02
RTCHour = $12
RTCMin = 0
RTCSec = 0
RTCCtrl = 0

Gosub set ' Set the time

Goto mainloop ' Skip over subroutines


' Subroutine to write time to RTC
set:
I2CWrite SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return

' Subroutine to read time from RTC
gettime:
I2CRead SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return

mainloop:
Gosub gettime ' Read the time from the RTC

' Display time on LCD
Lcdout $fe, 1, hex2 RTCMonth, "/", hex2 RTCDate, "/" , hex2 RTCYear,_
" ", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec

Pause 500

Goto mainloop



The chip is using a 20Mhz xtal if that has any bearing on the issue, but I guess I should still be able to read and write to the DS1307 if the timing was out ?

malc-c
- 22nd March 2010, 21:04
I've just loaded this code and this too shows 00:00:00 for date and time



@ __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF ; use this for 16F877a

DEFINE OSC 20
CLEAR

;----[LCD definitions]------------------------------------------------------
DEFINE LCD_DREG PORTB ' LCD Data port
DEFINE LCD_DBIT 0 ' starting Data bit (0 or 4)
DEFINE LCD_EREG PORTB ' LCD Enable port
DEFINE LCD_EBIT 5 ' Enable bit (on EasyPIC 5 LCD)
DEFINE LCD_RSREG PORTB ' LCD Register Select port
DEFINE LCD_RSBIT 4 ' Register Select bit (on EasyPIC 5 LCD)
DEFINE LCD_BITS 4 ' LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Command delay time in us
DEFINE LCD_DATAUS 50 ' Data delay time in us

SDA var PORTC.4 ' RTC data
SCL var PORTC.3 ' RTC clock

TRISC= %11111111
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


Guess the module is faulty :(

malc-c
- 22nd March 2010, 21:19
I've sussed it !

Some time ago I was using an 18F4550 which was set for USB comms

http://www.picbasic.co.uk/forum/attachment.php?attachmentid=4111&d=1269292685

Whilst I had scoured the data sheet I forgot that on the 18F4550 RC3 and RC4 are used for USB comms - a R of TFM has resolved the issue....

Hours of frustration..... ERRRRR !

Life's a learning curve !