|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2009-11-1 17:24:38
|
显示全部楼层
本实验再次更新,加入秒表功能,在正常走时中,按下S1键启动秒表,按下S2键停止秒表,再加上前面所述的测温功能,代码如下:
'这是一个M8+DS1302的数字钟程序
'portb.5接DS1302的RST,portb.4接sclk,portb.3接I/O?
'本机采用三键结构,其中pinc.2为选择调试键,pinc.4为数字加键,pinc.3为数字减键,相关数值调整后同步写入DS1302相应的寄存器。
$regfile = "m8def.dat"
$crystal = 8000000
$hwstack = 32 ' 设置堆栈大小
$swstack = 10
$framesize = 40
Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2
Config Lcd = 16 * 2
Config Adc = Single , Prescaler = Auto , Reference = Internal
Cursor Off
Declare Sub Outbit(a As Byte) '发送字节
Declare Sub Inbit(indat As Byte) '接收字节
Declare Sub Finish '结束操作
Declare Sub Begin '开始写入
Dim A As Byte
Dim B As Byte
Dim C As Byte
Dim D As Byte
Dim J As Byte
Dim Ax As Byte , Ad As Byte
Dim T(7) As Byte , Ts(7) As String * 2 , Tq As String * 2 , Ta As String * 2
Dim Tt As Byte
Dim W As Long , W1 As Word , W_adc As String * 4
Dim Shi As String * 2 , Fen As String * 2 , Miao As String * 2 , S As Bit , S1 As Byte , S2 As Byte , S3 As Byte , S4 As Byte
Start Adc
Call Begin '充电设定
A = &H90 '地址144
Call Outbit(a ) '写入地址
A = &B10101010 '允许充电。电阻4K,两个二极管
Call Outbit(a) '写入充电设置
Call Finish '结束操作
Waitms 10
Cls
Ax = 126
Ad = 127
Tt = 0
B = 0
C = 0
W = 0
W1 = 0
S = 0
D = 0
Do
If S = 0 Then
For J = 1 To 7
Ad = Ad + 2
A = Ad
Call Begin
Call Outbit(a) '写入读取数据的地址
Call Inbit(b) '读取数据
Call Finish '结束操作
B = Makedec(b) '取数,bcd2dec转换
Ts(j) = Str(b)
Ts(j) = Format(ts(j) , "00")
Next J
Locate 2 , 1
Lcd " " ; Ts(3) ; ":" ; Ts(2) ; ":" ; Ts(1)
Locate 1 , 1
Lcd "20" ; Ts(7) ; "-" ; Ts(5) ; "-" ; Ts(4) ; " " ; Ts(6) ; "day"
Ad = 127
Elseif S = 1 Then
Miao = Str(s3)
Fen = Str(s2)
Shi = Str(s1)
Miao = Format(miao , "00")
Fen = Format(fen , "00")
Shi = Format(shi , "00")
Locate 2 , 1
Lcd Shi ; ":" ; Fen ; ":" ; Miao
A = 129
Call Begin
Call Outbit(a)
Call Inbit(d)
Call Finish
If D <> B Then
S3 = S3 + 1
If S3 > 59 Then
S2 = S2 + 1
S3 = 0
If S2 > 59 Then
S1 = S1 + 1
S2 = 0
S3 = 0
End If
End If
End If
B = D
End If
If Pinc.2 = 0 Then '选择调试
Waitms 20
If Pinc.2 = 0 Then
Incr Tt
If Tt > 7 Then Tt = 0
End If
End If
For J = 1 To 30
W1 = Getadc(1)
W = W + W1
Next J
W = W / 30
W = W * 2560
W = W / 1094
W_adc = Str(w)
W_adc = Format(w_adc , "00.0")
Locate 2 , 12
Lcd W_adc ; "C"
If Tt <> 0 Then
Tq = Ts(tt)
Ts(tt) = " "
Locate 2 , 3
Lcd Ts(3) ; ":" ; Ts(2) ; ":" ; Ts(1)
Locate 1 , 1
Lcd "20" ; Ts(7) ; "-" ; Ts(5) ; "-" ; Ts(4) ; " " ; Ts(6) ; "day"
Ts(tt) = Tq
Waitms 800
If Pinc.4 = 0 Or Pinc.3 = 0 Then
Waitms 20
If Pinc.4 = 0 Then '数字加
B = Val(ts(tt))
Incr B
Elseif Pinc.3 = 0 Then '数字减
B = Val(ts(tt))
Decr B
End If
T(tt) = Makebcd(b)
C = Tt * 2
Ax = Ax + C
A = Ax
Call Begin '写入使能
Call Outbit(a) '首先写入地址
A = T(tt)
Call Outbit(a) '然后写入数据
Call Finish '写入后结束本次写入
Ax = 126
End If
End If
If Pinc.4 = 0 And Tt = 0 Then '启动秒表程序段
Waitms 20
If Pinc.4 = 0 Then
Shi = "00"
Fen = "00"
Miao = "00"
S1 = 0
S2 = 0
S3 = 0
S = 1
B = 0
A = 129
Bitwait Pinc.4 , Set
Cls
Call Begin
Call Outbit(a)
Call Inbit(b)
Call Finish
End If
End If
If Pinc.3 = 0 And S = 1 Then '停止秒表
Waitms 20
If Pinc.3 = 0 Then
S = 0
Cls
Bitwait Pinc.3 , Set
End If
End If
Loop
End
Sub Outbit(a As Byte ) '发送数据子程序
Local Temp As Byte , Tempi As Byte , I As Byte
Ddrb.4 = 1
Portb.4 = 0
Ddrb.3 = 1
Portb.3 = 0
For I = 0 To 7
Portb.4 = 0
Temp = 2 ^ I
Tempi = A And Temp
If Tempi = 0 Then
Portb.3 = 0
Else
Portb.3 = 1
End If
Portb.4 = 1
Next I
End Sub
Sub Inbit(indat As Byte ) '接收数据的子程序
Local Readtemp As Byte , Temp As Byte , I As Byte
Ddrb.4 = 1
Portb.4 = 1
Indat = 0
Readtemp = 0
Ddrb.3 = 0
Portb.3 = 1
For I = 0 To 7
Portb.4 = 1 '高电位换数据
Portb.4 = 0
If Pinb.3 = 1 Then
Temp = 2 ^ I
Readtemp = Readtemp + Temp
Elseif Pinb.3 = 0 Then
Temp = 0
End If
Next I
Indat = Readtemp
Portb.4 = 0
End Sub
Sub Finish '结束操作子程序
Ddrb.4 = 0
Portb.4 = 0
Ddrb.5 = 1
Portb.5 = 0
End Sub
Sub Begin '开始操作子程序
Ddrb.5 = 1
Portb.5 = 1
Ddrb.4 = 1
Portb.4 = 0
End Sub |
|