Giao tiếp máy tình với vi điều khiển 8051, điều khiển các thiết bị, gửi nhận dữ liệu giữa VDK và máy tính.

13 1.3K 6
Giao tiếp máy tình với vi điều khiển 8051, điều khiển các thiết bị, gửi nhận dữ liệu giữa VDK và máy tính.

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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.

Đề tài: Giao tiếp máy tính với vi điều khiển 8051 I Lập trình Nhiệm vụ 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 liệu lên máy tính nhận liệu gửi xuống từ máy tính sau điều khiển 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í truyền tín hiệu điều khiển xuống vi điều khiển 8051 Sơ đồ khối a Nhận – gửi liệu máy tính Start Mở cổng Nhận liệu từ VĐK Hiển thị liệu, xử lí, lưu Gửi liệu trở lại cho VĐK End b Khối giao tiếp nhận – gửi liệu vi điều khiển Start Khởi tạo Gửi liệu cho máy tính Nhận liệu từ máy tính, xử lí End Các chương trình a Chương trình khởi tạo void khoitao() // ham khoi tao { TMOD=0x20; //Chon timer che TH1=0xFD; //toc baud la 9600 SCON=0x50; //0101 0000: chon che TR1=1; } Nhiệm vụ : khởi tạo thông số cho vi điều khiển b Chương trình gửi 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;iascii) //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 byte du lieu vào ghi SBUF while(TI==0){} //doi du lieu duoc gui xong TI=0; //xoa co truyen } } Kiểm tra chương trình : gán hai giá trị cố định cho nhiệt độ độ ẩm giá trị tùy ý để gửi lên máy tính c Chương trình nhận xử lí liệu void nhanxuli() //ham nhan va xu li du lieu gui xuong tu may tinh { char d; //bien luu ki tu nhan duoc tu may tinh while(RI==0) ; //doi den co tin hieu gui xuong tu may tinh { d=SBUF; //nhan ki tu vua gui xuong RI=0; // xoa co nhan } switch(d) //xet ki tu vua nhan duoc { case 'q': //neu la q thi bat quat P1_2=1; break; //thoat khoi lenh switch case 'd': //neu la d thi bat den P1_1=1; break; //thoat khoi lenh switch case 'p': P1_0=1; break; //thoat khoi lenh switch case '1': P1_2=0; break; //thoat khoi lenh switch case '2': P1_1=0; break; //thoat khoi lenh switch case '3': P1_0=0; break; default: //thoat khoi lenh switch // neu khac thi bat het P1=0xFF; break; //thoat khoi lenh switch } } Kiểm tra chương trình : nạp chương trình vào vi điều khiển sau chạy mô Tiến hành bật tắt thiết bị cách nhấn nút bật tắt form điều khiển máy tính Quan sát tín hiệu ngõ chân điều khiển vi điều khiển proteus Kết : tất chân điều khiển hoạt động ý đồ lập trình Chương trình vi điều khiển #include //Khai báo thu vien cho 89x51 #include //Khai báo thu vien xu li chuoi int nhietdo=29; //gia lap nhiet int doam=33; //gia lap am void khoitao() // ham khoi tao { TMOD=0x20; //Chon timer che TH1=0xFD; //toc baud la 9600 SCON=0x50; //0101 0000: chon che TR1=1; } void guidulieu( int n); //ham gui so nguyen void nhanxuli(); //ham nhan du lieu va xu li void main() //chuong trinh chinh { khoitao(); //goi ham khoi tao guidulieu(nhietdo); //goi ham gui nhiet guidulieu(doam); // gui am nhanxuli(); // nhan va xu li xu lieu } 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;iascii) //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 byte du lieu vào ghi SBUF while(TI==0){} //doi du lieu duoc gui xong TI=0; //xoa co truyen } } void nhanxuli() //ham nhan va xu li du lieu gui xuong tu may tinh { char d; //bien luu ki tu nhan duoc tu may tinh while(RI==0) ; //doi den co tin hieu gui xuong tu may tinh { d=SBUF; RI=0; //nhan ki tu vua gui xuong // xoa co nhan } switch(d) //xet ki tu vua nhan duoc { case 'q': //neu la q thi bat quat P2_2=1; break; //thoat khoi lenh switch case 'd': //neu la d thi bat den P2_1=1; break; //thoat khoi lenh switch case 'p': P2_0=1; break; //thoat khoi lenh switch case '1': P2_2=0; break; //thoat khoi lenh switch case '2': P2_1=0; break; //thoat khoi lenh switch case '3': P2_0=0; break; default: //thoat khoi lenh switch // neu khac thi bat het P2=0xFF; break; //thoat khoi lenh switch } } Chương trình 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) { 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(); } } private 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 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) { } 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 { serialPort1.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) { } } }

Ngày đăng: 03/07/2016, 11:58

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan