Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic16f877a-usart

Status
Not open for further replies.

tio_rafael

New Member
Hi all

I'm trying to receive some data from a gps module and send it to my PC using PIC16F877A USART. These are the relevant parameters of my project:

9600bps, 8N1 and no flow control.

My code:
(clk=4MHz)
------------------------------------------------------

#INCLUDE <P16F877A.INC>
__CONFIG _CP_OFF & _CPD_OFF & _DEBUG_OFF & _LVP_OFF & _WRT_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC

BCF STATUS,RP1
#DEFINE BANK0 BCF STATUS,RP0 ;SET BANK0
#DEFINE BANK1 BSF STATUS,RP0 ;SET BANK1

BAUD_RATE_SPBRG EQU .25 ;SPBRG

CBLOCK 0X70
DATA_BYTE ;RECEIVED BYTE
ENDC

ORG 0X0000
GOTO SERIAL_INIT

CLRF DATA_BYTE
CLRF PORTC

SERIAL_INIT
BANK1
MOVLW B'10000000'
MOVWF TRISC ;TX RC6 AND RX RC7
MOVLW B'00000110'
MOVWF ADCON1 ;DIGITAL I/O
MOVLW BAUD_RATE_SPBRG
MOVWF SPBRG ;BAUD RATE CONFIG
MOVLW B'00000000'
MOVWF INTCON ;NO INTERRUPTIONS
MOVLW B'00100100'
MOVWF TXSTA ;ACTIVATE TX AND HIGHT BAUD RATE
BANK0
MOVLW B'10010000'
MOVWF RCSTA ;ACTIVATE SERIAL AND RX
GOTO RECEIVE

RECEIVE
BANK1
BTFSS PIR1,5 ;DOES RECEPTION BUFFER IS FULL?
GOTO RECEIVE ;NO-WAIT
GOTO CHECK_OERR ;YES-CHECK OERR

CHECK_OERR
BANK0
BTFSS RCSTA,1 ;DOES OERR OCCURRED?
GOTO CHECK_FERR ;NO-CHECK FERR
GOTO SERIAL_INIT ;YES-RESET

CHECK_FERR
BANK0
BTFSS RCSTA,2 ;DOES FERR OCCURRED?
GOTO GET_DATA ;NO-GET RECEIVED DATA
GOTO SERIAL_INIT ;YES-RESET

GET_DATA
BANK0
MOVLW RCREG
MOVWF DATA_BYTE
GOTO TRANSMIT

TRANSMIT
BANK0
BTFSC PIR1,4 ;DOES TXREG IS EMPTY?
GOTO TRANSMIT ;NO-WAIT
MOVLW DATA_BYTE ;YES-SET DATA TO TRANMIT
MOVWF TXREG
GOTO TRANSMIT_LOOP

TRANSMIT_LOOP
BANK1
BTFSS TXSTA,1 ;DOES DATA WERE SENT?
GOTO TRANSMIT_LOOP ;NO-WAIT
GOTO RECEIVE ;YES-RECEIVE
END

------------------------------------------------------


What is wrong with the code ?? Only the sending part is working, I tested it separately.
 

Attachments

  • gpsrs232.asm
    1.5 KB · Views: 184
Nigel Goodwin said:
You should use the 'code' tags to format your code correctly - but if you check my tutorials the last RS232 one shows how to use the 877 UART.

You have a very good tutorial there, but it doesn't check errors like OERR and FERR. According to the datasheet OERR, when is set, won't let you receive more data. So, is essential to check this bit, and if this error occurs you must reset your receiver. Correct?

Code:
#INCLUDE <P16F877A.INC>

