钻石会员
主题
回帖0
积分23707
阅读权限50
注册时间2011-1-20
最后登录1970-1-1
在线时间 小时
|
本帖最后由 keanu131 于 2012-9-17 20:26 编辑
中元节过去很久了,小鬼还是不少。刚才的问题已经解决了,把1302读写信号线像双绞线那样搅在一起就好了。。。我汗。。。。
版主大人:看您觉得如果有必要的话,可以移到单片机区去了。。。
-------------------------------------------------------------
更新分隔,原标题:
DS1302插上VCC2(5V)读出的数就乱了,拔下来好了。。。求教
RT,同时给出显示结果和代码。bascom 2.0
http://ww2.sinaimg.cn/large/9d382e6bjw1dwxkf37s7hj.jpg- $regfile = "m128def.dat"
- $crystal = 8000000
- $hwstack = 32 ' 设置堆栈大小
- $swstack = 10
- $framesize = 40
- Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Porta.5 , Db6 = Porta.6 , Db7 = Porta.7 , E = Portc.7 , Rs = Portc.6
- Config Lcd = 16 * 2
- Cursor Off Noblink
- Cls
- Dim Secs As Byte
- Dim Mins As Byte
- Dim Hour As Byte
- Dim Dates As Byte
- Dim Month As Byte
- Dim Day As Byte
- Dim Year As Byte
- Dim Tri As Byte
- '----------------------- Define RTC I/O Port --------------------------
- Config Portb.5 = Output
- Config Portb.4 = Output
- Ds_rst Alias Portb.5 'reset pin.
- Ds_dat Alias Portb.3 'IO(data pin).
- Ds_dati Alias Pinb.3 'IO(data pin). Used for reading
- Ds_clk Alias Portb.4 'Clock pin.
- '----------------------- Write Commands For DS1302 --------------------------
- Const Writesec = &H80 ' Write Seconds
- Const Writemin = &H82 ' Write Minutes
- Const Writehour = &H84 ' Write Hour
- Const Writedates = &H86 ' Write Date
- Const Writemonth = &H88 ' Write Month
- Const Writeday = &H8A ' Write Day
- Const Writeyear = &H8C ' Write Year
- Const Writectrl = &H8E ' Write Protect
- 'Const Writectrl = &H90
- Const Protect = &H80
- Const Unprotect = &H00
- '------------------------- Read Commands For DS1302 -------------------------
- Const Readsec = &H81 ' Read seconds from DS1302
- Const Readmin = &H83 ' Read minutes from DS1302
- Const Readhour = &H85 ' Read hours from DS1302
- Const Readdates = &H87 ' Read date from DS1302
- Const Readmonth = &H89 ' Read month from DS1302
- Const Readday = &H8B ' Read day from DS1302
- Const Readyear = &H8D ' Read year from DS1302
- Const Readprotect = &H8F ' Read Protect
- Const Readctrl = &H91
- Declare Sub Ds1302_w_byte(byval Wdata As Byte)
- Declare Sub Ds1302_r_byte(wdata As Byte )
- Declare Sub Write_ds1302(byval Cmd As Byte , Byval Wdata As Byte)
- Declare Function Read_ds1302(byval Cmd As Byte) As Byte
- Declare Sub Gettime
- Declare Sub Settime
- Declare Sub Ds1302_init
- Call Ds1302_init
- Do
- Call Gettime
- Locate 1 , 1
- Lcd " " ; Hex(year) ; "/" ; Hex(month) ; "/" ; Hex(dates) ; " Week " ; Hex(day)
- Locate 2 , 1
- Lcd " " ; Hex(hour) ; ":" ; Hex(mins) ; ":" ; Hex(secs)
- Waitms 500
- Loop
- End
- Sub Ds1302_init
- Tri = Read_ds1302(&Hc1)
- If Tri <> &HF0 Then 'Clock is stopped
- Call Write_ds1302(writectrl , Unprotect)
- Call Write_ds1302(writesec , &H01) 'Let it run
- Call Write_ds1302(writemin , &H28)
- Call Write_ds1302(writehour , &H18)
- Call Write_ds1302(writedates , &H17)
- Call Write_ds1302(writemonth , &H09)
- Call Write_ds1302(writeyear , &H12)
- Call Write_ds1302(writeday , &H01)
- Call Write_ds1302(&H90 , &HAA)
- Call Write_ds1302(&Hc0 , &HF0)
- Call Write_ds1302(writectrl , Protect)
- End If
- End Sub
- Sub Settime
- Call Write_ds1302(writectrl , Unprotect)
- Call Write_ds1302(writesec , Secs)
- Call Write_ds1302(writemin , Mins)
- Call Write_ds1302(writehour , Hour)
- Call Write_ds1302(writeday , Day)
- Call Write_ds1302(writedates , Dates)
- Call Write_ds1302(writemonth , Month)
- Call Write_ds1302(writeyear , Year)
- Call Write_ds1302(writectrl , Protect)
- End Sub
- Sub Gettime
- Secs = Read_ds1302(readsec)
- Mins = Read_ds1302(readmin)
- Hour = Read_ds1302(readhour)
- Dates = Read_ds1302(readdates)
- Month = Read_ds1302(readmonth)
- Day = Read_ds1302(readday)
- Year = Read_ds1302(readyear)
- End Sub
- Sub Ds1302_w_byte(wdata As Byte)
- Config Portb.3 = Output
- Local I As Byte
- Reset Ds_clk
- Waitus 2
- For I = 0 To 7
- If Wdata.i = 1 Then
- Set Ds_dat
- Else
- Reset Ds_dat
- End If
- Waitus 2
- Set Ds_clk
- Waitus 2
- Reset Ds_clk
- Next I
- End Sub
- Sub Ds1302_r_byte(wdata As Byte )
- Config Pinb.3 = Input
- Pinb.3 = 1
- Local I As Byte
- Waitus 2
- For I = 0 To 7
- Wdata.i = Ds_dati
- Waitus 2
- Set Ds_clk
- Waitus 2
- Reset Ds_clk
- Next I
- End Sub
- Sub Write_ds1302(cmd As Byte , Wdata As Byte)
- Reset Ds_rst
- Reset Ds_clk
- Set Ds_rst
- Call Ds1302_w_byte(cmd)
- Call Ds1302_w_byte(wdata)
- Set Ds_clk
- Reset Ds_rst
- End Sub
- Function Read_ds1302(cmd As Byte)
- Local I As Byte
- Local X As Byte
- Reset Ds_rst
- Reset Ds_clk
- Set Ds_rst
- Call Ds1302_w_byte(cmd)
- Call Ds1302_r_byte(x)
- Set Ds_clk
- Reset Ds_rst
- Read_ds1302 = X
- End Function
复制代码 |
|