View previous topic :: View next topic |
Author |
Message |
jaikumar
Joined: 15 Dec 2006 Posts: 109
|
Help!. Why does this variable get corrupted. |
Posted: Mon Aug 02, 2010 12:50 am |
|
|
I have connected RB0 to determine mains power off. Checking with dmm
I can see the power supply for microcontroller is being maintained for 3
seconds before falling off.
The software works for simple turning on a port bit on power off.
But the variable lampDuration gets corrupted immediately on power off.
Why?
Also note when the lampDuration variable is accessed all interrupts are off.
Any help would be great.
Thanks,
Jai.
Code: |
#INT_TIMER1
void TIMER1_isr()
{
Ticker -= 65536; // Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{
Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per second
lampDuration++; // Increment number of seconds
}
}
#INT_EXT
void ext_isr(void)
{
powerOff = 1;
}
void main()
{
output_low(PIN_A1);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(A0_VR_A1_VR);
ext_int_edge(L_TO_H);
setup_vref(VREF_LOW | 12);
Ticker = TIMER1_FREQUENCY;
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 );
init_ext_eeprom();
lampDuration = 0;
powerOff = 0;
enable_interrupts(INT_EXT);
disable_interrupts( INT_TIMER1 );
lampDuration = read_int32_ext_eeprom(5);
enable_interrupts( GLOBAL );
while(1)
{
if (powerOff) {powerOff = 0; disable_interrupts(INT_EXT); printf("%ld\r\n",lampDuration); write_int32_ext_eeprom(5, lampDuration);}
while (!input(PIN_B4))
{
enable_interrupts( INT_TIMER1 );
printf("%ld\r\n",lampDuration);
}
if (input(PIN_B4))
{
if (lampDuration > 100) {lampDuration = 0;}
disable_interrupts( INT_TIMER1 );
}
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 02, 2010 12:28 pm |
|
|
Make a test program that only tests if you can write to external eeprom
successfully during a power-down.
Get rid of all this stuff. Make it be a simple test.
Quote: |
output_low(PIN_A1);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(A0_VR_A1_VR);
ext_int_edge(L_TO_H);
setup_vref(VREF_LOW | 12);
Ticker = TIMER1_FREQUENCY;
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 );
init_ext_eeprom();
lampDuration = 0;
powerOff = 0;
enable_interrupts(INT_EXT);
disable_interrupts( INT_TIMER1 );
lampDuration = read_int32_ext_eeprom(5);
enable_interrupts( GLOBAL );
#INT_TIMER1
void TIMER1_isr()
{
Ticker -= 65536; // Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{
Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per second
lampDuration++; // Increment number of seconds
}
}
#INT_EXT
void ext_isr(void)
{
powerOff = 1;
} |
|
|
|
jaikumar
Joined: 15 Dec 2006 Posts: 109
|
|
Posted: Mon Aug 02, 2010 11:40 pm |
|
|
It works when i write to eeprom when power is on. When i turn of power. See the line print before writing to eeprom, that variable get's corrupted even before writing to eeprom.
regards,
jai |
|
|
jaikumar
Joined: 15 Dec 2006 Posts: 109
|
|
Posted: Tue Aug 03, 2010 1:17 am |
|
|
Finally it works. i tried this
I made the variable in the timer section not changing when poweroff, It works but need more testing.
AFTER TESTING THIS DID NOT WORK.
Regards,
Jai
Last edited by jaikumar on Tue Aug 03, 2010 3:29 am; edited 1 time in total |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 03, 2010 1:48 am |
|
|
Show some example values to explain why you think is is corrupted.
Someone can correct me if I am wrong but
Code: |
#INT_EXT
void ext_isr(void)
{
powerOff = 1;
}
|
Is a port b interrupt and to clear this you have to actually read port b which would mean, with your code, once in interrupt it will continuously interrupt preventing your code from running.
Based on this it is probably not even writing your value to eeprom.
Change your printf for when the power fails so it is different to the other printf, this will confirm it is actually executing that code. |
|
|
|