Dear các bro. Sao mình không mở một cái tutorial về giao tiếp PIC với PC bằng UASART nhỉ. Em làm rồi mà lúc chạy lúc không. Chẳng hiểu sao. OSC đã setup đúng, baud rate đã setup đúng.
Code:
list P = 16F877
include "P16f877.inc"
; ------------------
; CONFIGURATION FUSE
; ------------------
;
__CONFIG _CP_OFF & _WRT_ENABLE_ON & _XT_OSC & _WDT_ON & _PWRTE_OFF & _BODEN_ON & _LVP_ON & _CPD_OFF & _DEBUG_OFF
; CRYSTAL SPEED = 4000000Hz
;
CBLOCK 0x20
dataL
ENDC
;
; -------------
; PROGRAM START
; -------------
;
org 0x0000 ; startup = 0000h
;
Start movlw 0x00 ; setup ports
movwf PORTA
movlw 0x00
movwf PORTB
movlw 0x40
movwf PORTC
movlw 0x00
movwf PORTD
movlw 0x00
movwf PORTE
bsf STATUS,RP0 ; RAM Page 1
movlw 0x00
movwf TRISA
movlw 0x00
movwf TRISB
movlw 0x80
movwf TRISC
movlw 0xFF
movwf TRISD
movlw 0xFF
movwf TRISE
;
; -------------------------
; SET ANALOG/DIGITAL INPUTS
; -------------------------
;
movlw 0x06 ; all digital
movwf ADCON1
;
; ------------------------------------
; SET BAUD RATE TO COMMUNICATE WITH PC
; ------------------------------------
; Boot Baud Rate = 19200, No Parity, 1 Stop Bit
;
movlw 0x0C ; 19200 baud
movwf SPBRG
movlw b'00100100' ; brgh = high (2)
movwf TXSTA ; enable Async Transmission, set brgh
movlw b'10010000' ; enable Async Reception
bcf STATUS,RP0 ; RAM Page 0
movwf RCSTA
;
; ------------------------------------
; PROVIDE A SETTLING TIME FOR START UP
; ------------------------------------
;
clrf dataL
settle decfsz dataL,F
goto settle
;
movf RCREG,W
movf RCREG,W
movf RCREG,W ; flush receive buffer
Loop call Receive ; wait for a char
movwf TXREG ; echo data back to sender
goto Loop
;
; ----------------------------
; RECEIVE CHARACTER FROM RS232
; ----------------------------
; This routine does not return until a character is received.
;
Receive btfss PIR1,RCIF ; (5) check for received data
goto Receive
;
movf RCREG,W
return
;
; ------------------------------------
; WAIT UNTIL RS232 IS FINISHED SENDING
; ------------------------------------
;
TransWt bsf STATUS,RP0
WtHere btfss TXSTA,TRMT ; (1) transmission is complete if hi
goto WtHere
;
clrf STATUS ; RAM Page 0
return
;
;
end