View previous topic :: View next topic |
Author |
Message |
chefgage
Joined: 20 Mar 2012 Posts: 2
|
PIC18f4520 - writting to eeprom address |
Posted: Tue Mar 20, 2012 8:28 am |
|
|
Hi,
PIC18f4520
Compiler version 4.057
PIC mounted on a PIC demo board
In simple terms I am writing a value (int8) to a memory address in the eeprom every time an external interrupt happens (which is triggered
by an external switch).
This value is then displayed on a set of LED's when another external interrupt happens (triggered by another switch).
The code for this part is as follows: (when tested this works ok)
Code: |
#int_EXT
void log_fault_isr(void) {
delay_ms(20);
write_eeprom(0,count); //count is a int8 variable
}
#int_EXT1
void EXT_isr(void) {
value = read_eeprom(0);
output_c(~value);
}
|
What I want to do now, is every time the interrupt int_EXT happens the value of 'count' will be written to the next memory address, i.e. address 0
then address 1 etc..
Also everytime the interrupt int_EXT1 happens the value will be read from the next address i.e address0, then address 1 etc..
I have tried something along the lines of:
Code: |
for (i=0;i<=255,++i) //where i is the address//
|
But it appears that the value is not being written to an address on the eeprom?
Any ideas??
Thanks, Mark |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Mar 20, 2012 5:02 pm |
|
|
because of the relatively LONG delay to do an EEPROM write
i would NEVER do it inside the ISR - that could be too much time to steal from the foreground.
rather i would declare and use a 1 bit int flag variable set a flag inside the ISR - and then in the MAIN execution loop - do the write and then clear the flag bit for re use by the ISR
here are some of the many of rules i live by when writing an ISR:
inside your handler ......
* no floating point calculations ( and avoid 32 bit int calls too )
* no mult, division or modulo ops
* no trig function calls
* never use a delay statement
* never do a printf()
* never disable other interrupts or only with great care
* never write to eeprom
there are ways around all of these things BASED on coupling to an ISR
but try to never execute these things inside the ISR |
|
|
chefgage
Joined: 20 Mar 2012 Posts: 2
|
|
Posted: Wed Mar 21, 2012 2:05 am |
|
|
Thanks for that,
As for writing to the next eeprom address each time a button is pressed what would the syntax/code be. When writing to the eeprom I have:
write_eeprom(0,value); // '0' is the address,
// value is the value to be written
I have tried something like:
for (i=0;i++)
write_eeprom(i,value);
But this appears not to work, while I have my working code I would like to get this little bit working before looking at implementing what has been said in the second post
Any ideas?
Thanks |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Wed Mar 21, 2012 7:38 am |
|
|
asmboy wrote: |
* no mult, division or modulo ops
|
Just some quick exceptions to that:
Modulo math is replaced by an AND instruction if you are using a power of 2 as what you are dividing with: val % 4, for example, converts to val & 0x03
Also depending on you chip, your multiply instruction can be 1 clock cycle (PIC24Fs for example), though division is almost always slow (dividing by powers of 2 convert the division into shift operations, which are quicker, but still take a while when dividing with higher powers of two).
Sorry, don't mean to sound contradictory at all, but sometimes those can come in handy in the ISR, but I do agree to avoid them in general or at least make sure you document the exceptions so a less experienced programmer doesn't foul something up later on in life. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Mar 21, 2012 8:27 am |
|
|
Quote: |
for (i=0;i++)
write_eeprom(i,value); |
lets look at basic C here.......
which is lacking in what you showed
and for example filling the first 8 bytes with 'value'
Code: |
for (i=0; i<8; i++;) {
write_eeprom(i,value);
}
|
might work a tad better ....
|
|
|
|