Example 1
Code:
CounterA var Byte
Flash var Byte
LED var PortB.0
TRISB=0
For CounterA=1 to Flash
High LED
Pause 500
Low LED
Pause 500
Next CounterA
EndLoop:
Pause 1000
Goto EndLoop
End
In the above example, when you turn your PIC on, the LED may flash an indeterminate number of times... because the contents of variable FLASH cannot be guaranteed at Power-Up and you've NOT preset the contents before using that variable.... don't assume any variable is zero following power-up, so you must use the CLEAR instruction (see manual) or preset the variable...
Example2
Code:
CounterA var Byte
Flash var Byte
LED var PortB.0
TRISB=0
Flash=1
For CounterA=1 to Flash
High LED
Pause 500
Low LED
Pause 500
Next CounterA
EndLoop:
Pause 1000
Goto EndLoop
End
Now in the above example, the LED will always flash ONCE.
Let's now suppose, we want to ADD ONE FLASH to our program every time we power-up... we need to increment the current content of FLASH and save it... we will start with TWO FLASHES at initial Program time (and the first time the program is run) and increment thereafter...
Example 3
Code:
CounterA var Byte
Flash var Byte
LED var PortB.0
TRISB=0
Data @0,2
Read 0,Flash
For CounterA=1 to Flash
High LED
Pause 500
Low LED
Pause 500
Next CounterA
Flash=Flash+1
Write 0,Flash
EndLoop:
Pause 1000
Goto EndLoop
End
The DATA Command presets the EEPROM contents at the time you program your PIC. Don't assume your EEPROM contents are zero (unless your programmer has been set to burn the contents of your EEPROM that way), so the best way is ALWAYS to use DATA to preset the EEPROM contents that you are going to use.
READ now reads the EEPROM contents at Power-Up, presetting the variable FLASH. Prior to the program ending, the WRITE command sets a new value in EEPROM to be used the next time you Power-Up.
This is good for counting up to 255 flashes, because we're using a BYTE and the variable will roll through zero when we've used up all eight bits. If you need to save a WORD, you must do so as two BYTE halves, because EEPROMS only work in BYTEs. If you need to save a BIT, then you have to waste an entire BYTE to be able to do that - because EEPROMS only work in BYTES.
I trust that will answer most of your query...
Bookmarks