PDA

View Full Version : A way to remove lookup tables and calculate those values?



sccoupe
- 30th April 2011, 15:46
I have a number of lookup tabes to represent a number of plot points for a serial graphics controller. These plotting points are the center points of circles that are drawn. The center points are also in a circular formation. The x and y coordinates that are sent to the controller from the pic are each 2 bytes in length and each byte is sent seperately. In the example below lets say we want to plot 4 evenly spaced dots around a circle. The way I do it now is to lookup for position 0 for x1 (x byte 1), x2 (x byte 2), y1 (y byte 1), and then y2 (y byte 2), then the hole process again for positions 1, 2 and 3. I then send those byte hex values out the seriel pin and repeat. Is there a better way?


LOOKUP z, [$00, $00, $00, $01], x1
LOOKUP z, [$00, $33, $88, $FF], x2
LOOKUP z, [$00, $00, $01, $01], y1
LOOKUP z, [$00, $3C, $A1, $FF], y2
SEROUT gpioa.1, 2, [x1,x2,y1,y2]

Thanks

sccoupe
- 30th April 2011, 21:34
I just tried crunching the 4 seperate 8 bit lookups with a single 32 bit lookup2 and it didnt seem to help the speed at all.

LOOKUP2 z, [$00000000, $0033003C, $008801A1, $01FF01FF], x1
SEROUT gpioa.1, 2, [x1.BYTE3,x1.BYTE2,x1.BYTE1,x1.BYTE0]

Archangel
- 30th April 2011, 22:42
Can you use the usart serial port? Serout & serout2 are bit bang serial output and
S L O W your MCU, even debug might be faster.

HenrikOlsson
- 1st May 2011, 07:47
Hi,
What do you mean by "a better way"? One that executes faster or takes less codespace or looks "cleaner" or just "generally" better?

Calculating the values will almost certanly increase the execution time (run slower) but might produce less code than a bunch of lookup tables. PBP do have SIN/COS/ATN which might be useful if you're dealing with circles, angles etc but again, it'll probably be slower than a lookuptable.

Using HSEROUT as Joe suggests will allow the program to continue execute while the byte is being sent. To gain the most out of it you'll need to place the HSEROUT between each lookup, like:

LOOKUP z, [$00, $00, $00, $01], x1 'Get x1
HSEROUT [x1] 'Send x1
LOOKUP z, [$00, $33, $88, $FF], x2 'Now get x2 while x1 is being sent in the background.
HSEROUT [x2] 'It's likely that x1 isn't fully sent yet so HSEROUT will "hang" until there's room in TXREG.
LOOKUP z, [$00, $00, $01, $01], y1
HSEROUT [y1]
LOOKUP z, [$00, $3C, $A1, $FF], y2
HSEROUT [y2]
That will make it lookup "the next" byte while the previous one is being sent. Lookup is most likely way faster than the time it takes the USART to send the byte so sending the data is still the bottleneck, I think.

Obviously you can replace the individual HSEROUT statements with a GOSUB by placing the looked up byte in the same variable each time.

/Henrik.

aratti
- 1st May 2011, 10:50
Since it is not clear to me what you want to acheive, I will attach a simple program in VB6 from which you can see how to use maths for drawing as many points you want on any given circle. All the maths are inside command1_click and should be not too difficult for you to translate the VB code into PBP code.

Cheers

Al.

sccoupe
- 2nd May 2011, 21:06
I tried using sin/cos calculations and it resulted in the same speed or maybe slightly slower. I am only using 9600 baud out. This may be where im hanging up. I didnt figure that it would be the bottleneck in the operation. The next test will be to bump up the baud rate and see what happens.

EDIT: I changed over to the USART and set it up at 19200 and sure enough its displaying the gauge points twice as fast. Now, I have to tie the 2 USART transmit pins together because I have to initiate communication with the GPU at 9600 baud and then change the baud rate. I need to change the baud rate for one USART on the fly and re-DEFINE-ing the baud rate doesnt seem to work.

cncmachineguy
- 2nd May 2011, 22:11
I think you will have to address the baud rate reg directly if you want to change on the fly. As near as I can tell, DEFINE will only work once in the program. Someone correct me if I am wrong

Charles Linquis
- 3rd May 2011, 02:47
You can change the baud rate "on the fly". I generally try to find a combination of BRGH and BRG16 that will give me both of the baud rates that I want with writing only to SPBRG.

I normally run at 40Mhz, so with the following in the header

DEFINE HSER_RCSTA 90H
DEFINE HSER_TXSTA 24H
DEFINE HSER_CLROERR 1

Then, in the main program...


You can run at 9600 baud with the statement:

SPBRG = 255


Or at 57600 baud with the statement:

SPBRG = 42