BỘ CÔNG THƯƠNG
TRƯỜNG ĐẠI HỌC CÔNG NGHIỆP TP. HCM
VI ĐIỀU KHIỂN
BÀI BÁO CÁO 4
Giảng viên : PHẠM QUANG TRÍ
Sinh viên : VŨ ĐỨC HOÀNG
TP.HCM – 2021
0
0
1.Sơ đồ nguyên lí kết nối phần cứng :
2. Lưu đồ giải thuật của chương trình điều khiển(Firmware)
0
0
0
0
3.Mã nguồn của chương trình điều khiển(Firmware)
#define in_size 1
#define out_size 1
unsigned char readbuff[in_size] absolute 0x500;
unsigned char writebuff[out_size] absolute 0x540;
void interrupt(void)
{
if(USBIF_bit==1)
{
USBIF_bit=0;
USB_Interrupt_Proc();
}
}
void main()
{
ADCON1 |=0xFF;
CMCON |=0x07;
PORTB=0x00; LATB=0x00;
TRISB0_bit=TRISB1_bit=TRISB2_bit =TRISB3_bit= 1;
PORTE=0x00; LATE=0x00;
TRISE1_bit =TRISE0_bit=TRISE2_bit= 0;
UPUEN_bit=1;
FSEN_bit=1;
HID_enable(&readbuff,&writebuff);
USBIF_bit=0;
USBIE_bit=1;
GIE_bit=1;
PEIE_bit=1;
LATE0_bit= 0;
LATE1_bit= 0;
LATE2_bit= 0;
while(1)
{
if (Button(&PORTB, 0,10,0))
{
while(Button(&PORTB,0,10,0));
writebuff[0]='A';
HID_Write(&writebuff,out_size);
}
0
0
if (Button(&PORTB, 1,10,0))
{
while(Button(&PORTB,1,10,0));
writebuff[0]='B';
HID_Write(&writebuff,out_size);
}
if (Button(&PORTB,2,10,0))
{
while(Button(&PORTB,2,10,0));
writebuff[0]='C';
HID_Write(&writebuff,out_size);
}
if (Button(&PORTB,3,10,0))
{
while(Button(&PORTB,3,10,0));
writebuff[0]='D';
HID_Write(&writebuff,out_size);
}
if (HID_Read()!=0)
{
if (readbuff[0]=='a')
{
LATE0_bit = 1;
writebuff[0]='1';
HID_Write(&writebuff,out_size);
}
else if(readbuff[0]=='b')
{
LATE0_bit = 0;
writebuff[0]='2';
HID_Write(&writebuff,out_size);
}
if (readbuff[0]=='c')
{
LATE1_bit = 1;
writebuff[0]='3';
HID_Write(&writebuff,out_size);
}
else if(readbuff[0]=='d')
{
LATE1_bit = 0;
0
0
writebuff[0]='4';
HID_Write(&writebuff,out_size);
}
if (readbuff[0]=='e')
{
LATE2_bit = 1;
writebuff[0]='5';
HID_Write(&writebuff,out_size);
}
else if(readbuff[0]=='f')
{
LATE2_bit = 0;
writebuff[0]='6';
HID_Write(&writebuff,out_size);
}
if (readbuff[0]=='g')
{
LATE0_bit = 0;
LATE1_bit = 0;
LATE2_bit = 0;
writebuff[0]='7';
HID_Write(&writebuff,out_size);
}
}
}
}
4. Lưu đồ giải thuật của chương trình điều khiển(Software)
0
0
0
0
0
0
0
0
5. Mã nguồn chương trình của chương trình điều khiển(Software)
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 UsbLibrary;
namespace baituan4
{
public partial class Form1 : Form
{
byte[] readbuff = new byte[2];// chứa dữ liệu nhận được từ usb
byte[] writebuff = new byte[2];// chứa dữ liệu gửi đi từ usb
int count = 0;
int count1 = 0;
int count2 = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult answer = MessageBox.Show("Do you want to exit the
program?", "Question", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (answer == DialogResult.No)
{
e.Cancel = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
usbHidPort1.VendorId = 0x04D8;
usbHidPort1.ProductId = 0x0001;
0
0
usbHidPort1.CheckDevicePresent(); // kiem tra su co mat cua thiet bi
}
private void usbHidPort1_OnDataRecieved(object sender,
DataRecievedEventArgs args)
{
if (InvokeRequired)
{
try
{
Invoke(new
DataRecievedEventHandler(usbHidPort1_OnDataRecieved), new object[]
{ sender, args });
}
catch { }
}else
{
if(usbHidPort1.SpecifiedDevice != null)
{
readbuff = args.data;
if(readbuff[1] == '1')
{
pictureBox2.Image = baituan4.Properties.Resources.on;
}
if(readbuff[1] == '2')
{
pictureBox2.Image = baituan4.Properties.Resources.off1;
}
if (readbuff[1] == '3')
{
pictureBox1.Image = baituan4.Properties.Resources.on;
}
if (readbuff[1] == '4')
{
pictureBox1.Image = baituan4.Properties.Resources.off1;
}
if (readbuff[1] == '5')
{
pictureBox3.Image = baituan4.Properties.Resources.on;
}
if (readbuff[1] == '6')
0
0
{
pictureBox3.Image = baituan4.Properties.Resources.off1;
}
if (readbuff[1] == '7')
{
pictureBox1.Image = baituan4.Properties.Resources.off1;
pictureBox3.Image = baituan4.Properties.Resources.off1;
pictureBox2.Image = baituan4.Properties.Resources.off1;
}
if (readbuff[1] == 'A')
{
count++;
textBox2.Text= count.ToString();
}
if (readbuff[1] == 'B')
{
count1++;
textBox1.Text = count1.ToString();
}
if (readbuff[1] == 'C')
{
count2++;
textBox3.Text = count2.ToString();
}
if (readbuff[1] == 'D')
{
count = count1 = count2 = 0;
textBox2.Text = count.ToString();
textBox3.Text = count2.ToString();
textBox1.Text = count1.ToString();
}
}
}
}
private void usbHidPort1_OnDataSend(object sender, EventArgs e)
{
}
0
0
private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)
{
}
private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)
{
}
private void usbHidPort1_OnSpecifiedDeviceArrived(object sender,
EventArgs e)
{
textstatus.Text = "CONNECTED";
textstatus.BackColor = Color.Lime;
pictureBox1.Image = baituan4.Properties.Resources.off1;
pictureBox2.Image = baituan4.Properties.Resources.off1;
pictureBox3.Image = baituan4.Properties.Resources.off1;
}
private void usbHidPort1_OnSpecifiedDeviceRemoved(object sender,
EventArgs e)
{
if (InvokeRequired)
{
Invoke(new
EventHandler(usbHidPort1_OnSpecifiedDeviceRemoved),new object[]
{sender ,e });
}
else
{
textstatus.Text = "DISCONNECTED";
textstatus.BackColor = Color.Red;
pictureBox1.Image = baituan4.Properties.Resources.off1;
pictureBox2.Image = baituan4.Properties.Resources.off1;
pictureBox3.Image = baituan4.Properties.Resources.off1;
}
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
0
0
usbHidPort1.RegisterHandle(Handle);
}
protected override void WndProc(ref Message m)
{
usbHidPort1.ParseMessages(ref m);
base.WndProc(ref m);
}
private void groupBox3_Enter(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if(usbHidPort1.SpecifiedDevice != null)
{
writebuff[1] = (byte)'a';
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
else
{
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (usbHidPort1.SpecifiedDevice != null)
{
writebuff[1] = (byte)'b';
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
else
{
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
0
0
}
}
private void button4_Click(object sender, EventArgs e)
{
if (usbHidPort1.SpecifiedDevice != null)
{
writebuff[1] = (byte)'c';
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
else
{
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void button3_Click(object sender, EventArgs e)
{
if (usbHidPort1.SpecifiedDevice != null)
{
writebuff[1] = (byte)'d';
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
else
{
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void button8_Click(object sender, EventArgs e)
{
if (usbHidPort1.SpecifiedDevice != null)
{
writebuff[1] = (byte)'e';
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
else
{
0
0
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void button7_Click(object sender, EventArgs e)
{
if (usbHidPort1.SpecifiedDevice != null)
{
writebuff[1] = (byte)'f';
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
else
{
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void button6_Click(object sender, EventArgs e)
{
if (usbHidPort1.SpecifiedDevice != null)
{
DialogResult answer = MessageBox.Show("Do you want to reset
SW?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == DialogResult.Yes)
{
writebuff[1] = (byte)'D';
count = count1 = count2 = 0;
textBox2.Text = count.ToString();
textBox3.Text = count2.ToString();
textBox1.Text = count1.ToString();
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
}
else
{
0
0
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void button5_Click(object sender, EventArgs e)
{
if (usbHidPort1.SpecifiedDevice != null)
{
DialogResult answer = MessageBox.Show("Do you want to reset
LED?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == DialogResult.Yes)
{
writebuff[1] = (byte)'g';
usbHidPort1.SpecifiedDevice.SendData(writebuff);
}
}
else
{
MessageBox.Show("Device not found. Please reconnect USB device
to use!", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
6. Chi tiết chức năng từng khối giao diện
- Toàn bộ giao diện điều khiển
0
0
- Groupbox “Device Infomation” chứa 1 label và 1 textbox
Textbox “DISCONNECTED” có chức năng hiển thị trạng thái kết nối của giao
diện với mạch mô phỏng. Khi bật mạch mô phỏng textbox sẽ chuyển sang màu
xanh và chữ hiển thị sẽ đổi thành “CONNECTED”
- Tiếp theo là groupbox “Switch Status” chứa 3 label và 3 textbox
0
0
3 textbox trên hiển thị số lần nhấn nút nhấn trên mạch mô phỏng
- Groupbox “Led Control” chứa 3 label, 3 picturebox và 6 nút nhấn
Khi nhấn vào bất kì nút “ON” nào thì picturebox của LED đó sẽ chuyển thành
hình đèn sáng. Ví dụ nhấn vào “ON” của LED0:
Và ngược lại khi nhấn “OFF” LED0 :
- Cuối cùng là 2 nút nhấn chức năng “RESET SW” và “RESET LED”
0
0
“RESET SW” có chức năng đưa tất cả số đang hiển thị trên textbox trong
groupbox “Switch Status” về 0
“RESET LED” có chức năng chuyển hết tất cả hình trong pictruebox của “LED
Control” về hình đèn tắt.
7.Video minh chứng:
Link video : />
0
0