Peter Moreton wrote: > Hi, > > I'm using the Microchip C18 compiler, and a PIC18F452 to relay RS232 > serial data from a host computer to a serially controlled CCTV camera. > I'm using the hardware USART on RC6/RC7 to communicate with the PC, and > this works fine. > > Now I'm trying to use the C18 'Software UART' library functions to > implement a software UART on RD6/RD7. > > Trouble is, the C18 manual says you (the user) has to define the > following delays: > DelayRXHalfBitUART as ((((2*FOSC/(4*baud))+1) / 2)- 12 cycles > DelayRXBitUART ((((2*FOSC/(8*baud))+1) / 2)- 9 cycles > DelayTXBitUART ((((2*FOSC/(4*baud))+1) / 2)- 14 cycles > > I guess these routines would need to be written in assembler to be > accurate (?) and my assembler is pretty poor, so I'm looking for any > kind soul who have already done this, and can send me some pointers or > sample code. > > (My system clocks at 4 Mhz with 4x PLL enabled = 16 Mhz, and the comms > is running at 4800 baud) > > Thanks, Peter Moreton > For short delays like that, you can always just use a number of nop() calls equal to the number of cycles you need to delay. The compiler translates nop() directly into a nop instruction, so you're assured one cycle of delay for each nop() call. If you want to use a real delay loop, something like this would work: void DelayTXBitUART(void) { _asm movlw 12/3 // number of cycles to delay / 3 delayloop: decfsz WREG, 1, 0 // 2 cycles on skip, one otherwise bra delayloop // 2 cycles nop // to make the last iteration 3 cycles instead of just 2 _endasm } I don't remember if WREG is defined, but I know there is a way to access it by name. Another method is similar to using repeated nops, but generates smaller code: void DelayRXHalfBitUART(void) { _asm bra $+2 bra $+2 bra $+2 bra $+2 nop _endasm } Each bra $+2 just branches to the next instruction, and takes two clock cycles. With the nop, you haev a total of 9 cycles of delay. You might also try looking at the library source code. There are a few delay functions for multiples of 10, 100, 1000, and 10000. Obviously not suited for your applications, but a good way to see some other methods of creating delay loops. By the way, you have the function names mixed up. DelayTXBitUART is the 12 cycle delay, DelayRXHalfBitUART is 9 cycles, and DelayRXBitUART is 14 cycles. This is on page 105 of the C18 Libraries pdf file. Hope this helps, Daniel Imfeld -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body