Hướng dẫn giao tiếp COM RS232 với VĐK PIC16F887 điều khiển LED






CODE GIAO DIỆN:
Imports System.IO.Ports

Public Class Form1
    Dim lenPort As Integer
    Dim connect As Integer
    Dim ports As Array
    Dim led1, led2, led3, led4, led5, led6, led7, led8 As Boolean


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ports = SerialPort.GetPortNames()

        For i = 0 To UBound(ports)
            ComboBox1.Items.Add(ports(i))

        Next
    End Sub

    Private Sub Button_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_connect.Click
        If ComboBox1.Text = "" Or Baud_rate.Text = "" Then
            MsgBox("Bạn chưa chọn cổng COM hoặc Baud Rate")
        Else
            If connect = 0 Then
                connect = 1
                Button_connect.Text = "DisConnect"
                lblketnoi.Text = "Kết nối thành công"
                SerialPort1.PortName = ComboBox1.Text
                SerialPort1.BaudRate = 9600
                Convert.ToInt16(Baud_rate.Text)
                SerialPort1.Parity = Parity.None
                SerialPort1.StopBits = StopBits.One
                SerialPort1.DataBits = 8
                ComboBox1.Enabled = False
                Baud_rate.Enabled = False
                Buttonexit.Enabled = False
                Buttonl1.Enabled = True
                ButtonL2.Enabled = True
                ButtonL3.Enabled = True
                ButtonL4.Enabled = True
                ButtonL5.Enabled = True
                ButtonL6.Enabled = True
                ButtonL7.Enabled = True
                ButtonL8.Enabled = True
                Buttonclose.Enabled = True
                SerialPort1.Open()
            Else
                connect = 0
                Button_connect.Text = "Connected"
                lblketnoi.Text = "Đã ngắt kết nối"
                SerialPort1.Close()
                ComboBox1.Enabled = True
                Baud_rate.Enabled = True
                Buttonexit.Enabled = True
                Buttonsend.Enabled = True
                Buttonl1.Enabled = False
                ButtonL2.Enabled = False
                ButtonL3.Enabled = False
                ButtonL4.Enabled = False
                ButtonL5.Enabled = False
                ButtonL6.Enabled = False
                ButtonL7.Enabled = False
                ButtonL8.Enabled = False
                Buttonclose.Enabled = False



            End If
        End If

    End Sub


    Private Sub Buttonsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonsend.Click
        SerialPort1.Write(datasent.Text)
        If datasent.Text = "1" Then
            led1 = True
        End If
        If datasent.Text = "1" Then
            led1 = True
        End If
        If datasent.Text = "2" Then
            led2 = True
        End If
        If datasent.Text = "3" Then
            led3 = True
        End If
        If datasent.Text = "4" Then
            led4 = True
        End If
        If datasent.Text = "5" Then
            led5 = True
        End If
        If datasent.Text = "6" Then
            led6 = True
        End If
        If datasent.Text = "7" Then
            led7 = True
        End If
        If datasent.Text = "8" Then
            led8 = True
        End If
        If datasent.Text = "0" Then
            led1 = False
            led2 = False
            led3 = False
            led4 = False
            led5 = False
            led6 = False
            led7 = False
            led8 = False

        End If


    End Sub

    Private Sub Buttonexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonexit.Click
        MessageBox.Show("Bạn có chắc là thoát chương trình?", "Question", MessageBoxButtons.OK, MessageBoxIcon.Question)
        Close()
    End Sub


    Private Sub TIMER1_tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If led1 = True Then
            OvalShape1.FillColor = Color.Red
        Else
            OvalShape1.FillColor = Color.LightGray

        End If
        If led2 = True Then
            OvalShape2.FillColor = Color.Red
        Else
            OvalShape2.FillColor = Color.LightGray

        End If

        If led3 = True Then
            OvalShape3.FillColor = Color.Red
        Else
            OvalShape3.FillColor = Color.LightGray

        End If

        If led4 = True Then
            OvalShape4.FillColor = Color.Red
        Else
            OvalShape4.FillColor = Color.LightGray

        End If

        If led5 = True Then
            OvalShape5.FillColor = Color.Red
        Else
            OvalShape5.FillColor = Color.LightGray

        End If

        If led6 = True Then
            OvalShape6.FillColor = Color.Red
        Else
            OvalShape6.FillColor = Color.LightGray

        End If

        If led7 = True Then
            OvalShape7.FillColor = Color.Red
        Else
            OvalShape7.FillColor = Color.LightGray

        End If

        If led8 = True Then
            OvalShape8.FillColor = Color.Red
        Else
            OvalShape8.FillColor = Color.LightGray

        End If


    End Sub


    Private Sub Buttonl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonl1.Click
        If led1 = False Then
            led1 = True
            OvalShape1.BackColor = Color.Red
        Else
            led1 = False
            OvalShape1.BackColor = Color.White

        End If
        SerialPort1.Write("1")
    End Sub

    Private Sub ButtonL2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonL2.Click
        If led2 = False Then
            led2 = True
            OvalShape2.BackColor = Color.Red
        Else
            led2 = False
            OvalShape2.BackColor = Color.White
        End If
        SerialPort1.Write("2")
    End Sub

    Private Sub ButtonL3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonL3.Click
        If led3 = False Then
            led3 = True
            OvalShape3.BackColor = Color.Red
        Else
            led3 = False
            OvalShape3.BackColor = Color.White
        End If
        SerialPort1.Write("3")
    End Sub

    Private Sub ButtonL4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonL4.Click
        If led4 = False Then
            led4 = True
            OvalShape4.BackColor = Color.Red
        Else
            led4 = False
            OvalShape4.BackColor = Color.White
        End If
        SerialPort1.Write("4")
    End Sub

    Private Sub ButtonL5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonL5.Click
        If led5 = False Then
            led5 = True
            OvalShape5.BackColor = Color.Red
        Else
            led5 = False
            OvalShape5.BackColor = Color.White
        End If
        SerialPort1.Write("5")
    End Sub

    Private Sub ButtonL6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonL6.Click
        If led6 = False Then
            led6 = True
            OvalShape6.BackColor = Color.Red
        Else
            led6 = False
            OvalShape6.BackColor = Color.White
        End If
        SerialPort1.Write("6")
    End Sub

    Private Sub ButtonL7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonL7.Click
        If led7 = False Then
            led7 = True
            OvalShape7.BackColor = Color.Red
        Else
            led7 = False
            OvalShape7.BackColor = Color.White
            SerialPort1.Write("7")
        End If
    End Sub

    Private Sub ButtonL8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonL8.Click
        If led8 = False Then
            led8 = True
            OvalShape8.BackColor = Color.Red
        Else
            led8 = False
            OvalShape8.BackColor = Color.White
        End If
        SerialPort1.Write("8")
    End Sub

    Private Sub Buttonclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonclose.Click
        SerialPort1.Write("0")
        led1 = False
        led2 = False
        led3 = False
        led4 = False
        led5 = False
        led6 = False
        led7 = False
        led8 = False
    End Sub
End Class

 code CCS:
#include <16f887.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#byte PORTD=0x08
#bit LED1=PORTD.0
#bit LED2=PORTD.1
#bit LED3=PORTD.2
#bit LED4=PORTD.3
#bit LED5=PORTD.4
#bit LED6=PORTD.5
#bit LED7=PORTD.6
#bit LED8=PORTD.7

char ch=0;
void main()
{
   set_tris_d(0);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   PORTD=0x00;
   while(TRUE){}
}
#INT_RDA
void serial_isr()
{
   ch=getc();
   if(ch=='1')LED1=~LED1;
   if(ch=='2')LED2=~LED2;
   if(ch=='3')LED3=~LED3;
   if(ch=='4')LED4=~LED4;
   if(ch=='5')LED5=~LED5;
   if(ch=='6')LED6=~LED6;
   if(ch=='7')LED7=~LED7;
   if(ch=='8')LED8=~LED8;
   if(ch=='0')PORTD=0x00;
}


Previous
Next Post »