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.

Critique my RS-232 asm code for PIC16F84A

Status
Not open for further replies.

dak246

Member
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 said:
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.
 
: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!
 
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top