View previous topic :: View next topic |
Author |
Message |
janiga
Joined: 10 Apr 2006 Posts: 22
|
Storing number problem |
Posted: Tue May 30, 2006 9:05 am |
|
|
Hello,
I am trying to store and read an incrementing number that will eventually reach into the 100s of thousands. This is for a cycle counter for a machine. I am using the write and read eeprom but when i try to write or read for instance 100000 it comes back as 34464.
Can someone help with proper method for writing and reading large numbers.
Thanks in advance!
Code: |
#include "C:\CompilerFiles\PICC Compiler\TitanWLCD\PIC16F917TitanVersion1.h"
#include <input.c>
#include <LCD.C>
#include <string.h>
#include <stdio.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC //Internal RC Osc
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES MCLR //Master Clear pin enabled
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //Brownout reset
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=4000000)
#int_timer0
unsigned long data2;
void WRITE_FLOAT_EXT_EEPROM(int n, unsigned long data)/////////////////////////////////////////////
{
int i;
for (i = 0; i < 4; i++)
write_eeprom(i + n, *(((int *)&data) + i) ) ;
}
unsigned long READ_FLOAT_EXT_EEPROM(int n)
{
for (i = 0; i < 4; i++)
*(&data2 + i) = read_eeprom(i + n);
return(data2);
}
WRITE_FLOAT_EXT_EEPROM(00,100000);
data2 = READ_FLOAT_EXT_EEPROM(00);
|
Last edited by janiga on Tue May 30, 2006 9:20 am; edited 1 time in total |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Tue May 30, 2006 9:16 am |
|
|
Why are you using a float? unsigned 32-bit integer may be more efficient.
Use the byte-wise read and write functions along with the make8() and make32() functions. Also keep in mind that the EEPROM has a limited life time. You may need to devise a scheme for rolling through locations to maximize use. Or consider an external FRAM or battery backed SRAM.
Next, looking at your code, check on how an "unsigned long" is defined by the compiler, it may only be 16 bits
And I don't find a function called "write_float_ext_eeprom" or its companion "read_float_ext_eeprom" in the list of built-in functions (haven't updated the compiler in a while though). Is this something you wrote yourself or borrowed from driver or example code? If so, are you sure it is working properly? Always check the LST file to see exactly what the PIC is trying to do. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Guest
|
Updated |
Posted: Tue May 30, 2006 9:32 am |
|
|
Code updated with functions that are built in with CCS. |
|
|
janiga
Joined: 10 Apr 2006 Posts: 22
|
Reply |
Posted: Tue May 30, 2006 9:37 am |
|
|
Code works fine with any number up to 10000, i havent tryied to get an exact count of where to error begins but with 10k its fine however when i tried 100000 and higher it did not work. |
|
|
Fro_Guy
Joined: 26 Apr 2006 Posts: 12
|
|
Posted: Tue May 30, 2006 9:57 am |
|
|
Make you varable a int32. long is 16 bit in the compiler, so your overflowing your varrable
Cheers |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 31, 2006 2:49 am |
|
|
As mentioned by others, your 16-bit long variable is overflowing after 65535.
Beware that eeprom can only be written a limited number of times, it kind of 'wears out' (note that reading is no problem). From Chapter 19.4 of the PIC16F917 datasheet: Quote: | Data EEPROM Memory
Byte Endurance (-40°C ≤ TA ≤ +85°C): minimum 100K, typical 1M Erase/Writes
Byte Endurance (+85°C ≤ TA ≤ +125°C): minimum 10K, typical 100K Erase/Writes |
Another good thing is to take meassures against data corruption when power is switched of during the write to eeprom.
Depending on your design and requirements you can consider several options:
- General: only update the bytes that have changed.
- Don't use eeprom but some other backup medium like FRAM. Some real time clock chips have a few spare bytes of battery backed RAM you can use.
- Only write the counter at power down. Add hardware to detect power failure and use the remaining capacitance in your power supply to get a few miliseconds for storing your counter.
- Reconsider how accurate your counter must be. For example you can write once for every 10 updates, if power is switched of rarely you will loose a small number of counts.
- Design a rotation scheme for using different memory locations. Instead of repeatedly writing to the same address you can design a scheme where the counter is written to another address every time.
Some usefull threads:
Internal eeprom life problem ! * HELP *
Write then Read of data eeprom is no proof of saved data
OT: SuperCap for SD memory 'loss of power' write completion |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Storing number problem |
Posted: Wed May 31, 2006 1:10 pm |
|
|
janiga wrote: |
Code: |
void WRITE_FLOAT_EXT_EEPROM(int n, unsigned long data)/////////////////////////////////////////////
{
int i;
n*=4;
for (i = 0; i < 4; i++)
write_eeprom(i + n, *(((int *)&data) + i) ) ;
}
unsigned long READ_FLOAT_EXT_EEPROM(int n)
{
n*=4;
for (i = 0; i < 4; i++)
*(&data2 + i) = read_eeprom(i + n);
return(data2);
}
|
|
It takes 4 bytes to store a float |
|
|
|