基于51單片機的溫室控制與管理系統仿真設計(源程序+仿真文件)分享!
0.png (45.87 KB, 下載次數: 56)
下載附件
2019-3-18 18:02 上傳
實物電路中MAX232與8051的連接方法:
(1) MAX232的11腳(TIN)接8051的P3.1(TXD)
(2) MAX232的12腳(R1OUT)接8051的P3.0(RXD)
通過上位機控制時,設COM3,COM4對連
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (23.29 KB, 下載次數: 58)
下載附件
2019-3-18 18:04 上傳
0.png (17.21 KB, 下載次數: 56)
下載附件
2019-3-18 18:04 上傳
0.png (10.28 KB, 下載次數: 46)
下載附件
2019-3-18 18:05 上傳
上位機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)
2019-3-18 10:05 上傳
點擊文件名下載附件
|