Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

DS1307 RTC with PIC16F877

Status
Not open for further replies.

lats

Full Member level 4
Full Member level 4
Joined
Oct 27, 2005
Messages
216
Helped
13
Reputation
26
Reaction score
6
Trophy points
1,298
Activity points
2,945
ds1307 proton basic

hi i'm trying to interface DS1307RTC with PIC16f877. I had done interfacing of ds1307 with 89c51 and was sucessful. i have modified the same code a bit for PIC16f877 but this isn't working i'm unable to find out the problem, please help me.
One thing more, in 89c51 code i had written " unsigned char ReadI2Crtc(bit ACK_Bit)" but in pic it gave error: "bit variable must be global or static" so i changed it to "unsigned char ReadI2Crtc(unsigned char ACK_Bit)" is this causing the problem. Please help me....
thanks a lot.
 

ds1307 basic

Which C compiler did you use?

I don't think your macros are gonna address the right thing you wanted.
CC5X from BKND does allow you to address the bits in a byte in RAM, but
not the way we use for 8051.

PIC is not 8051!

Cheers,
 

ds1307 pic16f877

hitech picc
 

ds1307 picc

You asked the same question on another forum, but perhaps this response (again) may help others aswell? Keep in mind it is PIC Basic, but it is well commented

Written With Proton PDS **broken link removed**

The DS1307 is a great piece of kit, it provides real time date and clock values, and interfaces with the PIC micro via I2C. The values it holds for date/time are, Secs, Mins, Hours, Day, Date, Month and Year

Example of interfacing with a DS1307. Note that on the DS1037 Pin 8 is connected to 5V, Pin 4 is connected to GND and Pin 3 is connected to ground if no backup battery is used!
**broken link removed**
Simulation of the above circuit: **broken link removed**

Code:
Device = 16F876
Xtal = 4
ALL_DIGITAL = True
PORTB_PULLUPS = True

' Setup the LCD
LCD_DTPIN = PORTB.4
LCD_RSPIN = PORTB.2
LCD_ENPIN = PORTB.3
LCD_INTERFACE = 4
LCD_LINES = 2
LCD_TYPE = 0
 

' Define I2C bus ports
SDA_Pin = PORTA.0 'DS1307 SDA pin
SCL_PIN =PORTA.1 'DS1307 SCL pin

Dim Temp1 As Byte
Dim Temp2 As Byte
Dim TempVal As Byte

Dim Secs As Byte
Dim Mins As Byte
Dim Hrs As Byte
Dim day As Byte
Dim Date As Byte
Dim Month As Byte
Dim Year As Byte
Dim Ctrl As Byte

Dim Secs_last As Byte

'Initialize LCD

Delayms 100
Cls

' Set initial DS1307 time / Date

Secs = 0                     ' Set seconds
Mins = 30                    ' Set minutes
Hrs = 12                      ' Set hours

Day = 1                       ' Set day of week value

Date = 30                     ' Day of month value
Month = 11                   ' Month value
Year = 6                       ' Year value

Ctrl = 0                        ' Set the control byte (leave as 0 in this example)


' The DS1307 works with data in BCD format, so convert BIN to BCD

TempVal=Secs
GoSub BIN_TO_BCD
Secs=TempVal

TempVal=Mins
GoSub BIN_TO_BCD
Mins=TempVal

TempVal=Hrs
GoSub BIN_TO_BCD
Hrs=TempVal

TempVal=Day
GoSub BIN_TO_BCD
Day=TempVal

TempVal=Date
GoSub BIN_TO_BCD
Date=TempVal

TempVal=Month
GoSub BIN_TO_BCD
Month=TempVal

TempVal=Year
GoSub BIN_TO_BCD
Year=TempVal

BStart

' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0).
' The second byte tells the DS 1307 where to start reading, 0 is at the start.
' The Ctrl byte contains advanced features, read the datasheet for more info
Busout 11010000, 0, [Secs, Mins, Hrs, day, Date, Month, Year, Ctrl] 'Write initial values for time / Date

BStop

Delayms 20

Main:

BStart

' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0).
' The second byte tells the DS 1307 where to start reading, 0 is at the start.
BusIn 11010001, 0, [Secs, Mins, Hrs, day, Date, Month, Year, Ctrl]

BStop

' The DS1307 sends it data in BCD, therefore it must be changed to
' BIN so that it can be easily used (eg, print onto an LCD)

TempVal=Secs
GoSub BCD_TO_BIN
Secs=TempVal

TempVal=Mins
GoSub BCD_TO_BIN
Mins=TempVal

TempVal=Hrs
GoSub BCD_TO_BIN
Hrs=TempVal

TempVal=Date
GoSub BCD_TO_BIN
Date=TempVal

TempVal=Month
GoSub BCD_TO_BIN
Month=TempVal

TempVal=Year
GoSub BCD_TO_BIN
Year=TempVal


 

If Secs - Secs_last = 0 Then Goto Main                'If there is update in Secs, display time and Date

