Pages: [1]   Go Down
Author Topic: Convertire int to byte e non funziona il byte() cast  (Read 455 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Salve, ho una funzione

Code:
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )

che scrive su eprom esterna.
se la lancio facendo :
Code:
i2c_eeprom_write_byte( 0x50, 0, '3'  );
   delay(10);
   i2c_eeprom_write_byte( 0x50, 1, '2' );
   delay(10);

e poi leggo i byte scritti, funziona, e mi stampa prima tre poi 2.

e a tale funzione gli volgli dare in pasto una variabile e faccio :
Code:
    int x =3;
    byte b = byte(x);
   
   i2c_eeprom_write_byte( 0x50, 0, x  );

mica lo scrive 3!!

come mai? come faccio a convertire una variabile in byte??? (non byte*)
Logged

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its not a real conversion thet you must use, just a cast.

int x = 3;
byte b = (byte) x;

the '()' are around the type you want to cast, not like you did : byte(x) which the compiler understand as a function.

It could works if you wrote :

Code:

byte Byte(int x)
{
  return (byte) x;
}

void setup()
{
  int x = 3;
  int b = Byte(x);
}

void loop()
{
}

But notice that byte is different from Byte because of uppercase. This is the only other way in my own opinion.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok grazie della risposta, ma è così macchinosa la cosa?

http://arduino.cc/en/Reference/ByteCast

perchè così non va?

C'è qualche cosa di più facile per sostituire   '3'  con x=3?
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 453
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    int x =3;
    byte b = byte(x);
   
   i2c_eeprom_write_byte( 0x50, 0, x  );

mica lo scrive 3!!

prova a scrivere

Code:
   
    int x =3;  
   i2c_eeprom_write_byte( 0x50, 0, (byte)x  );
[/quote]
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, non funzione, magari sbaglio altro non lo so....

cmq posto tutto il codice di porva per scrittura e lettura byte da eeprom esterna.

Ho utilizzato anche altre funzioni come la write_page che riceve byte* che con itoa converto int in string è funziona alla grande.

qui invce non riesco a farmi ristampare la variabile, quindi se non me la stampa credo che neanche la salva.

e pure che mi sembra talmente banale...
Sul reference di byte()  c'è scritto che byte(x) riceve qualsiasi dato e restituisce byte. Perchè non va???????
Code:

  #include <Wire.h> //I2C library



  void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
    int rdata = data;
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.send(rdata);
    Wire.endTransmission();
  }

  void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddresspage >> 8)); // MSB
    Wire.send((int)(eeaddresspage & 0xFF)); // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.send(data[c]);
    Wire.endTransmission();
  }

  byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.receive();
    return rdata;
  }

  void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,length);
    int c = 0;
    for ( c = 0; c < length; c++ )
      if (Wire.available()) buffer[c] = Wire.receive();
  }




  void setup()
  {
   
    Wire.begin(); // initialise the connection
    Serial.begin(9600);   
   

    int x =3;
   
   
   i2c_eeprom_write_byte( 0x50, 0, (byte)x  );
   delay(10);
   i2c_eeprom_write_byte( 0x50, 1, '2' );
   delay(10);
   i2c_eeprom_write_byte( 0x50, 2, '1' );
   delay(10);
   i2c_eeprom_write_byte( 0x50, 3, '0' );   
   delay(10); 
   Serial.println("Memory written"); 
   delay(10);
  }

  void loop()
  {

    Serial.println(i2c_eeprom_read_byte(0x50, 0));     
    Serial.println(" ");
    delay(1000);
   
    Serial.println(i2c_eeprom_read_byte(0x50, 1));     
    Serial.println(" ");
    delay(1000);
   
    Serial.println(i2c_eeprom_read_byte(0x50, 2));     
    Serial.println(" ");
    delay(1000);
   
    Serial.println(i2c_eeprom_read_byte(0x50, 3));     
    Serial.println(" ");
    delay(1000);   

  }
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 453
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Tu fai :
Serial.println(i2c_eeprom_read_byte(0x50, 0));     
ma prova a cambiarlo in
Serial.println(i2c_eeprom_read_byte(0x50, 0),DEC);     
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

GRANDEEEEE!!

HO postato tutto il codice perchè mi era venuto il dubbio che quello che non andava era la stampa!!!
grazie, ma faccio ancora errori banali di programmazione
!!
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 453
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Dicono che sbagliando si impara  smiley-wink
Logged

Pages: [1]   Go Up
Jump to: