PIC sends data into xbee

Have you got the greatest 48 bit multiplier ever conceived? Prove it - post your code here.

Moderator: phalanx

Post Reply
ceibawx
Posts: 82
Joined: Mon Oct 13, 2008 9:42 am
Location: baltimore,MD, USA

PIC sends data into xbee

Post by ceibawx » Mon Jan 05, 2009 11:50 am

Hi ,
I met problem.
I send 0x0F for 200 times first, Then send core data.
But in RX XBEE, I didn't receive 200 0x0F. Sometime 0F is received , sometimes it is not 0F but E8. Sometime the total number is 90, sometime it is around 140.

Who can help me?

What's the probable reason? In PIC CODE OR in Xbee?


PIC CODE

Code: Select all

list             p=16F877           ; list directive to define processor
#include         <p16f877.inc>      ; processor specific variable definitions


; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

; __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC
; & _WRT_OFF & _LVP_OFF & _CPD_OFF



;***** VARIABLE DEFINITIONS

w_temp           EQU     0x7D       ; variable used for context saving
status_temp      EQU     0x7E       ; variable used for context saving
pclath_temp      EQU     0x7F       ; variable used for context saving

ByteCounter      EQU     0x7C       ;address used for byte counter
ByteCounter1     EQU     0x60

#define          CTS    PORTC,5    ;C5 is connected to XBEE CTS


;**********************************************************************
       ORG       0x000             ; processor reset vector
       goto      Main               ; go to beginning of program
;**********************************************************************
; isr code can go here or be located as a call subroutine elsewhere

       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

       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                       ; return from interrupt

Main:
       banksel   TRISC
       bcf       TRISC,6            ;c6=0 as output
       
; UART module setup
       banksel   SPBRG                   ;115.2k as 0x0A=8.7US
       movlw     0x15                 ; 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--yes.2cond time
       bsf       RCSTA,CREN         ; 4

;******************Delay for 10ms******************************


      
       
;****************send a 0x0F as start flag repeatly************
Sendrepeat:
       MOVLW     0x0F
       MOVWF     0x61  ;
       MOVLW     0xC8             ;C8=200.  1000 is not allowed in PIC.       
       MOVWF     ByteCounter1      ;maximum is FF in 8 bits.
loop0: 
       MOVF      0x61,0       
       CALL      SerialTX0
       DECFSZ    ByteCounter1,1
       GOTO      loop0
       GOTO      Record        ;????? 

SerialTX0:  
       BANKSEL   PORTC  
       BTFSC     PORTC,5          ;CTS=PORTC,5, TEST C5,  CTS
       GOTO      SerialTX0
       BANKSEL   TXSTA
       BTFSS     TXSTA,TRMT       ;TXSTA,1 =default value is 1.
       GOTO      SerialTX0         ; make sure data transmission is sucessful ?       
       BANKSEL   PIR1
       BTFSS     PIR1,TXIF
       GOTO      SerialTX0
       MOVWF     TXREG
       CALL      Reset0
       return 
Reset0:       
       BANKSEL   RCSTA
       BSF       RCSTA,SPEN    ; enable receive from TXREG to TSR.       
       return
;*****************save data into PIC memory************************
Record:       
       MOVLW     0x0A  ; substitute your code to load W here
       MOVWF     0x50  ;
       MOVLW     0x0B
       MOVWF     0x51  ;
       MOVLW     0x0C
       MOVWF     0x52  ;
       MOVLW     0x0D
       MOVWF     0x53  ;
       MOVLW     0x0E
       MOVWF     0x54  ;
       MOVLW     0x0F
       MOVWF     0x55  ; 

;****************send real data out of PIC**********************  
Send:
       MOVLW     0x06
       MOVWF     ByteCounter  ;ByteCounter=d10
       MOVLW     0x50
       MOVWF     FSR
Loop: 
       MOVF      INDF,0       ; substitute your code to load W here
       CALL      SerialTX
       INCF      FSR,1
       DECFSZ    ByteCounter,1
       GOTO      Loop
       CALL      DelayBig ;
       GOTO      Record
       

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      Reseting
       return

DelayBig:
       BANKSEL   RCSTA    ;05,A5=400u;
       MOVLW     0x05     
       MOVWF     0x20     ;46,F5=10ms.FF,FF=40ms,88,FF=20ms
LOOP2:                    ;;03,C5=O DELAY;
       MOVLW     0xA5     ;08,F5=1ms;88,44=5ms
       MOVWF     0x21     ;04,95=290us;05,95=390u;05,D5=600u;05,A5=400u;
LOOP1:
       DECFSZ    0x21,1
       GOTO      LOOP1
       DECFSZ    0x20,1
       GOTO      LOOP2
       BANKSEL   RCSTA
       BSF       RCSTA,SPEN    ; enable receive from TXREG to TSR.       
       return

Reseting:       
       BANKSEL   RCSTA
       BSF       RCSTA,SPEN    ; enable receive from TXREG to TSR.       
       return

       END
;*********************transmit data finish******************************




 

VStar650CL
Posts: 389
Joined: Sat Nov 08, 2008 1:39 pm
Location: Little Rock, AR

Post by VStar650CL » Tue Jan 06, 2009 10:12 am

