Serial comm with PIC18F4520 in Assembly

Electronics Computer Programming Q&A
Post Reply
Jon Henry
Posts: 3
Joined: Mon Oct 12, 2009 3:51 am
Contact:

Serial comm with PIC18F4520 in Assembly

Post by Jon Henry »

IM trying to get serial comm working between a PIC18F4520 and a pc. Ive got some code written that I sampled from another forum but IM having problems getting the correct results. I get info in to my pc but it isnt the right info.

Heres the PIC code:

Code: Select all

Start
	bsf		OSCCON,IRCF2	;set to 8MHz clock
	bsf		OSCCON,IRCF1
	bsf		OSCCON,IRCF0
	movlw	0x7f			;set PortA to all digital
	movwf	ADCON1
	clrf	TRISA			;set PortA pins to output
	clrf	LATA			;turn off LEDs
	movlw	b'00010010'		;set PortB - RB4=RX=input
	movwf	TRISB

	movlw	0x0c			;set for 9600 baud @ 8MHz clock
	movwf	SPBRG
	bcf		TXSTA,BRGH		;clear Baud Rate Select Bit (low speed)
	bcf		BAUDCTL,BRG16	;select 8-bit baud rate generator
	bcf		TXSTA,SYNC		;asynchronous mode
	bsf		RCSTA,SPEN
	bsf		TXSTA,TXEN		;transmit enable
        movlw	0x60
Send
	incf	WREG
	movwf	TXREG
Loop
	btfss	TXSTA, TRMT
	goto 	Loop
	goto Send

	end
Like I said before, I get data in but its just appears to be random numbers. IM not too sure on the format of what the PIC will send ether it be ascii or straight decimal. A little direction would be greatly appreciated.
Thanks,
Jon
SETEC_Astronomy
Posts: 582
Joined: Tue May 09, 2006 12:44 am
Contact:

Re: Serial comm with PIC18F4520 in Assembly

Post by SETEC_Astronomy »

Are you sure both ends (PIC and PC) are set to use the same baud rate?
Jon Henry
Posts: 3
Joined: Mon Oct 12, 2009 3:51 am
Contact:

Re: Serial comm with PIC18F4520 in Assembly

Post by Jon Henry »

Yes, both at 9600.
Jon Henry
Posts: 3
Joined: Mon Oct 12, 2009 3:51 am
Contact:

Re: Serial comm with PIC18F4520 in Assembly

Post by Jon Henry »

IM trying to transmit one byte at a time, it is definitely sending something. When I monitor COM1 with hyperterminal, I get symbols such as what looks like an uppercase P followed by lowercase t ( Pt). Is the PIC turning my numbers into ascii or something of that nature?
psycho
Posts: 388
Joined: Thu Jan 10, 2008 8:13 pm
Location: Northwest Indiana
Contact:

Re: Serial comm with PIC18F4520 in Assembly

Post by psycho »

If you send 0x31 (31 HEX), that would translate to the computer displaying a '1'. If you send 0xFE, the computer will not be able to display it correctly because there is no printable ASCII char for 0xFE.

However, I agree that this seems to be a setup problem in the baud rate/word length or something similar. What are your oscillator settings? Also, when you bsf OSCON,xxxx are you sure that is correct?

I will look at the datasheet.

Kevin
psycho
Posts: 388
Joined: Thu Jan 10, 2008 8:13 pm
Location: Northwest Indiana
Contact:

Re: Serial comm with PIC18F4520 in Assembly

Post by psycho »

Ummm. Look on page 203 of the datasheet. The code snippet above doesn't seem to be for this particular device. In a comment above, it says RB4 is the input for the RX. However, in the datasheet, RB4 has nothing to do with the USART.

Also, I don't see in the code above where the baud rate is set. Basically what the code above does is useless in this part since the USART is on port C.

Read through the datasheet and if you run into any problems, leave a note on here.

Kevin
dobrien
Posts: 37
Joined: Thu May 15, 2008 6:01 am
Contact:

Re: Serial comm with PIC18F4520 in Assembly

Post by dobrien »

The Microchip web site has two Applications Notes
1. AN774 Asynchronous Communications with the PICmicro® USART. with sample code
for the PIC16,PIC17 and the PIC18. It is for the PIC18F452.
http://www.microchip.com/stellent/idcpl ... e=en012073

2.The other is PIC18F452 to PIC18F4520 migration.
http://ww1.microchip.com/downloads/en/D ... 39647a.pdf

Good luck !
Dan
dyarker
Posts: 1917
Joined: Fri Aug 22, 2003 1:01 am
Location: Izmir, Turkiye; from Rochester, NY
Contact:

Re: Serial comm with PIC18F4520 in Assembly

Post by dyarker »

Hyperterminal assumes everything is ASCII, the PIC is sending a binary number. Set Hyperterminal to capture to file, have the PIC send something, stop capture, view the file with a hexidecimal editor. If b'00010010' is the number being sent, you will see 12 in the hex editor (that's 12hex, not twelve).

Another possibility ... if you are connecting the serial output of the PIC directly to the PC's serial port, then the bits are inverted. At logic levels a "1" is high and a "0" is low. PC serial ports are EIA-232 (formerly RS-232). In EIA-232 a "1" is a negative voltage, and a "0" is a positive voltage. This would also explain two characters on screen for one sent. The first "1" bit is being read as a start bit.

Cheers,
Dale Y
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 27 guests