PDA

View Full Version : real time clock



maria
- 17th May 2004, 18:01
Hi there,

I'm using PIC BASIC with a PIC16F877. I'm wondering if there's anyone out there who could tell me how to connect a real time clock IC (the Dallas DS1302 is the one I have) to the PIC and get the PIC to display the time on an LCD? I have my LCD up and running so I wouldnt need too much detail on the LCD bit.

Thanks!

Best Wishes,
Maria.

Melanie
- 17th May 2004, 19:16
Have a look at the RTC thread in ths section (set your display to show posts from the last 30 days or more).

PS. Posts do get read by folks regardless what section you post them in... posting in multiple sections is unnescessary.

CocaColaKid
- 20th May 2004, 14:19
Maria,

You might want to try this code. This is how I control this chip. If you need help with it just ask and I'll see if I can provide you some assistance.

'-------------------------- Clock Variables ---------------------------------

rst var portb.2 ' DS1302 Reset Pin
clk var portb.1 ' DS1302 Clock Pin
dq var portb.0 ' DS1302 Data 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

trisb = 0
trisa = 0

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

NL2TTL
- 21st April 2005, 11:57
when i use this code, with a serout commando the compiler give a error on the code HEX2.

And when i leave the HEX2 in the serout and replace them bij # the counting gives problems what does the hex2 does, and how can i display the time and date on a console pc with the serout command?

NavMicroSystems
- 21st April 2005, 12:21
RTFM

There is no HEX modifier with SEROUT, use SEROUT2 instead.

see PBP Manual Section 5.73 & 5.74

NL2TTL
- 21st April 2005, 13:56
already fix the problem use the and and shift >>4 function

Art
- 29th June 2005, 22:20
Hey CocaColaKid, thanx for the code it worked, however, I suggest an improvement as stated in the DS1302 datasheet U R supposed to read
the seconds again at the end in case the second ticks over from 59 to
00 while you were reading the mins, hrs, etc. this could result in erroneous
Real Time reading.

Suggest modification here:
ss var byte 'seconds
ss2 var byte 'seconds comparison byte

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
reg_adr = readsec ' Read seconds
gosub w_in
ss2 = mem
IF ss <> ss2 THEN start 'COMPARE FIRST AND SECOND READINGS AND TRASH IF DIFFERENT.
lcdout $fe,1,"TIME:",HEX2 hh,":",HEX2 mm,":",HEX2 ss
pause 500
goto start

Cheers, Art.

bahattinkor
- 26th July 2005, 19:57
dear friends,

I have a problem with the DS1302, PIC16f877 and PIC Proton+ combination. I designed a basic circuit in PROTEUS, and put your code in it. I worked fine. There was no problem. but when I made that circuit real, I met with problems. When I powered the PIC I can not see the time runing. The only thing that I see is " 00:00:80" and it always stays same. I used portd.0,d1 and d2 in the connection of PIc and DS1302. I used 32.768khz crystal. And there is no any mistake in connections. How can I fix this problem ?
And one more small question. How can I reset LCD ? in PicBasic PRo I used to written "Flags=0". In Proton+ what should I write instead of this ?

mister_e
- 27th July 2005, 03:31
For info on Proton... www.picbasic.org/forum

I doubt PROTON have this option or it's hidden somewhere in the Help file, or somewhere on the forum.

BUT try


Print $fe,$28
Print $fe,$0c
Print $fe,1

This should work

bot402
- 27th July 2005, 12:16
You should know better than that Steve. The PROTON does everything the PBP does, and a whole lot more!

To reset the LCD, simply use: -

Dim BPF as Byte SYSTEM ' Bring the system variable into BASIC

Clear BPF ' Force a reset on the LCD the next time it's used

However, this is the wrong forum for the PROTON compiler. As long as your not using a pirated version of the compiler, you are welcome to use the dedicated PROTON forum

BobK
- 27th July 2005, 14:22
Hello bahattinkor,

I had the same problem for 2 days using a DS1337. It displayed the default time and if I programmed a new starting time that would display but would continue from there. This was on a printed circuit board that was already working but had a little accident with a floating wire that went into the side of the power supply box and blew everything up! After replacing most of the parts the board worked for a few days then quit. I put new chips on the board again but to no gain. I put all of the same parts into a newer board and they worked fine. Went back to the old board and found no connection between one side of the crystal and the DS 1337.

