I have a big trouble with usart PIC16f877

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 ...

Ignoring the code for a while. How is the serial connected to the PIC?.

Best Wishes

Reply to
Roger Hamlett

Where are your initialization routines for TRISA, TRISC, TXSTA, RCSTA etc.? Are you setting up ADCON1 so you have digital outputs?

Why do you keep clearing RP0, but ignore RP1?

Your Fosc is 4MHz? You have an inverting RS-232 driver external to the chip?

Is the echo working?

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

You should read the Application note AN774 and try the sample code, which is an interrupt driven communication application:

formatting link

This is the code it uses to check for a specific character:

movf RCREG,W ;get received data xorlw 0x0d ;compare with btfsc STATUS,Z ;check if the same

You need to check the STATUS register.

Also, it looks like you are checking for the character you are transmitting, although it is the same as that previously received. It is usually a good idea to save and restore the W register when you call a function.

Paul

Reply to
Paul E. Schoen

Two possibilities come to my mind:

  1. Are you absolutely certain you are sending a "G" instead of a "g"? Forgive me if that isn't the source of your problem, but little things like that can waste lots of time and are suprisingly easy mistakes.

  1. The "BNZ" instruction doesn't exist in the PIC16F architecture. Have you defined some kind of macro named "BNZ" to emulate the functionality of a "Branch Not Zero" instruction? If you have, are you absolutely certain it works correctly? What kind of development tools/assembler are you using? The assembler that comes with MPLAB should barf like crazy if you select your processor type as 16F877 (although it would work on the PIC18F family of parts since they do include a BNZ instruction) and try to use a BNZ instruction.

Reply to
Default

Sweet. And here I've been wishing for that integrated capability all along...

Reply to
Default

BNZ is a standard MPLAB assembler pseudo-instruction. It generates two instructions-- a BTFSS and a GOTO.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

I am sending 'G' not 'g'. I have tried if the BNZ is working and I saw it works. Movlw 0x10 xorlw 0x10 bnz A0 this code works. I still cant find a solution

Reply to
gpsstajer

The incomin wire into the RX, and the outcoming is out of TX. I checked the value of them from COM 1.

Reply to
gpsstajer

I mean what electronics are between the wires and the chip... The chip expects TTL serial, not 'RS232'. A MAX232 or similar buffer chip is _required_ between the PIC and the PC. Without this, the input will be being overdriven, and the output will never work, and the signals will be inverted from what the PIC expects.

Best Wishes

Reply to
Roger Hamlett

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

Reply to
gpsstajer

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
gpsstajer

ElectronDepot 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.