- 阅读权限
- 200
- UID
- 2
- 帖子
- 343
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 368 小时
- UID
- 2
- 帖子
- 343
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 368 小时
|
恒温恒湿箱控制程序
'-------------------------------------------------------------------------------
'Name : 恒温恒湿箱控制程序.bas
'Copyright : http://avr.cnta.net
'MicroChip : AtMega16
'Compiler : BASCOM-AVR 2.0.7.3
'Author : slyt/箫天
'-------------------------------------------------------------------------------
$regfile = "m16def.dat"
$crystal = 8000000
$hwstack = 100
$swstack = 100
$framesize = 100
$baud = 19200
$lib "glcdKS108.lib"
'==================================声明函数=====================================
Declare Sub Disp_10x20(byval Wr_dat As String , Byval Wr_x As Byte , Byval Wr_y As Byte)
Declare Sub Getit()
Declare Sub Get_sht11()
Declare Sub Init_sht11()
Declare Function Get_key_code() As Byte
'==================================配置资源=====================================
Config Graphlcd = 128 * 64sed , Dataport = Porta , Controlport = Portc , Ce = 3 , Ce2 = 4 , Cd = 7 , Rd = 6 , Reset = 2 , Enable = 5
Config Int1 = Falling
On Int1 Int1_int
Enable Int1
Config Timer0 = Timer , Prescale = 1024 '128us*256=32.768ms
On Timer0 Timer0_isr
Enable Timer0
'================================定义控制变量===================================
Dim Sht_timer_counter As Byte '检测温湿度时间,1秒=30
Dim Key_timer_counter As Byte '不按按钮时间,最长4秒,120
Dim Set_select As Bit '选择设置项 温度或者湿度
Dim Power_on As Bit '启动工作
Heat_on Alias Portb.4 : Ddrb.4 = 1 '加热控制
Water_on Alias Portb.3 : Ddrb.3 = 1 '加湿控制
Light_on Alias Portb.2 : Ddrb.2 = 1 '照明控制
Beep Alias Portb.1 : Ddrb.1 = 1 '提示音
Led_red Alias Portc.0 : Ddrc.0 = 1 'LED指示
Led_grn Alias Portc.1 : Ddrc.1 = 1 'LED指示
'================================定义按钮变量===================================
Ddrd = &B00000011
Portd = &HFF
Key_pin Alias Pind
Dim Key_press As Word '按钮是否按下
Dim Key_code As Byte
Const Key_select = &B1110
Const Key_up = &B1101
Const Key_down = &B1011
Const Key_start = &B0111
'===============================定义SHT11变量===================================
Dim Ctr As Byte
Dim Dataword As Word
Dim Command As Byte
Dim Dis As String * 20
Dim Temperature As Integer
Dim Humidity As Integer
Dim Old_temperature As Integer
Dim Old_humidity As Integer
Dim Calc1 As Single
Dim Calc2 As Single
Dim Calc3 As Single
Dim Rhlinear As Single
Dim Rhlintemp As Single
Dim Tempc As Single
Dim Tempf As Single
Dim Com_temperature As Integer
Dim Set_temp As Byte '温度设置值,存储在EEPROM
Dim Set_rh As Byte '湿度设置值,存储在EEPROM
Const C1 = -4 '计算湿度参数
Const C2 = 0.0405
Const C3 = -0.0000028
Const T1c = .01 '计算温度参数
Const T2 = .00008
Const T1f = .018
Sck Alias Portb.7 '定义SHT11时钟引脚
Dataout Alias Portb.6 '定义SHT11数据引脚
Datain Alias Pinb.6
Setio Alias Ddrb.6
Ddrb.7 = 1
Ddrb.6 = 1
'================================定义显示变量===================================
Dim I As Byte , J As Byte , K As Byte
Dim Zimo(3) As Byte
Dim Dianzhen As Long At Zimo Overlay
Dim Lcdstr As String * 2
'##################################绘制界面#####################################
Cls
Showpic 0 , 0 , Back
Showpic 4 , 31 , Set_set '温度SET
Showpic 45 , 31 , Set_set '湿度SET
Showpic 88 , 31 , Heat '加热图标
Showpic 88 , 0 , Water '加湿图标
Showpic 107 , 0 , Pwr_off
'读取温湿度存储值
Readeeprom Set_temp , 10
Readeeprom Set_rh , 20
If Set_temp = &HFF Then Set_temp = 30
If Set_rh = &HFF Then Set_rh = 50
Writeeeprom Set_temp , 10
Writeeeprom Set_rh , 20
Lcdstr = Str(set_temp)
Disp_10x20 Lcdstr , 13 , 30 '显示设置温度
Lcdstr = Str(set_rh)
Disp_10x20 Lcdstr , 54 , 30 '显示设置湿度
Sound Beep , 2000 , 210
Waitms 1000
Boxfill(88 , 4) -(103 , 22) , 0 '清除加湿图标
Waitms 500
Boxfill(88 , 23) -(103 , 39) , 0 '清除加热图标
Waitms 500
Boxfill(4 , 23) -(11 , 39) , 0 '清除温度SET字样
Boxfill(45 , 23) -(52 , 39) , 0 '清除湿度SET字样
'###############################################################################
Sht_timer_counter = 30
Key_timer_counter = 0
Key_press = 0
Power_on = 0
Heat_on = 0
Water_on = 0
Light_on = 0
Led_grn = 1
Temperature = 0
Humidity = 0
Old_temperature = 1
Old_humidity = 1
Sound Beep , 1000 , 210
Enable Interrupts
Do
If Sht_timer_counter > 30 Then '1秒钟检测1次温湿度
If Key_timer_counter > 120 Then '不操作超过4秒
Boxfill(4 , 23) -(11 , 39) , 0 '清除温度SET字样
Boxfill(45 , 23) -(52 , 39) , 0 '清除湿度SET字样
Writeeeprom Set_temp , 10 '存储设置值
Writeeeprom Set_rh , 20
Old_temperature = 100
Old_humidity = 100
Key_press = 0
End If
If Key_press = 0 Then '若无按钮按下
Call Init_sht11() '初始化STH11
Call Get_sht11() '获取温湿度值
If Temperature <> Old_temperature Then
Lcdstr = Str(temperature)
If Temperature < 10 Then Lcdstr = " " + Lcdstr
Disp_10x20 Lcdstr , 13 , 30 '温度显示
Old_temperature = Temperature
End If
If Humidity <> Old_humidity Then
Lcdstr = Str(humidity)
If Humidity < 10 Then Lcdstr = " " + Lcdstr
Disp_10x20 Lcdstr , 54 , 30 '湿度显示
Old_humidity = Humidity
End If
If Power_on = 1 Then '若启动工作
If Temperature <> 99 And Humidity <> 0 Then '判断是否连接传感器
If Com_temperature < Set_temp Then
Heat_on = 1 '启动加热
Showpic 88 , 31 , Heat '加热图标
End If
If Temperature >= Set_temp Then
Heat_on = 0 '停止加热
Boxfill(88 , 23) -(103 , 39) , 0 '清除加热图标
End If
If Humidity < Set_rh Then
Water_on = 1 '启动加湿
Showpic 88 , 0 , Water '加湿图标
End If
If Humidity >= Set_rh Then
Water_on = 0 '停止加湿
Boxfill(88 , 4) -(103 , 22) , 0 '清除加湿图标
End If
End If
Else
Water_on = 0
Heat_on = 0
Light_on = 0
End If
Key_timer_counter = 0 '按钮不操作定时复位
End If
Sht_timer_counter = 0 '显示定时复位
End If
Loop
End
'###############################################################################
'==================================读取按钮值===================================
Function Get_key_code() As Byte
Local Get_key As Byte
Get_key = Key_pin
Shift Get_key , Right , 4
Get_key_code = Get_key
End Function
'=================================读取温湿度值==================================
Sub Get_sht11()
Command = &B00000011
Call Getit '读取温度
Tempf = T1f * Dataword
Tempf = Tempf - 40
Tempc = T1c * Dataword
Tempc = Tempc - 40
Temperature = Round(tempc)
If Temperature > 99 Then Temperature = 99
If Temperature < 0 Then Temperature = 0
Com_temperature = Fix(tempc)
If Com_temperature > 99 Then Com_temperature = 99
If Com_temperature < 0 Then Com_temperature = 0
Command = &B00000101
Call Getit '读取湿度
Calc2 = C2 * Dataword
Calc3 = Dataword * Dataword
Calc3 = Calc3 * C3
Calc2 = Calc2 + Calc3
Rhlinear = C1 + Calc2
Calc1 = T2 * Dataword
Calc1 = Calc1 + T1c
Calc2 = Tempc - 25
Calc1 = Calc2 * Calc1
Rhlintemp = Calc1 + Rhlinear
Humidity = Round(rhlintemp)
If Humidity > 99 Then Humidity = 99
If Humidity < 0 Then Humidity = 0
End Sub
'=================================初始化SHT11===================================
Sub Init_sht11()
Set Dataout
For Ctr = 1 To 12
Set Sck
Waitus 2
Reset Sck
Waitus 2
Next Ctr
End Sub
'==================================操作SHT11====================================
Sub Getit()
Local Datavalue As Word
Local Databyte As Byte
Set Sck '启动传输
Reset Dataout
Reset Sck
Set Sck
Set Dataout
Reset Sck
Shiftout Dataout , Sck , Command , 1 '发送测量命令 00000011-测温度,00000101-测温度
Setio = 0 '设置数据口为输入
Set Sck 'SCK置高
Reset Sck 'SCK置低
Waitms 400 '等待400毫秒, 转换时间: 20/80/320ms--8/12/14bit
Shiftin Datain , Sck , Databyte , 1 '读取MSB
Datavalue = Databyte
Setio = 1 '发送ACK
Reset Dataout
Set Sck
Reset Sck
Setio = 0
Shiftin Datain , Sck , Databyte , 1 '读取LSB
Shift Datavalue , Left , 8
Datavalue = Datavalue Or Databyte
Dataword = Datavalue
Setio = 1 '发送ACK
Reset Dataout
Set Sck
Reset Sck
Setio = 0
Shiftin Datain , Sck , Databyte , 1 '读取CRC字节
Setio = 1
Set Dataout
Set Sck
Reset Sck
End Sub
'===============================写10X20点阵数字=================================
Sub Disp_10x20(wr_dat As String , Wr_x As Byte , Wr_y As Byte )
Local Index As Word
Local Temp_s As String * 1
Local Ww As Byte
Local Wx As Byte
Local Wy As Byte
Local Mybyte As Byte
Ww = Len(wr_dat)
For Mybyte = 1 To Ww
Temp_s = Mid(wr_dat , Mybyte , 1)
Select Case Temp_s
Case "0" To "9"
Index = Asc(temp_s) - 48
Case " "
Index = 10
Case Else
Index = 0
End Select
Index = Index * 30
For I = 10 To 1 Step -1
Zimo(1) = Lookup(index , Shuzi10x20) '列字节1
Incr Index
Zimo(2) = Lookup(index , Shuzi10x20) '列字节2
Incr Index
Zimo(3) = Lookup(index , Shuzi10x20) '列字节3
Incr Index
Wy = Wr_y + I
For J = 0 To 19
Wx = Wr_x + J
If Dianzhen.0 = 1 Then
Pset Wx , Wy , 1
Else
Pset Wx , Wy , 0
End If
Shift Dianzhen , Right , 1
Next
Next
Wr_y = Wr_y - 12
Next
End Sub
'===============================按钮中断========================================
Int1_int:
Key_code = Key_pin
Shift Key_code , Right , 4
If Key_code <> &B1111 Then
Incr Key_press
Sound Beep , 180 , 210
If Key_press = 1 Then '按钮第一次按下
Select Case Key_code
Case Key_start '开始按钮
Toggle Power_on
Led_red = Power_on '指示灯
Light_on = Power_on '照明灯
If Power_on = 1 Then
Showpic 107 , 0 , Pwr_on
Else
Showpic 107 , 0 , Pwr_off
Boxfill(88 , 23) -(103 , 39) , 0 '清除加热图标
Boxfill(88 , 4) -(103 , 22) , 0 '清除加湿图标
Boxfill(107 , 23) -(122 , 39) , 0 '清除风扇图标
End If
Key_press = 0
Sht_timer_counter = 25
Case Key_down To Key_select
Lcdstr = Str(set_temp)
Disp_10x20 Lcdstr , 13 , 30 '显示设置温度
Lcdstr = Str(set_rh)
Disp_10x20 Lcdstr , 54 , 30 '显示设置湿度
If Set_select = 0 Then
Showpic 4 , 31 , Set_set '温度SET
Boxfill(45 , 23) -(52 , 39) , 0 '清除湿度SET字样
Else
Showpic 45 , 31 , Set_set '湿度SET
Boxfill(4 , 23) -(11 , 39) , 0 '清除温度SET字样
End If
Key_timer_counter = 0 '按钮不操作定时复位,计时开始
End Select
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
Waitms 100
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
End If
If Key_press > 1 Then '按钮第N次按下
Select Case Key_code
Case Key_start '开始按钮
Toggle Power_on
Led_red = Power_on '指示灯
Light_on = Power_on '照明灯
If Power_on = 1 Then
Showpic 107 , 0 , Pwr_on
Else
Showpic 107 , 0 , Pwr_off
Boxfill(88 , 23) -(103 , 39) , 0 '清除加热图标
Boxfill(88 , 4) -(103 , 22) , 0 '清除加湿图标
Boxfill(107 , 23) -(122 , 39) , 0 '清除风扇图标
End If
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
Waitms 100
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
Case Key_select
Toggle Set_select
If Set_select = 0 Then
Showpic 4 , 31 , Set_set '温度SET
Boxfill(45 , 23) -(52 , 39) , 0 '清除湿度SET字样
Lcdstr = Str(set_temp)
Disp_10x20 Lcdstr , 13 , 30 '显示设置温度
Else
Showpic 45 , 31 , Set_set '湿度SET
Boxfill(4 , 23) -(11 , 39) , 0 '清除温度SET字样
Lcdstr = Str(set_rh)
Disp_10x20 Lcdstr , 54 , 30 '显示设置湿度
End If
Key_timer_counter = 0 '按钮不操作定时复位,计时开始
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
Waitms 100
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
Case Key_up
Do
If Set_select = 0 Then
Incr Set_temp
If Set_temp > 50 Then Set_temp = 50
Lcdstr = Str(set_temp)
Disp_10x20 Lcdstr , 13 , 30 '显示设置温度
Else
Incr Set_rh
If Set_rh > 80 Then Set_rh = 80
Lcdstr = Str(set_rh)
Disp_10x20 Lcdstr , 54 , 30 '显示设置湿度
End If
Key_timer_counter = 0 '按钮不操作定时复位,计时开始
Waitms 100
If Get_key_code() <> Key_up Then
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
Exit Do
Else
Waitms 200
If Get_key_code() <> Key_up Then '判断按住不放
Exit Do
End If
End If
Loop
Case Key_down
Do
If Set_select = 0 Then
Decr Set_temp
If Set_temp < 25 Then Set_temp = 25
Lcdstr = Str(set_temp)
Disp_10x20 Lcdstr , 13 , 30 '显示设置温度
Else
Decr Set_rh
If Set_rh < 10 Then Set_rh = 10
Lcdstr = Str(set_rh)
Disp_10x20 Lcdstr , 54 , 30 '显示设置湿度
End If
Key_timer_counter = 0 '按钮不操作定时复位,计时开始
Waitms 100
If Get_key_code() <> Key_down Then
Do
Key_code = Get_key_code()
Loop Until Key_code = &B1111
Exit Do
Else
Waitms 200
If Get_key_code() <> Key_down Then '判断按住不放
Exit Do
End If
End If
Loop
End Select
End If
End If
Return
'-------------------------------------------------------------------------------
Timer0_isr:
Incr Sht_timer_counter
Incr Key_timer_counter
Return
'============================10X20点阵数字======================================
Shuzi10x20:
Data &HE0 , &HFF , &H00 , &HF8 , &HFF , &H03 , &H3C , &H80 , &H07 , &H0E
Data &H00 , &H0E , &H0E , &H00 , &H0E , &H0E , &H00 , &H0E , &H3C , &H80
Data &H07 , &HF8 , &HFF , &H03 , &HC0 , &HFF , &H00 , &H00 , &H00 , &H00 '0
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &HE0 , &H00 , &H00 , &HF0
Data &H00 , &H00 , &HFE , &HFF , &H0F , &HFE , &HFF , &H0F , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 '1
Data &HF0 , &H00 , &H0E , &HFC , &H00 , &H0F , &H1C , &H80 , &H0F , &H0E
Data &HC0 , &H0E , &H0E , &H60 , &H0E , &H0E , &H38 , &H0E , &H1C , &H1E
Data &H0E , &HFC , &H0F , &H0E , &HF0 , &H03 , &H0E , &H00 , &H00 , &H00 '2
Data &H70 , &H80 , &H01 , &H7C , &H80 , &H07 , &H1E , &H00 , &H0F , &H0E
Data &H0E , &H0E , &H0E , &H0E , &H0E , &H0E , &H0E , &H0E , &H1E , &H1F
Data &H0F , &HFC , &HFB , &H07 , &HF0 , &HF1 , &H01 , &H00 , &H00 , &H00 '3
Data &H00 , &HF0 , &H00 , &H00 , &HFE , &H00 , &H80 , &HE7 , &H00 , &HE0
Data &HE1 , &H00 , &H3C , &HE0 , &H00 , &HFE , &HFF , &H0F , &HFE , &HFF
Data &H0F , &H00 , &HE0 , &H00 , &H00 , &HE0 , &H00 , &H00 , &H00 , &H00 '4
Data &HFE , &HC7 , &H01 , &HFE , &HC7 , &H07 , &H8E , &H03 , &H0F , &HCE
Data &H01 , &H0E , &HCE , &H01 , &H0E , &HCE , &H01 , &H0E , &H8E , &H87
Data &H0F , &H0E , &HFF , &H07 , &H0E , &HFE , &H01 , &H00 , &H00 , &H00 '5
Data &HE0 , &HFF , &H00 , &HF8 , &HFF , &H03 , &H3C , &H8F , &H07 , &H8E
Data &H03 , &H0E , &H8E , &H03 , &H0E , &H8E , &H03 , &H0E , &H1E , &H07
Data &H0F , &H3C , &HFF , &H07 , &H30 , &HFC , &H01 , &H00 , &H00 , &H00 '6
Data &H0E , &H00 , &H00 , &H0E , &H80 , &H0F , &H0E , &HF0 , &H0F , &H0E
Data &HFE , &H0F , &H8E , &H7F , &H00 , &HEE , &H07 , &H00 , &HFE , &H01
Data &H00 , &H3E , &H00 , &H00 , &H1E , &H00 , &H00 , &H00 , &H00 , &H00 '7
Data &HF0 , &HF1 , &H01 , &HFC , &HFB , &H07 , &H1E , &H1F , &H0F , &H0E
Data &H0E , &H0E , &H0E , &H0E , &H0E , &H0E , &H0E , &H0E , &H1E , &H1F
Data &H0F , &HFC , &HFB , &H07 , &HF0 , &HF1 , &H01 , &H00 , &H00 , &H00 '8
Data &HF8 , &H87 , &H03 , &HFC , &H9F , &H07 , &H1E , &H3C , &H0F , &H0E
Data &H38 , &H0E , &H0E , &H38 , &H0E , &H0E , &H38 , &H0F , &H3C , &H9E
Data &H07 , &HF8 , &HFF , &H03 , &HE0 , &HFF , &H00 , &H00 , &H00 , &H00 '9
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 'Space
'===============================声明图片========================================
Back:
$bgf "back.bgf"
Heat:
$bgf "heat.bgf"
Fan:
$bgf "fan.bgf"
Water:
$bgf "water.bgf"
Set_set:
$bgf "set.bgf"
Pwr_on:
$bgf "on.bgf"
Pwr_off:
$bgf "off.bgf"
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
|
|