CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to achieve faster communication

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Einly



Joined: 10 Sep 2003
Posts: 60

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger ICQ Number

How to achieve faster communication
PostPosted: Wed Jun 02, 2004 8:09 pm     Reply with quote

Dear all,

I am using pic16f876 to receive data from AD7716. Below is some of my command line:

byte chan[16];

for(i=0;i<128;i++)
{
output_high(SCLK);
delay_cycles(1);
output_low(SCLK);
shift_left(chan0,16,input(SDATA));
}

When I check with the oscilloscope, I notice that it takes me quite a lot of time to execute the whole for loop (more than 1ms). I notice that time taken for the command shift_left is the problem. May I know:

1) How can I check the time taken for each command, e.g. output_high, shift_left?

2) I would like to shift in 128 bits from the adc? How can I maximize the speed? Is there any other way so that I can shift in all the bits in a faster way? What is the fastest way to do this?

Thanks for your reply.


Einly
_________________
Einly
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

PostPosted: Wed Jun 02, 2004 9:14 pm     Reply with quote

Try about using hardware the SPI ?
You did not tell us the speed of you oscillator !
If you are running it at 20MHz the SPI can do as fast as 5MHz.....
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Wed Jun 02, 2004 10:39 pm     Reply with quote

How about something like this, it should be much faster:

Code:
byte bit_pattern={1,2,4,8,16,32,64,128};

byte i,j;

byte chan[16];

for(i=16;i>0;i--)
{
    chan[i-1]=0
   
    for(j=8;j>0;j--)
    {
        output_high(SCLK);
        delay_cycles(1);
        output_low(SCLK);
        if (input(SDATA))
            chan[i-1]|=bit_pattern[j-1];
    }
}
Ttelmah
Guest







Re: How to achieve faster communication
PostPosted: Thu Jun 03, 2004 2:36 am     Reply with quote

Einly wrote:
Dear all,

I am using pic16f876 to receive data from AD7716. Below is some of my command line:

byte chan[16];

for(i=0;i<128;i++)
{
output_high(SCLK);
delay_cycles(1);
output_low(SCLK);
shift_left(chan0,16,input(SDATA));
}

When I check with the oscilloscope, I notice that it takes me quite a lot of time to execute the whole for loop (more than 1ms). I notice that time taken for the command shift_left is the problem. May I know:

1) How can I check the time taken for each command, e.g. output_high, shift_left?

2) I would like to shift in 128 bits from the adc? How can I maximize the speed? Is there any other way so that I can shift in all the bits in a faster way? What is the fastest way to do this?

Thanks for your reply.


Einly

Really the best way to calculate the times, is to look at the assembler listing. You can though also use the MPLAB simulator to step through routines like this and find out what is going on.
Now you don't say the processor clock rate (critical lacking data!...). The time taken for the 'output_high', and 'output_low', will depend on what I/O mode is seected for the port. The fastest mode is to use #fast_io, set the pins as an output and an input respectively, and these instructions are then just simple single instructions on the processor. The shift will involve the shifting each byte in turn, and handling the carry. The loop itself, will involve the incrementing of the byte, subtracting 128, testing for carry, and jumping if required.
Your loop will be slightly faster, if you count down from 128 to 0, but the change will only be a couple of instructions. It is the operation of shifting 16 bytes for every incoming bit that is the 'killer'.
You will therefore get more speed with:
[code]
byte chan[16];
int8 * addr;
int i,j;

j=0;
addr=&char[j];
do {
for(i=8;i;i--)
{
output_high(SCLK);
delay_cycles(1);
output_low(SCLK);
shift_left(addr,1,input(SDATA));
}
addr++;
j++;
}
while (j<16);
[code]
I'd have to sit down and work out which way the counters need to be, to get the bits in the right order (the above is only a guess...).
The key points are that only one byte is being rotated inside the data transfer loop. The address changes in the outer loop, and the change is done by simply incrementing the pointer (which is more efficient than looking us the address of an array element each time).
Combine this with using fast_io, and I'd expect a total time perhaps 1/4 your current figure.
Seperately, it is worth realising, that the entire inner loop, can be replaced using the SPI hardware, which does exactly this operation, at a maximum rate just 1/4 the incoming processor clock.

Best Wishes
Einly



Joined: 10 Sep 2003
Posts: 60

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger ICQ Number

Do you have example code of how to do hardware spi?
PostPosted: Sun Jun 06, 2004 9:31 pm     Reply with quote

Dear all,

Thanks for your reply. I am using 20MHz processor clock. I would like to know can anyone give me some lines of how to do hardware spi?
_________________
Einly
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Sun Jun 06, 2004 10:31 pm     Reply with quote

Take a look at the file ex_spi.c in the \Examples directory.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group