0 Usuarios y 1 Visitante están viendo este tema.
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);}
#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ándarint 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){}; }
Write_Float_Eeprom(0,Valor_ADC0); delay_us(200); Read_Float_Eeprom(0); //ponle data = printf("%4.2f \r\n", data);