PDA

View Full Version : Working with memory



AvionicsMaster1
- 31st May 2013, 15:05
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




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.

HenrikOlsson
- 31st May 2013, 18:07
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.

HenrikOlsson
- 1st June 2013, 05:05
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.

Sherbrook
- 1st June 2013, 17:54
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

Demon
- 2nd June 2013, 13:49
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

AvionicsMaster1
- 3rd June 2013, 22:01
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?

HenrikOlsson
- 4th June 2013, 06:14
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

WRITE 3, WORD memory_output4
Then, to read it back

READ 3, WORD memory_output6
[/code]

/Henrik.

AvionicsMaster1
- 6th June 2013, 13:47
Thanks. Actually, that clears up a few other questions I had about storing and reading memory. I appreciate it.