HByte/LByte - falsches shift Ergebniss

OP (Firma: Hobby) #2319458
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?
OP (Firma: Hobby) #2319495
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
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren