PIC usart problem

Hi everyone, I am having a big trouble with my PIC's serial communication. I have codes like these: GPS:             CALL     set4800
A0:         CALL RCread             CALL    TXsend             XORLW    'G'             BNZ     A0
     MOVLW    0XFF             MOVWF    PORTA             RETURN I have incoming messages and I want to take the one which starts with 'G', so I write these simple codes. Althoug I have sentences which starts with 'G', PortA never gets FF. I tried these codes with Proteus and it works. But it doesnt work with my pic16f877 in my circuit. From TX output I get the bytes which I sent from RC, I see that there are G's in RC but they dont make the port A to be FF. Please hellllppppp me. Here are my functions: RCread: bcf STATUS,RP0 ; Select Bank 0. getc1 btfss PIR1,RCIF     ; Skip if RC int flag set      goto getc1 ; Try again      movf     RCREG,W ; Read the character      bcf PIR1,RCIF ; Clear the interrupt flag      return
TXsend     bcf STATUS,RP0 ; Select Bank 0. putc1         btfss PIR1,TXIF ; Skip if TXbuffer empty      goto putc1 ; Try again      movwf TXREG ; Write it!      bcf STATUS,RP0 ; Select Bank 0.          return set4800:    bsf        STATUS,RP0             MOVLW    D'51'             MOVWF    SPBRG             movlw 0xA4 ; CSRC/TXEN (Internal clock, 8 bit mode, Async ;operation, High Speed)             movwf TXSTA ; Write to TX control register.             BSF        TXSTA,BRGH             BCF        STATUS,RP0                 ;             RETURN
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
snipped-for-privacy@gmail.com wrote:

Hi Ali. There appear to be at least a couple of problems with your code.
First, you didn't say what crystal freq you're using, but your value of 51D into SPBRG suggests you're trying to use 19.2 with a 16mhz xtal.
Secondly, you didn't explicitly configure the UART receiver. You need to put some value into RCSTA.
Third, you need to configure the "direction" reigisters of BOTH port A and also Port C UART pins 6 [input] and 7 [output].
Also, be aware that port A has only 6 pins so you'll never see FFh there, and also that port A4 is open-drain and requires a pullup resistor in order to see a "1".
You should probably also "explicitly" configure the port A pins for digital I/O and not analog I/O, by setting the ADCON0 and ADCON1 registers, if not using them for A/D conversions.
It helps to have another routine called "setup" at the beginning that explictly goes around and configures every I/O pin on the chip, as well as other critical registers like PIE and the interrupt flags, etc. Then you won't forget which condition these pins come up in at boot-up time.
- dan michaels www.oricomtech.com ==================
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Oops, RC7 should be an input and RC6 an output.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
snipped-for-privacy@gmail.com wrote:

[snippage]
You need to initialize the RCSTA register along with the TXSTA registers. You need to set both TRISC<7:6> to 11. Here is the quote from PIC16F877 spec. sheet.
Bit SPEN (RCSTA<7>), and bits TRISC<7:6>, have to be set in order to configure pins RC6/TX/CK and RC7/ RX/DT as the Universal Synchronous Asynchronous Receiver Transmitter.
Tell us the values you stuff into the following registers:
TRISC: RCSTA: TXSTA: SPBRG:
Also, we need to know your crystal frequency and desired baud rate (4800 baud?)
Also, it is not prudent to ignore the FERR and OERR bits in RCSTA when receiving since either error can cause the USART to lock up. If either of those bits are set, you need to toggle CREN. My PIC ASM is rusty, but something like:
BTFSS RCSTA, FERR BTC RCSTA, CREN BTFSS RCSTA, OERR BTC RCSTA, CREN BTS RCSTA, CREN
While the UART does not use the any A/D pins, it is prudent to disable the A/D converter until after you get your basic UART working.
ADCON0 = 0 ADCON1 = 15
-------------------------------------------
After you have done all of that, let us know if you are still having problems.
-Wayne
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Oops, I got the polarity of the skip instructions backwards, it should be:
BTFSC RCSTA, FERR BTC RCSTA, CREN BTFSC RCSTA, OERR BTC RCSTA, CREN BTS RCSTA, CREN
That's why I don't write in assembler very often, I make too many mistakes.
-Wayne
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload
I thank you, all. I tried the solutions you suggeested me and I found the mistake. Because of the fast incoming messages my usart crashed. After every read operation, I reset the usart if there is an error. And it works correct. Here is the full program, I also made some changes with the configuration of USART. THANKS AGAIN here is the code: It is working :)         #include <p16f877.inc>         __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_OFF & _LVP_OFF & _DEBUG_OFF & _CPD_OFF cblock 0x70     temp endc
            org     0x0000 reset         clrf PCLATH             goto     main             NOP             NOP irq            nop
main:        clrf    STATUS            ;main program
            BSF        STATUS,RP0        ;durum kontrol iin kullanilacak portb'nin kosullanmasi             clrf    TRISA            ;HATA durumlarinin kontrol edilecegi ktk             clrf    TRISD             BCF        STATUS,RP0
            call    setUSART
            CALL     GPSAL                    ;gps'den gelen veriler 1dk sre ile alinir
            RETURN
GPSAL:        CALL     set4800             MOVLW    0X00            ;for mutliplexer             movwf    PORTD A0:            CALL     RXget             MOVWF    temp             CALL    TXsend             MOVF    temp,W             XORLW    'G'             BNZ        A0             MOVLW    '.'             CALL    TXsend             MOVF    temp,W             CALL    TXsend
            MOVLW    0XFF             MOVWF    PORTA             goto    GPSAL             RETURN
setUSART:    bsf RCSTA,SPEN         ; asyncron active     bsf         RCSTA,CREN                 ; rx active             bsf        STATUS,RP0                ;BANK1             bcf        TXSTA,SYNC                ;asyncron active             BSF        TXSTA,TXEN                ;tx active             BSF        TXSTA,BRGH                ;High Baud rate             movlw    0xc0         ;set tris bits for TX and RX             iorwf    TRISC,F             BCF        STATUS,RP0                ;AGAIN BANK0             RETURN
set4800:    bsf        STATUS,RP0             MOVLW    D'51'             MOVWF    SPBRG             BCF        STATUS,RP0             RETURN
set19200:    bsf        STATUS,RP0             MOVLW    D'12'             MOVWF    SPBRG             BCF        STATUS,RP0             RETURN
RXget: getc1         btfSS PIR1,RCIF     ; Skip if RC int flag set              goto getc1 ; Try again             btfsc    RCSTA,OERR    ;if overrun error occurred             goto    ErrSerialOverr    ; then go handle error             btfsc    RCSTA,FERR    ;if framing error occurred             goto    ErrSerialFrame    ; then go handle error             btfss    RCSTA,RX9D    ;if first stop bit error occurred             goto    ErrSerialFrame    ; then go handle error              movf    RCREG,W ; Read the character              return
TXsend btfsS PIR1, TXIF      goto TXsend     movwf TXREG      bcf PIR1,TXIF ; Clear the interrupt flag
             return
ErrSerialOverr:         bcf    RCSTA,CREN    ;reset the receiver logic         bsf    RCSTA,CREN    ;enable reception again         return
;error because FERR framing error bit is set or first stop bit is zero ;can do special error handling here - this code simply clears and continues
ErrSerialFrame:         movf    RCREG,W        ;discard received data that has error         return
        END
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Add image file
Upload

Polytechforum.com is a website by engineers for engineers. It is not affiliated with any of manufacturers or vendors discussed here. All logos and trade names are the property of their respective owners.