Working with memory


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305

    Default Working with memory

    I'm trying to get more familiar with reading and writing to the memory of a 12F683 @4Mhz using PBP3 and simulating it on Proteus's serial out display.

    What I want to know is how to write a large number, word sized, to memory, byte size, and then reassmble it. Like this

    Code:
    clear
    trisio =%001101
    gpio = 0
    include "modedefs.bas"
    
    #config
           __CONFIG _INTOSCIO & _WDTE_ON & _CP_OFF & _MCLRE_OFF & _BOREN_ON & _IESO_OFF & _PWRTE_OFF  & _CPD_OFF &  _FCMEN_OFF	
    #endconfig
    
    define OSC 8
    osccon = %01111110 'sets 8 MHz oscillator
    
    ansel = %00100101   ' 
    cmcon0 = 7         ' Turn off comparatos      
    
    memory_output1 var byte    ' memory variables
    memory_output2 var byte
    memory_output3 var byte
    memory_output4 var word
    memory_output5 var byte
    memory_output6 var word
    memory_output7 var word
    
    Main:
    write 0,5  'playing with memory
    write 1,20
    write 2,20
    read 0, memory_output1
    read 1, memory_output2
    reaD 2, memory_output3
    memory_output4 =  memory_output3 * memory_output2 * memory_output1 * memory_output1
    
    write 3, memory_output4.byte0
    write 4, memory_output4.byte1
    
    read 3, memory_output4.byte0
    read 4, memory_output4.byte1
    
    memory_output6 = memory_output4.byte0 & memory_output4.byte1 | memory_output7
    
    serout2 gpio.1,84,[10,13,dec memory_output3," ",dec memory_output2," ",dec memory_output6,10,13]
    serout2 gpio.1,84,[10,13,dec memory_output7,10,13]
    serout2 gpio.1,84,[10,13,"----------------------------------------------",10,13] 
    
    goto main
    Memory 4 value is 10,000. What I'm trying to do is write memory 4 byte 0 value to memory location 3 and byte1 value to memory location 4. Then I want to read those bytes and assemble them into a word and display it.

    I cut and pasted this code from the full program so it may be awkward in places. Your insights would be greatly appreciated.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Working with memory

    Hi,
    To put two bytes together into a WORD you can do one of several things:
    A: Result.Byte1 = H_Byte : Result.Byte0 = L_Byte
    B: Result = (H_Byte << 8) + L_Byte
    C: Result = H_Byte*256 + L_Byte
    D: .....

    However, the READ and WRITE commands have built in support for WORDs (and LONGs on 18F parts) so there's no real need to fiddle with it manually if you don't want to. It's covered in the manual.

    /Henrik.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Working with memory

    Hi,
    (I wrote an anwer to this question yesterday but it seems to have gone missing...)

    Both the READ and WRITE commands have WORD (and LONG for 18F) support built in so you don't have to handle it "manually" if you don't want to. Just look it up in the manual.

    But, to answer the question, there are several ways to put to two bytes "back into a word", for example:
    (Result is the WORD, Value_H and Value_L are the two bytes read from EEPROM.)
    A) Result.Byte1 = Value_H : Result.Byte0 = Value_L
    B) Result = (Value_H << 8) + Value_L
    C) Result = (Value_L * 256) + Value_L

    /Henrik.

  4. #4
    Join Date
    Oct 2011
    Posts
    54


    Did you find this post helpful? Yes | No

    Default Re: Working with memory

    If you are trying to get 'memory_output4' into 'memory_output6' replace line after
    read 4 with

    memory_output6.byte0 = memory_output4.byte0
    memory_output6.byte1 = memory_output4.byte1

    memory_output6 should now be 10,000

    Phil

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Working with memory

    Henrik, both your posts were moderated. I posted this thread in Admin forum in hopes Lester can figure why system is targetting you but not others.

    Sorry for the frustration.

    Robert
    Last edited by Demon; - 2nd June 2013 at 14:53. Reason: post in admin forum

  6. #6
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Working with memory

    I ended up doing something like what Sherbrook replied with code like this

    WRITE 3, memory_output4.byte0
    WRITE 4, memory_output4.byte1

    READ 3, memory_output6.byte0
    READ 4, memory_output6.byte1

    And the number came out correctly displayed

    I'm going to try Henriks' other solutions also.

    Henrik can you school me a little more please? From the manual what is line 5 (my numbers) doing:

    1 WRITE will not work on devices with on-chip I2C interfaced serial EEPROM like the
    2 PIC12CE67x and PIC16CE62x parts. Use the I2CWRITE instruction instead.
    3 WRITE 5, B0 ' Send value in B0 to EEPROM
    4 location 5
    5 WRITE 0, Word W1, Word W2, B6
    6 WRITE 10, Long L0 ' PBPL only

    I'm assuming it's trying to write two 16 bit values to the variable B6. Will one overwrite the other or will they combine to make one 32 bit word?

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Working with memory

    Hi,
    I'm assuming it's trying to write two 16 bit values to the variable B6. Will one overwrite the other or will they combine to make one 32 bit word?
    No, it will write W1 (a word), then W2 (a word), then B6 (a byte) to the EEPROM (a total of 5 bytes) starting at location 0. To be honest I don't know if it writes the least or the most significant byte first but that's easy to find out. It doesn't really matter if you use READ with the WORD modifier since I'm sure it's been designed to match.

    If memory_output4 is a WORD and you want to store it in EEPROM starting at location 3 then
    Code:
    WRITE 3, WORD memory_output4
    Then, to read it back
    Code:
    READ 3, WORD memory_output6
    [/code]

    /Henrik.

  8. #8
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Working with memory

    Thanks. Actually, that clears up a few other questions I had about storing and reading memory. I appreciate it.

Similar Threads

  1. SERIN Not Working, SEROUT Working
    By cc1984 in forum Serial
    Replies: 11
    Last Post: - 26th October 2010, 20:16
  2. working with external memory
    By malc-c in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 25th September 2010, 00:35
  3. Replies: 11
    Last Post: - 17th August 2010, 17:45
  4. Replies: 4
    Last Post: - 2nd March 2007, 07:12
  5. Use internal program memory like DATA memory
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th December 2006, 19:38

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