Need help with write function


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Need help with write function

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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.

  3. #3
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    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

  4. #4
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Archilochus; - 3rd June 2006 at 00:21.

  6. #6
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default

    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.

  7. #7
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    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

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

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

  9. #9
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Wink Feeling as an apprentice among these guys (and girls) allow me to add the followings

    Hi Chris,
    EEPROM is already a reserved word.
    So in your code, you should be using a different name for EEPROM subroutine, say WriteRom.


    Code:
    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?



    -----------------------------------------------
    Last edited by sayzer; - 3rd June 2006 at 11:03.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  10. #10
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default

    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?

  11. #11
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. WRITE: One more PBP 2.60 Surprise ...
    By Acetronics2 in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 26th August 2009, 10:10
  2. Replies: 5
    Last Post: - 29th May 2008, 19:03
  3. Need the code to write to a memory
    By Hamlet in forum General
    Replies: 0
    Last Post: - 20th August 2007, 01:22
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 18:27
  5. Storing Strings using the Write command
    By BobP in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 1st November 2005, 12:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts