基于51单片机的温室控制与管理系统仿真设计(源程序+仿真文件)分享!
实物电路中MAX232与8051的连接方法:
(1) MAX232的11脚(TIN)接8051的P3.1(TXD)
(2) MAX232的12脚(R1OUT)接8051的P3.0(RXD)
通过上位机控制时,设COM3,COM4对连
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
上位机C#程序(VS2008)源程序如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using Microsoft.Win32;
- namespace 温室控制程序
- {
- public partial class Form_Main : Form
- {
- public Form_Main()
- {
- InitializeComponent();
- }
- //--------------------------------------------------------------------
- // 代理函数
- //--------------------------------------------------------------------
- delegate void SetTextCallBack(string s);
- //--------------------------------------------------------------------
- // 关闭窗体时提示确认
- //--------------------------------------------------------------------
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (MessageBox.Show("您确定要退出系统吗?", "确认",
- MessageBoxButtons.YesNo, MessageBoxIcon.Question,
- MessageBoxDefaultButton.Button2) == DialogResult.No)
- {
- e.Cancel = true;
- }
- }
- //--------------------------------------------------------------------
- // 获取串口列表
- //--------------------------------------------------------------------
- public void GetComList()
- {
- RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
- if (keyCom != null)
- {
- string[] sSubKeys = keyCom.GetValueNames();
- comboBox1.Items.Clear();
- foreach (string sName in sSubKeys)
- {
- string sValue = (string)keyCom.GetValue(sName);
- comboBox1.Items.Add(sValue);
- }
- }
- }
- //--------------------------------------------------------------------
- // 退出系统
- //--------------------------------------------------------------------
- private void button_退出_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- //--------------------------------------------------------------------
- //打开串口
- //--------------------------------------------------------------------
- private void button_打开串口_Click(object sender, EventArgs e)
- {
- if (button_打开串口.Text == "打开串口")
- {
- if (serialPort1.IsOpen) serialPort1.Close();
- serialPort1.PortName = comboBox1.Text;
- try
- {
- serialPort1.Open();
- toolStripStatusLabel1.Text = comboBox1.Text + " 打开";
- button_打开串口.Text = "关闭串口";
- }
- catch { toolStripStatusLabel1.Text = "端口打开错误!!!"; }
- }
- else
- {
- serialPort1.Close();
- toolStripStatusLabel1.Text = "端口关闭";
- button_打开串口.Text = "打开串口";
- }
- }
- //--------------------------------------------------------------------
- // 加载窗体时执行
- //--------------------------------------------------------------------
- private void Form_Main_Load(object sender, EventArgs e)
- {
- comboBox1.Items.Clear();
- GetComList();
- if (comboBox1.Items.Count != 0) comboBox1.SelectedIndex = 1;
- RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\温室监控系统2010",true);
- if (reg == null)
- {
- reg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\温室监控系统2010");
- }
- if (reg != null)
- {
- comboBox1.Text = (string)reg.GetValue("端口");
- }
- }
- //--------------------------------------------------------------------
- // 关闭窗体时将当前串口选项写入注册表
- //--------------------------------------------------------------------
- private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
- {
- if (serialPort1.IsOpen) serialPort1.Close();
- RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\温室监控系统2010",true);
- if (reg != null) reg.SetValue("端口", comboBox1.Text);
- }
- //--------------------------------------------------------------------
- // 喷灌按钮操作
- //--------------------------------------------------------------------
- private void button_喷灌_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen)
- { MessageBox.Show("串口未打开,控制命令无法发送!", "提示"); return; }
- if (button_喷灌.Text == "喷灌")
- {
- //发送控制指令 开启继电器
- serialPort1.WriteLine("$PUMP_OPEN");
- button_喷灌.Text = "停止喷灌";
- }
- else
- {
- //发送控制指令 关闭继电器
- serialPort1.WriteLine("$PUMP_CLOSE");
- button_喷灌.Text = "喷灌";
- }
- }
- //--------------------------------------------------------------------
- // 通风按钮操作
- //--------------------------------------------------------------------
- private void button_通风_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen)
- { MessageBox.Show("串口未打开,控制命令无法发送!", "提示"); return; }
- if (button_通风.Text == "通风")
- {
- //发送控制指令 开启电机1
- serialPort1.WriteLine("$WIND_OPEN");
- button_通风.Text = "停止通风";
- }
- else
- {
- //发送控制指令 关闭电机1
- serialPort1.WriteLine("$WIND_CLOSE");
- button_通风.Text = "通风";
- }
- }
- //--------------------------------------------------------------------
- // 采光按钮操作
- //--------------------------------------------------------------------
- private void button_采光_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen)
- { MessageBox.Show("串口未打开,控制命令无法发送!", "提示"); return; }
- if (button_采光.Text == "采光")
- {
- //发送控制指令 开启电机2
- serialPort1.WriteLine("$LIGHT_OPEN");
- button_采光.Text = "停止采光";
- }
- else
- {
- //发送控制指令 关闭电机2
- serialPort1.WriteLine("$LIGHT_CLOSE");
- button_采光.Text = "采光";
- }
- }
- //--------------------------------------------------------------------
- // 显示代理函数
- //--------------------------------------------------------------------
- private void SetText(string s)
- {
- if (label_温度.InvokeRequired)
- {
- SetTextCallBack d = new SetTextCallBack(SetText);
- Invoke(d, new object[] { s });
- }
- else
- {
- label_温度.Text = s.Trim() + " ℃";
- label_温度.Refresh();
- }
- }
- //--------------------------------------------------------------------
- // 串口接收温度数据并通过代理程序显示
- //--------------------------------------------------------------------
- private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
- {
- try { SetText(serialPort1.ReadLine()); } catch { }
- }
- }
- }
复制代码
单片机源程序如下:
- //-----------------------------------------------------------------
- // 名称: 温室控制系统仿真设计
- //-----------------------------------------------------------------
- // 说明: K1~K3按键分别控制通风、采光及水泵电机开关,LCD显示当前温度值.
- // 上位机按钮可分别实现K1~K3的控制功能,系统运行时,下位机温度值
- // 将刷新显示在上位机C#窗体中.
- //
- //-----------------------------------------------------------------
- #include <reg51.h>
- #include <intrins.h>
- #include <string.h>
- #include <stdio.h>
- #define INT8U unsigned char
- #define INT16U unsigned int
- #define MAX_CHAR 11 //允许接收并保存的最大字符个数
- volatile INT8U recv_buff[MAX_CHAR+1]; //串口接收缓冲
- volatile INT8U Buf_Index = 0; //缓冲索引
- extern INT8U Read_Temperature(); //读传感器温度函数
- extern void LCD_Initialize(); //LCD初始化函数
- extern void LCD_ShowString(INT8U , INT8U,INT8U *);//在指定行/列显示字符串函数
- extern INT8U Temp_Value[]; //从DS18B20读取的数据
- extern void delay_ms(INT16U x); //延时函数
- char Disp_Buffer[17]; //LCD显示缓冲
- volatile INT8U recv_OK = 0; //上位机命令串接收成功标识
- //相关按键、控制引脚等定义
- sbit K1 = P1^5; //通风电机开关控制按键
- sbit K2 = P1^6; //采光电机开关控制按键
- sbit K3 = P1^7; //水泵开关控制按键
- sbit F_IN1 = P1^0; //通风电机控制端
- sbit F_IN2 = P1^1;
- sbit F_IN3 = P1^2; //采光电机控制端
- sbit F_IN4 = P1^3;
- sbit RELAY = P2^4; //水泵控制继电器
- sbit LED_1 = P2^5; //通风电机开关指示灯
- sbit LED_2 = P2^6; //采光电机开关指示灯
- sbit LED_3 = P2^7; //水泵指示灯
- //-----------------------------------------------------------------
- // 串口输出字符串
- //-----------------------------------------------------------------
- void PutStr(char *s)
- {
- }
- //-----------------------------------------------------------------
- // 主函数
- //-----------------------------------------------------------------
- void main()
- {
- }
- //-----------------------------------------------------------------
- // INT0中断函数
- //-----------------------------------------------------------------
- void INT0_ISR() interrupt 0
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
温室监控系统仿真设计.zip
(533.84 KB, 下载次数: 102)
|