Lập trình giao tiếp máy tính qua cổng COM. Một chương trình ở vi điều khiển dùng để truyền dữ liệu lên máy tính và nhận dữ liệu gửi xuống từ máy tính sau đó điều khiển các thiết bị ( quạt, đèn, phun sương). Một chương trình ở máy tính dùng để nhận tín hiệu từ vi điều khiển sau đó xử lí và truyền tín hiệu điều khiển xuống vi điều khiển 8051.
Trang 1Đề tài: Giao tiếp máy tính với vi điều khiển 8051
Lập trình giao tiếp máy tính qua cổng COM Một chương trình ở vi điều khiển dùng để truyền dữ liệu lên máy tính và nhận dữ liệu gửi xuống từ máy tính sau đó điều khiển các thiết bị ( quạt, đèn, phun sương) Một chương trình ở máy tính dùng
để nhận tín hiệu từ vi điều khiển sau đó xử lí và truyền tín hiệu điều khiển xuống
vi điều khiển 8051
a Nhận – gửi dữ liệu trên máy tính.
Start
Mở cổng
Nhận dữ liệu từ VĐK
Hiển thị dữ liệu, xử lí, lưu
Gửi dữ liệu trở lại cho VĐK
End
Trang 2b Khối giao tiếp nhận – gửi dữ liệu trên vi điều khiển.
3 Các chương trình con
a Chương trình khởi tạo
{
TMOD=0x20; //Chon timer che do 2
TH1=0xFD; //toc do baud la 9600
SCON=0x50; //0101 0000: chon che do 1
TR1=1;
Start Khởi tạo
Gửi dữ liệu cho máy tính
Nhận dữ liệu từ máy tính, xử lí
End
Trang 3}
Nhiệm vụ : khởi tạo các thông số cho vi điều khiển
b Chương trình gửi dữ liệu lên cho máy tính
void guidulieu(int n) // dinh nghia ham gui du lieu
{
unsigned char a[5]; //mang chua cac ki tu cua du lieu int i; //bien dem
for(i=0;i<5;i++) //vong lap tach cac ki tu trong du lieu
{
a[i]=(n%10)+48; //tach chu so hang don vi (->ascii)
n=n/10; //loai bo chu so hang don vi
}
for(i=2;i>=0;i ) //vong lap gui cac ki tu len pc
{
SBUF=a[i]; //Ghi 1 byte du lieu vào thanh ghi SBUF
while(TI==0){} //doi du lieu duoc gui xong
}
}
Kiểm tra chương trình : gán hai giá trị cố định cho nhiệt độ và độ ẩm một giá trị tùy ý để gửi lên máy tính
c Chương trình nhận và xử lí dữ liệu.
{
while(RI==0) ; //doi den khi co tin hieu gui xuong tu may tinh
Trang 4{
d=SBUF; //nhan ki tu vua gui xuong
}
switch(d) //xet ki tu vua nhan duoc {
P1_2=1;
P1_1=1;
case 'p':
P1_0=1;
case '1':
P1_2=0;
case '2':
P1_1=0;
case '3':
P1_0=0;
Trang 5default: // neu khac thi bat het P1=0xFF;
}
}
Kiểm tra chương trình : nạp chương trình vào vi điều khiển sau đó chạy mô phỏng Tiến hành bật và tắt các thiết bị bằng cách nhấn nút bật và tắt trong form điều khiển trên máy tính Quan sát tín hiệu ngõ ra ở các chân điều khiển của vi điều khiển trong proteus
Kết quả : tất các chân điều khiển đều hoạt động như ý đồ lập trình
4 Chương trình ở vi điều khiển
#include <at89x51.h> //Khai báo thu vien cho 89x51
#include <string.h> //Khai báo thu vien xu li chuoi
{
TMOD=0x20; //Chon timer che do 2
TH1=0xFD; //toc do baud la 9600
SCON=0x50; //0101 0000: chon che do 1
TR1=1;
}
void nhanxuli(); //ham nhan du lieu va xu li
void main() //chuong trinh chinh
Trang 6
}
void guidulieu(int n) // dinh nghia ham gui du lieu
{
unsigned char a[5]; //mang chua cac ki tu cua du lieu int i; //bien dem
for(i=0;i<5;i++) //vong lap tach cac ki tu trong du lieu
{
a[i]=(n%10)+48; //tach chu so hang don vi (->ascii)
n=n/10; //loai bo chu so hang don vi
}
for(i=2;i>=0;i ) //vong lap gui cac ki tu len pc
{
SBUF=a[i]; //Ghi 1 byte du lieu vào thanh ghi SBUF
while(TI==0){} //doi du lieu duoc gui xong
}
}
{
while(RI==0) ; //doi den khi co tin hieu gui xuong tu may tinh
Trang 7{
d=SBUF; //nhan ki tu vua gui xuong
}
switch(d) //xet ki tu vua nhan duoc {
P2_2=1;
P2_1=1;
case 'p':
P2_0=1;
case '1':
P2_2=0;
case '2':
P2_1=0;
case '3':
P2_0=0;
Trang 8default: // neu khac thi bat het P2=0xFF;
}
}
5 Chương trình trên máy tính.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace choncongCOM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] comlist = SerialPort.GetPortNames();
int[] comnumberlist = new int[comlist.Length];
for (int i = 0; i < comlist.Length; i++)
{
comnumberlist[i] = int.Parse(comlist[i].Substring(3));
}
Array.Sort(comnumberlist);
foreach (int comnumber in comnumberlist)
{
comboBox1.Items.Add("COM" + comnumber.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
Trang 9{
if (comboBox1.Text == "")
{
MessageBox.Show("vui long chọn cổng COM", "thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (serialPort1.IsOpen)
{
serialPort1.Close();
button1.Text = "Kết Nối";
comboBox1.Enabled = true;
groupBox3.Enabled = false;
}
else
{
try
{
serialPort1.Open();
button1.Text = "Ngắt Kết Nối";
comboBox1.Enabled = false;
groupBox3.Enabled = true;
}
catch
{
MessageBox.Show("khong the mo cong " + comboBox1.Text, "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
Trang 10private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Write(textBox1.Text);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
Action<string> serialPort1ReceiverAction;
private void serialPort1_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
serialPort1ReceiverAction = serialPortReceiver;
try
{
this.BeginInvoke(serialPort1ReceiverAction, serialPort1.ReadExisting()); }
catch { }
}
private void serialPortReceiver(string input)
{
if(input.Length==6)
{
textBox2.Text += "\r\nNhiệt độ (độ C): ";
textBox2.Text += input.Substring(0,3);
textBox2.Text += "\r\nĐộ ẩm (%): ";
textBox2.Text += input.Substring(3);
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
Trang 11{
}
private void button3_Click(object sender, EventArgs e)
{
if (button3.Text == "Bật")
{
{
serialPort1.Write("q");
button3.Text = "Tắt";
}
}
else
{
try
{
serialPort1.Write("1");
button3.Text = "Bật";
}
catch
{
MessageBox.Show("không thể tắt quạt", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button4_Click(object sender, EventArgs e)
{
if (button4.Text == "Bật")
{
try
{
serialPort1.Write("d");
button4.Text = "Tắt";
}
catch
{
MessageBox.Show("không thể bật đèn", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
Trang 12serialPort1.Write("2");
button4.Text = "Bật";
}
catch
{
MessageBox.Show("không thể tắt đèn", "loi", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void button5_Click(object sender, EventArgs e)
{
if (button5.Text == "Bật")
{
try
{
serialPort1.Write("p");
button5.Text = "Tắt";
}
catch
{
MessageBox.Show("không thể bật phun sương", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
serialPort1.Write("3");
button5.Text = "Bật";
}
catch
{
MessageBox.Show("không thể tắt phun sương", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void groupBox3_Enter(object sender, EventArgs e)
{
}
private void groupBox2_Enter_1(object sender, EventArgs e)
{
Trang 13} } }