Autor Tema: Como almacenar una variable float en la eeprom del Pic  (Leído 2417 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Medusa

  • PIC18
  • ****
  • Mensajes: 252
Como almacenar una variable float en la eeprom del Pic
« en: 25 de Septiembre de 2009, 19:13:06 »
Gracias migsantiago...funciona    :-/  :-/
« Última modificación: 28 de Septiembre de 2009, 13:56:11 por Medusa »

Desconectado migsantiago

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8257
    • Sitio de MigSantiago
Re: Como almacenar una variable float en la eeprom del Pic
« Respuesta #1 en: 25 de Septiembre de 2009, 19:59:38 »
Directo de la ayuda de CCS, solo modifícala para eeprom interna:

Citar
How do I write variables to EEPROM that are not a byte?

--------------------------------------------------------------------------------

 

The following is an example of how to read and write a floating point number from/to EEPROM.  The same concept may be used for structures, arrays or any other type.

 

n is an offset into the EEPROM.  

For floats you must increment it by 4.  

For example, if the first float is at 0, the second one should be at 4, and the third at 8.

 

WRITE_FLOAT_EXT_EEPROM(long int n, float data) {

   int i;

   for (i = 0; i < 4; i++)

     write_ext_eeprom(i + n, *(((int8*)&data + i) ) ;

}

 

float READ_FLOAT_EXT_EEPROM(long int n) {

   int i;

   float data;

   for (i = 0; i < 4; i++)

     *(((int8*)&data + i) = read_ext_eeprom(i + n);

   return(data);

}


Desconectado Medusa

  • PIC18
  • ****
  • Mensajes: 252
Re: Como almacenar una variable float en la eeprom del Pic
« Respuesta #2 en: 28 de Septiembre de 2009, 11:46:21 »
Hola, he intentado usar la sugerencia de migsantiago, pero no logro escribir ni leer correctamente la variable float en la eeporm, cuando a la entrada del ADC0 hay 5V, el valor de la variable debe de ser 13.45, que es el valor que necesito almacenar en la eeprom.

Citar
#include <18F452.h>
#DEVICE ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar

int j;
float data;

void Write_Float_Eeprom(address, float data)
{
   int8 i;
   for(i = 0; i < 16; ++i){
     write_eeprom(address + i, *((int8 *)(&data) + i));
   }
}

float Read_Float_Eeprom( address)
{
   int8 i;
   float data;
   for(i = 0; i < 16; ++i)
   {
     *((int8 *)(&data) + i) = read_eeprom(address + i);
   }
   return data;
}


void Init_EEprom(){
   for(j = 0; j < 16; ++j){
        write_eeprom(j,0xff);
   }
}

void main() {

   int32 Buffer_ADC0;
   float Valor_ADC0;   

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_32);
   set_adc_channel(0);
   delay_us(10);

   Init_EEprom();
   
   set_adc_channel(0);
   delay_us(4);
   Buffer_ADC0 = read_adc();
   delay_us(10);
   Valor_ADC0 =  (float)Buffer_ADC0 *(0.0529);
   printf("%4.2f \r\n", Valor_ADC0);

   Write_Float_Eeprom(0,Valor_ADC0);
   delay_us(200);
   Read_Float_Eeprom(0);
   printf("%4.2f \r\n", data);


while (TRUE){};   
  }

Desconectado migsantiago

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8257
    • Sitio de MigSantiago
Re: Como almacenar una variable float en la eeprom del Pic
« Respuesta #3 en: 28 de Septiembre de 2009, 19:39:19 »
No sirve porque no estás recibiendo lo que read_eeprom lee:

Código: [Seleccionar]
  Write_Float_Eeprom(0,Valor_ADC0);
   delay_us(200);
   Read_Float_Eeprom(0); //ponle data =
   printf("%4.2f \r\n", data);