PDA

View Full Version : DS1307 issue



Scampy
- 28th October 2013, 13:20
Guys need some advice.

I have an EasyPIC5 board for developing my projects, and for the life of me I have no idea what's happening with getting the time from a DS1307, even though it's been used before to program the PIC with the current version of my project. I have breadboarded the DS1307 and also have a RTC2 module which is basically the DS1307 chip and the resistors / xtal on a small PCB which plugs into a header on the EasyPIC5 board, but still get the same issue, regardless of which PIC I use.

I've tried some of the examples and test code I wrote a few years back and even re-loaded the latest project, and still the same problem. That is that it displays the date as 10:10:10 and the time as 10 Oct 2010. If I disconnect the DS chip everything reverts to zero (ie 00:00:00 and 00 00 0000). I've used several 18F4580 PICs and loaded code which is running in working projects, and the same thing. I don't think it's a timing issue as the LCD displays other info (such as temperature from a DS18B20).

My two recent projects use this chip and having loaded the code have the opportunity to manually set the time, but the result isn't written to the DS1307 either. I've checked the jumpers on the EasyPIC5 board and all seem correct (there's not much to change, just pull up / down resistors on the header for that port)

Anyone have any ideas...

Amoque
- 28th October 2013, 15:33
Without code it is difficult to isolate the issue, but if there is any doubt you may use the read and write routines from here: I2C Clock (http://www.picbasic.co.uk/forum/showthread.php?t=18449) The time and date registers are the same addresses for 1337 and 1307 and that code I know is without problem.

Beyond, I would verify resistors on I2C lines, "$" to designate hex values and, just because it caused me a problem, try a new crystal (and verify capacitance requirements)? Some of this is unlikely by your symptoms listed, but sometimes just looking again...

Can you read the status register before applying values? Are "default" register values returning logical results? Any output from heartbeat (I think 1Hz at startup)? I read it requires a transistor to run an LED, but have not tested anything with it. Clock should run from startup, yes? Try only reads to eliminate "write" problems and anything else you can think of to be confident it is "on" and ticking...

Can you verify data to clock chip on SDA line?

Amoque
- 28th October 2013, 16:14
I have verified that 1Hz heartbeat is default from power up. Also, a transistor is required to drive LED. I used a 3904, but any similar...

Scampy
- 28th October 2013, 18:21
Thanks once again for your input to my posts.

Here's my basic code.





ASM
__CONFIG _CONFIG1H, _OSC_HSPLL_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM

DEFINE OSC 48 ; config settings 18F4580, 20mhz crystal
ADCON1 = $0F
clear



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



RTCSec var byte ' Seconds
RTCMin var byte ' Minutes
RTCHour var byte ' Hours
RTCWDay var byte ' Weekday
RTCDay var byte ' Day
RTCMonth var byte ' Months
RTCYear var byte ' Year
RTCCtrl var byte ' Control
SetTime var byte ' 12/24 Hour Clock
SetSec var byte ' Seconds
SetMin var byte ' Minutes
SetHour var byte ' Hours
SetDay var byte ' Day
SetMonth var byte ' Months
SetYear var byte ' Year

RTCSec = 0
RTCMin = 1
RTCHour= 2
RTCWDay =1
RTCDay =1
RTCMonth=10
RTCYear =13
RTCCtrl =0


Counter1 var byte
counter2 var byte
counter3 var word

CounterA var byte ' General purpose Variable

TimeH var byte ' Variable to store current hour
TimeM var Byte ' Variable to store current minutes

'************************************************* ***************

CCP1CON = %00001100 '
CCP2CON = %00001100


'************************************************* ***************
'DS18B20 setting

DQ VAR PORTA.5 ' One-wire data pin
temperature VAR WORD ' Temperature storage
count_remain VAR BYTE ' Count remaining
count_per_c VAR BYTE ' Count per degree C

'************************************************* ***************
'analog settings

ADCON0 = 0 'Set ADCON0
ADCON1 = %00001111 'Set D i/o
CMCON = 7 'Disable Comparators

'************************************************* ***************
'Port settings

CCP1CON = %00001100 '
CCP2CON = %00001100 '
TRISA = %11101111 '
TRISB = %00000011
TRISC = %00011011
TRISD = %00000011 '

SCLpin var PORTC.3 ' RTC pin - clk
SDApin var PORTC.4 ' RTC pin - data


Main:

I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl]

timeH=(RTCHour>>4) 'convert the BCD format of the hours register and store in variable timeH
timeH=(timeH &$03)*10
timeH=timeH+(RTCHour&$0F)

timeM=(RTCMin>>4)
timeM=(timeM &$07)*10
timeM=timeM+(RTCMin&$0F) 'convert the BCD format of the mins register and store in variable timeM

If TimeH = 0 and timeM = 0 then
Counter3 = 0
endif

If TimeH <0 then
Counter1 = 0
endif
If timeH >0 then
Counter1 = TimeH * 60
endif



lcdout $FE,$80,"Counter 3 = ",dec Counter3
I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl]
If RTCHour.6=1 then

CounterA=(RTCHour>>4)&$01 ' Work-Out 12 or 24 hour Display for Hours
else
CounterA=(RTCHour>>4)&$03
endif
CounterA=CounterA*10+(RTCHour&$0F) ' Display Hours appropriately for 12 or 24 hour Mode
If RTCHour.6=1 then
LCDOut $FE,$D4,#CounterA
else
LCDOut $FE,$D4,#CounterA Dig 1,#CounterA Dig 0
endif
LCDOut ":",#(RTCMin>>4)&$0F,#RTCMin&$0F," "

Counter3 = counterA*60 + TimeM

GOTO Main


The strange thing is that even if I remove the DS1307 from the breadboard and connect the SDA wire to the junction of the 4.7K pull up resistor (ie as if the SDA line was being pulled high) the LCD displays 10:10.

I've also tried code that is running in my current LED lights (for fish tank) using the same chip. This also displays the same issue with the breadboarded chip, but if that chip is removed and placed back in the light controller it picks up the 1307 and displays the current time.

I'll look at the links and see what develops

Malcolm

richard
- 28th October 2013, 22:07
I had similar issues with a ds1307 and discovered by reading the data sheet ,that if you don't have a backup battery connected to "vbat"
then "vbat" must be connected to ground. this fixed the 10:10:10 problem for me

Scampy
- 28th October 2013, 22:22
Richard, thanks for the tip, and yes that does fix the 10:10 problem, however I still can't get the DS1307 to run. I've also been porting some other examples that use the timers in the pIC rather than external devices to keep time as I've given up on the DS1307 for now !

SUNFLOWER
- 29th October 2013, 01:01
I saw the same time numbers awhile ago. The problem came from an accelerometer connected reversed power and ground causing voltage regulator stress. Problem cleared without damage after sensor was removed and installed correctly. Schematic located within http://www.harbornet.com/sunflower/PCB.jpg and code http://www.harbornet.com/sunflower/PCB.pbp

richard
- 29th October 2013, 01:36
the 1307 can be restarted by writing $80 and then 0 to the seconds register. the silicon chip magazine published a pic based clock many years back called a
" PIC TOC " that can be user calibrated for accuracy . I built one 10 years ago and its still running and keeping good time
.

Art
- 30th October 2013, 13:29
I wonder if the pic could be driven by the 32KHz crystal directly,
then you'd only have to use a 1:1 timer to count 15 bit overflow, and that's a second.

Melanie's Easy and accurate Clocks works, does the PCB have the I2C pullup resistors,
and if so, have you turned off weak pullups on the chip?

Art
- 30th October 2013, 13:54
Also, PBP I2CREAD has an error call you could use to verify the I2CREAD happened.