PDA

View Full Version : Need help with write function



Christopher4187
- 2nd June 2006, 20:46
Yes, I have RTFD and still don't quite understand how to do it. In the PBP manual, it says that you can do something like:

Write 0, Brakes
Write 1, Lights

Then, you can read them by doing this:

Read 0, Brakes
Read 1, Lights

But according to the 16F688 datasheet, it says that the memory register is different and has to be accesed through the EECON1, EECON2........and I am not sure how to set up the code to write.

Can someone help me with the write function please?

Thanks,

Chris

Melanie
- 2nd June 2006, 21:00
DATA, READ and WRITE are the correct commands for the internal EEPROM in the 16F688 chip. Make sure your version of PICBasic supports this chip, and you have any patches installed. Check on MeLabs website.

paul borgmeier
- 2nd June 2006, 21:19
Just to add ... PBP takes care of all the EECON1 and EECON2 stuff so you do not have to - you just need DATA, READ, and WRITE.

See example code on melabs website
http://www.melabs.com/resources/samples/pbp/ee.bas

Paul Borgmeier
Salt Lake City, Utah
USA

Christopher4187
- 2nd June 2006, 22:08
Thanks for the help. I am still a bit unclear, so I want to know if I can do something like this (I would do it myself with the PIC but I won't be able to until tomorrow):

Beep var byte
flash var byte


Gosub EEPROM


Start:

if portb.4=1 then
beep=beep+1
gosub EEPROM

if beep=1 then
high portb.0
endif

if portb.3=1 then
flash=flash+1
gosub EEPROM

if flash=2 then
high portb.1
endif

goto start


EEPROM:

write 0, beep
write 1, flash
return

I know this isn't a very complex code but the reason I need to write to the EEPROM is because I need to change functions on an alarm system after the PIC is already programmed. Some questions.... if for example I just say "flash var byte," does the PIC automatically recognize flash as a zero? Lets say the PIC has been running for a while and flash=7. Now, I power off the PIC and it holds the 7 in memory. If I goto power up the PIC and I have it go directly to the write command, will it simply just write over flash but still make it 7? Also, do I need the read command or can I just say "If beep=1 then......?"

Thanks,

Chris

Archilochus
- 2nd June 2006, 23:19
Hi Chris,
Maybe one of the pros will add some more answers, but for now...



if for example I just say "flash var byte," does the PIC automatically recognize flash as a zero?

PBP does not initialize RAM variables to any value, and on power up the RAM variables will contain an unknown value. You can use the CLEAR command to clear all RAM variables to 0, or just set each variable to 0 manually: flash=0

After any power up, you'll need to READ your saved settings from EEPROM to restore your user selections.

Keep in mind the EEPROM has a limited number of write/erase cycles (check data sheet for details).

Arch

Christopher4187
- 3rd June 2006, 03:16
OK, so I understand when you have used the PIC for a while you just simply read the EEPROM and everything is OK. What I don't understand is the very first time you power up the PIC, how do you deal with the memory? I mean, if you go to read it, it's an unknown value.

paul borgmeier
- 3rd June 2006, 06:10
Look at the DATA command in the PBP manual. It will tell you that you can use this command to set the initial values stored in EEPROM when the device is initially programmed.

Use the WRITE command to change the values when the program is running.
Use the READ command to read the values when the program is running.

Paul Borgmeier
Salt Lake City, Utah
USA

Melanie
- 3rd June 2006, 07:43
Example 1


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


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


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...

sayzer
- 3rd June 2006, 09:43
Hi Chris,
EEPROM is already a reserved word.
So in your code, you should be using a different name for EEPROM subroutine, say WriteRom.




EEPROM 0,[0,0]
'This will write 0 to EEPROM location 0
'and 0 to EEPROM location 1.
'works only during the programming of PIC
'Not each time PIC powers up

Beep var byte
Flash var byte


Gosub readrom
'Since EEPROM locations 0 and 1 have known values now,
'we can read and store them.
'This will be done each time PIC powers up.
'So each time PIC powers up we will get the last saved values.

Start:

if portb.4=1 then
beep=beep+1
gosub WriteRom 'We Write the new value into EEPROM.
gosub ReadRom 'Then we read it right away and store it.
if beep=1 then high portb.0
endif

if portb.3=1 then
flash=flash+1
gosub WriteRom 'We Write the new value into EEPROM.
gosub ReadRom 'Then we read it right away and store it.
if flash=2 then high portb.1
endif

goto start


WriteRom:
write 0, beep 'Takes approx. 10mSec to complete.
pause 15
write 1, flash
pause 15
return

ReadRom:
read 0, beep 'Reading is almost instantenous
read 1, flash
return

end



- In your code, when will you "low" the ports after you pull them high?
- Pulling the ports high is(are) inside the IF statements; are not they supposed to be outside the IF statements but inside the main loop?



-----------------------------------------------

Christopher4187
- 12th July 2006, 01:56
Thanks Melanie, Paul, Sayzer and Arch. I have been successful in writing and reading a 16F688. Just one question in response to the post by Sayzer. I used the data command, write command and read command. I see Sayzer used the EEPROM to store the variables and when looking at the PBP manual, I don't understand what the difference is. Just by using Data to program, read to view and write to store, everything worked great. What's the difference between Data and EEPROM and why would someone choose EEPROM over Data?

mister_e
- 12th July 2006, 04:12
Unless you want to use the 'label' feature as stated in the DATA section, there's no difference as same program the internal EEPROM at programming time.

The main difference is just in the way to write the statement.

I feel that one or the other is an BASICStamp statement...

Now i don't know why but i always use the DATA statement... or MPASM assembler DE