I don't use Mikrobasic, but here is an easy way to do it without interrupts...just translate the code to proper syntax. (And set the symbols of cTxReg etc. to the appropriate values. Leading characters are: c=constant, b=byte.)
bDelayTime = 0
CheckForData:
if cTxReg = cDataWaiting then 'check hardware Tx register for incoming data.
serin = bSerialDataIn 'get data...
goto SerialDataReceived
else 'no data waiting...
pause 1 'wait 1ms...change to match your baud rate, since some speed may overwrite
bDelayTime = bDelayTime + 1
if bDelayTime = cMaxWaitTime then
goto NoSerialDataReceived
endif
goto CheckForData
endif
For those wanting to do it with interrupts, do a quick search for "buffer" on the Proton forum. ( http://www.picbasic.org/forum/ ) Les, the PDS author, wrote a subroutine for 18F's to buffer all incoming serial data. It should be possible (given enough ingenuity) to convert it to PBP or Mikrobasic.
For those wanting to do it with interrupts, download the Soft Uart for P12 by Warren Schroeder project from http://www.mikroe.com/en/projects/
It's an excellent tutorial as well as an interrupt driven full-duplex software UART. The code is quite simple and easy to follow.
Hi,
Full-duplex software UART is not possible or possible only if the baud rate
is very low and interrupts are used. (Software UARTs use bit-banging).
Soft Uart for P12 by Warren Schroeder:
If you look at the code you will see that RX uses an interrupt and that in
the SUB TX_19200 interrupts are disabled. This means that while
you are sending data you cannot receive data at the same time. (Not Full-duplex).
Best regards,
Luciano
Last edited by Luciano; - 9th January 2008 at 18:31.
It could be done if you send and receive in the interrupt. Set it to interrupt at 2*baud, and alternate Tx/Rx. At 9600 baud, with 19200kHz interrupt rate on a 20MHz crystal, you get 260 code cycles to perform each...that should be more than enough, and cycles left over for other regular code. You might even get close to that rate with a 4MHz crystal if you are thrifty with your code.
Or, in the interrupt do the Tx send first, then all Rx work, then Tx setup for the next bit, without alternating, so the "Tx send" portion is actually very short and always the same length. This might be more efficient, but maybe a little more coding.
Luciano -
I will try that code. The last time I tried the Soft_Uart_Read was blocking. I see v6.0.0.0 has a heap of changes.
I just found it frustrating when trying to convert a simple working PBP program to MikroE Basic. What better way to learn a new language than to convert code you already know and understand in PBP.
I'll stick with PBP for now until I need ethernet or have some time to do some test projects.....
bill.
Hi,
Your code does not work with software UARTs.
Software UART routines use bit-banging techniques and can use almost any I/O pin
of the microcontroller. Software UARTs have big limitations and if the microcontroller has
hardware UARTs just use them!
Best regards,
Luciano
One nice thing about Proton+ is in the forum, Les the compiler author occasionally posts misc. PDS code. (In addition to helping out on the forum when needed.) For instance, he's posted a full Space Invaders game that works with the Crownhill development board... which is modeled in the VSM simulator so you can play it right on your PC screen (or the actual development board).
He's also posted code to use as a serial buffer for the hardware usart, so it will receive data in the background, and fill it in when you get around to using a hserin command. More examples are speech decompression routines, and a full .wav file player design with CF FAT and audio out routines.
These are just a few examples. Some of them can easily be ported to PBP; others may take some work.
Bookmarks