Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 11th May 2004, 04:41 AM   (permalink)
Default Critique my RS-232 asm code for PIC16F84A

I just want to get some opinions on whether or not this code will work for rs-232 communication (at 9600bps) to a PC. ( PIC external ocillator 20mhz )

Code:
SendData
		bcf	PORTA,0     ;Send start bit
		movwf	TX_DATA
		movlw	0x08
		movwf	BIT_CTR     ;Set bit counter to 8
DataLoop
		call	Delay
		btfss	TX_DATA,0
		bcf	PORTA,0     ;Send a 0 bit
		btfsc	TX_DATA,0
		bsf	PORTA,0     ;Send a 1 bit
		rrf	TX_DATA,1
		decfsz	BIT_CTR,1  ;Decrement bit counter
		goto	DataLoop
		call	Delay
		bsf	PORTA,0     ;Send stop bit
		return

;Should delay either 102.4uS or 102.2uS
Delay
		movlw	0xA9
		movwf	COUNTER
Loop
		decfsz	COUNTER,1
		goto	Loop
		return
The delay loop should delay for about 102.2uS, and after compensation for the time it takes to perform the diffrent number of instructions between bits it should come out to a delay in the range of 103.4uS-104.4uS. Will this range still allow it to work properly at 9600bps? Is there anything im missing or any ways I could optimize this? Thanks.
dak246 is offline  
Old 11th May 2004, 07:08 AM   (permalink)
Default Re: Critique my RS-232 asm code for PIC16F84A

Quote:
Originally Posted by dak246
I just want to get some opinions on whether or not this code will work for rs-232 communication (at 9600bps) to a PC. ( PIC external ocillator 20mhz )

Code:
SendData
		bcf	PORTA,0     ;Send start bit
		movwf	TX_DATA
		movlw	0x08
		movwf	BIT_CTR     ;Set bit counter to 8
DataLoop
		call	Delay
		btfss	TX_DATA,0
		bcf	PORTA,0     ;Send a 0 bit
		btfsc	TX_DATA,0
		bsf	PORTA,0     ;Send a 1 bit
		rrf	TX_DATA,1
		decfsz	BIT_CTR,1  ;Decrement bit counter
		goto	DataLoop
		call	Delay
		bsf	PORTA,0     ;Send stop bit
		return

;Should delay either 102.4uS or 102.2uS
Delay
		movlw	0xA9
		movwf	COUNTER
Loop
		decfsz	COUNTER,1
		goto	Loop
		return
The delay loop should delay for about 102.2uS, and after compensation for the time it takes to perform the diffrent number of instructions between bits it should come out to a delay in the range of 103.4uS-104.4uS. Will this range still allow it to work properly at 9600bps? Is there anything im missing or any ways I could optimize this? Thanks.
It's essentially identical to my RS232 tutorial, except the delay routine is slightly different - it's based on a very old application note. I've used it for years, and it's always been very reliable - assuming your delay routine is correct it should be fine.

The delay being slightly out won't make any difference, RS232 is an asyncronous protocol, so it re-sycnronises every data word.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 11th May 2004, 09:50 AM   (permalink)
Default

:idea:

If you are using an "rx" routine, don't forget to add a 1/2 bit delay after finding the start bit.....

You will need to watch those timings when you ramp up the baud rate, 115200 is reliable at 8mhz.....

Another little "tip" on the rx routine to give yourself a little extra processing time...... After you have "rx'd" 8 bits, don't wait 1bit delay (assuming 8,n,1) simply wait until the stop bit arrives, you can then go and process other data, for example storing the rx'd bytebefore returning for the next 1, tricky at high speed without this dodge.

Hope this helps!
__________________
Regards


MATT!
Matt(Pic progger) is offline  
Old 11th May 2004, 06:36 PM   (permalink)
Default

Thanks for the input. Nigel i just took a look at the link to your tutorials in your signature and it answered alot of my questions about receiving data from the PC. Thanks for a great resource.
dak246 is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 05:18 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker