kien_war
28-06-2007, 07:41 PM
Em đã đọc TUT-UART trên picvietnam nhưng có vài chỗ em còn chưa hiểu.
Sau đây là đoạn chương trình lập trình giap tiếp UART truyền nhận giữa PIC và PC qua RS232:
list p=16f877A
#include <p16f877A.inc>
__CONFIG 0x393A
errorlevel -302
w_temp EQU 0x71
status_temp EQU 0x72
pclath_temp EQU 0x73
ACC EQU 0x74
R2 EQU 0x7A
R3 EQU 0x7B
R4 EQU 0x7C
;************************************************* *********************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move STATUS register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,W ; move PCLATH register into W register
movwf pclath_temp ; save off contents of PCLATH register
;================================================= ============================
BANKSEL RCREG
MOVF RCREG,W
MOVWF PORTD
MOVWF PORTB ; Chuyen du lieu nhan duoc ra PortB
BANKSEL TXREG ;
MOVWF TXREG ; Chuyen du lieu nguoc lai PC
;================================================= =============================
movf pclath_temp,w ; retrieve copy of PCLATH register
movwf PCLATH ; restore pre-isr PCLATH register contents
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie
main
BANKSEL ADCON1
MOVLW 0x06
MOVWF ADCON1
CALL Init_serialport
BANKSEL TRISB
CLRF TRISB ; Cho dang PORTB la dang out
CLRF TRISD
BSF INTCON,GIE
BSF INTCON,PEIE
GOTO $
Init_serialport ; Module khoi tao cho Serial
BANKSEL TXSTA
BSF TRISC,7 ; Chon chan rb2 la dang in
BCF TRISC,6
MOVLW B'00100100' ; Chon che do 8 bit, Hightbaudrate
MOVWF TXSTA
MOVLW D'25' ; Chon che do 9.600
MOVWF SPBRG
BSF PIE1,RCIE ; Cho phep ngat ( receive )
BANKSEL RCSTA
MOVLW B'10010000' ;
MOVWF RCSTA
RETURN
DELAY255 ; Ham delay
movlw 0xff ; delay 255 mS
D0 movwf R2
D1 movlw 0xC7 ; delay 1mS
movwf R3
movlw 0x01
movwf R4
Delay_0
decfsz R3, f
goto $+2
decfsz R4, f
goto Delay_0
decfsz R2 ,f
goto D1
retlw 0x00
END ; directive 'end of program'
;//////////////////////////////////////
Em có vài điều muốn hỏi:
1. list p=16f877A, __CONFIG 0x393A, errorlevel -302 có ý nghĩa gì
2. Các biến dùng cho pic16F877A nằm trong khoảng từ bao nhiêu 0xab đến bao nhiêu oxcd ?!
3. Khi chạy hết main nó có lệnh: "goto $" ... sau câu lệnh đó nó nhảy đến đâu ?! Có phải là vị trí "ORG 0x004" không ?!
4. Ba câu lệnh sau đây có ý nghĩa gì ?!
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie
5. Hàm delay em đã đọc và hiểu, nhưng em ko thấy nó dùng trong chương trình, vậy viết để làm gì
6. ADCON1 có tác dụng gì ?! Sao lại phải truyền giá trị ox06 cho nó ?!
7. Thanh ghi PCLATH là thanh ghi gì vậy, sao lại phải lưu lại nó ?!
8. Biến ACC em thấy có khai báo nhưng ko được sử dụng ?!
9. Ở hàm Delay255ms có label D0 nhưng em ko thấy sử dụng, và ở TUT-Delay cũng ko có nhãn nằm ở vị trí này ?!
... Nếu có gì ngu dốt mong các anh bỏ qua và giúp đỡ cho em, em cần câu trả lời gấp lắm ạ, thx các anh đã đọc topic này !!!
Sau đây là đoạn chương trình lập trình giap tiếp UART truyền nhận giữa PIC và PC qua RS232:
list p=16f877A
#include <p16f877A.inc>
__CONFIG 0x393A
errorlevel -302
w_temp EQU 0x71
status_temp EQU 0x72
pclath_temp EQU 0x73
ACC EQU 0x74
R2 EQU 0x7A
R3 EQU 0x7B
R4 EQU 0x7C
;************************************************* *********************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move STATUS register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,W ; move PCLATH register into W register
movwf pclath_temp ; save off contents of PCLATH register
;================================================= ============================
BANKSEL RCREG
MOVF RCREG,W
MOVWF PORTD
MOVWF PORTB ; Chuyen du lieu nhan duoc ra PortB
BANKSEL TXREG ;
MOVWF TXREG ; Chuyen du lieu nguoc lai PC
;================================================= =============================
movf pclath_temp,w ; retrieve copy of PCLATH register
movwf PCLATH ; restore pre-isr PCLATH register contents
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie
main
BANKSEL ADCON1
MOVLW 0x06
MOVWF ADCON1
CALL Init_serialport
BANKSEL TRISB
CLRF TRISB ; Cho dang PORTB la dang out
CLRF TRISD
BSF INTCON,GIE
BSF INTCON,PEIE
GOTO $
Init_serialport ; Module khoi tao cho Serial
BANKSEL TXSTA
BSF TRISC,7 ; Chon chan rb2 la dang in
BCF TRISC,6
MOVLW B'00100100' ; Chon che do 8 bit, Hightbaudrate
MOVWF TXSTA
MOVLW D'25' ; Chon che do 9.600
MOVWF SPBRG
BSF PIE1,RCIE ; Cho phep ngat ( receive )
BANKSEL RCSTA
MOVLW B'10010000' ;
MOVWF RCSTA
RETURN
DELAY255 ; Ham delay
movlw 0xff ; delay 255 mS
D0 movwf R2
D1 movlw 0xC7 ; delay 1mS
movwf R3
movlw 0x01
movwf R4
Delay_0
decfsz R3, f
goto $+2
decfsz R4, f
goto Delay_0
decfsz R2 ,f
goto D1
retlw 0x00
END ; directive 'end of program'
;//////////////////////////////////////
Em có vài điều muốn hỏi:
1. list p=16f877A, __CONFIG 0x393A, errorlevel -302 có ý nghĩa gì
2. Các biến dùng cho pic16F877A nằm trong khoảng từ bao nhiêu 0xab đến bao nhiêu oxcd ?!
3. Khi chạy hết main nó có lệnh: "goto $" ... sau câu lệnh đó nó nhảy đến đâu ?! Có phải là vị trí "ORG 0x004" không ?!
4. Ba câu lệnh sau đây có ý nghĩa gì ?!
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie
5. Hàm delay em đã đọc và hiểu, nhưng em ko thấy nó dùng trong chương trình, vậy viết để làm gì
6. ADCON1 có tác dụng gì ?! Sao lại phải truyền giá trị ox06 cho nó ?!
7. Thanh ghi PCLATH là thanh ghi gì vậy, sao lại phải lưu lại nó ?!
8. Biến ACC em thấy có khai báo nhưng ko được sử dụng ?!
9. Ở hàm Delay255ms có label D0 nhưng em ko thấy sử dụng, và ở TUT-Delay cũng ko có nhãn nằm ở vị trí này ?!
... Nếu có gì ngu dốt mong các anh bỏ qua và giúp đỡ cho em, em cần câu trả lời gấp lắm ạ, thx các anh đã đọc topic này !!!