__CONFIG _CP_OFF & _CPD_OFF & _DEBUG_OFF & _LVP_OFF & _WRT_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC

	#DEFINE BANK0 BCF	STATUS,RP0 ;SET BANK0
	#DEFINE BANK1 BSF	STATUS,RP0 ;SET BANK1
	
	BAUD_RATE_SPBRG	EQU	.25	;SPBRG

	CBLOCK 	0X70
				DATA_BYTE	;RECEIVED BYTE
	ENDC

	ORG	0X0000
	GOTO	SERIAL_INIT

	CLRF	DATA_BYTE
	CLRF	PORTC
	BCF 	STATUS,RP1

SERIAL_INIT
	BANK1
	MOVLW	B'10000000'
	MOVWF	TRISC		;TX RC6 AND RX RC7
	MOVLW	B'00000110'
	MOVWF	ADCON1		;DIGITAL I/O
	MOVLW	BAUD_RATE_SPBRG
	MOVWF	SPBRG		;BAUD RATE CONFIG
	MOVLW	B'00000000'
	MOVWF	INTCON          ;NO INTERRUPTIONS
	MOVLW	B'00100100'
	MOVWF	TXSTA		;ACTIVATE TX AND HIGHT BAUD RATE
	BANK0
	MOVLW	B'10010000'
	MOVWF	RCSTA		;ACTIVATE SERIAL AND RX
	GOTO 	RECEIVE

RECEIVE
	BANK1
	BTFSS	PIR1,RCIF	;DOES RECEPTION BUFFER IS FULL?
	GOTO	RECEIVE		;NO-WAIT
	GOTO	CHECK_OERR	;YES-CHECK OERR

CHECK_OERR
	BANK0
	BTFSS	RCSTA,OERR	;DOES OERR OCCURRED?
	GOTO	CHECK_FERR	;NO-CHECK FERR
	GOTO	SERIAL_INIT	;YES-RESET

CHECK_FERR
	BANK0
	BTFSS	RCSTA,FERR	;DOES FERR OCCURRED?
	GOTO	GET_DATA	;NO-GET RECEIVED DATA
	GOTO	SERIAL_INIT	;YES-RESET

GET_DATA
	BANK0
	MOVLW	RCREG
	MOVWF	DATA_BYTE
	GOTO 	TRANSMIT

TRANSMIT
	BANK0
	BTFSC	PIR1,TXIF	;DOES TXREG IS EMPTY?
	GOTO 	TRANSMIT	;NO-WAIT
	MOVLW	DATA_BYTE	;YES-SET DATA TO TRANMIT
	MOVWF	TXREG
	GOTO 	TRANSMIT_LOOP

TRANSMIT_LOOP
	BANK1
	BTFSS	TXSTA,TRMT	;DOES DATA WERE SENT?
	GOTO	TRANSMIT_LOOP	;NO-WAIT
	GOTO 	RECEIVE		;YES-RECEIVE
	END

Thanks for the 'code' tag trick .... ;)
 
Last edited:
tio_rafael said:
You have a very good tutorial there, but it doesn't check errors like OERR and FERR. According to the datasheet OERR, when is set, won't let you receive more data. So, is essential to check this bit, and if this error occurs you must reset your receiver. Correct?

Those sort of things are really from 30/40 years ago - it's uncommon now to bother with either error flags or handshaking - and if your serial link isn't very good then the error flags are pretty useless anyway, they are much too restricted to give meaningful results.
 
hi tio,
Made a few changes to you code, its very simple, but it works.

You should be able to expand/improve on it as required.

Regards

EDIT: this subr should read.
RXERR1:
BSF RCSTA,CREN
MOVLW '?'
MOVWF DATA_BYTE; + this line
GOTO ISTXRDY
 
Last edited:
ericgibbs said:
hi tio,
Made a few changes to you code, its very simple, but it works.

You should be able to expand/improve on it as required.

Regards

EDIT: this subr should read.
RXERR1:
BSF RCSTA,CREN
MOVLW '?'
MOVWF DATA_BYTE; + this line
GOTO ISTXRDY

Thanks a lot! Your code really works.

Thank you all for your time and helpful comments!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top