In my Arduino project I get some char variables into an array from a serial connection (Xbee modul).
The recieving of the variables works but later I have to convert two variables of the array into one uint16_t variable because a PWM driver is connected and it has a transfer parameter what is composed of the two chars. This works with values from 0x0100 to 0x017F but if the highest Bit of the lower Byte is set, for example 0x0180 then I get 0xFF80. I do not understand why it does not work.
- initializing of the variables
char input[10] = {0}; //puffer für die Daten
char current; //puffer für aktuelles Zeichen
int incount = 0; //Index für Puffer
bool transfin = false; //Bool zum setzen ob Transfer erfolgreich war
2.recieve the data from XBee modul
if ((XBee.available() > 3))
{
while ((incount < 4) & (!transfin))
{
current = XBee.read(); //aktuelles Zeichen einlesen
if (current != 88)
{
//schreiben der Daten in den Puffer
input[incount] = current;
incount++;
//Serial.write("In Puffer schreiben");
}
else
{
// puffer mit NULL Zeichen abschließen, damit das Ende der Zeichenkette
// durch string Operationen erkannt wird
input[incount] = '\0';
incount++;
transfin = true;
Serial.write("Ende Uebertragung\n");
}
}
3.send data to PWM driver
terg= input[2] | uint16_t(input[1]) << 8 ;
for(int x=0;x<34;x+=2)
{
if(motor[x] == input[1])
{
pwm.setPWM(motor[x+1],0,terg);
}
}
With the array motor I choose the correct channel which shall be set.
Thanks for your help.