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 mov txreg, W clrb _tx ;send start bit mov W, #baudrate mov delay, W mov W, #9 mov count, W txbaudwait decsz delay jmp txbaudwait mov W, #baudrate mov delay, W decsz count jmp sendnextbit mov W, #9 mov count, W setb _tx ;send stop bit retw #0 sendnextbit rr txreg sb C ;check next bit to tx jmp setlo setb _tx ;send a high bit jmp txbaudwait setlo clrb _tx ;send a low bit jmp txbaudwait ; end ; ********************************************* ; ** Serial RX routine ** ; ** before call this one ; ********************************************* ; 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 snb _rx jmp $-1 mov W, #baudrate mov delay, W rxbaudwait decsz delay jmp $-1 mov W, #baudrate mov delay, W decsz count jmp recvnextbit mov W, #9 mov count, W retw #0 recvnextbit clrb C snb _rx setb C rr rcreg jmp 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 $10 delay equ $11 txreg equ $12 rcreg equ $13 ; 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 use this site a lot. Here is a donation for the masses+I referenced some code, and ported to the SX28 running turbo mode on a 4Mhz XTAL. The code was based on the code here: http://www.sxlist.com/techref/ubicom/lib/io/osi2/serial/rs232at9600on16F84noint_sx.htm
The RX routine was improved to correctly sample the data byte coming in. The original authors code should have waited longer before sampling the data for reliable read. The code improvement was to wait for 1.5 bits on reception of the start bit. This places the sampling in the center of each incoming bit.
The RX routine was also modified to transmit a full stop bit. This gives terminal programs proper time to sync up to incoming start bits in a framed data stream of multiple bytes.
The code has been tested at 9600,19200,38400,57600 baud. A oscilloscope was used to determing the minimum timing error for each bit. The code could be modified for other baud rates also.
Enjoy!
-Dan; ************************************************* ; ** Serial TX routine ** ; ** TXreg must have data to be transmitted ** ; ************************************************* ; TXpin - is the bit used for TX pin. ; TXreg - is the byte to be transmitted ; delay - is a counter to set serial delay ; count - counts bits transmitted. ; ; This routine transmits RS232 Data in the standard ; format "N81". This routine transmits a full Stop ; bit before returning. ; ; The baud rate based on a 4Mhz Xtal is set by ; changing the value within the Bauddelay Constant: ; TXBaudDelay equ $65 ;9600 Baud ; TXBaudDelay equ $31 ;19200 Baud ; TXBaudDelay equ $17 ;38400 Baud ; TXBaudDelay equ $0E ;57600 Baud RS232out clrb TXpin ;send start bit mov W, #9 mov count, W mov W, #TXBaudDelay mov delay, W txbaudwait decsz delay jmp $-1 mov W, #TXBaudDelay mov delay, W decsz count jmp sendnextbit setb TXpin ;send stop bit decsz delay jmp $-1 RETP sendnextbit rr TXreg sb C jmp setlo setb TXpin jmp txbaudwait setlo clrb TXpin jmp txbaudwait ; ********************************************* ; ** Serial RX routine ** ; ** RXreg Contains Data Received ** ; ********************************************* ; Note: RXpin - is the bit used for RX pin. ; RXreg - is the byte to be transmitted ; delay - is a counter to set serial delay ; count - counts bits received. ; ; This routine Receives RS232 Data in the ; standard format "N81". ; ; The baud rate based on a 4Mhz Clock is set by ; changing the value of Bauddelay Constant: ; RXBaudDelay equ $65 ;9600 Baud ; RXBaudDelay equ $31 ;19200 Baud ; RXBaudDelay equ $17 ;38400 Baud ; RXBaudDelay equ $0E ;57600 Baud ; ; The routine centers the reading of each bit at ; the center of each bit being received. This off ; set is accomplished by changing the first delay ; to 1.5 times the normal bit delay. ; ; The original Code read Data at the start of the ; valid data bit. This lead to unreliable RX at higher ; data transmission speeds (38.5kbaud,57.6kbaud). ; ; Note that this only works for values where ; RXBaudDelay * 1.5 < $ff. RS232in snb RXpin ;Loop Until Data jmp $-1 ;Jump portion of Loop mov W, #9 ;Initialize counter mov count, W mov W, #RXBaudDelay mov delay, W clc ;Clear Carry! rr delay ;Divide by 2 mov W, #RXBaudDelay ;Move baudrt to W add delay, W ;Add to Delay rxbaudwait decsz delay jmp $-1 mov W, #RXBaudDelay mov delay, W decsz count jmp recvnextbit retp recvnextbit clrb C snb RXpin setb C rr RXreg jmp rxbaudwait
file: /Techref/scenix/lib/io/osi2/serial/rs232at9600on16F84noint_sx.htm, 7KB, , updated: 2005/7/15 14:08, local time: 2024/11/22 07:49,
3.128.198.90: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://sxlist.com/Techref/scenix/lib/io/osi2/serial/rs232at9600on16F84noint_sx.htm"> scenix lib io osi2 serial rs232at9600on16F84noint_sx</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.