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 CCS Technical Support

Control the program of the pic

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



Joined: 03 Dec 2011
Posts: 27

View user's profile Send private message

Control the program of the pic
PostPosted: Tue Feb 07, 2012 4:52 am     Reply with quote

Hi everyone
Is there a way or a simple code to control the program so it makes
microcontroller work and execute instructions let say for 5 times,
and after power it up at 6th time it enter endless loop. Thank you.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Tue Feb 07, 2012 6:04 am     Reply with quote

Yes, for most 'F' PICs that is trivial. Five minutes tops!

John
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Feb 07, 2012 7:58 am     Reply with quote

Increment a counter in EEPROM each time the PIC starts. When the counter reaches 5 do the other behavior.
_________________
The search for better is endless. Instead simply find very good and get the job done.
ssaakmnt



Joined: 03 Dec 2011
Posts: 27

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 3:25 am     Reply with quote

yeah..but if anyone can put a short example code of counter in EEPROM it would be appreciated..thanks
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 5:18 am     Reply with quote

I can give you my bank account number. As soon as I have received your money I'll start doing your (school) work. Wink

Here on this forum we like people to learn something. Just giving you the answer means you'll be returning many times with problems you could easily solve yourself (and that are boring to us).

Try to create a program yourself. When you get stuck you can post whatever you have and we will help you to get going.
ssaakmnt



Joined: 03 Dec 2011
Posts: 27

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 12:47 am     Reply with quote

ok..I already wrote a code but I did not post it because in fact its first time with EEPROM
Code:
write_eeprom(0x00,i++);
value=read_eeprom(0x0);
 
if (value>5)
while(1)
{output_b(0);}

while(1)
{output_b(255);}

}

The program is supposed to increment (int i) every time we power the pic and save
the value of i after power off. When we power at 6th time pic do other behavior.
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 2:56 am     Reply with quote

You need to read the last value from the EEPROM, before you write to the EEPROM.
You also need to ensure the EEPROM cell is set to '0' when you program the chip.
So:
Code:

#ROM int8 getenv("EEPROM_ADDRESS") = {0}
//This writes '0' into the EEPROM when the chip is programmed.

void main(void) {
   int value;
   value=read_eeprom(0); //Get the value last stored in the EEPROM
   if (value<6) write_eeprom(0,++value);
   //Increment the value in the EEPROM

  if (value>5)
     output_b(0);
  else
     output_b(255); //Will do this five times

  //The chip will now go to sleep. This doesn't matter, since the value
  //is already output on the port, and will save power.
  sleep();
}

Now, a couple of things here.

First, I stop updatiing the EEPROM, once the count gets to 6. This saves EEPROM life, but also stops the code doing five more cycles after another 250 power cycles, which will otherwise happen when the int value gets to 256, and then wraps back to 0.....
Second, once the value is output on the port, the chip can just stop running. The lines will remain driven when this happens, and save power.

Best Wishes
ssaakmnt



Joined: 03 Dec 2011
Posts: 27

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 5:08 am     Reply with quote

Thank you this helps a lot
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 5:10 am     Reply with quote

Ttelmah wrote:
Second, once the value is output on the port, the chip can just stop running. The lines will remain driven when this happens, and save power.
Really? Cool, didn't know that; so it essentially latches to it's last state, and doesn't need to use power (well, extra power) to keep it that way?
Learn a new thing every day in this joint!
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