串行通信PC x PIC

时间:2016-01-08 19:04:37

标签: c++ vb.net pic

我正在尝试在PC和PIC16f873a之间进行通信,使用PC上的vb.net和PIC中的C ++。我正在使用max232。按电脑上的按钮我无法点亮灯光: 编辑:

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Close()
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    Try
        SerialPort1.Open()
        Button7.Enabled = True
        Button6.Enabled = False
        GroupBox1.Enabled = True
        GroupBox2.Enabled = True
        GroupBox4.Enabled = True
        ToolStripStatusLabel1.Text = "Connect to COM3"

    Catch ex As Exception
        Console.WriteLine(ex.Message)
        MsgBox("COM. Port Not Found", MsgBoxStyle.Exclamation)
    End Try
End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
    SerialPort1.Close()
    Button7.Enabled = False
    Button6.Enabled = True
    GroupBox1.Enabled = False
    GroupBox2.Enabled = False
    GroupBox4.Enabled = False
    ToolStripStatusLabel1.Text = "No Connection"
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    SerialPort1.Write(CStr(Int(1)))
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    SerialPort1.Write(Chr(2))
End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    SerialPort1.Write(Chr(CInt(TextBox1.Text)))
End Sub
End Class

我的PIC是:

#define LED1 PORTB.f0
unsigned char input;
void main() {
    TRISB = 0x00;
    PORTB = 0b00001110;
    SPBRG=129;
    TXSTA=0b00100110;
    RCSTA=0b10010000;
    UART1_Init(9600);           // initialize USART module
    delay_ms(100);              //  (8 bit, 9600 baud rate, no parity bit...)
    while (1) {
        PORTB.F1 = 1;
        if (Uart1_Data_Ready()) {
            input=uart1_read();
            if (input == 1)
                LED1 = 1;
            else
                LED1 = 0;
        }
    }
}

我使用的是20MHz xtal。

0 个答案:

没有答案