with a 4Mhz Xtal, and the code that I use, I can get 9600 baud Tx or Rx not both, because I'm not using interrupts to keep the routine as simple as possible, you can include it almost in any program with minor modifications, I know that here in the list are better ones but this one is very simple :-). Serial TX routine: ; ************************************************* ; ** transmit routine ** ; ** W must have the data to be transmitted ** ; ************************************************* ; Note: _tx is the bit used for transmit data, must be set as output. ; baudrate is a constant see initialization. ; you must declare as registers the follow: txreg, delay & count ; send movwf txreg bcf _tx ;send start bit movlw baudrate movwf delay movlw .9 movwf count txbaudwait decfsz delay goto txbaudwait movlw baudrate movwf delay decfsz count goto sendnextbit movlw .9 movwf count bsf _tx ;send stop bit retlw 0 sendnextbit rrf txreg btfss _c ;check next bit to tx goto setlo bsf _tx ;send a high bit goto txbaudwait setlo bcf _tx ;send a low bit goto txbaudwait ; end ; ********************************************* ; ** Serial RX routine ** ; ** before call this one, delay = baudrate ** ; ********************************************* ; same as below but _rx must be declared as pin, and configured as input. ; also declare rcreg as register ; count must have the value 9 before call this routine (needed only the ; first time you call it). ; receive btfsc _rx goto $-1 movlw baudrate movwf delay rxbaudwait decfsz delay goto $-1 movlw baudrate movwf delay decfsz count goto recvnextbit movlw .9 movwf count retlw 0 recvnextbit bcf _c btfsc _rx bsf _c rrf rcreg goto rxbaudwait ; Remember that when you call receive routine, it will keep looping until a low state (start bit) can be detected, then it will get the 8 bits and exit routine, if you are planning to receive many bytes, you must call this one again, as much as characters to receive you are especting. with some programming you can modify that to do other jobs in the mean time, but keep in mind the timming factor, at 4Mhz and high baud rates it's very critical!. there is no parity check, only 8N1 format. At the start of the program you must declare the follow: ; ; definitions... ; count equ 0x10 delay equ 0x11 txreg equ 0x12 rcreg equ 0x13 ; clockrate equ .2000000 ;Xtal value (2Mhz in this case) fclk equ clockrate/4 baudrate equ ((fclk/.2400)/3-2) ;2400 is the baud rate ; ; for 4mhz clockrate must be .4000000, etc. but remember that not all combinations work, you must done this calculation by hand to make shure that the baudrate value will not be more than FF or it won't work, in this case the value is : 2e6/4= 5e5/2400 = 208/3 = 69-2 =67.44\ , the value for the baudrate constant will be 67, remember that if the decimal component is more that .5 you must round up the number to avoid errors, also, the smaller value for baudrate, the higher posibility of reception error, so if the number is bigger, will work better. I hope that it can be useful for you, anyway if doesn't work write me and i will try to help you... see you. Leandro J. Laporta (LU2AOQ) mail: lu2aoq@yahoo.com wrk: Arg. Assoc. for Space Tech. ham: TCP/IP high speed group HSG
Code:
I used your code as the base to do a 57600Bps transmitter. I had a 20Mhz Xtal and used a delay value (bitdelay) of 18 to get 57600Bps. I added a nop so that the start bit is the same length as the other bits and made the stop bit a low. I also had to add a delay of one bit length after the byte is sent to stop hyperterm from getting confused when the function is called repeatedly. To use simply call senddata with data in W and make sure you have bitdelay defined (18 in my case). Thanks Leandro for your above code. Cheers -alex green senddata movwf send movlw bitdelay movwf temp3 movlw .9 movwf count bcf portb,4 ;send start bit nop ;even out bit times next decfsz temp3 goto $ -1 movlw bitdelay ;rest of program is 9 instructions movwf temp3 decfsz count goto sendnextbit bcf portb,4 ;send stop bit movlw bitdelay ;Delay for line to settle movwf temp3 ;Delay for line to settle decfsz temp3 ;Delay for line to settle goto $ -1 ;Delay for line to settle bsf portb,4 decfsz temp3 goto $ -1 return sendnextbit rrf send btfss status,c ;check next bit to tx goto setlo bsf portb,4 ;send a high bit goto next setlo bcf portb,4 ;send a low bit goto next+
Questions:
hi,James Newton replies: , +
your code seems fine to many applications but i've a question. What if i increase the clock speed to 8Mhz and during the rcx time i've to check a few I/O ports using btfss instructions.Does the UASRT pin works independently?
HELLo+
It is a pretty good code by the looks of it.. However I am new to assembly and I quite dont understand what _tx stand for and what u mean when u sate in the begining that "_tx is the bit used for transmit data, must be set as output"
SID
file: /Techref/microchip/rs232at9600on16f84noint.htm, 7KB, , updated: 2005/12/7 10:41, local time: 2024/11/25 02:10,
18.116.23.59:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/microchip/rs232at9600on16f84noint.htm"> microchip rs232at9600on16f84noint</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.