پاسخ : پاسخ : [پروژه] ساعت دقیق (باDS1307) + تاریخ (شمسی,میلادی) + دم
چیز خاصی نداره :
[code=vb]Sub procedure Ds1307_set_dayofweek(dim Dayofweek_1_7 As Byte )[/code]
برنامه کلی DS1307 :
[code=vb]
'
'
$regfile = "m8def.dat" 'config micro
$crystal = 8000000
Config Lcdpin = Pin , Db4 = Portd.0 , Db5 = Portd.1 , Db6 = Portd.2 , Db7 = Portd.3 , E = Portd.4 , Rs = Portd.5
Declare Function Ds1307_is_play0_pause1()as Byte
Declare Sub Ds1307_play()
Declare Sub Ds1307_pause()
Declare Sub Ds1307_config()
Declare Sub Ds1307_set_second(byval Second_0_59 As Byte)
Declare Function Ds1307_get_second_0_59() As Byte
Declare Sub Ds1307_set_minute(byval Minute_0_59 As Byte)
Declare Function Ds1307_get_minute_0_59() As Byte
Declare Function Ds1307_is_24h_0_12h_1_mode() As Byte
Declare Sub Ds1307_set_24h_or_12h_mode(byval _24h_0_12h_1 As Byte)
Declare Function Ds1307_is_pm_1_am_0() As Byte
Declare Function Ds1307_get_hour_0_23() As Byte
Declare Function Ds1307_get_hour_0_12() As Byte
Declare Sub Ds1307_set_hour_0_23(byval Hour_0_23 As Byte )
Declare Sub Ds1307_set_hour_0_11(byval Hour_0_12 As Byte , Byval Am0_pm1 As Byte )
Declare Function Ds1307_get_dayofweek_1_7()as Byte
Declare Sub Ds1307_set_dayofweek(byval Dayofweek_1_7 As Byte )
Declare Function Ds1307_get_dayofmonth_1_31()as Byte
Declare Sub Ds1307_set_dayofmonth(byval Dayofmonth_1_31 As Byte )
Declare Function Ds1307_get_monthofyear_1_12()as Byte
Declare Sub Ds1307_set_monthofyear(byval Monthofyear_1_12 As Byte )
Declare Function Ds1307_get_year_0_99()as Byte
Declare Sub Ds1307_set_year(byval Year_0_99 As Byte )
Declare Sub Ds1307_external_1hz_on()
Declare Sub Ds1307_external_4096hz_on()
Declare Sub Ds1307_external_8192hz_on()
Declare Sub Ds1307_external_32768hz_on()
Declare Sub Ds1307_external_off_sqwe_0()
Declare Sub Ds1307_external_off_sqwe_1()
Declare Function Ds1307_read_ram(byval Ram_byte_index_0_55 As Byte) As Byte
Declare Sub Ds1307_write_ram(byval Ram_byte_index_0_55 As Byte , Byval Value As Byte)
End
Sub Ds1307_config() 'Config ds1307
Waitms 50
Dim A As Byte
Dim B As Byte
'i2c pin config
Config Scl = Portc.5 'set scl_i2c pin
Config Sda = Portc.4 'set sda_i2c pin
'i2cspeed=100khz for ds1307 'set i2c speed 100khz
Config I2cdelay = 10
End Sub
Function Ds1307_is_play0_pause1()as Byte 'timer and date counter is pause or play?
I2cstart 'i2c start
I2cwbyte &B11010000 'write mode
I2cwbyte 0 'set registr in index 0
I2cstart 'restart i2c for change mode
I2cwbyte &B11010001 'read mode
I2crbyte A , Nack 'read defult registr(reister 0)
I2cstop 'stop i2c
A = A And &B10000000 '==> 0 or 10000000
If A = 0 Then 'bit_7 is 0
Ds1307_is_play0_pause1 = 0
Else 'bit_7 is 1
Ds1307_is_play0_pause1 = 1
End If
End Function
Sub Ds1307_play() 'timer and date conter-->play
I2cstart
'set defult register index
I2cwbyte &B11010000
I2cwbyte 0
'change to reade mode
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A
A = A And &B01111111 'bit_7=0 ==> set counter to play
If B = A Then
I2cstop
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cwbyte A
I2cstop
End Sub
Sub Ds1307_pause() 'timer and date conter-->pause
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A
A = A Or &B10000000 'bit_7=1 ==> set counter to pause
If B = A Then
I2cstop
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cwbyte A
I2cstop
End Sub
'--------------921203-------
Sub Ds1307_set_second(byval Second_0_59 As Byte)
If Second_0_59 > 59 Then
Exit Sub
End If
A = Makebcd(second_0_59)
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cstart
I2cwbyte &B11010001
I2crbyte B , Nack
B = B And &B10000000 'extract bit_7
A = A Or B 'merge
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cwbyte A
I2cstop
End Sub
'-------------921205---------
Function Ds1307_get_second_0_59() As Byte
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
A = A And &B01111111 'clear bit_7
Ds1307_get_second_0_59 = Makedec(a)
End Function
Sub Ds1307_set_minute(byval Minute_0_59 As Byte) 'ok
If Minute_0_59 > 59 Then
Exit Sub
End If
A = Makebcd(minute_0_59)
I2cstart
I2cwbyte &B11010000
I2cwbyte 1
I2cwbyte A
I2cstop
End Sub
Function Ds1307_get_minute_0_59() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 1
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_minute_0_59 = Makedec(a)
End Function
'-------------921206---------
Function Ds1307_is_24h_0_12h_1_mode() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
A = A And &B01000000
If A = 0 Then
Ds1307_is_24h_0_12h_1_mode = 0
Else
Ds1307_is_24h_0_12h_1_mode = 1
End If
End Function
'-------------921209---------
Sub Ds1307_set_24h_or_12h_mode(byval _24h_0_12h_1 As Byte) 'ok
If _24h_0_12h_1 > 1 Then
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A And &B01000000
If B = _24h_0_12h_1 Then
I2cstop
Exit Sub
End If
If B = 0 Then 'is 24h mode -->change to 12h mode
B = A And &B00111111
B = Makedec(b)
If B < 12 Then 'set am
A = A And &B00011111
A = A Or &B01000000
Elseif B = 12 Then 'set pm
A = A And &B00011111
A = A Or &B01100000
Else 'b > 12 ->set pm and houre= h -12
A = A And &B00111111
A = Makedec(a)
A = A - 12
A = Makebcd(a)
A = A Or &B01100000
End If
Else 'is 12h mode -->change to 24h mode
B = A
B = B And &B00100000
If B = 0 Then 'is am
A = A And &B00111111
Else 'is pm
A = A And &B00011111
A = Makedec(a)
If A = 12 Then
A = Makebcd(a)
Else 'is<12
A = A + 12
A = Makebcd(a)
End If
End If
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cwbyte A
I2cstop
End Sub
Function Ds1307_is_pm_1_am_0() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
B = A And &B01000000
If B = 0 Then 'is 24h mode
B = A And &B00111111
B = Makedec(b)
If B < 12 Then 'is am
Ds1307_is_pm_1_am_0 = 0
Else 'is pm
Ds1307_is_pm_1_am_0 = 1
End If
Else 'is 12h mode
B = A And &B00100000
If B = 0 Then 'is am
Ds1307_is_pm_1_am_0 = 0
Else ' is pm
Ds1307_is_pm_1_am_0 = 1
End If
End If
End Function
Function Ds1307_get_hour_0_23() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
B = A And &B01000000
If B = 0 Then 'is 24h mode
B = A And &B00111111
Ds1307_get_hour_0_23 = Makedec(b)
Else 'is 12h mode
B = A And &B00100000 'is am or pm?
A = A And &B00011111
A = Makedec(a)
If B = 0 Then 'is am
Ds1307_get_hour_0_23 = A
Else 'is pm
If A = 12 Then
Ds1307_get_hour_0_23 = A
Else
Ds1307_get_hour_0_23 = A + 12
End If
End If
End If
End Function
Function Ds1307_get_hour_0_12() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
B = A And &B01000000
If B = 0 Then 'is 24h mode
B = A And &B00111111
B = Makedec(b)
If B > 12 Then B = B - 12
Ds1307_get_hour_0_12 = B
Else 'is 12h mode
B = A And &B00011111
B = Makedec(b)
Ds1307_get_hour_0_12 = B
End If
End Function
Sub Ds1307_set_hour_0_23(byval Hour_0_23 As Byte ) 'ok
If Hour_0_23 > 23 Then
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A And &B01000000
A = A And &B11000000
If B = 0 Then 'is 24h mode
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00111111
A = A Or Hour_0_23
Else 'is 12h mode
If Hour_0_23 < 12 Then 'am
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00011111
A = A Or Hour_0_23
Elseif Hour_0_23 = 12 Then
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00011111
A = A Or Hour_0_23
A = A Or &B00100000
Else 'pm is>12
Hour_0_23 = Hour_0_23 - 12
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00011111
A = A Or Hour_0_23
A = A Or &B00100000
End If
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cwbyte A
I2cstop
End Sub
Sub Ds1307_set_hour_0_11(byval Hour_0_12 As Byte , Byval Am0_pm1 As Byte ) 'ok
If Hour_0_12 > 12 Then
Exit Sub
Elseif Am0_pm1 > 1 Then
Exit Sub
Elseif Hour_0_12 = 0 Then
Am0_pm1 = 0
Elseif Hour_0_12 = 12 Then
Am0_pm1 = 1
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A And &B01000000
A = A And &B11000000
If B = 0 Then 'is 24h mode
If Am0_pm1 = 1 Then
If Hour_0_12 <> 12 Then 'is <> 12
Hour_0_12 = Hour_0_12 + 12
End If
End If
A = A Or Makebcd(hour_0_12)
Else 'is 12h mode
A = A Or Makebcd(hour_0_12)
If Am0_pm1 = 1 Then
A = A Or &B00100000 'pm
Else
A = A And &B11011111 'am
End If
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cwbyte A
I2cstop
End Sub
Function Ds1307_get_dayofweek_1_7()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 3
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_dayofweek_1_7 = Makedec(a)
End Function
Sub Ds1307_set_dayofweek(byval Dayofweek_1_7 As Byte ) 'ok
If Dayofweek_1_7 = 0 Then
Exit Sub
Elseif Dayofweek_1_7 > 7 Then
Exit Sub
End If
Dayofweek_1_7 = Makebcd(dayofweek_1_7)
I2cstart
I2cwbyte &B11010000
I2cwbyte 3
I2cwbyte Dayofweek_1_7
I2cstop
End Sub
Function Ds1307_get_dayofmonth_1_31()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 4
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_dayofmonth_1_31 = Makedec(a)
End Function
Sub Ds1307_set_dayofmonth(byval Dayofmonth_1_31 As Byte ) 'ok
If Dayofmonth_1_31 = 0 Then
Exit Sub
Elseif Dayofmonth_1_31 > 31 Then
Exit Sub
End If
Dayofmonth_1_31 = Makebcd(dayofmonth_1_31)
I2cstart
I2cwbyte &B11010000
I2cwbyte 4
I2cwbyte Dayofmonth_1_31
I2cstop
End Sub
Function Ds1307_get_monthofyear_1_12()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 5
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_monthofyear_1_12 = Makedec(a)
End Function
Sub Ds1307_set_monthofyear(byval Monthofyear_1_12 As Byte ) 'ok
If Monthofyear_1_12 = 0 Then
Exit Sub
Elseif Monthofyear_1_12 > 12 Then
Exit Sub
End If
Monthofyear_1_12 = Makebcd(monthofyear_1_12)
I2cstart
I2cwbyte &B11010000
I2cwbyte 5
I2cwbyte Monthofyear_1_12
I2cstop
End Sub
Function Ds1307_get_year_0_99()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 6
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_year_0_99 = Makedec(a)
End Function
Sub Ds1307_set_year(byval Year_0_99 As Byte ) 'ok
If Year_0_99 > 99 Then
Exit Sub
End If
Year_0_99 = Makebcd(year_0_99)
I2cstart
I2cwbyte &B11010000
I2cwbyte 6
I2cwbyte Year_0_99
I2cstop
End Sub
Sub Ds1307_external_1hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010000
I2cstop
End Sub
Sub Ds1307_external_4096hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010001
I2cstop
End Sub
Sub Ds1307_external_8192hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010010
I2cstop
End Sub
Sub Ds1307_external_32768hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010011
I2cstop
End Sub
Sub Ds1307_external_off_sqwe_0() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00000000
I2cstop
End Sub
Sub Ds1307_external_off_sqwe_1() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B10000000
I2cstop
End Sub
Function Ds1307_read_ram(byval Ram_byte_index_0_55 As Byte) As Byte 'ok
If Ram_byte_index_0_55 > 55 Then
Exit Function
End If
Ram_byte_index_0_55 = Ram_byte_index_0_55 + 8
I2cstart
I2cwbyte &B11010000
I2cwbyte Ram_byte_index_0_55
I2cstart
I2cwbyte &B11010001
I2crbyte Ds1307_read_ram , Nack
I2cstop
End Function
Sub Ds1307_write_ram(byval Ram_byte_index_0_55 As Byte , Byval Value As Byte) 'ok
If Ram_byte_index_0_55 > 55 Then
Exit Sub
End If
Ram_byte_index_0_55 = Ram_byte_index_0_55 + 8
I2cstart
I2cwbyte &B11010000
I2cwbyte Ram_byte_index_0_55
I2cwbyte Value
I2cstop
End Sub
[/code]
برگرفته از : www.Rahmanian.US
نوشته اصلی توسط masoud.moghaddam21
[code=vb]Sub procedure Ds1307_set_dayofweek(dim Dayofweek_1_7 As Byte )[/code]
برنامه کلی DS1307 :
[code=vb]
'
'
$regfile = "m8def.dat" 'config micro
$crystal = 8000000
Config Lcdpin = Pin , Db4 = Portd.0 , Db5 = Portd.1 , Db6 = Portd.2 , Db7 = Portd.3 , E = Portd.4 , Rs = Portd.5
Declare Function Ds1307_is_play0_pause1()as Byte
Declare Sub Ds1307_play()
Declare Sub Ds1307_pause()
Declare Sub Ds1307_config()
Declare Sub Ds1307_set_second(byval Second_0_59 As Byte)
Declare Function Ds1307_get_second_0_59() As Byte
Declare Sub Ds1307_set_minute(byval Minute_0_59 As Byte)
Declare Function Ds1307_get_minute_0_59() As Byte
Declare Function Ds1307_is_24h_0_12h_1_mode() As Byte
Declare Sub Ds1307_set_24h_or_12h_mode(byval _24h_0_12h_1 As Byte)
Declare Function Ds1307_is_pm_1_am_0() As Byte
Declare Function Ds1307_get_hour_0_23() As Byte
Declare Function Ds1307_get_hour_0_12() As Byte
Declare Sub Ds1307_set_hour_0_23(byval Hour_0_23 As Byte )
Declare Sub Ds1307_set_hour_0_11(byval Hour_0_12 As Byte , Byval Am0_pm1 As Byte )
Declare Function Ds1307_get_dayofweek_1_7()as Byte
Declare Sub Ds1307_set_dayofweek(byval Dayofweek_1_7 As Byte )
Declare Function Ds1307_get_dayofmonth_1_31()as Byte
Declare Sub Ds1307_set_dayofmonth(byval Dayofmonth_1_31 As Byte )
Declare Function Ds1307_get_monthofyear_1_12()as Byte
Declare Sub Ds1307_set_monthofyear(byval Monthofyear_1_12 As Byte )
Declare Function Ds1307_get_year_0_99()as Byte
Declare Sub Ds1307_set_year(byval Year_0_99 As Byte )
Declare Sub Ds1307_external_1hz_on()
Declare Sub Ds1307_external_4096hz_on()
Declare Sub Ds1307_external_8192hz_on()
Declare Sub Ds1307_external_32768hz_on()
Declare Sub Ds1307_external_off_sqwe_0()
Declare Sub Ds1307_external_off_sqwe_1()
Declare Function Ds1307_read_ram(byval Ram_byte_index_0_55 As Byte) As Byte
Declare Sub Ds1307_write_ram(byval Ram_byte_index_0_55 As Byte , Byval Value As Byte)
End
Sub Ds1307_config() 'Config ds1307
Waitms 50
Dim A As Byte
Dim B As Byte
'i2c pin config
Config Scl = Portc.5 'set scl_i2c pin
Config Sda = Portc.4 'set sda_i2c pin
'i2cspeed=100khz for ds1307 'set i2c speed 100khz
Config I2cdelay = 10
End Sub
Function Ds1307_is_play0_pause1()as Byte 'timer and date counter is pause or play?
I2cstart 'i2c start
I2cwbyte &B11010000 'write mode
I2cwbyte 0 'set registr in index 0
I2cstart 'restart i2c for change mode
I2cwbyte &B11010001 'read mode
I2crbyte A , Nack 'read defult registr(reister 0)
I2cstop 'stop i2c
A = A And &B10000000 '==> 0 or 10000000
If A = 0 Then 'bit_7 is 0
Ds1307_is_play0_pause1 = 0
Else 'bit_7 is 1
Ds1307_is_play0_pause1 = 1
End If
End Function
Sub Ds1307_play() 'timer and date conter-->play
I2cstart
'set defult register index
I2cwbyte &B11010000
I2cwbyte 0
'change to reade mode
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A
A = A And &B01111111 'bit_7=0 ==> set counter to play
If B = A Then
I2cstop
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cwbyte A
I2cstop
End Sub
Sub Ds1307_pause() 'timer and date conter-->pause
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A
A = A Or &B10000000 'bit_7=1 ==> set counter to pause
If B = A Then
I2cstop
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cwbyte A
I2cstop
End Sub
'--------------921203-------
Sub Ds1307_set_second(byval Second_0_59 As Byte)
If Second_0_59 > 59 Then
Exit Sub
End If
A = Makebcd(second_0_59)
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cstart
I2cwbyte &B11010001
I2crbyte B , Nack
B = B And &B10000000 'extract bit_7
A = A Or B 'merge
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cwbyte A
I2cstop
End Sub
'-------------921205---------
Function Ds1307_get_second_0_59() As Byte
I2cstart
I2cwbyte &B11010000
I2cwbyte 0
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
A = A And &B01111111 'clear bit_7
Ds1307_get_second_0_59 = Makedec(a)
End Function
Sub Ds1307_set_minute(byval Minute_0_59 As Byte) 'ok
If Minute_0_59 > 59 Then
Exit Sub
End If
A = Makebcd(minute_0_59)
I2cstart
I2cwbyte &B11010000
I2cwbyte 1
I2cwbyte A
I2cstop
End Sub
Function Ds1307_get_minute_0_59() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 1
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_minute_0_59 = Makedec(a)
End Function
'-------------921206---------
Function Ds1307_is_24h_0_12h_1_mode() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
A = A And &B01000000
If A = 0 Then
Ds1307_is_24h_0_12h_1_mode = 0
Else
Ds1307_is_24h_0_12h_1_mode = 1
End If
End Function
'-------------921209---------
Sub Ds1307_set_24h_or_12h_mode(byval _24h_0_12h_1 As Byte) 'ok
If _24h_0_12h_1 > 1 Then
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A And &B01000000
If B = _24h_0_12h_1 Then
I2cstop
Exit Sub
End If
If B = 0 Then 'is 24h mode -->change to 12h mode
B = A And &B00111111
B = Makedec(b)
If B < 12 Then 'set am
A = A And &B00011111
A = A Or &B01000000
Elseif B = 12 Then 'set pm
A = A And &B00011111
A = A Or &B01100000
Else 'b > 12 ->set pm and houre= h -12
A = A And &B00111111
A = Makedec(a)
A = A - 12
A = Makebcd(a)
A = A Or &B01100000
End If
Else 'is 12h mode -->change to 24h mode
B = A
B = B And &B00100000
If B = 0 Then 'is am
A = A And &B00111111
Else 'is pm
A = A And &B00011111
A = Makedec(a)
If A = 12 Then
A = Makebcd(a)
Else 'is<12
A = A + 12
A = Makebcd(a)
End If
End If
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cwbyte A
I2cstop
End Sub
Function Ds1307_is_pm_1_am_0() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
B = A And &B01000000
If B = 0 Then 'is 24h mode
B = A And &B00111111
B = Makedec(b)
If B < 12 Then 'is am
Ds1307_is_pm_1_am_0 = 0
Else 'is pm
Ds1307_is_pm_1_am_0 = 1
End If
Else 'is 12h mode
B = A And &B00100000
If B = 0 Then 'is am
Ds1307_is_pm_1_am_0 = 0
Else ' is pm
Ds1307_is_pm_1_am_0 = 1
End If
End If
End Function
Function Ds1307_get_hour_0_23() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
B = A And &B01000000
If B = 0 Then 'is 24h mode
B = A And &B00111111
Ds1307_get_hour_0_23 = Makedec(b)
Else 'is 12h mode
B = A And &B00100000 'is am or pm?
A = A And &B00011111
A = Makedec(a)
If B = 0 Then 'is am
Ds1307_get_hour_0_23 = A
Else 'is pm
If A = 12 Then
Ds1307_get_hour_0_23 = A
Else
Ds1307_get_hour_0_23 = A + 12
End If
End If
End If
End Function
Function Ds1307_get_hour_0_12() As Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
B = A And &B01000000
If B = 0 Then 'is 24h mode
B = A And &B00111111
B = Makedec(b)
If B > 12 Then B = B - 12
Ds1307_get_hour_0_12 = B
Else 'is 12h mode
B = A And &B00011111
B = Makedec(b)
Ds1307_get_hour_0_12 = B
End If
End Function
Sub Ds1307_set_hour_0_23(byval Hour_0_23 As Byte ) 'ok
If Hour_0_23 > 23 Then
Exit Sub
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A And &B01000000
A = A And &B11000000
If B = 0 Then 'is 24h mode
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00111111
A = A Or Hour_0_23
Else 'is 12h mode
If Hour_0_23 < 12 Then 'am
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00011111
A = A Or Hour_0_23
Elseif Hour_0_23 = 12 Then
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00011111
A = A Or Hour_0_23
A = A Or &B00100000
Else 'pm is>12
Hour_0_23 = Hour_0_23 - 12
Hour_0_23 = Makebcd(hour_0_23)
Hour_0_23 = Hour_0_23 And &B00011111
A = A Or Hour_0_23
A = A Or &B00100000
End If
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cwbyte A
I2cstop
End Sub
Sub Ds1307_set_hour_0_11(byval Hour_0_12 As Byte , Byval Am0_pm1 As Byte ) 'ok
If Hour_0_12 > 12 Then
Exit Sub
Elseif Am0_pm1 > 1 Then
Exit Sub
Elseif Hour_0_12 = 0 Then
Am0_pm1 = 0
Elseif Hour_0_12 = 12 Then
Am0_pm1 = 1
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
B = A And &B01000000
A = A And &B11000000
If B = 0 Then 'is 24h mode
If Am0_pm1 = 1 Then
If Hour_0_12 <> 12 Then 'is <> 12
Hour_0_12 = Hour_0_12 + 12
End If
End If
A = A Or Makebcd(hour_0_12)
Else 'is 12h mode
A = A Or Makebcd(hour_0_12)
If Am0_pm1 = 1 Then
A = A Or &B00100000 'pm
Else
A = A And &B11011111 'am
End If
End If
I2cstart
I2cwbyte &B11010000
I2cwbyte 2
I2cwbyte A
I2cstop
End Sub
Function Ds1307_get_dayofweek_1_7()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 3
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_dayofweek_1_7 = Makedec(a)
End Function
Sub Ds1307_set_dayofweek(byval Dayofweek_1_7 As Byte ) 'ok
If Dayofweek_1_7 = 0 Then
Exit Sub
Elseif Dayofweek_1_7 > 7 Then
Exit Sub
End If
Dayofweek_1_7 = Makebcd(dayofweek_1_7)
I2cstart
I2cwbyte &B11010000
I2cwbyte 3
I2cwbyte Dayofweek_1_7
I2cstop
End Sub
Function Ds1307_get_dayofmonth_1_31()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 4
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_dayofmonth_1_31 = Makedec(a)
End Function
Sub Ds1307_set_dayofmonth(byval Dayofmonth_1_31 As Byte ) 'ok
If Dayofmonth_1_31 = 0 Then
Exit Sub
Elseif Dayofmonth_1_31 > 31 Then
Exit Sub
End If
Dayofmonth_1_31 = Makebcd(dayofmonth_1_31)
I2cstart
I2cwbyte &B11010000
I2cwbyte 4
I2cwbyte Dayofmonth_1_31
I2cstop
End Sub
Function Ds1307_get_monthofyear_1_12()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 5
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_monthofyear_1_12 = Makedec(a)
End Function
Sub Ds1307_set_monthofyear(byval Monthofyear_1_12 As Byte ) 'ok
If Monthofyear_1_12 = 0 Then
Exit Sub
Elseif Monthofyear_1_12 > 12 Then
Exit Sub
End If
Monthofyear_1_12 = Makebcd(monthofyear_1_12)
I2cstart
I2cwbyte &B11010000
I2cwbyte 5
I2cwbyte Monthofyear_1_12
I2cstop
End Sub
Function Ds1307_get_year_0_99()as Byte 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 6
I2cstart
I2cwbyte &B11010001
I2crbyte A , Nack
I2cstop
Ds1307_get_year_0_99 = Makedec(a)
End Function
Sub Ds1307_set_year(byval Year_0_99 As Byte ) 'ok
If Year_0_99 > 99 Then
Exit Sub
End If
Year_0_99 = Makebcd(year_0_99)
I2cstart
I2cwbyte &B11010000
I2cwbyte 6
I2cwbyte Year_0_99
I2cstop
End Sub
Sub Ds1307_external_1hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010000
I2cstop
End Sub
Sub Ds1307_external_4096hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010001
I2cstop
End Sub
Sub Ds1307_external_8192hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010010
I2cstop
End Sub
Sub Ds1307_external_32768hz_on() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00010011
I2cstop
End Sub
Sub Ds1307_external_off_sqwe_0() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B00000000
I2cstop
End Sub
Sub Ds1307_external_off_sqwe_1() 'ok
I2cstart
I2cwbyte &B11010000
I2cwbyte 7
I2cwbyte &B10000000
I2cstop
End Sub
Function Ds1307_read_ram(byval Ram_byte_index_0_55 As Byte) As Byte 'ok
If Ram_byte_index_0_55 > 55 Then
Exit Function
End If
Ram_byte_index_0_55 = Ram_byte_index_0_55 + 8
I2cstart
I2cwbyte &B11010000
I2cwbyte Ram_byte_index_0_55
I2cstart
I2cwbyte &B11010001
I2crbyte Ds1307_read_ram , Nack
I2cstop
End Function
Sub Ds1307_write_ram(byval Ram_byte_index_0_55 As Byte , Byval Value As Byte) 'ok
If Ram_byte_index_0_55 > 55 Then
Exit Sub
End If
Ram_byte_index_0_55 = Ram_byte_index_0_55 + 8
I2cstart
I2cwbyte &B11010000
I2cwbyte Ram_byte_index_0_55
I2cwbyte Value
I2cstop
End Sub
[/code]
برگرفته از : www.Rahmanian.US
دیدگاه