View previous topic :: View next topic |
Author |
Message |
ssaakmnt
Joined: 03 Dec 2011 Posts: 27
|
Control the program of the pic |
Posted: Tue Feb 07, 2012 4:52 am |
|
|
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
|
|
Posted: Tue Feb 07, 2012 6:04 am |
|
|
Yes, for most 'F' PICs that is trivial. Five minutes tops!
John |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Feb 07, 2012 7:58 am |
|
|
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
|
|
Posted: Wed Feb 08, 2012 3:25 am |
|
|
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
|
|
Posted: Wed Feb 08, 2012 5:18 am |
|
|
I can give you my bank account number. As soon as I have received your money I'll start doing your (school) work.
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
|
|
Posted: Thu Feb 09, 2012 12:47 am |
|
|
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
|
|
Posted: Thu Feb 09, 2012 2:56 am |
|
|
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
|
|
Posted: Thu Feb 09, 2012 5:08 am |
|
|
Thank you this helps a lot |
|
|
RoGuE_StreaK
Joined: 02 Feb 2010 Posts: 73
|
|
Posted: Thu Feb 09, 2012 5:10 am |
|
|
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! |
|
|
|