📄 ex0031.bas
字号:
'------------------------------------------------------------------------------
'名 称 : ex0031-1.bas
'目 的 : 试验读取pcf8563时间日期,在LCD显示
'按 键 说 明 : 按键sw13为 调整/正常 功能按钮,按键sw14,sw15,sw16,sw11为调整按钮←↓→↑
'
'目 标 芯 片 : Mega16
'试 验 环 境 : mega16_32%20kit.pdf(www.basicavr.com M16试验板)
'编 译 环 境 : BASCOM-AVR 1.11.8.1 DEMO
'跳 线 设 置 : J42/J43 =12 J46=23,J16按晶体情况连接;没提到跳线均为空
'熔 丝 说 明 : 外部晶振12M, JTAG禁止
'------------------------------------------------------------------------------
$regfile = "m16def.dat"
'$lib "mcsbyte.lbx" ' use the byte lib since we do not need longs
$crystal = 12000000
$baud = 19200
'declare used subs
Declare Sub Settime(byval S1 As Byte , Byval M1 As Byte , Byval H1 As Byte , Byval D1 As Byte , Byval Month1 As Byte)
Declare Sub Gettime()
Declare Sub Time_start()
Declare Sub Time_stop()
Declare Sub Time_set()
'Declare variables
Dim Tm(7) As Byte At &H60 '定义存放时间日期的数组
Dim Show_str(7) As String * 4 '定义变量 tm()对应内容 调整时间时显示用
Dim I As Byte , Temp As Byte
Dim J As Byte
Dim Clock_state As Bit '时钟的工作状态标志,0:工作;1 调整
'These are pointers to tm() for simple handling. 下面定义的几个变量对应tm(1-7)
Dim S As Byte At &H60 Overlay
Dim M As Byte At &H61 Overlay
Dim H As Byte At &H62 Overlay
Dim D As Byte At &H63 Overlay
Dim W As Byte At &H64 Overlay
Dim Month As Byte At &H65 Overlay
Dim Year As Byte At &H66 Overlay
Dim Cent As Integer
Dim B As Byte '键值
Dim Set_up As Byte '调整时增加量 +时为2,-时为0,不操作时为1
Dim Set_addr As Byte '要调整的寄存器地址 时间、日期的地址为02H到08H
Dim Byte_old_key As Byte '记录旧键值
'configure the used port pin for I2C
Config I2cdelay = 5 ' default slow mode
Config Sda = Portd.2
Config Scl = Portd.3
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portb.3 , Rs = Portb.4
Config Lcd = 16 * 2
Config Kbd = Portc , Delay = 50 '键盘端口和消抖延时
Clock_state = 0
Set_up = 1
Set_addr = 2
Show_str(1) = "sec"
Show_str(2) = "min"
Show_str(3) = "hou"
Show_str(4) = "day"
Show_str(5) = "wek"
Show_str(6) = "mon"
Show_str(7) = "yer"
' not needed since the pins are in the right state
'I2cinit
'Call Settime(0 , 0 , 0 , 1 , 1 ) '设置时间 1-1,0:0:0,
Cls
Do
Call Gettime
'since the values are stored in BCD format we can use Hex() to display them
H = H And &H3F
M = M And &H7F
S = S And &H7F
Cent = Month And &H80
If Cent = 0 Then
Cent = 1900
Else
Cent = 2000
End If
Month = Month And &H3F
Cent = Cent + Makedec(year)
D = D And &H3F
W = W And &H7
Locate 1 , 1 '指定LCD显示位置
Lcd Hex(h ) ; ":" ; Hex(m) ; ":" ; Hex(s) 'LCD显示
Locate 2 , 1
Lcd Cent ; "-" ; Hex(month) ; "-" ; Hex(d) ; " w " ; Hex(w)
B = Getkbd() '读取键值
If B = 0 Then '设置 工作/调整状态
Clock_state = Not Clock_state
Locate 1 , 10 '指定LCD显示位置
If Clock_state = 1 Then
Lcd "set " 'LCD显示
Else
Lcd " "
End If
End If
Locate 1 , 10 '指定LCD显示位置
If Clock_state = 1 Then
Lcd "set " ; Show_str(set_addr -1) 'LCD显示
Select Case B
Case 4 : Set_addr = Set_addr - 1
Case 8 : Set_up = 0
Case 9 : Set_up = 2
Case 12 : Set_addr = Set_addr + 1
End Select
If Set_addr > 8 Then
Set_addr = 8
End If
If Set_addr < 2 Then
Set_addr = 2
End If
If Set_up <> 1 Then
Call Time_set()
Set_up = 1
End If
End If
Waitms 100
Loop
End
Sub Gettime()
'there are 2 ways to get the time. With low level i2c calls or with a high level call
'first the high level call
Tm(1) = 2 ' point to second register
I2creceive &HA3 , Tm(1) , 1 , 7 ' write the second address and get 7 bytes back
'i2creceive will first write 1 byte from tm(1) which is 2, and then will read 5 bytes and store it onto tm(1)-tm(5)
'and optional with low level calls
' For I = 1 To 7
' Temp = I - 1
' I2cstart
' I2cwbyte &HA2 'write addres of PCF8583
' I2cwbyte Temp 'select register
' I2cstart 'repeated start
' I2cwbyte &HA3 'write address for reading info
' I2crbyte Tm(i) , Nack 'read data
' Next
'I2cstop
End Sub
Sub Settime(s1 As Byte , M1 As Byte , H1 As Byte , D1 As Byte , Month1 As Byte)
'values are stored as BCD values so convert the values first
Tm(1) = Makebcd(s1) 'seconds
Tm(2) = Makebcd(m1) 'minutes
Tm(3) = Makebcd(h1) 'hours
Tm(4) = Makebcd(d1) 'days
Tm(5) = 5 'week
Tm(6) = Makebcd(month1) Or &H80 'cent=20
Tm(7) = Makebcd(06) 'year=06 默认06年
Call Time_stop()
I2cstart 'repeated start
I2cwbyte &HA2 'write mode
I2cwbyte 2 'select seconds Register
For I = 1 To 7
I2cwbyte Tm(i)
Next
I2cstop
Call Time_start()
End Sub
Sub Time_stop()
I2cstart 'generate start
I2cwbyte &HA2 'write address
I2cwbyte 0 'select control register
I2cwbyte 32 'clock stop
I2cstop
End Sub
Sub Time_start()
I2cstart 'generate start
I2cwbyte &HA2 'write address
I2cwbyte 0 'select control register
I2cwbyte 0 'clock run
I2cstop
End Sub
Sub Time_set()
I2cstart
I2cwbyte &HA2 'write addres of PCF8583
I2cwbyte Set_addr 'select register
I2cstart 'repeated start
I2cwbyte &HA3 'write address for reading info
I2crbyte Temp , Nack 'read data
I2cstop
' Print "t"
' Print Temp
' If Set_addr <> 6 Then '处理星期部分
Temp = Makedec(temp)
' End If
' print temp
Temp = Temp + Set_up
Temp = Temp - 1
' print temp
' If Set_addr <> 6 Then
Temp = Makebcd(temp)
' End If
Print Temp
Call Time_stop()
I2cstart 'repeated start
I2cwbyte &HA2 'write mode
I2cwbyte Set_addr 'select seconds Register
I2cwbyte Temp
I2cstop
Call Time_start()
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -