LIST P=16F688, R=DEC ; Include file, change directory if needed
INCLUDE "P16F688.INC"
__CONFIG _CP_OFF&_CPD_OFF&_PWRTE_ON&_WDT_OFF&_INTRC_OSC_NOCLKOUT;
W_TMP EQU 20H
FSR_TMP EQU 21H
STATUS_TMP EQU 22H
PCLATH_TMP EQU 23H
TIMEH EQU 24H
TIMEM EQU 25H
TIMEL EQU 26H
RECEIVE EQU 27H
MODE EQU 28H
ORG 0X000
GOTO MAIN
MAIN
CALL PORT_INIT
CALL INTER_INIT
CALL USART_INIT
MOVLW 'A'
MOVWF TXREG
BCF PIR1,TXIF
CALL DELAY_TX
CALL DELAY
BTFSS MODE,1
GOTO LOOP
MOVLW 'B'
MOVWF TXREG
BCF PIR1,TXIF
BCF MODE,1
LOOP
GOTO MAIN
USART_INIT
BANKSEL SPBRG
MOVLW 19H ;Set braud rate 9600bps
MOVWF SPBRG
MOVLW B'00100100' ;Serial port enabled,BRGH SET 1
MOVWF TXSTA
MOVLW B'10010000' ;Serial port enabled,SPEN,CREN SET 1
MOVWF RCSTA
BANKSEL PORTA
RETURN
DELAY_TX ;320 Us
MOVLW 40H
MOVWF TIMEH
LOOP0
NOP
NOP
DECFSZ TIMEH,1
GOTO LOOP0
RETURN
INTER_INIT
BANKSEL INTCON
BSF INTCON,PEIE
BSF INTCON,GIE
BANKSEL PORTA
BANKSEL PIE1
BSF PIE1,RCIE
RETURN
INSV7
BANKSEL INTCON
BCF INTCON,GIE
MOVWF W_TMP ;Copy W to TEMP register
SWAPF STATUS,W ;Swap status to be saved into W
CLRF STATUS ;bank 0, regardless of current bank, Clears IRP,RP1,RP0
MOVWF STATUS_TMP ;Save status to bank zero STATUS_TEMP register
MOVF PCLATH,W ;Only required if using pages 1, 2 and/or 3
MOVWF PCLATH_TMP ;Save PCLATH into W
CLRF PCLATH ;Page zero, regardless of current page
BCF STATUS,IRP ;Return to Bank 0
MOVF FSR,W ;Copy FSR to W
MOVWF FSR_TMP ;Copy FSR from W to FSR_TEMP
NOP
CALL DELAY
CALL DELAY
BANKSEL PIR1
BTFSS PIR1,RCIF
GOTO POP
MOVF RCREG,W
MOVWF RECEIVE
BCF PIR1,RCIF
BSF MODE,1
nop
POP
NOP
MOVF FSR_TMP,W
MOVWF FSR
MOVF PCLATH_TMP,W
MOVWF PCLATH
MOVF STATUS_TMP,W
MOVWF STATUS
MOVF W_TMP,W
BANKSEL INTCON
BSF INTCON,GIE
RETFIE
PORT_INIT
MOVLW 00H
MOVWF MODE
BANKSEL TRISA
MOVLW 00H
MOVWF TRISA
MOVLW 2FH
MOVWF TRISC
BANKSEL PORTA ;不亮
MOVLW 0FFH
MOVWF PORTA
NOP
RETURN
DELAY
MOVLW 13h
MOVWF TIMEH
MOVLW 0FFH
LOP1
MOVWF TIMEM
LOP2
MOVWF TIMEL
LOP3
DECFSZ TIMEL,1
GOTO LOP3
DECFSZ TIMEM,1
GOTO LOP2
DECFSZ TIMEH,1
GOTO LOP1
NOP
BANKSEL PORTA
RETURN
END
用上位机调试,单片机可以给上位机发送字符‘A’,但是上位机给单片机发送任何数据,单片机都不返回,调试了两天了,请各位高手不吝赐教,感激不尽 |