Orcino
Joined: 07 Sep 2003 Posts: 56
|
FLOAT / 24LC256 |
Posted: Sun Oct 15, 2006 7:10 pm |
|
|
Hi, this a lsmall code for test a var FLOAT with the 24LC256. I write
19.508016 and read 19.508018 Somebody can help me ?
#include <18F452.h>
#fuses HS,NOWDT,NOPUT,NOLVP,NOBROWNOUT,NOCPD,NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <24256>
float lat1=19.508016;
WRITE_FLOAT_EXT_EEPROM(long int n, float data);
float READ_FLOAT_EXT_EEPROM(long int n);
float leitura;
long int end=20;
INT8 CONT=0;
//***********************************************************
// Fun��o principal
//***********************************************************
void main()
{
init_ext_eeprom();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
// setup_comparator(NC_NC_NC_NC);
// setup_vref(VREF_LOW|-2);
// setup_low_volt_detect(FALSE);
// setup_oscillator(False);
ext_int_edge(2,H_TO_L);
// enable_interrupts(INT_EXT2);
// enable_interrupts(GLOBAL);
while (1)
{
IF(CONT < 7 )
{
WRITE_FLOAT_EXT_EEPROM(end,lat1);
delay_ms(500);
leitura = READ_FLOAT_EXT_EEPROM(end);
printf("Dado e %f\n\r",leitura);
CONT++;
}
}
}
//***********************************************************
//***********************************************************
WRITE_FLOAT_EXT_EEPROM(long int n, float data)
{
int i;
for (i = 0; i < 4; i++)
write_ext_eeprom(i + n, *(&data + i) ) ;
}
float READ_FLOAT_EXT_EEPROM(long int n)
{
int i;
float data;
for (i = 0; i < 4; i++)
*(&data + i) = read_ext_eeprom(i + n);
return(data);
}
//***********************************************************
Thanks
Orcino |
|
hfelton
Joined: 07 Sep 2006 Posts: 2
|
Re: FLOAT / 24LC256 |
Posted: Mon Oct 16, 2006 12:13 am |
|
|
Orcino wrote: | Hi, this a lsmall code for test a var FLOAT with the 24LC256. I write
19.508016 and read 19.508018 Somebody can help me ?
|
yes - floats only have around 6-digits of precision. look up and/or think about how this data is stored.
also, please use code-tags around any code-posting you will do in the future. |
|