Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

UART is displaying unexpected characters

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
Dear All,
Today experiment with UART + MCU 16f877a
1. I wrote a program and compiled successfully
2. Simulated in Proteus with virtual terminal
3. displayed unexpected character
Please find the attachment for picturs
Advice is needed
Thanks in advance

Code:
char_loader	equ			0x20	;bank 0
LIST P=P16F877
#include <p16f877a.inc>
__config 3f39
			org	0x000
start		goto	main
main		call	sys_init
loop		call	load_char
			goto	loop			

load_char	movlw	'T'
			movwf	char_loader
			call	send_char
			movlw	'E'
			movwf	char_loader
			call	send_char
			movlw	'S'
			movwf	char_loader
			call	send_char
			movlw	'T'    
			movwf	char_loader
			call	send_char
			movlw	'1'
			movwf	char_loader
			call	send_char
			movlw	'2'
			movwf	char_loader
			call	send_char
			movlw	'3'
			movwf	char_loader
			call	send_char
			return
			
send_char	btfss	PIR1,TXIF			;Check the USART transmit buffer is empty
			goto	$-1
			movf	char_loader,w
			movwf	TXREG
			return

sys_init	bsf		STATUS,5
			movlw	b'00000000'		
			movwf	TRISC
			movlw	0x11
			movwf	SPBRG			; Set Baud rate 9600bps
			movlw	b'10100000'		; 8-bit transmit, transmitter enabled,
			movwf	TXSTA			; asynchronous mode, low speed mode
			bcf		STATUS,5
			bsf		RCSTA,SPEN			;Serial port enabled 
									;(configures RC7/RX/DT and 
									;RC6/TX/CK pins as serial port pins)
			return				


end

uart.jpg
 

Maybe your baudrate settings are wrong. In proteus double click Virtual terminal and choose different baudrates and see. If you are getting something on Virtual termainal then it means PIC is transmitting the data.

Should this LIST P=P16F877 be LIST P=P16F877A?

What is your Clock Frequency. I think your BRGH is 0 and your SPBRG = 0x11 is wrong.

If your BRGH = 0 then your SPBRG should be as follows.

Fosc = 4 MHz SPBRG for 9600 baudrate = d6 or 0x06
Fosc = 10 MHz SPBRG for 9600 baudrate = d15 or 0x0F
Fosc = 16 MHz SPBRG for 9600 baudrate = d25 or 0x19
Fosc = 20 MHz SPBRG for 9600 baudrate = d31 or 0x1F
 
Last edited:
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Maybe your baudrate settings are wrong. In proteus double click Virtual terminal and choose different baudrates and see. If you are getting something on Virtual termainal then it means PIC is transmitting the data.


Dear internetuser2k12 ,

Really thanks for advice
Yes you correct the problem is existing on baudrates

1. I load decimal 25 to SPBRG and select 2400 baudrates of virtual terminal property then it is working fine,

Result
UART1.jpg

2. Then I changed the board rate value (SPBRG) and also virtual terminal value accordingly (SPBRG = 6 , board rate = 9600)
BRGH.jpg
3. BRGH: High Baud Rate Select bit chaged to 0 and 1 and checked
4. The result is displaying as following picture
uart2.jpg

Please advice
 

For BRGH = 1 and baudrate = 9600, SPBRG is 25d or 0x19 and not 6 or 0x06

Zip and post your project files and proteus file. I will check it.

If BRGH = 0, then 6 is right for 9600 baudrate.

Try these

Code:
;for BRGH = 0, baudrate = 9600

movlw	0x06
movwf	SPBRG			; Set Baud rate 9600bps
movlw	b'10100000'		; 8-bit transmit, transmitter enabled,
movwf	TXSTA


;for BRGH = 1, baudrate = 9600

movlw	0x19
movwf	SPBRG			; Set Baud rate 9600bps
movlw	b'10100100'		; 8-bit transmit, transmitter enabled,
movwf	TXSTA
 

Attachments

  • spbrg 9.6k.jpg
    spbrg 9.6k.jpg
    97.2 KB · Views: 181
Last edited:

Dear internetuser2k12 .

Yes I got the point
But BRGH = 0 and SPBRG = 6 (9600) Not working for me
However I attached zip file. Please check
Thanks in advance
 

Attachments

  • usart.rar
    40.7 KB · Views: 122

Which compiler are you using? CCSC, Hi Tech C, or C18?

Maybe it is a Proteus bug. Try to check it on real hardware.
 
Last edited:
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear internetuser2k12 ,

I am using Language tool versions: MPASMWIN.exe v5.36, mplink.exe v4.36, mplib.exe v4.36 and MPLAB IDE v8.53

Maybe it is a Proteus bug

Could you have such experience early

Tx
PA3040
 

I checked your Project using Proteus. I got the same result as you are getting. Sometime ago another person had a problem similar to this but using AVR MCU. He compiled the code for 9600 baud but was getting the correct display at 2400 baud in Proteus.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Dear internetuser2k12 ,
Thanks again again for reply
1. Now I need to study the receiver part
2. First can you please explain the procedure to send the character to the PIC ( using Proteus virtual terminal)
Please advice
Thanks in advance
 

Run the Proteus Simulation and right click Virtual terminal windows not the Virtual Terminal Part and choose Echo typed Characters.
The Characters tyoed are sent to MCU.
 

