圣龙扬特-AVR电子
标题:
BASCOM-AVR drive DS3231
[打印本页]
作者:
箫天
时间:
2020-2-27 21:48
标题:
BASCOM-AVR drive DS3231
[attach]950[/attach]
' *********************************************
' * DS3231(RTC)I2C *
' * (Ver 1.01) *
' * Copyright By Slyt 2020.2.27 *
' *********************************************
'...................................................................................
$regfile
=
"m16def.dat"
' specify the used micro
$crystal
=
7372800
' used crystal frequency
$hwstack
=
32
' default use 32 for the hardware stack
$swstack
=
64
' default use 64 for the SW stack
$framesize
=
64
'...................................................................................
Config
Lcd
=
16
*
2
Config
Lcdpin
=
Pin
,
Db4
=
Porta
.
4
,
Db5
=
Porta
.
5
,
Db6
=
Porta
.
6
,
Db7
=
Porta
.
7
,
E
=
Porta
.
1
,
Rs
=
Porta
.
0
Cursor
Off
Noblink
Config
Date
=
Ymd
,
Separator
=
Minus
'yy-mm-dd
Config
Clock
=
Soft
Config
Scl
=
Portc
.
0
'Is serial clock SCL
Config
Sda
=
Portc
.
1
'Is serial data SDA
I2cinit
'...................................................................................
Declare
Sub
Write3231_date
()
Declare
Sub
Write3231_time
()
Declare
Sub
Read3231datetime
()
Declare
Sub
Read3231temp
()
'...................................................................................
Deflcdchar
0
,
12
,
18
,
18
,
12
,
32
,
32
,
32
,
32
' replace [x] with number (0-7)
Const
Write_address
=
&
B11010000
'Write address &HD0
Const
Read_address
=
&
B11010001
'Read address &HD1
Const
Seconds_address
=
&
H00
Const
Minutes_address
=
&
H01
Const
Hour_address
=
&
H02
Const
Weekday_address
=
&
H03
Const
Day_address
=
&
H04
Const
Month_address
=
&
H05
Const
Year_address
=
&
H06
Const
Temp_address
=
&
H11
'...................................................................................
Dim
Mybyte
As
Byte
Dim
Addrwr
As
Byte
Dim
Weekday
As
Byte
Dim
Weekday_str
(
7
)
As
String
*
5
Dim
Temp_h
As
Byte
Dim
Temp_l
As
Byte
Dim
Temp_s
As
String
*
1
Weekday_str
(
1
)
=
"Sun. "
Weekday_str
(
2
)
=
"Mon. "
Weekday_str
(
3
)
=
"Tues."
Weekday_str
(
4
)
=
"Wed. "
Weekday_str
(
5
)
=
"Thur."
Weekday_str
(
6
)
=
"Fri. "
Weekday_str
(
7
)
=
"Sat. "
'***********************************************************************************
'初始化日期
Date
$
=
"20-02-27"
Time
$
=
"15:42:55"
Weekday
=
5
' Fur.
Write3231_date
Write3231_time
Waitms
10
Cls
Do
Read3231datetime
Locate
1
,
1
Lcd
"20"
;
Date
$
Locate
2
,
1
Lcd
Time
$
Locate
1
,
12
Lcd
Weekday_str
(
weekday
)
Read3231temp
Locate
2
,
10
Lcd
Temp_s
;
Str
(
temp_h
)
;
"."
;
Str
(
temp_l
)
;
" "
Locate
2
,
16
Lcd
Chr
(
0
)
Waitms
500
Loop
End
'***********************************************************************************
Sub
Write3231_date
()
_day
=
Makebcd
(
_day
)
:
_month
=
Makebcd
(
_month
)
:
_year
=
Makebcd
(
_year
)
:
Weekday
=
Makebcd
(
weekday
)
I2cstart
I2cwbyte
Write_address
' Send write address
I2cwbyte
Weekday_address
' Starting address in 3231
I2cwbyte
Weekday
' Weekday
I2cwbyte
_day
' Send data to day
I2cwbyte
_month
' Send data to month
I2cwbyte
_year
' Send data to year
I2cstop
End
Sub
'-----------------------------------------------------------------------------------
Sub
Write3231_time
()
_sec
=
Makebcd
(
_sec
)
:
_min
=
Makebcd
(
_min
)
:
_hour
=
Makebcd
(
_hour
)
I2cstart
' Generate start code
I2cwbyte
Write_address
' Send write address
I2cwbyte
Seconds_address
' Starting address in 3231
I2cwbyte
_sec
' Send data to seconds
I2cwbyte
_min
' Send data to minutes
I2cwbyte
_hour
' Send data to hours
I2cstop
End
Sub
'-----------------------------------------------------------------------------------
Sub
Read3231datetime
()
I2cstart
' Generate start code
I2cwbyte
Write_address
' Send write address
I2cwbyte
Seconds_address
' Start address in 3231
I2cstart
' Generate start code
I2cwbyte
Read_address
' Send read address
I2crbyte
_sec
,
Ack
' Seconds
I2crbyte
_min
,
Ack
' Minutes
I2crbyte
_hour
,
Ack
' Hours
I2crbyte
Weekday
,
Ack
' Weekday
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
)
:
Weekday
=
Makedec
(
weekday
)
_day
=
Makedec
(
_day
)
:
_month
=
Makedec
(
_month
)
:
_year
=
Makedec
(
_year
)
End
Sub
'-----------------------------------------------------------------------------------
Sub
Read3231temp
()
'(
DS3231的温度数据是存于地址为0X11,0X12的两个8位存储器中的,其中0x11中存放着温度数据的
高8位(其中含有一位正负温度标志位),0x12中高两位存放着数据的小数位(0,0.25,0.5,0.75)。
')
I2cstart
' Generate start code
I2cwbyte
Write_address
' Send address
I2cwbyte
Temp_address
' Start address in 3231
I2cstart
' Generate start code
I2cwbyte
Read_address
' Send address
I2crbyte
Temp_h
,
Ack
' MSB of Temp
I2crbyte
Temp_l
,
Nack
' LSB of Temp
I2cstop
Shift
Temp_l
,
Right
,
6
Temp_l
=
Temp_l
*
25
If
Temp_h
.
7
=
1
Then
Temp_s
=
"-"
If
Temp_h
.
7
=
0
Then
Temp_s
=
" "
End
Sub
'-----------------------------------------------------------------------------------
作者:
查子账
时间:
2022-5-26 14:10
作者:
yyj679
时间:
2024-8-24 06:39
要弄8025,感谢老大
欢迎光临 圣龙扬特-AVR电子 (http://avr.cnta.net/)
Powered by Discuz! X2.5