Maker Pro
Maker Pro

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
 
R

Roger Hamlett

Jan 1, 1970
0
Hi everyone,
I am having a big trouble with my PIC's serial communication. I have
codes like these:
<snipped>
Ignoring the code for a while. How is the serial connected to the PIC?.

Best Wishes
 
S

Spehro Pefhany

Jan 1, 1970
0
Hi everyone,
I am having a big trouble with my PIC's serial communication. I have
codes like these:

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


Best regards,
Spehro Pefhany
 
P

Paul E. Schoen

Jan 1, 1970
0
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

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

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en012073

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

movf RCREG,W ;get received data
xorlw 0x0d ;compare with <CR>
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
 
D

Default

Jan 1, 1970
0
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





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.

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

Default

Jan 1, 1970
0
Spehro Pefhany said:
BNZ is a standard MPLAB assembler pseudo-instruction. It generates two
instructions-- a BTFSS and a GOTO.


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

Spehro Pefhany

Jan 1, 1970
0
2. 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?

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


Best regards,
Spehro Pefhany
 
G

gpsstajer

Jan 1, 1970
0
Default said:
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.

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

gpsstajer

Jan 1, 1970
0
Roger said:
Ignoring the code for a while. How is the serial connected to the PIC?.

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

Roger Hamlett

Jan 1, 1970
0
gpsstajer said:
The incomin wire into the RX, and the outcoming is out of TX. I checked
the value of them from COM 1.
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
 
G

gpsstajer

Jan 1, 1970
0
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
 
G

gpsstajer

Jan 1, 1970
0
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ü için kullanilacak portb'nin
kosullanmasi
clrf TRISA ;HATA durumlarinin kontrol edilecegi kütük
clrf TRISD
BCF STATUS,RP0

call setUSART

CALL GPSAL ;gps'den gelen veriler 1dk süre 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
 
Top