Forum: Compiler & IDEs HByte/LByte - falsches shift Ergebniss


von Max D. (Firma: Hobby) (fmhweb)


Lesenswert?

Moin,

wie üblich möchte ich das HByte nach links shiften und mit dem LByte 
addieren.

Ich lese einen SRF02 aus:
1
uint16_t getRange(void){
2
  uint16_t x;
3
  x = i2cRead(SRF02,2);          // x = 1
4
  lcd_string_Int(x);             // Ausgabe = 1
5
  lcd_string(" / ");
6
  x = (i2cRead(SRF02,2) << 8);   // x = -27648   ???
7
  lcd_string_Int(x);             // Ausgabe = -27648
8
  x += i2cRead(SRF02,3);
9
  return(x);
10
}

Ich kann einfach nicht nachvollziehen warum ich beim zweiten mal mit 
shiften den Wert -27648 bekomme, besonders weil alles unsigned ist. Es 
sollte doch 256 sein. Die Funktion i2cRead übergibt den Wert als 
uint8_t.
1
void lcd_string_Int(uint16_t tmp_int){
2
  unsigned char buffer[20];
3
  itoa(tmp_int, buffer, 10);
4
  lcd_string(buffer);
5
}

PS: Gerade hatte ich es, dass im HByte Register eine 0 stand. Trotzdem 
hat er beim shiften -27648 daraus gemacht. Warum passiert das?

von (prx) A. K. (prx)


Lesenswert?

itoa geht von "int" aus. utoa verwenden.

von Max D. (Firma: Hobby) (fmhweb)


Lesenswert?

Danke. Da ich ja ausschließlich unsigned Variabel benutze, habe ich es 
jetzt auf utoa geändert. Jetzt bekomme ich den Wert 37888.

von (prx) A. K. (prx)


Lesenswert?

Also 0x9400, d.h es wurde 0x94 von I2C gelesen. Ob das nun passt oder 
nicht kannst nur du wissen.

von Max D. (Firma: Hobby) (fmhweb)


Lesenswert?

Es wurden in zwei Durchgängen 0x01 und 0x00 im HByte Register gelesen. 
Erst nach dem Bit-Shift änderte sich das Ergebniss auf diese hohe Zahl.

37888 = (i2cRead(SRF02,2) << 8);
37888 = (1 << 8);

von Max D. (Firma: Hobby) (fmhweb)


Lesenswert?

Wenn ich sowas mache klappt es:
1
uint16_t x;
2
x = (1 << 8);
3
lcd_string_Int(x);

Irgendwie muss beim return Wert was nicht stimmen und ich kann es 
eionfach nicht erkennen. Funktion i2cRead:
1
unsigned char i2cRead(char address, char reg){
2
   uint16_t read_data = 0;
3
4
   TWCR = 0xA4;                                                  // send a start bit on i2c bus
5
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit  
6
   TWDR = address;                                               // load address of i2c device
7
   TWCR = 0x84;                                                  // transmit 
8
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit
9
   TWDR = reg;                                                   // send register number to read from
10
   TWCR = 0x84;                                                  // transmit
11
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit
12
13
   TWCR = 0xA4;                                                  // send repeated start bit
14
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit 
15
   TWDR = address+1;                                             // transmit address of i2c device with readbit set
16
   TWCR = 0xC4;                                                  // clear transmit interupt flag
17
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit
18
   TWCR = 0x84;                                                  // transmit, nack (last byte request)
19
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit 
20
   read_data = TWDR;                                             // and grab the target data
21
   TWCR = 0x94;                                                  // send a stop bit on i2c bus
22
   return read_data;
23
24
}

von Simon K. (simon) Benutzerseite


Lesenswert?

unsigned char als Rückgabetyp kann keinen 16 Bit Integer aufnehmen.

von Max D. (Firma: Hobby) (fmhweb)


Lesenswert?

Oh nee, oh nee. Wie unangenehm. Das war es, ich habe es nicht erkannt.
Das hat jetzt auch einen zweiten Beitrag von mir beantwortet.

Vielen Dank.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.