16F877A UART 模組測試 III

這次使用UART模組配合PC端VB程式

來做傳送與接收的工作

主要將VB程式上設定的PORTB狀態以TxD方式傳出

再由PIC端收到且將值放入PORTB端後傳回Success!!字串

PIC程式

	list	p=16f877a,r=DEC

	include"p16f877.inc"

temp	equ	0x20
cnt	equ	0x21
DEY1	equ	0x22
DEY2	equ	0x23
DEY3	equ	0x24
W_temp	equ	0x25
STATUS_temp	equ	0x26
temp1	equ	0x27

	org	0x00
	goto	main

	;中斷向量0x04
	org	0x04
	banksel	INTCON
	bcf	INTCON,GIE	;致能中斷總開關
	banksel	W_temp
	movwf	W_temp
	swapf	STATUS,W
	movwf	STATUS_temp
	banksel	PIR1
	btfsc	PIR1,RCIF	;檢查RCIF是否為1
	call	send_data	;溢位,作清除動作
	swapf	STATUS_temp,W
	movwf	STATUS
	swapf	W_temp,F
	swapf	W_temp,W
	banksel	INTCON
	bsf	INTCON,GIE	;致能中斷總開關

	retfie

main:
	call	UART_Init
	banksel	temp
	clrf	temp
	movlw	.10
	movwf	cnt
	movlw	0x00
	movwf	PORTB
	movwf	PORTD
	call	DELAY		;延遲程式執行
	goto	$

send_data:
	banksel	RCREG
	movf	RCREG,W
	movwf	PORTD
send_data2:
	call	DELAY
	banksel	PIR1
	btfsc	PIR1,TXIF
	goto	get_data
	goto	send_data2
get_data:
	nop
	movf	temp,W
	call	text_DT1
	banksel	TXREG
	movwf	TXREG
	nop
	incf	temp,F
	decfsz	cnt,F
	goto	send_data2
	clrf	temp
	movlw	.10
	movwf	cnt
	return

text_DT1:	addwf	PCL
	DT	"Success!!@"

;;;;;;;;;;;;;;;;;;;;
UART_Init:
	banksel	TRISC
	bcf		TRISC,6
	bsf		TRISC,7
	clrf	TRISB
	clrf	TRISD

	movlw	.25
	movwf	SPBRG		;設定鮑率
	bsf	TXSTA,BRGH	;選擇快速鮑率
	bcf	TXSTA,SYNC	;選擇非同步模式
	bcf	TXSTA,TX9D	;選擇8bit資料封包
	bsf	TXSTA,TXEN	;致能傳送模組

	banksel	RCSTA
	bsf	RCSTA,CREN	;致能接收模組
	bcf	RCSTA,RX9D	;選擇8bit資料封包
	bsf	RCSTA,SPEN	;致能串列通訊模組
	bsf	INTCON,GIE	;致能中斷總開關
	bsf	INTCON,PEIE	;致能周邊中斷
	banksel	PIE1
	bsf	PIE1,RCIE	;致能傳送模組中斷
				;傳送模組中斷旗號RCIF需以讀取
				;RCREG方式清除
	return

DELAY:
	MOVLW   .5
	MOVWF   DEY3
DELAY1:
	MOVLW   .250
	MOVWF   DEY2
INNER2:
	MOVLW   .25
	MOVWF   DEY1
INNER1:
	DECFSZ  DEY1,F
	GOTO    INNER1
	DECFSZ  DEY2,F
	GOTO    INNER2
	DECFSZ  DEY3,F
	GOTO    DELAY1
	RETURN
	END

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

VB端程式

Dim com_port As Integer
Dim buffer As Variant
Dim Out_value(0) As Byte

Private Sub clr_input_Click()

    txtInputData.Text = ""

End Sub

Private Sub cmd_start_Click()

        MSComm1.CommPort = Val(com_port)    '設定連接埠
        MSComm1.Settings = "9600,N,8,1"     '設定鮑率
        MSComm1.PortOpen = True             '開啟連接
        MSComm1.Handshaking = None
        MSComm1.RTSEnable = False
        MSComm1.RThreshold = 1
        MSComm1.SThreshold = 1
        cmd_stop.Enabled = True
        cmd_start.Enabled = False

End Sub

Private Sub cmd_stop_Click()

    MSComm1.PortOpen = False
    cmd_stop.Enabled = False
    cmd_start.Enabled = True

End Sub
Private Sub Command3_Click()

    If MSComm1.PortOpen = True Then
        MSComm1.Output = Out_value

    End If
End Sub

Private Sub Form_Load()

    cmd_stop.Enabled = False
    com_port = 1

End Sub

Private Sub Label1_Click(Index As Integer)

    Select Case Index

    Case 0
        If Shape1(7).FillColor = RGB(255, 255, 255) Then
            Shape1(7).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 1
        Else
            Shape1(7).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 1
        End If
    Case 1
        If Shape1(6).FillColor = RGB(255, 255, 255) Then
            Shape1(6).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 2

        Else
            Shape1(6).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 2

        End If
    Case 2
        If Shape1(5).FillColor = RGB(255, 255, 255) Then
            Shape1(5).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 4

        Else
            Shape1(5).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 4

        End If
    Case 3
        If Shape1(4).FillColor = RGB(255, 255, 255) Then
            Shape1(4).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 8

        Else
            Shape1(4).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 8

        End If
    Case 4
        If Shape1(3).FillColor = RGB(255, 255, 255) Then
            Shape1(3).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 16

        Else
            Shape1(3).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 16

        End If
    Case 5
        If Shape1(2).FillColor = RGB(255, 255, 255) Then
            Shape1(2).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 32

        Else
            Shape1(2).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 32

        End If
    Case 6
        If Shape1(1).FillColor = RGB(255, 255, 255) Then
            Shape1(1).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 64

        Else
            Shape1(1).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 64

        End If
    Case 7
        If Shape1(0).FillColor = RGB(255, 255, 255) Then
            Shape1(0).FillColor = RGB(255, 0, 0)
            Out_value(0) = Out_value(0) + 128

        Else
            Shape1(0).FillColor = RGB(255, 255, 255)
            Out_value(0) = Out_value(0) - 128

        End If
    End Select
End Sub

Private Sub MSComm1_OnComm()

    Select Case MSComm1.CommEvent

        Case comEvReceive

            buffer = MSComm1.Input

            If buffer <> "@" Then

                txtInputData.Text = txtInputData.Text & buffer
            ElseIf buffer = "@" Then
                txtInputData.Text = txtInputData.Text & vbCrLf

            End If

    End Select

End Sub

Private Sub Option1_Click()

    com_port = 1

    Text1.Text = ""
    Option3.Value = False

End Sub

Private Sub Option2_Click()

    com_port = 2

    Text1.Text = ""
    Option3.Value = False

End Sub

Private Sub Text1_Change()

        Option3.Enabled = True
        Option3.Value = True

        com_port = Val(Text1.Text)

    If Text1.Text = "" Then
        Option3.Enabled = False
    End If

End Sub

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
VB端程式畫面

PIC端程式畫面

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料