' The Dec2 modifier makes sure that each value will have 2 characters, eg 1 becomes 01
Print At 1,1,"Time: ",Dec2 Hrs, ":", Dec2 Mins,":", Dec2 Secs
Print At 2,1,"Date: ", Dec2 Date, "-", Dec2 Month, "-", Dec2 Year

Secs_last = Secs

Goto Main

BCD_TO_BIN:                                    ' Convert the BCD values into BIN

     Temp1 = $0F & TempVal                     ' Clear off the top four bits
     Temp1 = DIG Temp1, 0
     Temp2 = TempVal >> 4                       ' Shift down four to read 2 BCD value
     Temp2 = DIG Temp2, 0
     TempVal = Temp2 * 10 + Temp1

     Return

BIN_TO_BCD:

     Temp1 = Dig TempVal, 0                     ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
     Temp2 = Dig TempVal, 1                     ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
     Temp2 = Temp2 << 4                          ' MOVE NUMBER OVER TO 2ND NIBBLE
     TempVal = Temp1 ^ Temp2                  ' XOR THEM TOGTHER TO MAKE THE WHOLE BCD NUMBER
    
     Return
[/url]
 
ds1307 proton

Why you don't use the internal I2C interface of the PIC16F87x ?
 

ds1307 pic16f

Hello!

In this page:
hxxp://www.techdesign.be/projects/007/007.htm
you will find a project with complete source code in C for DS1307 .

Good luck!

Cristi
 

code ds1307 pic16f877

Configuration in PIC is important for Accessing port pin as input u have to Put 1 in direction pin. For output u hvae to send 0. check the configuration of port pins. which could sort the problem.
 

pic16f rtc

Can u use DS1302 i can provide you the code for that with pic?
 

ds1307 pic basic

Hello,

ds1307 is quite easy to use, but you have to check the accuracy from the crystal. Perhaps do have to chance the ceramic capacitor.

best wishes,

mike
 

interfacing rtc with pic 16f877amicrocontroller

i have this code for ds1302 with pic16f876, wich is a part of a led moving sign, for real time clock, i give as is. excuse my english, i can't give more details caus my bad english.hope that help.
 

pic16f877a rtc

beny said:
i have this code for ds1302 with pic16f876, wich is a part of a led moving sign, for real time clock, i give as is. excuse my english, i can't give more details caus my bad english.hope that help.

Thanks!

Added after 1 minutes:

cristi7521 said:
Hello!

In this page:
hxxp://www.techdesign.be/projects/007/007.htm
you will find a project with complete source code in C for DS1307 .

Good luck!

Cristi

Thanks!
 

ds1307 picbasic

lats said:
hitech picc

Hi, try with the ccs c compiler, and remember:

1. put the osc bit 7 of the RTC in 0 to put the clock on
2. put the pull up resistors in SDA SDC lines... (4k7 to 10k)

good luck
 

picbasic ds1307

beny said:
i have this code for ds1302 with pic16f876, wich is a part of a led moving sign, for real time clock, i give as is. excuse my english, i can't give more details caus my bad english.hope that help.
Hi beny,

Can you provide in ZIP instead of RAR?

Thank you, Mike
 

ds1307 advance timer source basic

beny said:
i have this code for ds1302 with pic16f876, wich is a part of a led moving sign, for real time clock, i give as is. excuse my english, i can't give more details caus my bad english.hope that help.

hai..
which type of display you use?

thanks...
 

http: / / www.techdesign.be/projects/007/007.htm

For Mike, sorry i have'nt winzip, and i can't send the file in .asm file.
for wyang this is a part of a personel project wich i made in 2006, in my free time, it's a "led moving sign", called too "dot led matrix" it displays messages, temperature, time and date. new1_ds1302.asm is a routine of pragram to configure, reset and read ds1302, there is an other routine for display.
 

ds1307 basic

Greetings,

Befor all the replies above, remember read the DS1307 manual.

You must put the DS1307 in "ON", if you dont, the RTC doesnt start runing....
keep than in mind, i told u that by experience....

good luck

PD... the pull up resistors (the 4k7) are good, same the entire schematic above
 
  • Like
Reactions: erez01

    erez01

    Points: 2
    Helpful Answer Positive Rating

Hi....I did a simple example related to the DS1307 RTC.
But after a certain period of time and the DS1307 is on the screen with the following image below freeze.Is there a problem I do wonder if the connection????I wrote protons+ codes, could you help me .. thank you ..

Image-Share - image-jpg-599-278

proton+ code:

Code Basic4GL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
Device = 16F876
XTAL = 4
ALL_DIGITAL = True
PORTB_PULLUPS = True
 
' Setup the LCD
LCD_DTPIN = PORTB.4
LCD_RSPIN = PORTB.2
LCD_ENPIN = PORTB.3
LCD_INTERFACE = 4
LCD_LINES = 2
LCD_TYPE = 0
LCD_DATAUS = 50
LCD_COMMANDUS = 2000
 
 
 
' Define I2C bus ports
SDA_PIN = PORTA.0 'DS1307 SDA pin
SCL_PIN = PORTA.1 'DS1307 SCL pin
 
