Now I know that my xbees connected to PC, they do communicate. So the problem is still in PIC code.
One question is , can I set RCSTA,SPEN bit every time? So when one byte is sent, SPEN is set one time. So there are 8 bytes data needed to send, I have to set SPEN bit for 8 times. Is it right?
I am wondering.
Another question is before I send a byte, I need check three flag bit. First is CTS bit, which is connected toXBEE CTS pin and tell us XBEE enable PIC to send data to XBEE or not.
Second is TXSTA,TRMT, which shows TSR empty or full, and tell us the last byte sending is successful or not.
And the last one is PIR1,TXIF, which shows TXREG is empty or full.
And now my code is followed.Can you see it for me?
Main:
banksel TRISC
bcf TRISC,6 ;c6=0 as output
; UART module setup
banksel SPBRG
movlw 0x14 ; rs232 baud as 57.6k as 0x15
movwf SPBRG ; Enable SPBRG register Baud Rate
banksel TXSTA ; Select memory bank for TXSTA SFR
bcf TXSTA,TX9 ; 6----8-bit transmission
bsf TXSTA,TXEN ; 5----Enable Transmission/set
bcf TXSTA,SYNC ; 4----Asynchronous mode
bsf TXSTA,BRGH ; 2----High Baud Rate
banksel RCSTA
bcf RCSTA,RX9 ; 6
bsf RCSTA,SPEN ; 7----cause TXIF=1----cause TRMT=1 --no????
bsf RCSTA,CREN ; 4
Loop:
MOVLW 0x42 ; substitute your code to load W here
CALL SerialTX
MOVLW 0x45
CALL SerialTX
MOVLW 0x39
CALL SerialTX
MOVLW 0x27
CALL SerialTX
MOVLW 0x15
CALL SerialTX
MOVLW 0x58
CALL SerialTX
MOVLW 0x63
CALL SerialTX
MOVLW 0x91
CALL SerialTX
CALL Delay20us ; this would be your delay routine
GOTO Main
SerialTX:
BANKSEL PORTC
BTFSC PORTC,5 ;CTS=PORTC,5, TEST C5, CTS
GOTO SerialTX
BANKSEL TXSTA
BTFSS TXSTA,TRMT ;TXSTA,1 =default value is 1.
GOTO SerialTX ; make sure data transmission is sucessful ?
BANKSEL PIR1
btfss PIR1,TXIF ;PIR1,4
goto SerialTX
movwf TXREG ;cause TRMT=0, FULL. causing 2nd data is not given to TXREG.
CALL Delay20us
return
Delay20us:
BANKSEL RCSTA ;05,A5=170us;03,F5=120US;2,F5=0;03,D5=6US-ERROR
MOVLW 0x06 ;03,C5=O DELAY;06/D5=320
MOVWF 0x20 ;
LOOP2:
MOVLW 0x15 ;
MOVWF 0x21 ;
LOOP1:
DECFSZ 0x21,F
GOTO LOOP1
DECFSZ 0x20,F
GOTO LOOP2
BANKSEL RCSTA
BSF RCSTA,SPEN
return
END
;*********************transmit data finish******************************
[/code]