system
May 11, 2011, 10:13pm
1
Salve, ho una funzione
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )
che scrive su eprom esterna.
se la lancio facendo :
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 :
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*)
Grag38
May 11, 2011, 10:26pm
2
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 :
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.
ok grazie della risposta, ma è così macchinosa la cosa?
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
perchè così non va?
C'è qualche cosa di più facile per sostituire '3' con x=3?
ferdinando:
int x =3;
byte b = byte(x);
i2c_eeprom_write_byte( 0x50, 0, x );
mica lo scrive 3!!
prova a scrivere
int x =3;
i2c_eeprom_write_byte( 0x50, 0, (byte)x );
[/quote]
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???????
#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);
}
Tu fai :
Serial.println(i2c_eeprom_read_byte(0x50, 0));
ma prova a cambiarlo in
Serial.println(i2c_eeprom_read_byte(0x50, 0),DEC);
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
!!
Dicono che sbagliando si impara