Need help speeding up code

Hi.
I’m corking on some code, but my problem is that the code is probably slow and it should be posible to speed it up a little. The problem is that my programming skills are rusty :-[

The code works that i read 8 pins, crate a byte out of it and writes it to a byte array.

Code:


        static byte[] singleline = new byte[128];
        static int teller = 0;

        static InputPort d0;
        static InputPort d1;
        static InputPort d2;
        static InputPort d3;
        static InputPort d4;
        static InputPort d5;
        static InputPort d6;
        static InputPort d7;

            // init d0-d7
            d0 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO51, false, Port.ResistorMode.PullDown);
            d1 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO52, false, Port.ResistorMode.PullDown);
            d2 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO53, false, Port.ResistorMode.PullDown);
            d3 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO54, false, Port.ResistorMode.PullDown);
            d4 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO55, false, Port.ResistorMode.PullDown);
            d5 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO56, false, Port.ResistorMode.PullDown);
            d6 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO57, false, Port.ResistorMode.PullDown);
            d7 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.IO58, false, Port.ResistorMode.PullDown);

//Slow code longer down in my program..........

            byte retbyte = 0;
            if (d0.Read()) retbyte += 1;
            if (d1.Read()) retbyte += 2;
            if (d2.Read()) retbyte += 4;
            if (d3.Read()) retbyte += 8;
            if (d4.Read()) retbyte += 16;
            if (d5.Read()) retbyte += 32;
            if (d6.Read()) retbyte += 64;
            if (d7.Read()) retbyte += 128;
            singleline[teller] = retbyte;
            teller++;

Is there a easy fix to this? i could also use HEX if that makes it easier.

/offtopic/
If it only could have port manipulation like arduino got, then i could read all 8 bits in one go.

We have added parallel port support (8bit) to next release coming in couple weeks. Using out new support will make your code runs 50 times faster or more! So yes, it is very fast :slight_smile:

What are you connecting to the 8-bits?

You can use Register class and read multiple pins at once, but it is an advanced technique (see documentation). We might get it on a more higher level, if GHI will include ParallelPort in their new update.

P.S. Gus you are fast! Great I was wondering if it is going to be there or not.

Have you tried:

singleline[teller] = d0.Read()|d1.Read()<<1|d2.Read()<<2|d3.Read()<<3|d4.Read()<<4|d5.Read()<<5|d6.Read()<<6|d7.Read()<<7;

In plain C its faster I dont know in managed code. Actually I dont even know if C# support bit operations at all.

Read() is a boolean - will not work as is. But

d0.Read() ? 1 : 0 

will work.

Wow… fast reply to day :slight_smile:

Gus: I’m hooked up betwene another mcu and a 8 bit lcd, trying to sniff some data. I know this chip is not ment to be a logic analycer but the speed on the lcd i slow so it may work.

Rest:
Going to try some of you’r solutions out, hope it works :slight_smile:

Tanks!

This code worked:

       singleline[teller] = (byte)((d0.Read() ? 1 : 0) | (d1.Read() ? 1 : 0 << 1) | (d2.Read() ? 1 : 0 << 2) | (d3.Read() ? 1 : 0 << 3) |(d4.Read() ? 1 : 0 << 4) | (d5.Read() ? 1 : 0 << 5) |(d6.Read() ? 1 : 0 << 6) | (d7.Read() ? 1 : 0 << 7));

But stil no satisfaction so i’l try to dig me down in the registers, wish me good luck :wink:

The ? 0: 1 is the same as the if version so it wont speed up.

What about,

((byte)d1.Read())<<1 and so on ?

Pablo:
So close but yet so far, it wont work I get an error that states “Cannot convert from ‘bool’ to ‘byte’”.

i’ll keep trying…

Assembling the byte is not a big issue here. Reading every bit using input port is slow. As Gus said ParallelPort in the coming update will help a lot.

You say your code is probably too slow… have you actually tried it? What kind of rate did you get.

The code below might be a little faster that an add.

byte retbyte = 0;
            if (d0.Read()) retbyte |= 1;
            if (d1.Read()) retbyte |= 2;
            if (d2.Read()) retbyte |= 4;
            if (d3.Read()) retbyte |= 8;
            if (d4.Read()) retbyte |= 16;
            if (d5.Read()) retbyte |= 32;
            if (d6.Read()) retbyte |= 64;
            if (d7.Read()) retbyte |= 128;

hey architect – can you point me to the register class documentation? thanks.

Here is the SDK class information:

http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/html/be700fb0-683f-e830-99f8-bc6dfad09569.htm

You will also need user guide for the microcontroller itself. User guide lists all the addresses for different registers.

See this thread:

http://www.tinyclr.com/forum/2/1779/

If you want to use register class just to speed up the parallel interface then do not waste your time :wink: We have a mush faster implementation coming.

Register class is very powerful for things like this one microframeworkprojects.com