Attachments

  • vt ss.jpg
    vt ss.jpg
    113.9 KB · Views: 164
  • vt sim.rar
    279.5 KB · Views: 103
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
The most likely reason you are unable to obtain the expect result for 9600 BAUD with the following settings FOSC = 4MHz, BRGH = 0 and SPBRG = 6 is the percentage of error of 6.99%.

Examine the table you've posted in reply #3, generally error rates greater than 1% are unreliable at best and unobtainable at worst.

Not all BAUD rates can be obtained for a given FOSC.

To obtain a 9600 BAUD rate for a FOSC = 4MHz set BRGH = 1 and SPBRG = 25 which offers an error rate of only 0.16% as indicated in the table posted in reply #4.

Another option would be to examine the tables and select a FOSC which offers the desired BAUD rate with an acceptable error rate of less than 1%.

The following online tool can aid in finding acceptable parameters for a desired BAUD rate:

PIC Microcontroller RS232 IO SPBRG Calculator


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear internetuser2k12,

Thanks for the reply

I edit my program adding receiver part. It does not working as per i spec
Can you please recheck the my program and please correct me
Thanks in advance

CODE
Code:
char_loader	equ			0x20
char_reciver	equ		0x21	
LIST P=P16F877A
#include <p16f877a.inc>
__config 3f39

			org	0x000
start		goto	main
main		call	sys_init
loop		call	load_char
			call	reci_char
			;call	display
			goto	loop			

load_char	movlw	'T'
			movwf	char_loader
			call	send_char
			movlw	'E'
			movwf	char_loader
			call	send_char
			movlw	'S'
			movwf	char_loader
			call	send_char
			movlw	'T'    
			movwf	char_loader
			call	send_char
			movlw	'1'
			movwf	char_loader
			call	send_char
			movlw	'2'
			movwf	char_loader
			call	send_char
loop1		;nop
		;	goto	loop1
			return
			
send_char	btfss	PIR1,TXIF			;Check the USART transmit buffer is empty
			goto	$-1
			movf	char_loader,w
			movwf	TXREG
			return

reci_char	btfss	PIR1,RCIF
			goto	$-1
			movf	RCREG,W
			movwf	PORTB
			return

sys_init	clrf	PORTB
			bsf		STATUS,5
			movlw	b'00000000'		
			movwf	TRISC
			movlw	.25
			movwf	SPBRG			; Set Baud rate 9600bps
			movlw	b'10100100'		; 8-bit transmit, transmitter enabled,
			movwf	TXSTA			; asynchronous mode, low speed mode
			clrf	TRISB
			bcf		STATUS,5
			bsf		RCSTA,SPEN			;Serial port enabled 
									;(configures RC7/RX/DT and 
									;RC6/TX/CK pins as serial port pins)
			movlw	b'10010000'
			movwf	RCSTA
			return				


end
 

I don't know assembly language much. For UART receive you have to use interrupts. Where is your interrupt routine?
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear internetuser2k12 ,
cann't we check the interrupt flag without enable GIE and PEEI

What I did is
Code:
reci_char	[COLOR="#FF0000"]btfss	PIR1,RCIF[/COLOR];RCIF: USART Receive Interrupt Flag bit
			goto	$-1
			movf	RCREG,W
			movwf	PORTB
			return

- - - Updated - - -

Dear bigdogguru,
I am pleased by your reply and great advice

May it be possible in actual hardware level ( still my hardware not ready)?

Please advice
 

Yes.

Reference: PIC16F87XA Datasheet, Section: 14.11 Interrupts, Page: 153
Note: Individual interrupt flag bits are set regardless
of the status of their corresponding mask bit or the GIE bit.


BigDog

- - - Updated - - -

Example PIC16F UART code in Assembly.

Using Interrupts:
Serial Communication Using PIC (USART)

Using Polling:
UART test program for 16F628

Of course, you will need to port the above examples to the PIC16F877A with a few modifications.

BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear bigdogguru,
Thanks for the reply

In my program post #12, after compile I load HEX file to Proteus . Then insert test to Proteus virtual terminal and enter . as per program

Code:
reci_char	btfss	PIR1,RCIF;RCIF: USART Receive Interrupt Flag bit
			goto	$-1
			movf	RCREG,W
			movwf	PORTB
			return

When set the RCIF, the result of RCREG load to the POTRB

Transmitter is working but receiver does not working
Please advice

- - - Updated - - -

Dear All I fund the problem
I could not set RX pin as input in TRIS register (The link provided by Bigdigguru)

Now it is receiving character and have to clear out more doubts, let you know later
 

Dear All,
Can I have the software to check UART IN MY pic Connected to serial port ( OS: windows 7 )
It would be much appreciated if you can provide a link to download free software
Thanks in advance
 

Use ReatTerm

It should not support for windows 7

Can I have similar one which support for windows 7
Thanks in advance
 

I prefer Putty, its open source and available free for download:

PuTTY Download Page

Reference: A.3.1 What ports of PuTTY exist?

Currently, release versions of PuTTY tools only run on full Win32 systems and Unix. ‘Win32’ includes versions of Windows from Windows 95 onwards (as opposed to the 16-bit Windows 3.1; see question A.3.5), up to and including Windows 7; and we know of no reason why PuTTY should not continue to work on future versions of Windows.

The Windows executables we provide are for the 32-bit ‘x86’ processor architecture, but they should work fine on 64-bit processors that are backward-compatible with that architecture. (We used to also provide executables for Windows for the Alpha processor, but stopped after 0.58 due to lack of interest.)

I've personally ran Putty on several 32-bit and 64-bit Windows versions without any issues.


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top