Mein Code der unteranderem eine BUS-Verbindung eines Masters und mehreren Slaves beinhaltet funktioniert soweit. Jetzt wollte ich noch etwas optimieren und habe zwei Unterprogramme von dennen ich nicht weiß wie man es besser machen könnte.

Beim ersten wird ein 32Bit Wert in einzelne Bits zerlegt und dann seriell gesendet. Dies macht der Master....

Code:
void zerlegen (unsigned long int wert)
{
   char bits01 , bits02 , bits03 , bits04 , bits05 , bits06 , bits07 , bits08 , bits09 , bits10;
   char bits11 , bits12 , bits13 , bits14 , bits15 , bits16 , bits17 , bits18 , bits19 , bits20;
   char bits21 , bits22 , bits23 , bits24 , bits25 , bits26 , bits27 , bits28 , bits29 , bits30;
   char bits31 , bits32 ;

   bits01 = bits02 = bits03 = bits04 = bits05 = bits06 = bits07 = bits08 = bits09 = bits10 = 0x00;
   bits11 = bits12 = bits13 = bits14 = bits15 = bits16 = bits17 = bits18 = bits19 = bits20 = 0x00;
   bits21 = bits22 = bits23 = bits24 = bits25 = bits26 = bits27 = bits28 = bits29 = bits30 = 0x00;
   bits31 = bits32 = 0x00;

   if ((wert & 0x80000000) == 0x80000000) bits01 = 1;
   if ((wert & 0x40000000) == 0x40000000) bits02 = 1;
   if ((wert & 0x20000000) == 0x20000000) bits03 = 1;
   if ((wert & 0x10000000) == 0x10000000) bits04 = 1;
   if ((wert & 0x08000000) == 0x08000000) bits05 = 1;
   if ((wert & 0x04000000) == 0x04000000) bits06 = 1;
   if ((wert & 0x02000000) == 0x02000000) bits07 = 1;
   if ((wert & 0x01000000) == 0x01000000) bits08 = 1;
   if ((wert & 0x00800000) == 0x00800000) bits09 = 1;
   if ((wert & 0x00400000) == 0x00400000) bits10 = 1;
   if ((wert & 0x00200000) == 0x00200000) bits11 = 1;
   if ((wert & 0x00100000) == 0x00100000) bits12 = 1;
   if ((wert & 0x00080000) == 0x00080000) bits13 = 1;
   if ((wert & 0x00040000) == 0x00040000) bits14 = 1;
   if ((wert & 0x00020000) == 0x00020000) bits15 = 1;
   if ((wert & 0x00010000) == 0x00010000) bits16 = 1;
   if ((wert & 0x00008000) == 0x00008000) bits17 = 1;
   if ((wert & 0x00004000) == 0x00004000) bits18 = 1;
   if ((wert & 0x00002000) == 0x00002000) bits19 = 1;
   if ((wert & 0x00001000) == 0x00001000) bits20 = 1;
   if ((wert & 0x00000800) == 0x00000800) bits21 = 1;
   if ((wert & 0x00000400) == 0x00000400) bits22 = 1;
   if ((wert & 0x00000200) == 0x00000200) bits23 = 1;
   if ((wert & 0x00000100) == 0x00000100) bits24 = 1;
   if ((wert & 0x00000080) == 0x00000080) bits25 = 1;
   if ((wert & 0x00000040) == 0x00000040) bits26 = 1;
   if ((wert & 0x00000020) == 0x00000020) bits27 = 1;
   if ((wert & 0x00000010) == 0x00000010) bits28 = 1;
   if ((wert & 0x00000008) == 0x00000008) bits29 = 1;
   if ((wert & 0x00000004) == 0x00000004) bits30 = 1;
   if ((wert & 0x00000002) == 0x00000002) bits31 = 1;
   if ((wert & 0x00000001) == 0x00000001) bits32 = 1;


   senden(bits01);
   senden(bits02);
   senden(bits03);
   senden(bits04);
   senden(bits05);
   senden(bits06);
   senden(bits07);
   senden(bits08);
   senden(bits09);
   senden(bits10);
   senden(bits11);
   senden(bits12);
   senden(bits13);
   senden(bits14);
   senden(bits15);
   senden(bits16);
   senden(bits17);
   senden(bits18);
   senden(bits19);
   senden(bits20);
   senden(bits21);
   senden(bits22);
   senden(bits23);
   senden(bits24);
   senden(bits25);
   senden(bits26);
   senden(bits27);
   senden(bits28);
   senden(bits29);
   senden(bits30);
   senden(bits31);
   senden(bits32);

}
Nachdem der Slave beim Empfangen den Wert wieder zusammengezogen hat möchte ich zwei Werte haben. action ---> ist der Befehl und empf ---> ist der Wert dazu. Ich habe den Wert dazu zerlegt und dann neu errechnet.

Code:
void zerlegenempf (unsigned long int wert)
{

char bits01 , bits02 , bits03 , bits04 , bits05 , bits06 , bits07 , bits08 , bits09 , bits10;
char bits11 , bits12 , bits13 , bits14 , bits15 , bits16 , bits17 , bits18 , bits19 , bits20;
char bits21 , bits22 , bits23 , bits24 , bits25 , bits26 , bits27 , bits28 , bits29 , bits30;
char bits31 , bits32 ;

 bits01 = bits02 = bits03 = bits04 = bits05 = bits06 = bits07 = bits08 = bits09 = bits10 = 0x00;
 bits11 = bits12 = bits13 = bits14 = bits15 = bits16 = bits17 = bits18 = bits19 = bits20 = 0x00;
 bits21 = bits22 = bits23 = bits24 = bits25 = bits26 = bits27 = bits28 = bits29 = bits30 = 0x00;
 bits31 = bits32 = 0x00;



	if ((wert & 0x80000000) == 0x80000000) bits01 = 1;
	if ((wert & 0x40000000) == 0x40000000) bits02 = 1;
	if ((wert & 0x20000000) == 0x20000000) bits03 = 1;
	if ((wert & 0x10000000) == 0x10000000) bits04 = 1;
	if ((wert & 0x08000000) == 0x08000000) bits05 = 1;
	if ((wert & 0x04000000) == 0x04000000) bits06 = 1;
	if ((wert & 0x02000000) == 0x02000000) bits07 = 1;
	if ((wert & 0x01000000) == 0x01000000) bits08 = 1;
	if ((wert & 0x00800000) == 0x00800000) bits09 = 1;
	if ((wert & 0x00400000) == 0x00400000) bits10 = 1;
	if ((wert & 0x00200000) == 0x00200000) bits11 = 1;
	if ((wert & 0x00100000) == 0x00100000) bits12 = 1;
	if ((wert & 0x00080000) == 0x00080000) bits13 = 1;
	if ((wert & 0x00040000) == 0x00040000) bits14 = 1;
	if ((wert & 0x00020000) == 0x00020000) bits15 = 1;
	if ((wert & 0x00010000) == 0x00010000) bits16 = 1;
	if ((wert & 0x00008000) == 0x00008000) bits17 = 1;
	if ((wert & 0x00004000) == 0x00004000) bits18 = 1;
	if ((wert & 0x00002000) == 0x00002000) bits19 = 1;
	if ((wert & 0x00001000) == 0x00001000) bits20 = 1;
	if ((wert & 0x00000800) == 0x00000800) bits21 = 1;
	if ((wert & 0x00000400) == 0x00000400) bits22 = 1;
	if ((wert & 0x00000200) == 0x00000200) bits23 = 1;
	if ((wert & 0x00000100) == 0x00000100) bits24 = 1;
	if ((wert & 0x00000080) == 0x00000080) bits25 = 1;
	if ((wert & 0x00000040) == 0x00000040) bits26 = 1;
	if ((wert & 0x00000020) == 0x00000020) bits27 = 1;
	if ((wert & 0x00000010) == 0x00000010) bits28 = 1;
	if ((wert & 0x00000008) == 0x00000008) bits29 = 1;
	if ((wert & 0x00000004) == 0x00000004) bits30 = 1;
	if ((wert & 0x00000002) == 0x00000002) bits31 = 1;
	if ((wert & 0x00000001) == 0x00000001) bits32 = 1;

	aktion = (bits01 * 0x80) + (bits02 * 0x40) + (bits03 * 0x20) + (bits04 * 0x10) + (bits05 * 0x08) + (bits06 * 0x04) + (bits07 * 0x02) + (bits08 * 0x01);
	
	empf = (bits09 * 0x800000) + (bits10 * 0x400000) + (bits11 * 0x200000) + (bits12 * 0x100000) + (bits13 * 0x080000) + (bits14 * 0x040000) + (bits15 * 0x020000) + (bits16 * 0x010000);
	empf = empf + (bits17 * 0x8000) + (bits18 * 0x4000) + (bits19 * 0x2000) + (bits20 * 0x1000) + (bits21 * 0x0800) + (bits22 * 0x0400) + (bits23 * 0x0200) + (bits24 * 0x0100);
	empf = empf + (bits25 * 0x80) + (bits26 * 0x40) + (bits27 * 0x20) + (bits28 * 0x10) + (bits29 * 0x08) + (bits30 * 0x04) + (bits31 * 0x02) + (bits32 * 0x01);

}
Das muß doch einfacher gehen. Bestimmt kein Problem für Experten. Vielen Dank im Voraus.



Edit: Ich hab dir da Code-Tags reingegeben, sieht imho besser aus als bold (PicNick, Mod)