I think the problem is, you're not allowing any time for the byte to transmit before jumping back to check CTS. This is mostly a matter of not checking things in the right order. Maybe it'll help if I explain the buffering setup on a PIC UART for you.

The PIC has what is called "double-buffering" on TX. There is TXREG, and also a hardware shift register (TSR) that you can't access. For data to go out, it first needs to pass from TXREG into TSR, and then from TSR to the output pin. This is why there are two different flags, TXIF and TRMT. TRMT means everything is empty, both the TSR and TXREG. TXIF means TXREG is empty only... the TSR might still be full with an outgoing byte.

Since you're only transmitting one byte and then waiting for CTS before transmitting another, you never need to check TXIF. The proper order for what you are doing is this:
1) Check TRMT... If full, do nothing. If empty, goto 2
2) Check CTS... If busy, do nothing. If clear, load TXREG and goto 3
3) TRMT will now show "full." Wait (do nothing) until it shows "empty."
4) Set a timer for some reasonable period for the XBEE to respond, say 10mS
5) Check CTS... if busy, the byte is done, you can now repeat the main loop. If clear, the XBEE hasn't processed the new byte yet...
6) ...so check the timer... If expired, the XBEE didn't receive the byte. Either reset the serial port or go back to 1 and try resending the byte

The above is the proper format anytime CTS is used. TXIF and other things only come into play when more than 1 byte is being sent at a time.

Hope this helps.

ceibawx
Posts: 82
Joined: Mon Oct 13, 2008 9:42 am
Location: baltimore,MD, USA

Post by ceibawx » Wed Jan 07, 2009 7:07 pm

Thanks for your words.
I know two buffer in TX. But I didn't mention the sequence . First CTS, then BUFFER,Thanks.
I will tell you my progress. Today ONE chip didn't work. I have to test tomorrow.

ICAC210
Posts: 38
Joined: Mon Jan 26, 2009 12:53 pm

Post by ICAC210 » Fri Mar 20, 2009 9:43 am

I was testing to send xbee to the XCTU WITHOUT CTS flow control and data were all good in the terminal. Does that mean I do not have overflow problem at the sending end and I don't need to worry about CTS?

When hooking up my another Xbee with PIC, the receiving data is wrong, so I think RTS is needed to control it.

sterling2
Posts: 19
Joined: Wed Mar 17, 2010 7:44 pm

Re: PIC sends data into xbee

Post by sterling2 » Thu Mar 18, 2010 11:49 pm

hi - I am trying to use a dsPIC30F3012 micro to send data to an Xbee for transmission to a PC. I am very new with both PICs and Xbees, so i am still getting used to all this. I was wondering what exactly needed to be configured for setting up UART communication between the dsPIC and the Xbee? I think i understand the setting up of the UART for transmission ok, but was wondering about any Xbee specific configurations - the baud rate register in the PIC is one i can think of, is there anything else? The number of stop bits maybe? I assume the baud and the number of stop bits have to be the same for the PIc and the Xbee?

Also, this may be a silly question, but do i have to worry about setting start bits and stop bits? or are start bits always 0 and stop always 1 and that is taken care of by the PIc and xbee themselves and i don't have to configure/write to any registers to set those up?

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: PIC sends data into xbee

Post by waltr » Thu Apr 01, 2010 11:42 am

Start and stop bits are defined by the asynchronous standard and both the PIC and XBee take care of them.

I do recommend getting the PIC to communicate directly, with wire, to the other device before inserting the XBees. This way you only need to debug one thing at a time. Once it works with a wire then insert the XBees for a wireless link.

The only other thing you may need to worry about is the Baud rate accuracy of each device. The PIC's data sheets show what baud rate divider value to use for different clocks and what the baud error is. The XBee also has a non-exact baud rates.
See this to learn more:
http://www.jsjf.demon.co.uk/xbee/xbee.html

konstantina
Posts: 2
Joined: Tue Jun 04, 2013 10:12 am

Re: PIC sends data into xbee

Post by konstantina » Tue Jun 04, 2013 10:23 am

Hello everyone.. I have a problem with the XBEE RF S1. I am trying to establish a wireless communication btw two XBEEs. I am using Pic24fj64ga102. I want to establish wireless communication btw my PCB (mainly consisting of the PIC and the XBEE series 1) and another XBEE which connected with my PC through this one https://www.sparkfun.com/products/8687. I want to press something one the one terminal which is connected with the XBEE with XBEE explrer board and send this byte wireless to the XBEE of my PCB. Afterwards, I want the XBEE to transmit this bit to the PIC through the UART protocol. Finally, I want the PIC to send this byte to PC through the FTDI RS232. Until now, I have managed to establish the communication btw the PIC and my PC through the FTDI. I don't know what happens in the rest of the communication and it doesn't work. Do you have any idea about which is the problem???

konstantina
Posts: 2
Joined: Tue Jun 04, 2013 10:12 am

Re: PIC sends data into xbee

Post by konstantina » Tue Jun 04, 2013 2:44 pm

I have a similar problem. I am using Pic24fj64ga102. I want to establish wireless communication btw my PCB (mainly consisting of the PIC and the XBEE series 1) and another XBEE which connected with my PC through this one https://www.sparkfun.com/products/8687. Nothing happened when I pressed something on my screebn using the X-CTU terminal. I want to press something on my terminal and turn on the light on my board.

Post Reply