The moral of this story is triple check your connections and use a meter!

BobK

bahattinkor
- 27th July 2005, 15:05
yes, I checked connections. voltages are normal but when I measured the cyrstal of Ds1302 with ossiloscope, I did not see any frequency. The ossilator does not ossilates. then I change DS1302 and 32.768Khz cyrstal with new ones but they are still not working. Can it be related with the Vcc that I used. I use 5V for Vcc for DS1302. Or Should I use any resistor or capacitor plus for the DS1302.

mister_e
- 27th July 2005, 15:43
Probably your Clock Halt flag is set..... it's undetermined when you put power on the RTC. Just clear this bit and you'll see oscillation on your scope.
The problem is the same on many RTC. See the secondes register for the CH bit.

Many cheaper probe will screw the signal coming from an Crystal. Place it on 10X and this will help a bit in many case.

sayzer
- 2nd February 2006, 15:23
Here is a modified version of the code posted by CocaColaKid above.

In the code above, the clock can only be set during the programming of PIC.

With the modification here, the user can set the time, date etc. at any given time.

Advise/post any additions, recommendations, modifications etc if any.

sayzer
- 24th February 2006, 09:16
As members are asking;

Attached is the updated version with more space.
(HEX Conversion is shorter)

------------------------------------------------------

shahidali55
- 2nd July 2006, 17:07
Hey CocaColaKid,
Your code for the DS1302 is really good and efficient...
I got a few questions,
how should i enable the trickle charge option ?
and how should i clear or set the clock halt flag???

Russ Kincaid
- 6th July 2006, 01:54
I am a neophyte at programming, but have been writing BASIC code for 40 years. This is how I would do the conversion:

DECX VAR BYTE(59)
A VAR BYTE
B VAR BYTE
C VAR BYTE

FOR B = 0 TO 59
FOR A = $0 TO $59
IF C = B THEN DECX(C) = A
C = C+1
NEXT A

NEXT B
' THIS ROUTINE GENERATES AN ARRAY WHERE
' DECX(0) = $0
' DECX (1) = $1
' ETC.
I didn't do it correctly the first time, this is better.

blainecf
- 11th August 2006, 19:21
For all you old timers, here's an alternate way of calculating Human Date/Time from the RTC bits:

(Also, because all info is read in one shot, there is less liklihood that the seconds won't match the minutes.)

I2CREAD DPIN,CPIN,RTC,I2C_Adr_B,[Rd_seconds, rd_minutes, rd_hours, Rd_DOW, Rd_DAy, RD_month, Rd_Year] ' in bcd code

Seconds = Rd_Seconds.0 + (Rd_Seconds.1 * 2) + (rd_seconds.2 * 4) + (rd_Seconds.3 * 8) + (rd_seconds.4 * 10) + (rd_seconds.5 * 20) + (Rd_Seconds.6 * 40)

Minutes = Rd_Minutes.0 + (Rd_Minutes.1 * 2) + (rd_Minutes.2 * 4) + (rd_Minutes.3 * 8) + (rd_Minutes.4 * 10) + (rd_Minutes.5 * 20) + (Rd_Minutes.6 * 40)

Hours = RD_Hours.0 + (Rd_hours.1 * 2) + (rd_hours.2 * 4) + (rd_hours.3 * 8) + (rd_hours.4 * 10)

am_pm = rd_hours.5
dow = rd_dow

day = rd_day.0 + (rd_day.1 * 2) + (rd_day.2 * 4) + (rd_day.3 * 8) + (rd_day.4 * 10) + (rd_day.5 * 20)

Month = rd_month.0 + (rd_month.1 * 2) + (rd_month.2 * 4) + (rd_month.3 * 8) + (rd_month.4 * 10)

Year = rd_year.0 + (rd_year.1 * 2) + (rd_year.2 * 4) + (rd_year.3 * 8) + (rd_year.4 * 10) + (rd_year.5 * 20) + (rd_year.6 * 40) + (rd_year.7 * 80)

Darrel Taylor
- 11th August 2006, 19:37
I suppose that might work. But it wouldn't be my first choice.

Only 30 or 40 times the needed code.

DT

jessey
- 29th August 2006, 07:09
Hello,

I added some code to Omer Yildiz's ds1302_LCD_setup.bas program that was modified from CocaColaKids original code example posted here & I added a timer and have a few questions if anyone could answer me.

I have read the DS1302 datasheet but couldn't figure out how to get the clock to show in the 12 hour mode for am & pm time. According to the datasheet I have to set bit 7 of the hours register to a high (1) but I don't understand Omer's code to do that. Also can anyone here explain what this kind of look up table is all about?

Quote from Omer's code.............................................. ...............
Lets burn the eeprom for something useful. A kind of lookup table.
eeprom 1,[31,28,31,30,31,30,31,31,30,31,30,31]

I've included the program here if anyone wants to look at it, I'm sure its not very efficient but it works.

These are the modifications below that I made to Omer's ds1302_LCD_setup.bas program. A possible application could be as a kitchen timer.

I added visual Lcd screens that shows you what your setting when setting the calendar and the clock. I also programmed the menu led to be used as a visual flashing indicator that the alarm time has elapsed. I added an up-count timer that increments each time the second variable of the DS1302 increments. I also added another 2 push buttons and followed Omer's nice example of his 2 button set and increment push button routine for setting the Seconds, Minutes and Hours times for the alarm. On initial power up the up-count timer is disabled, once your finished setting the times for the alarm and press the Set_Alarm_Timer_Push_Button then the up-counter starts incrementing. When the
set time equals the up-count time then the alarm buzzer sounds for approx 10 beeps then shuts off while the red led continues to flash until the Set_Timer_Push_Button is pressed to shut it off. Eventually I'll change it again to add more buttons, one for each of the Seconds, Minutes and Hours settings so the times can be set quicker but it works great as is.

There is one thing I noticed after getting the timer set up to work, is that when the up-count timer starts after your finished setting the alarm times, its not always accurate to the second. Because, depending on the time when your finished setting your times and then when you return to start the up-count timer, it very seldom starts exactly on a second change and can be out by as much as 900 ms. If this is only used as a kitchen timer then its not a big deal but in a time critical application it could be.

Thanks
jessey

sayzer
- 29th August 2006, 08:58
... Also can anyone here explain what this kind of look up table is all about?
Quote from Omer's code.............................................. ...............
Lets burn the eeprom for something useful. A kind of lookup table.
eeprom 1,[31,28,31,30,31,30,31,31,30,31,30,31]



Hi jessey,

Omer is me.

That code was so old and just for an example. I have modified it several times afterwards.

For the eeprom look up table, it works while the user is in setup menu. It is not used in any other place in the code.

While the user is changing the day, the table matches the number of days for the stored month.

If the year is one of those with 29 days, then the "EEPROM location 2" will be written by 29 so that when user gets into "changing the day" and if it is in February then the max days on LCD will be 29.

Examples:
If the month is 1 then LCD shows the max day as day31 then starts from day1.

If the month is 2 then LCD shows the max day as day28 or day29 depending on the year and then starts from day1.

.
..
...

if the month is 11 then LCD shows the max day as day30 then starts from day1.

etc...

jessey
- 31st August 2006, 13:17
Hi sayzer,

Thanks for the explanation of your eeprom code.

I went over the DS1302 data sheet again (actually many times) and I think I got it now, I'm hoping. I finally realized that you wrote your code for the reads and writes of the Registers in hex which threw me off. When I converted them over to binary for the hours then it made some sense. I set bit 7 and bit 5 in the binary number then converted them back to hex. I haven't tried it yet but I think this might work.

for am 12 hr mode
writehour = 00100011 = $C4
readhour = 10100011 = $A3

for pm 12 hr mode
readhour = 10101011 = $AB
writehour = 00101011 = $D4

Do you think this will do it? If I have this part right then I don't think I'll have any problems converting over the rest of the code to display it with the am and pm.

Thanks
jessey

sayzer
- 31st August 2006, 13:25
Take a look at this.
You should get nice ideas from Melanie's extreme (ly easy for her) work here.


http://www.melabs.com/resources/samples/submitted/MN1307.txt


------------------

jessey
- 1st September 2006, 02:23
Hi sayzer,

Thanks for sending me the link to Melanie's code.

Yea its way over my head, I don't understand the BCD system at all. I checked out BCD on the net and it says it can be used to save RAM space which can result in using half the storage space as compared to storing the same value in hex. If I read and understood that correctly then it would make sense to use it. This is the site where I read that: http://www.danbbs.dk/~erikoest/bcd.htm#top its got links to a lot of useful stuff that you didn't want to know! But I guess its all relevant if you want to learn programming.

This is the BCD code that I didn't understand when I first looked at Melanie's code:


' Subroutine Converts back to BCD
' -------------------------------
' CounterA is the entry variable
' CounterB holds the converted BCD result on exit
ConvertBCD:
CounterB=CounterA DIG 1
CounterB=CounterB<<4
CounterB=CounterB+CounterA DIG 0
Return

It looks a little similar to this routine below that I found on the link that I posted above:


SUBROUTINE PACKIT
On Entry:

Register B contain the upper byte of the BCD number.
Register C contains the lower byte of the BCD number.

On Exit:

Register A contains the Packed BCD Number.

To pack the number we must:

A <= (B)
Rotate A Left 4 times
A <= (A) + (C)

That's all. We could enhance our routine by checking to see if
the first number is zero. If it is, we do not need to rotate
4 times.



I have another question for you if you understand Melanie's ConvertBCD: subroutine above, what does the DIG 1 & DIG 0 do? I've seen it used in some of the code posted here on the forum but I don't understand what its function is?

Oh, and another question, you didn't confirm or not if my last post was a correct way to set the hours registry?

Thanks
jessey

sayzer
- 1st September 2006, 08:45
....
for am 12 hr mode
writehour = 00100011 = $C4
readhour = 10100011 = $A3

for pm 12 hr mode
readhour = 10101011 = $AB
writehour = 00101011 = $D4


hi jessey,

When reading or writing you change the register address command.

Reading: $85
Writing : $84

If you want to "Write Hour" to the chip, then you first send $84 command and prepare the chip to accept the incoming data for "hour". Then right after that you send your byte variable consisting your "hour data".

If sending 12Hr AM mode then the Bit7 of your byte should be "0" indicating 12Hr mode, and Bit5 should be "1 " indicating "AM".

Sending $85 command to the chip will be for "read hour" command and incoming data byte will consist of "hour data". Thus for reading, you do not set a byte to write to the chip; just read the incoming data.

In this case, I need to correct your arrangement as below.

12AM hr mode
writehour = 00100011
Bit7=0 12hr mode.
Bit5=1 AM mode.
Correct

readhour = 10100011
No need.
Just send $85 and read the incoming data.


12PM hr mode
writehour = 00101011
Bit7=0 12hr mode.
bit5=1 AM mode.
incorrect

The correct byte arrangement:
writehour = 00001011
Bit7=0 12Hr mode.
bit5=0 PM mode.


readhour = 10101011
No need.
Just send $85 and read the incoming data.





I have another question for you if you understand Melanie's ConvertBCD: subroutine above, what does the DIG 1 & DIG 0 do?



In a byte or word, the digits counts from right to left (just like Arabic).

Say we have a byte and it is set to Decimal 245.

ex:
Test VAR BYTE
Apple VAR BYTE

Test = 245

In this case, from right to left,
Test Dig 0 is 5
Test Dig 1 is 4
Test Dig 2 is 2

to extract any of these numbers and use in somewhere else, we use;

Apple = Test Dig 1

Apple is now 4.



How is it now ?
---------------------

jessey
- 4th September 2006, 03:04
Hi sayzer,

I can't seem to figure out what I'm doing wrong here. This is what I did to change to the 12 hour am & pm mode. I got rid of the "writehour CON $84" statement, as its not required anymore. Then I created these four con's and variable below:


Mode VAR BIT
Mode = 0 ' starts in am mode on initial power-up
writehour_am con $C4
writehour_pm con $D0
am CON 0
pm CON 1

Then in your setup: subroutine I added this code block to replace your code block for the hourx:


IF Mode = am THEN
reg_adr = writehour_am ' = 00100011
ELSE
reg_adr = writehour_pm ' = 00001011
ENDIF
outbyte = hourx
gosub w_out

Then in your in your sethour: subroutine I added this code block to replace yours for setting the hour:


IF CHNG = 0 THEN 'change the hour on display
pause 100
hour = hour + 1
IF hour > 12 THEN
IF Mode = am THEN
Mode = pm
ELSE
Mode = am
ENDIF
hour = 1
ENDIF
call send
ENDIF

And that's it. Seems simple enough but it won't work. After I finish setting the clock and return to the start: subroutine it shows two zero's instead of what I set it to. I added these modifications to your original code without my added timer counter routines to rule out any of the other changes I made to your code. I used a converter that is here: http://www.itlocation.com/en/software/prd56811,download,.htm to calculate the HEX values. Its a great converter because it allows copping and pasting the values into and out of the converter.

I found a discrepancy in the manual for what you said about bit 7.

You said:


If sending 12Hr AM mode then the Bit7 of your byte should be
"0" indicating 12Hr mode, and Bit5 should be "1 "
indicating "AM".


And the manual says:


AM-PM/12-24 MODE
Bit 7 of the hours register is defined as the 12- or 24-hour
mode select bit. When high, the 12-hour mode is selected.
In the 12-hour mode, bit 5 is the AM/PM bit with logic high
being PM.


I changed the bits to what the manual says but still ended up with the same results. I'll keep plugging away at it to see if I can figure it out but in the meantime would you have any ideas on what I've done wrong here?

Thanks
jessey

sayzer
- 4th September 2006, 12:13
IF hour > 12 THEN

IF Mode = am THEN
Mode = pm
ELSE
Mode = am
ENDIF

hour = 1

ENDIF




Why don't you try this:




IF hour > 12 THEN
TOGGLE Mode
hour = 1
ENDIF




For the Bit7 and Bit5, I checked again, you are right. I apologize.

Say your Bit7 and Bit5 are set correctly.
The rest, Bit4,Bit3,Bit2,Bit1 and Bit0 are consisting your hour data (in AM mode). In PM mode, Bit5 is also consisting hour data.
You are setting the hour data the same each time.

shahidali55
- 29th May 2007, 18:24
Is it possible to use a 32.768 Khz crystal with picbasic pro ?
I know the minimum given in manual is 4 Mhz, but is there anyway to use a 32.768 Khz to reduce power consumption ? ? ?

skimask
- 29th May 2007, 18:36
Is it possible to use a 32.768 Khz crystal with picbasic pro ?
I know the minimum given in manual is 4 Mhz, but is there anyway to use a 32.768 Khz to reduce power consumption ? ? ?

If you run a PIC at 32.768khz, with a PBP program that's compiled to run at 4Mhz, the only thing that'll happen is it'll run 122 times slower (i.e. pause 1 @ 4Mhz = pause 122 @ 32.768khz).

paul borgmeier
- 29th May 2007, 20:52
I know the minimum given in manual is 4 Mhz

Not that helps here, but 3 MHz also is an option as is given in the manual


Define OSC 3

shahidali55
- 30th May 2007, 10:59
I've already tried that skimask. . .
It really puts all the PBP routines off . . .
LCDOUT takes like 3 seconds to print 16 digits . .

skimask
- 30th May 2007, 13:40
It really puts all the PBP routines off . . .
LCDOUT takes like 3 seconds to print 16 digits . .
Which is exactly what I'd expect...unless you write the code in assembly, PBP will time everything like it was running 4Mhz. Maybe try 'lcd_commandus' and lcd_dataus' both set to 1 and see if it works like that at 32khz.

Which PIC are you using this time?
There is a clock switching feature in most newer PICs these days, can switch between the main (4Mhz or whatever) and the internal 32/37/40khz (depends on the datasheet you're looking at) 'backup' oscillator if configured correctly. I use it all the time to save power.

shahidali55
- 31st May 2007, 14:46
I'm using a 16F84A so there is no question of internal oscillator.
I tried several other things besides LCDOUT. Even FOR and WHILE statements take a lot o ftime to execute . . .
I used a external 32.768Khz oscillator.
Just to verify that the F84 was running at 32.768Khz, i tried a clock code on it and it does keep accurate time . . . down to a second a day.

skimask
- 31st May 2007, 16:09
I'm using a 16F84A so there is no question of internal oscillator. I tried several other things besides LCDOUT. Even FOR and WHILE statements take a lot o ftime to execute . . . I used a external 32.768Khz oscillator. Just to verify that the F84 was running at 32.768Khz, i tried a clock code on it and it does keep accurate time . . . down to a second a day.

If you're running 32.768khz, everything is going to take a lot longer to execute.
The clock is 32.768khz, but the PIC is only executing instructions at 8.192khz (Fosc/4), and if you're code has a lot of call's, gosub's, returns, anything that takes the code to 'somewhere else', it'll run half that fast again (4.096khz, Fosc/4 + one extra instruction for each 'somewhere else').

So, what are you asking? What's the point?

shahidali55
- 4th June 2007, 09:43
I think i'll stick to 4Mhz for now . . .

- 6th September 2007, 16:44
this is a very belated response to shahidali55.

I've added a tiny bit of code to "ds1302_user_interface.txt" to enable trickle charging. This is a small feat, but helped me understand how the register addressing and definitions are used.

edit: I'm using a 5v 0.47f supercap which doesn't need diodes or resistors, so "trickledef" below uses a bit sequence 1010 (for TCS) 00 (for DS) and 00 (for RS), see page 7 of the DS1302 datasheet for other options

cheers,
Tobie

==========================

'additional variables
writechrg var byte
tricklereg var byte
writechrg = $91 'Write Command for the trickle register
trickledef = $A0 'definition for a supercap which uses no diodes or resistors

' this goes in the "setup" subroutine, not as the statement first or last though
reg_adr = writechrg
outbyte = tricklereg
gosub w_out

- 10th September 2007, 14:23
hi,

here's a modified version with trickle charge enabled for a supercap. Note this includes no LCD interface.

A schematic is included in this pdf http://www.parallax.com/dl/appnt/jav2/appnote2.pdf

Here's a datasheet for the supercap, I used the 0.47f component: http://rocky.digikey.com/WebLib/Cooper-Bussmann/WEB%20DATA/Capacitors/P%20Series%20Aerogel%20Supercapacitors.pdf

thetrueman
- 15th October 2007, 09:06
Hi Melanie,

I used your code MN1307.txt which is very good coded and easy to understand. I just want to add some alarm time. I know and did with some if...then...endif way.

Now simply it works on alarm set time but output (portb.7) blinks and do not High permanently. I tried to find some solution but it is very simple that there should not be any problem with if...then...endif. But I think it is somethink with hardware related.

Please help me in this regard because I must need this Alarm Clock. Thanks in anticipation.

Best Regards,

Shahzad

shahidali55
- 10th November 2007, 16:20
Thanks for the trickle charge routines Tobie . . .

engrehaf
- 28th November 2012, 17:05
hey guys i am a new member here :)
i want some help in this topic if you can help

HEX2 !! what it is ?
because i use this sample code for 7 segment display, but i cannot deal with these variable like HEX2 hourx, HEX2 minutex and HEX2 seconds ???

and thank you :)

mackrackit
- 28th November 2012, 17:23
From the manual


BIN, DEC and HEX may also be followed by a number. Normally, these modifiers display exactly as many digits as are necessary, zero blanked (leading zeros are not sent). However, if a number follows the modifier, SEROUT2 will always send that number of digits, adding leading zeros as necessary. It will also trim of any extra high order digits. For example, BIN6 8 would send A001000" and BIN2 8 would send A00".

longpole001
- 26th May 2013, 11:56
I know this is old thread but have a need fro RTC and its using the ds1302

i did have a quick question in relation to how the I/O port is set on the PIC for I/O the interface to the DS1302 , in that the sample code does not change TRIS bits on the PIC from an output to an input for when it doing a read ( shiftin command ) of the DS1302 -

does issuing the shiftin command automatically change the tris bits for that port to allow input ???
cheers

Sheldon

xapmanis
- 3rd August 2013, 09:38
Hello longpole, i've been reading the thread today and some others about 1302 and i think shiftin and shiftout does all the work with tris and with clk. Just send the right registers for reading/writing and u should be fine.

longpole001
- 4th August 2013, 12:24
yes it does change the input /output of the tris control , how ever it not documented in any of the manuals or online help files that i have found to date

cesar35
- 1st March 2022, 12:13
hello know the post and very old I'm in need of a little help on this clock project using the DS1302 with alarm it even works more Think it has error, If anyone can help me I thank here all the files + simulation