> void write_data (Word towrite, Byte nbits) > { > Byte n; > > for(n = 0; n < nbits; n++) > { > > CLK_HIGH; > if( towrite & (0x0001 << n)) > { > SDIO_HIGH; > } > else > { > SDIO_LOW; > } > CLK_LOW; > > } > }
This will give very slow code, because a left shift by a variable will allways take lots of cycles. Even with optimisation on this will give slow code. This should work better: void write_data (Word towrite, Byte nbits) { Byte n; for(n = 0; n < nbits; n++) { CLK_HIGH; if( towrite & 0x001) { SDIO_HIGH; } else { SDIO_LOW; } CLK_LOW; towrite >>= 1; } } Now always one shift is done with each cycle of the loop. You should look at the dissassembler listing when you get this sort of problem. Then you can see, what the compiler does, and optimize your C code, so the compiler can generate better assembly for you. I also suggest, you allways turn optimization on! This works very well with gcc, I always use -Os, and haven't had any broken code problems so far. Regards, Peter _______________________________________________ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list