PDA

View Full Version : Code speed and execution time



aajgss
- 5th May 2011, 00:48
I am using a For/Next loop to sent the variable out to an I2C DAC. The variable is counting from 1 to 255.

The sawtooth waveform only has a frequency of 1.33 hz indication an execution time for each loop of 3mSec.

I am using a 16F690 PIC running at 8Mhz, PBP and MPLAB IDE V8.63. Does this time to execute one loop sound right?

The other question is the number of lines of code compiled. In the MPASM window it tell me the number of lines assembled is 9082. Again does this sound right?

Thanks
aajgss

cncmachineguy
- 5th May 2011, 00:57
Well that does seem really Slloooooowwwww. but maybe a little more info please. code and configs would go a long way here.

aajgss
- 5th May 2011, 01:11
Bert,
Thanks for the quick reply
OK more info - one other question how do I get my code into the scrolling window?

I have a line in there :- i2cread DPin,CPin,$90,1,[c], if I take it out the code doesnt work.


Code

DEFINE osc 20
DEFINE LOADER_USED 1
INCLUDE "MODEDEFS.BAS"
DEFINE I2C_SLOUT
ANSEL =%00000001 ' Enable ADC channel-AN0
ANSELH =%0000
'ADCON0.7 = 1
SSPCON =%00101011
TRISB.4 =0
TRISB.6 =0
TRISA =%00001111
TRISC =%00000000
CPin VAR PORTB.6
DPin VAR PORTB.4
a var word
c var byte

MAINLOOP
for a = 1 to 255
I2Cwrite DPin,CPin,$90,a
i2cread DPin,CPin,$90,1,[c]
next
goto Mainloop

dhouston
- 5th May 2011, 01:30
Add code tags - select the text and then click on # above the edit window.

DEFINE osc 20
DEFINE LOADER_USED 1
INCLUDE "MODEDEFS.BAS"
DEFINE I2C_SLOUT
ANSEL =%00000001 ' Enable ADC channel-AN0
ANSELH =%0000
'ADCON0.7 = 1
SSPCON =%00101011
TRISB.4 =0
TRISB.6 =0
TRISA =%00001111
TRISC =%00000000
CPin VAR PORTB.6
DPin VAR PORTB.4
a var word
c var byte

MAINLOOP
for a = 1 to 255
I2Cwrite DPin,CPin,$90,a
i2cread DPin,CPin,$90,1,[c]
next
goto Mainloop

cncmachineguy
- 5th May 2011, 01:32
The easy to answer question is to put your code in the cool code box, you have to do this:
[ code ]

put code here

[ / code ]

but with no spaces

Still thinking on the real question

Amoque
- 5th May 2011, 14:01
I am not familiar with I2C, but I do notice that in your initial description you say the crystal is 8M, but in the code it is defined 20M...

The rest I do not know, but some thoughts...

Commenting the 2 I2C lines and recompile would give an indication of how much code is actually there (in I2C part)--and how that interface affects speed. The difference (between with no I2C and with I2C) should match pretty closely with datasheet transaction time for DAC chip?

tenaja
- 5th May 2011, 15:18
Using the software implementations of i2c read/write are DREADFULLY SLOW. Yes, I am not surprised you get such a slow loop.

There are several ways to dramatically speed it up.
1. use the hardware i2c--this will speed it up by about 30-40x at least.
2. put the addressing with STARTs, etc, outside the loop, and just clock the data sequentially. This will get you almost 4x improvement.
3. Use a repeat until or do while instead of a for-loop. The For-loop is a lot slower than the other loops when timing is important...but it won't help nearly as much as 1 or 2.
4. Send 256 bytes instead of 255...that way it can init at 0 and loop until 0 again, and that's often a one-instruction savings, depending on the pic.

aajgss
- 5th May 2011, 23:23
Thanks for the replies, and how to put the code in.

Amoque,
Thanks, a slip of the finger, I have now changed that with no change in speed.

Tenaja

I dont know the differerence between I2C hardware and I2C read write, can you enlighten me?

Addressing with STARTS etc outside the loop. A brief example would be great.

As you can no doubt tell this is all new to me.

There was a request from cncmachineguy for 'configs'.

I have a few books on programming PICs and they tell you 'to set it to this' or 'set it to that', but no real explanation of why you do it. Does anyoneone know of a book that has all the real basic info that the other books expect you to already know?

Any comment on the 9000+ lines of code assembled. does this seem resonable?

Thanks
aajgss

tenaja
- 6th May 2011, 15:14
Unfortunately your chosen PIC does not have hardware implementation of i2c Master. If you want to use h/w i2c, you'll have to change PICs.

For start/stop, etc, you have to read the datasheet of the slave. They usually show graphical timing charts that show when the individual pins must be driven high or low. For a typical eeprom, like the 24LC08b for instance, a START is this...
eeprom_start:
high data
high clock
low data

Now those are states, not commands. While you can use the command, it is not recommended for several reasons. First, i2c should have pullups, and you are supposed to turn the pin into an input to let it get pulled high. Second, you have to wait for the pin to get pulled high, and third, my preference is to avoid PBP's High and Low commands because they turn the pin into an output then set it high...and that takes too many instructions. I like to manually set the Tris register myself, and manually set the port pins--but that is personal preference.

...and eeprom_stop is this...
low data
high clock
high data

Ack is the slave acknowledging the data; usually it is pulling the data line low...
Eeprom_Ack:
repeat
until SDA_Pin = 0

After you get that worked out, you look at the byte write (for instance) timing chart. My eeprom d/s shows this timing...

Eeprom_Start
i2cout Control
Ack
i2cout Address
ack

LoopStart:
i2cout YourData
ack
If NotDoneSendingData goto LoopStart
Eeprom_Stop

If sticking with the s/w commands, you'd probably want to make your own i2c command, too. The ones built in to PBP are painfully slow because they are designed to be idiot-proof. You can see the timing charts of the d/s to learn when to pull the clock and data pins low.

It can be done and is easy for the experienced...but this is not a trivial undertaking for a newbie. TIming charts in datasheets may be initially intimidating, but once you get it it's simple.

aajgss
- 6th May 2011, 23:24
tenaja

Thanks for the great explanation

As far as the PIC goes, I have the following

18F14K50
16C745
12F675dsPIC 33FJ120P
16F84
16F676
24FJ64GA002
16F81930F2010
16HV785
If any of these pop out as ones you know would be suitable, please let me know, otherwise I will go searching.

It is not critical that I use the 16F690, its just that it is installed on the LPC demo board.

My project is (what I thought in the start would be simple - now I know otherwise:confused:) to save a sinewave in an EEprom and get a value and send it out to the DAC. Then a switch to change the speed of the loop to give 50 or 60 hertz.

Thanks
aajgss

tenaja
- 8th May 2011, 03:35
I don't have your list of PICs memorized, so you'll have to read the datasheets. Just search for "master" in the i2c section and make sure it is not implemented in firmware.

GIven your 50/60hz output, I'm guessing a dc/ac converter? Most dc/ac converters use a "modified sine wave" that only has two or three steps in each direction. You should be fine with a lookup table...you can probably squeeze in a decent curve in the same space the s/w i2c commands consume, and save the hassle of the eeprom.

aajgss
- 12th May 2011, 03:44
OK, Checked all my PIC's and find that the 18F14K50 is listed in the data sheet as having a Master Sync Serial Port.

I have tried the I2Cread and I2Cwrite with this and never got it to work. Could this be because it is hardware and not software?

I shall continue and report back at the next hurdle!!

Thanks
aajgss