CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Help!. Why does this variable get corrupted.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jaikumar



Joined: 15 Dec 2006
Posts: 109

View user's profile Send private message

Help!. Why does this variable get corrupted.
PostPosted: Mon Aug 02, 2010 12:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 12:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 02, 2010 11:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 03, 2010 1:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 03, 2010 1:48 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group