'------------------------------------------------------------------------------- 'Name : Thermometer(DS18B20).bas 'Copyright : http://www.avrbase.com 'Micro : AtMega8 'Author : slyt/箫天 '------------------------------------------------------------------------------- $regfile = "m8def.dat" $crystal = 8000000 '$baud = 115200 $hwstack = 32 $swstack = 10 $framesize = 40 Config Portb = Output '0-7接LED A-G-DOT Config Portc = Output '0,1,2分别接LED共阳8,9,12(分别是个位,十位,百位) 'Config Serialin = Buffered , Size = 32 'Config Serialout = Buffered , Size = 32 Config 1wire = Portd.2 Dim Sled As String * 1 Dim Idx As Byte Dim Temp As Single Dim Wendu As Single Dim Waittime As Byte Dim Ledstring As String * 4 Dim Ledstr As String * 4 Dim Myword As Word Dim Dsdata(10) As Byte Dim Myinteger As Integer Portb = 0 Portc = 0 Osccal = &HB6 '转换精度是9位,精度0.5度,转换时间约为100ms '先启动一次转换 '( '初始化指令 1wreset '发送跳过ID码匹配指令 1wwrite &HCC '1wreset 'Write Scratchpad(写暂存存储器) [4EH] 1wwrite &H4E 'TH 1wwrite 0 'LH 1wwrite 0 '设置9位转换 1wwrite &H1F 1wreset 1wwrite &HCC '跳过ID码 1wwrite &H48 '将RAM的数据写入EEPROM中 Waitus 100 ') 1wreset 1wwrite &HCC '跳过ID码 1wwrite &HB8 '将EEPROM的数据读到RAM中 1wreset '发送跳过ID码匹配指令 1wwrite &HCC '发送温度转换指令 1wwrite &H44 Waitms 100 '( 1wreset 1wwrite &HCC '读取温度命令 1wwrite &HBE Dsdata(1) = 1wread(2) Myinteger = Makeint(dsdata(1) , Dsdata(2)) Temp = Myinteger * 0.0625 If Err = 1 Then Ledstring = "----" Else Ledstring = Fusing(temp , "#.#") End If '1wreset '发送跳过ID码匹配指令 '1wwrite &HCC '发送温度转换指令 '1wwrite &H44 ') 1wreset 1wwrite &HCC '读取温度命令 1wwrite &HBE Dsdata(1) = 1wread(2) Myinteger = Makeint(dsdata(1) , Dsdata(2)) Temp = Myinteger * 0.0625 Ledstring = Fusing(temp , "#.#") Do 1wreset 1wwrite &HCC '读取温度命令 1wwrite &HBE Dsdata(1) = 1wread(2) Myinteger = Makeint(dsdata(1) , Dsdata(2)) Temp = Myinteger * 0.0625 Wendu = Wendu + Temp Incr Dsdata(10) If Dsdata(10) = 10 Then Wendu = Wendu / 10 Ledstr = Fusing(wendu , "#.#") If Ledstr <> Ledstring Then Ledstring = Ledstr End If Wendu = 0 Dsdata(10) = 0 End If If Len(ledstring) < 4 Then Ledstring = "0" + Ledstring End If 1wreset '发送跳过ID码匹配指令 1wwrite &HCC '发送温度转换指令 1wwrite &H44 '显示第一个字符 Waittime = 0 Sled = Mid(ledstring , 1 , 1) Idx = Val(sled) If Idx = 0 Then If Sled = "-" Then Idx = 10 Portb = Lookup(10 , Leddata) Waittime = 8 Portc = &B100 End If Else Portb = Lookup(idx , Leddata) Waittime = 8 Portc = &B100 End If Waitms Waittime '显示第二个字符 Sled = Mid(ledstring , 2 , 1) Idx = Val(sled) If Sled = "-" Then Idx = 10 End If Portb = Lookup(idx , Leddata) Portc = &B010 If Mid(ledstring , 3 , 1) = "." Then Portb.7 = 0 End If Waitms 8 '显示第三个字符 Sled = Mid(ledstring , 4 , 1) Idx = Val(sled) If Sled = "-" Then Idx = 10 End If Portb = Lookup(idx , Leddata ) Portc = &B001 Loop End 'LED数字 (0-9) Leddata: Data &B11000000 , &B11111001 , &B10100100 , &B10110000 , &B10011001 Data &B10010010 , &B10000010 , &B11111000 , &B10000000 , &B10010000 '"-" Data &B10111111 |
欢迎光临 圣龙扬特-AVR电子 (http://avr.cnta.net/) | Powered by Discuz! X2.5 |