$regfile = "m8def.dat"
$crystal = 4000000
$lib "ds1307clock.lib"
$lib "mcsbyteint.lbx"
Config Timer0 = Timer , Prescale = 64
Enable Timer0
On Timer0 Tim0
Start Timer0
Enable Interrupts
Config Portc.0 = Output
Config Portc.1 = Output
Config Portc.2 = Output
Config Portb.0 = Output
Config Portb.1 = Output
Config Portb.2 = Output
Config Portb.3 = Output
Config Watchdog = 16
Config Adc = Single , Prescaler = Auto
Start Adc
Data_74164 Alias Portb.5
Clck_74164 Alias Portb.4
's1307 config
Config Scl = Portb.6
Config Sda = Portb.7
'addres hardware for ds1307
Const Ds1307w = &HD0 'for send
Const Ds1307r = &HD1 'for recive
Dim W As Word , C As Byte , D As Byte , Seg As Byte
Dim Segment1 As Byte , Segment2 As Byte , Segment3 As Byte , Segment4 As Byte , Segment5 As Byte
Dim Segment6 As Byte , Segment7 As Byte , Segment8 As Byte , Segment9 As Byte , Segment10 As Byte
Dim Segment11 As Byte , Segment12 As Byte , Segment13 As Byte , Segment14 As Byte , Segment15 As Byte , Segment16 As Byte
Dim _sec As Byte , _min As Byte , _hour As Byte , _day As Byte , _month As Byte , _year As Byte
Dim Weekday As Byte
Config I2cdelay = 250
Do
Gosub Ds1307
Segment1 = _sec
Segment1 = _sec Mod 10
Segment2 = _sec / 10
Segment2 = Segment2 Mod 6
Segment2 = Lookup(segment2 , Digi)
Segment1 = Lookup(segment1 , Digi)
'=========================================================seconde
Segment3 = _min
Segment3 = _min Mod 10
Segment3 = Lookup(segment3 , Digi)
Segment3 = Segment3 + 128
Segment4 = _min / 10
Segment4 = Segment4 Mod 6
Segment4 = Lookup(segment4 , Digi)
'==========================================================mine
Segment5 = _hour
Segment5 = _hour Mod 10
Segment5 = Lookup(segment5 , Digi)
Segment5 = Segment5 + 128
Segment6 = _hour / 10
Segment6 = Segment6 Mod 3
Segment6 = Lookup(segment6 , Digi)
'===========================================================
Segment7 = _day Mod 10
Segment7 = Lookup(segment7 , Digi)
Segment8 = _day / 10
Segment8 = Lookup(segment8 , Digi)
Segment9 = _month Mod 10
Segment9 = Lookup(segment9 , Digi)
Segment9 = Segment9 + 128
Segment10 = _month / 10
Segment10 = Lookup(segment10 , Digi)
Segment11 = _year Mod 10
Segment11 = Lookup(segment11 , Digi)
Segment11 = Segment11 + 128
Segment12 = _year / 10
Segment12 = Lookup(segment12 , Digi)
'===============================
W = Getadc(3)
W = W / 2
Segment13 = 99
Segment15 = W / 10
C = Segment15 * 10
Segment14 = W - C
D = Segment15 Mod 10
Segment15 = Lookup(d , Digi)
Segment14 = Lookup(segment14 , Digi)
Segment16 = W / 100
Segment16 = Lookup(segment16 , Digi)
'===========================================
Portc = Weekday
If Weekday > 7 Then Weekday = 0
If Pind.0 = 0 Then Gosub Dagige
If Pind.1 = 0 Then Gosub Saeat
If Pind.2 = 0 Then Gosub Dayy
If Pind.3 = 0 Then Gosub Monthh
If Pind.4 = 0 Then Gosub Yearr
If Pind.5 = 0 Then Gosub Rooz
Loop
Ds1307:
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' start address in 1307
I2cstart ' Generate start code
I2cwbyte Ds1307r ' send address
I2crbyte _sec , Ack
I2crbyte _min , Ack ' MINUTES
I2crbyte _hour , Ack ' Hours
I2crbyte Weekday , Ack ' Day of Week
I2crbyte _day , Ack ' Day of Month
I2crbyte _month , Ack ' Month of Year
I2crbyte _year , Nack ' Year
I2cstop
_sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
_day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)
Return
'===================================================================================
Dagige:
Incr _min
If _min > 59 Then _min = 0
_min = Makebcd(_min)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 1
I2cwbyte _min
I2cstop
Return
Saeat:
Incr _hour
If _hour > 23 Then _hour = 0
_hour = Makebcd(_hour)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 2
I2cwbyte _hour
I2cstop
Return
Return
Dayy:
Incr _day
If _day > 31 Then _day = 0
_day = Makebcd(_day)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 4
I2cwbyte _day
I2cstop
Return
Monthh:
Incr _month
If _month > 12 Then _month = 0
_month = Makebcd(_month)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 5
I2cwbyte _month
I2cstop
Return
Yearr:
Incr _year
If _year > 99 Then _year = 0
_year = Makebcd(_year)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 6
I2cwbyte _year
I2cstop
Return
Rooz:
Incr Weekday
If Weekday > 7 Then Weekday = 0
Weekday = Makebcd(weekday)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 3
I2cwbyte Weekday
I2cstop
Return
Tim0:
Timer0 = 210
Portb = Seg
Select Case Seg
Case 0
Shiftout Data_74164 , Clck_74164 , Segment1 , 1
Case 1
Shiftout Data_74164 , Clck_74164 , Segment2 , 1
Case 2
Shiftout Data_74164 , Clck_74164 , Segment3 , 1
Case 3
Shiftout Data_74164 , Clck_74164 , Segment4 , 1
Case 4
Shiftout Data_74164 , Clck_74164 , Segment5 , 1
Case 5
Shiftout Data_74164 , Clck_74164 , Segment6 , 1
Case 6
Shiftout Data_74164 , Clck_74164 , Segment7 , 1
Case 7
Shiftout Data_74164 , Clck_74164 , Segment8 , 1
Case 8
Shiftout Data_74164 , Clck_74164 , Segment9 , 1
Case 9
Shiftout Data_74164 , Clck_74164 , Segment10 , 1
Case 10
Shiftout Data_74164 , Clck_74164 , Segment11 , 1
Case 11
Shiftout Data_74164 , Clck_74164 , Segment12 , 1
Case 12
Shiftout Data_74164 , Clck_74164 , Segment13 , 1
Case 13
Shiftout Data_74164 , Clck_74164 , Segment14 , 1
Case 14
Shiftout Data_74164 , Clck_74164 , Segment15 , 1
Case 15
Shiftout Data_74164 , Clck_74164 , Segment16 , 1
End Select
Incr Seg
If Seg >= 16 Then Seg = 0
Return
Digi:
Data &B1111110
Data &B0110000
Data &B1101101
Data &B1111001
Data &B0110011
Data &B1011011
Data &B1011111
Data &B1110010
Data &B1111111
Data &B1111011