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

Reply to
ali.keles
Loading thread data ...

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

formatting link
====================

Reply to
dan michaels

[snippage]

You need to initialize the RCSTA register along with the TXSTA registers. You need to set both TRISC to 11. Here is the quote from PIC16F877 spec. sheet.

Bit SPEN (RCSTA), and bits TRISC, 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

Reply to
Wayne C. Gramlich

Oops, RC7 should be an input and RC6 an output.

Reply to
dan michaels

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

Reply to
Wayne C. Gramlich

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 __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=FC i=E7in kullanilacak portb'nin kosullanmasi clrf TRISA ;HATA durumlarinin kontrol edilecegi k=FCt=FCk clrf TRISD BCF STATUS,RP0

call setUSART

CALL GPSAL ;gps'den gelen veriler 1dk s=FCre 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

Reply to
ali.keles

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.