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
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.
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
lats said:hitech picc
Hi beny,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.
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.
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
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?