$regfile = "m48def.dat" $crystal = 8000000 $hwstack = 32 $swstack = 8 $framesize = 24 'Config ports & pins Config Portb = Output Config Portc.0 = Output Config Portc.1 = Output Config Portc.2 = Output Config Portc.3 = Output Config Portd.1 = Output Config Sda = Portc.4 Config Scl = Portc.5 Config 1wire = Portd.0 'Alias Led Alias Portd.1 'Constants Const Ds1307w = &HD0 'write DS1307 Const Ds1307r = &HD1 'read ds1307 Const Adsecs = &H00 'address seconds Const Admins = &H01 'address minutes Const Adhours = &H02 'adress hours Const Skiprom = &HCC 'skip ROM command DS18B20 Const Convert = &H44 'starts temperature convertion Const Readsp = &HBE 'read scratchpad 'Variables Dim Temp As Byte Dim Hours As Byte Dim Mins As Byte Dim Secs As Byte Dim Sc(9) As Byte Dim Tube(4) As Byte Dim H As Byte Dim I As Byte Dim J As Byte Dim Duration As Byte Dim Dg As Integer Dim Pos As Integer 'Functions & Subs Declare Function 7seg(byval Q As Byte) As Byte 'Interrupts Config Int0 = Falling On Int0 Sethour Enable Int0 Config Int1 = Falling On Int1 Setmin Enable Int1 'Set states Portb = 0 Portc = 0 Led = 0 'Mainloop Do Disable Interrupts I2cstart I2cwbyte Ds1307w I2cwbyte Adsecs I2cstop I2cstart I2cwbyte Ds1307r I2crbyte Secs , Ack I2crbyte Mins , Ack I2crbyte Hours , Nack Hours = Hours And &B00111111 'removes bit6 & bit7 Hours = Makedec(hours) 'converts BCD into Decimal Mins = Makedec(mins) Enable Interrupts Tube(2) = Mins / 10 'splits the hours and minutes into Temp = Tube(2) * 10 '4 digits so that it can be displayed Tube(1) = Mins - Temp Tube(4) = Hours / 10 Temp = Tube(4) * 10 Tube(3) = Hours - Temp Duration = 20 Pos = 2 Gosub Multiplex Duration = 20 Pos = 1 Gosub Multiplex Duration = 20 Pos = 0 Gosub Multiplex Duration = 255 Pos = -1 Gosub Multiplex Duration = 20 Pos = 0 Gosub Multiplex Duration = 20 Pos = 1 Gosub Multiplex Duration = 20 Pos = 2 Gosub Multiplex Disable Interrupts Led = 1 1wreset 1wwrite Skiprom 1wwrite Convert Waitms 750 'wait while the convertion is done Led = 0 1wreset 1wwrite Skiprom 1wwrite Readsp Sc(1) = 1wread(9) If Sc(9) = Crc8(sc(1) , 8) Then 'check the validity of the data Dg = Makeint(sc(1) , Sc(2)) 'and converts it into degr. Celsius Dg = Dg / 16 End If Enable Interrupts 'splits the temperature into 2 digits Tube(4) = Dg / 10 Temp = Tube(4) * 10 Tube(3) = Dg - Temp Tube(2) = 11 'degr. symbol Tube(1) = 12 '"C" Duration = 20 Pos = -4 Gosub Multiplex Duration = 20 Pos = -3 Gosub Multiplex Duration = 20 Pos = -2 Gosub Multiplex Duration = 255 Pos = -1 Gosub Multiplex Duration = 20 Pos = -2 Gosub Multiplex Duration = 20 Pos = -3 Gosub Multiplex Duration = 20 Pos = -4 Gosub Multiplex Loop End Sethour: Incr Hours If Hours > 23 Then Hours = 0 Tube(4) = Hours / 10 Temp = Tube(4) * 10 Tube(3) = Hours - Temp Temp = Makebcd(hours) Reset Temp.6 I2cstart I2cwbyte Ds1307w I2cwbyte Adhours I2cwbyte Temp I2cstop Waitms 200 Return Setmin: Incr Mins If Mins > 59 Then Mins = 0 Tube(2) = Mins / 10 Temp = Tube(2) * 10 Tube(1) = Mins - Temp Temp = Makebcd(mins) I2cstart I2cwbyte Ds1307w I2cwbyte Adsecs I2cwbyte &B00000000 I2cstop I2cstart I2cwbyte Ds1307w I2cwbyte Admins I2cwbyte Temp I2cstop Waitms 200 Return Setclock: Return Multiplex: Do For I = 1 To 4 Temp = 7seg(tube(i)) Portb = Temp J = I + Pos Portc.j = 1 Waitms 5 Portc.j = 0 Next Incr H Loop Until H = Duration H = 0 Return Function 7seg(byval Q As Byte) As Byte 'This function builds the 7 segment character Select Case Q Case 1 : 7seg = &B01111100 Case 2 : 7seg = &B00010010 Case 3 : 7seg = &B00011000 Case 4 : 7seg = &B00101100 Case 5 : 7seg = &B00001001 Case 6 : 7seg = &B00000001 Case 7 : 7seg = &B01111000 Case 8 : 7seg = &B00000000 Case 9 : 7seg = &B00001000 Case 0 : 7seg = &B01000000 Case 11 7seg = &B00101010 'degr. Case 12 7seg = &B01000011 'C End Select End Function