Dim Temp1 As Byte
Dim Temp2 As Byte
Dim TempVal As Byte
 
Dim Saniye As Byte         'saniye=secs
Dim Dakika As Byte         'dakika=mins
Dim Saat As Byte           'saat=hrs
Dim GunAdi As Byte        'gunadi=day
Dim Gun As Byte            'gun=date
Dim Ay As Byte              'ay=month
Dim Yil As Byte              'yil=year
Dim Ctrl As Byte
 
Dim Secs_last As Byte
 
Dim right As PORTA.2
Dim exit As  PORTA.3
 
'Initialize LCD
 
DelayMS 50
Cls
 
Saniye = 00
Dakika = 30                    ' Set minutes
Saat  = 8                    ' Set hours
 
GunAdi  = 2                     ' Set day of week value
 
Gun = 9                    ' Day of month value
Ay= 10                   ' Month value
Yil = 11                     ' Year value
 
Ctrl = 0                     ' Set the control byte (leave as 0 in this example)
 
 
' Set initial DS1307 time / Date
 
' The DS1307 works with data in BCD format, so convert BIN to BCD
 
TempVal=Saniye 
GoSub BIN_TO_BCD
Saniye=TempVal
 
TempVal=Dakika
GoSub BIN_TO_BCD
Dakika=TempVal
 
TempVal=Saat
GoSub BIN_TO_BCD
Saat=TempVal
 
TempVal=GunAdi
GoSub BIN_TO_BCD
GunAdi=TempVal
 
TempVal=Gun
GoSub BIN_TO_BCD
Gun=TempVal
 
TempVal=Ay
GoSub BIN_TO_BCD
Ay=TempVal
 
TempVal=Yil
GoSub BIN_TO_BCD
Yil=TempVal
'-----------------------------MENU--------------------------------------
BStart
 
' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0).
' The second byte tells the DS 1307 where to start reading, 0 is at the start.
' The Ctrl byte contains advanced features, read the datasheet for more info
BusOut 11010000, 0, [Saniye, Dakika, Saat , GunAdi, Gun , Ay, Yil, Ctrl] 'Write initial values for time / Date
 
BStop
 
DelayMS 50
 
'-----------------------------MENU--------------------------------------
Main:
 
BStart
 
' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0).
' The second byte tells the DS 1307 where to start reading, 0 is at the start.
BusIn 11010001 , 0 , [Saniye, Dakika, Saat , GunAdi, Gun , Ay, Yil, Ctrl]
 
 
BStop
'-----------------------------MENU--------------------------------------
 
' The DS1307 sends it data in BCD, therefore it must be changed to
' BIN so that it can be easily used (eg, print onto an LCD)
 
TempVal=Saniye 
GoSub BCD_TO_BIN
Saniye=TempVal
 
TempVal=Dakika
GoSub BCD_TO_BIN
Dakika=TempVal
 
TempVal=Saat
GoSub BCD_TO_BIN
Saat=TempVal
 
TempVal=GunAdi
GoSub BCD_TO_BIN
GunAdi=TempVal
 
TempVal=Ay
GoSub BCD_TO_BIN
Ay=TempVal
 
TempVal=Yil
GoSub BCD_TO_BIN
Yil=TempVal
 
 
 
 
If Saniye - Secs_last = 0 Then     'If there is update in Secs, display time and Date
GoTo Main
EndIf                         
 
Print At 1,1,DEC2 Saat,  ":", DEC2 Dakika, ":", DEC2 Saniye
Print At 2,1,DEC2 Gun ,  "/", DEC2 Ay,     "/", DEC2 Yil
GoSub gunler
Secs_last = Saniye
GoTo Main
'------------bcd yi bin e çevirme--------------------
BCD_TO_BIN:                                    ' Convert the BCD values into BIN
 
     Temp1 = $0F & TempVal                     ' Clear off the top four bits
     Temp1 = Dig Temp1, 0
     Temp2 = TempVal >> 4                       ' Shift down four to read 2 BCD value
     Temp2 = Dig Temp2, 0
     TempVal = Temp2 * 10 + Temp1
 
     Return
'------------bin den bcd ye çevirme--------------------
BIN_TO_BCD:
 
     Temp1 = Dig TempVal, 0                     ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
     Temp2 = Dig TempVal, 1                     ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
     Temp2 = Temp2 << 4                          ' MOVE NUMBER OVER TO 2ND NIBBLE
     TempVal = Temp1 ^ Temp2                  ' XOR THEM TOGTHER TO MAKE THE WHOLE BCD NUMBER
    
     Return
'------------gün isimleri--------------------     
gunler:
If GunAdi=1 Then Print At 1,11,"PAZAR"
If GunAdi=2 Then Print At 1,11,"PTESI" 
If GunAdi=3 Then Print At 1,11,"SALI " 
If GunAdi=4 Then Print At 1,11,"CRSMB" 
If GunAdi=5 Then Print At 1,11,"PRSMB"
If GunAdi=6 Then Print At 1,11,"CUMA "
If GunAdi=7 Then Print At 1,11,"CTESI"  
Return

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top