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.
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.
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.
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.