Professional Documents
Culture Documents
II.THIẾT BỊ
Sơ đồ nguyên lý:
Nhiệm vụ:
+ Xác định nhiệt độ lò hơi qua cảm biến nhiệt độ rồi gửi giá trị nhiệt độ lên giao
diện máy tính
+ Nhận khoảng nhiệt độ người dùng yêu cầu từ giao diện
+ Bật đèn cảnh báo khi nhiệt độ ra ngoài khoảng nhiệt yêu cầu và tắt đèn cảnh
báo khi nhiệt độ nằm trong khoảng nhiệt độ yêu cầu
+ Thay đổi thông số UART khi người dùng yêu cầu
Code Atmega16:
#ifndef IO_H
#define IO_H
#include <io.h>
#endif
#ifndef DELAY_H
#define DELAY_H
#include <delay.h>
#endif
#ifndef THUVIENUART_H
#define THUVIENUART_H
#include "thuvien_uart.h"
#endif
#ifndef THUVIENADC_H
#define THUVIENADC_H
#include "thuvien_adc.h"
#endif
#ifndef SBI
#define SBI
#define sbi(y,x) y|=(1<<x)
#endif
#ifndef CBI
#define CBI
#define cbi(y,x) y&=~(1<<x)
#endif
#define VREF 5
void SendTemp(int t)
{
if(t >= 10) SendTemp(t/10);
SentDataUart(t%10+48);
}
void ConvertDataReceive()
{
int j;
if(datareceive[0] == 'D')
{
Tmax = 0;
Tmin = 0;
for(j=1;j<i;j++)
{
if(datareceive[j] == '+') break;
Tmax = Tmax*10 + (datareceive[j] - 48);
}
j++;
for(;j<i;j++) Tmin = Tmin*10 + (datareceive[j] - 48);
}
else StartUart(SizeData[(int)(datareceive[1]-48)],Parity[(int)(datareceive[3]-
48)],datareceive[2]-48,BaudRate[(int)(datareceive[0]-48)]);
i = 0;
status = 0;
}
void main(void)
{
int datasent, t, j;
for(j=0; j<10;j++) datareceive[j] = 0;
StartAdc(VREF_AVCC,RESULT_ADC_RIGHT,ADC7,CLOCK8_ADC);
EnableAdc();
StartUart(FRAME8BIT,NOTPARITY,ONESTOPBIT,51);
EnableIntGeneral();
ddr_led = 0xff;
port_led = 0;
sbi(port_led,led);
while(1)
{
datasent = ConvertAdc(0);
t = (int)((datasent*VREF)/10.24);
if(t > Tmax) sbi(port_led,ledmax);
else cbi(port_led,ledmax);
if(t < Tmin) sbi(port_led,ledmin);
else cbi(port_led,ledmin);
SendTemp(t);
SentDataUart('!');
if(status) ConvertDataReceive();
delay_ms(500);
}
}
V.GIAO DIỆN MÁY TÍNH
Nhiệm vu:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace InterfacePC
{
public partial class Form1 : Form
{
SerialPort _SerialPort = new SerialPort("COM1",9600,Parity.None,8,StopBits.One);
private void _KhoiDong()
{
try
{
var Coms = SerialPort.GetPortNames();
textMax.Text = "30";
textMin.Text = "25";
BoxPort.Sorted = true;
for (int i = 0; i < Coms.Length; i++)
{
BoxPort.Items.Add(Coms[i]);
BoxPort.SelectedIndex = 0;
}
}
catch (Exception)
{
throw;
}
}
public Form1()
{
InitializeComponent();
}
_SerialPort.Write(BoxBaud.SelectedIndex.ToString()+BoxData.SelectedIndex.ToString()+BoxS
top.SelectedIndex.ToString()+BoxParity.SelectedIndex.ToString()+"!");
_SerialPort.Close();
while (_SerialPort.IsOpen == true) {};
_SerialPort.BaudRate = Int32.Parse(BoxBaud.Text);
_SerialPort.DataBits = Int32.Parse(BoxData.Text);
_SerialPort.Parity = (Parity)System.Enum.Parse(typeof(Parity), BoxParity.Text);
_SerialPort.StopBits = (StopBits)System.Enum.Parse(typeof(StopBits), BoxStop.Text);
_SerialPort.Open();
while (_SerialPort.IsOpen != true) {};
}
}
}
VI.KẾT QUẢ