PDA

View Full Version : Storing Text in EEPROM - ideas wanted



jamie_s
- 23rd July 2006, 04:16
Hi all,

Im working on a project, using a PIC16F648A which monitors signals on 4x pins and automatically switches some outputs based on the inputs.

The project also serially outputs status of the inputs and outputs every 3 seconds, but I want to be able to label (name) the inputs on the status display.

Instead of hard coding the names, I want to be able to write the names to the eeprom and then read them back for use in the status display, but unortunately the only thing i've ever had experience with eeprom is writing and reading a couple of digits.

Now, the port names will be limited to 8 or 10 characters x 4 ports and also a label for the physical device (ie device name)

So say port 1 name is "Port0001" port 2 is "port0002" and so on and the device name is "Switch01".

For me to store these names in eeprom, obviously the text names need to be converted from ascii to decimal? and then this result written and then when read, converted from decimal back to ascii chars?

If anyone could assist in providing some help and possible code examples etc, would be appreciated.

DynamoBen
- 23rd July 2006, 04:24
In a way you are making this harder than it needs to be.
You don't need to convert from ASCII to decimal, because ASCII is decimal.

All you need to do is write/store the ASCII strings into EEPROM. Then read them back via a subroutine that includes the LCDOut command. You may want to terminate your stored strings with a 0 (decimal 0 not ASCII) to indicate end of string, this allows each string to be different lengths.

jamie_s
- 23rd July 2006, 05:34
Ok, so far i have got this code, which works fine as far as getting the input for the port name



INCLUDE "modedefs.bas" ' Modes for SerOut

CMCON = 7 ' set portA to digital
TRISA = %00100000 ' Port A all output but MCLR
TRISB = %00001010 ' Port B all output but 1 - debug in

DEFINE DEBUGIN_REG PORTB ' Set Debug pin port
DEFINE DEBUGIN_BIT 1 ' Set Debug pin bit
DEFINE DEBUGIN_BAUD 9600 ' Set Debug baud rate
DEFINE DEBUGIN_MODE 1 ' Set Debug mode: 0 = true, 1 = inverted

DEFINE DEBUG_REG PORTB ' Set Debug pin port
DEFINE DEBUG_BIT 2 ' Set Debug pin bit
DEFINE DEBUG_BAUD 9600 ' Set Debug baud rate
DEFINE DEBUG_MODE 1 ' Set Debug mode: 0 = true, 1 = inverted


port1name var byte[8]

Main: ' Display "Waiting for input" every 5 seconds if timeout period expires
DEBUGIN 5000, idle, [STR port1name\8\13]

DEBUG "Received: ",STR port1name\8,13,10

Goto Main

idle:
DEBUG "Waiting for input"

goto main


So from here, now i need some help on how i write the byte array to the eeprom and terminate with decimal 0, and also read the strings from the eeprom, upto the decimal 0?

The byte array will need to be converted into a word? and then write highbyte and lowbyte?

DynamoBen
- 23rd July 2006, 14:49
Before we get too far, are you using internal EEProm or External? What type of pic are you using?

If its internal take a look at the DATA, and READ commands. These in combination with with a for/next loop should give you what you are looking for. The reason for 0 is as you are reading data from EEProm when you read 0 that tells the loop to stop and exit.

If External what type is it, I2C?

jamie_s
- 23rd July 2006, 16:34
I am using the internal eeprom on a 16f628a / 648a

DynamoBen
- 24th July 2006, 04:26
To add your strings via the DATA command:

DATA $00,"Switch01",0

Example of reading the above and displaying it on an lcd:

For CounterA = 0 to 16
Read CounterA,value
If CounterA=0 then goto Main
LCDOUT value
Next CounterA

This should be enough to get you started.

jamie_s
- 24th July 2006, 05:05
Ok,
Ive got this far:


INCLUDE "modedefs.bas" ' Modes for SerOut

CMCON = 7 ' set portA to digital
TRISA = %00100000 ' Port A all output but MCLR
TRISB = %00001010 ' Port B all output but 1,3 (Si and CCP)

DEFINE DEBUGIN_REG PORTB ' Set Debug pin port
DEFINE DEBUGIN_BIT 1 ' Set Debug pin bit
DEFINE DEBUGIN_BAUD 9600 ' Set Debug baud rate
DEFINE DEBUGIN_MODE 1 ' Set Debug mode: 0 = true, 1 = inverted

DEFINE DEBUG_REG PORTB ' Set Debug pin port
DEFINE DEBUG_BIT 2 ' Set Debug pin bit
DEFINE DEBUG_BAUD 9600 ' Set Debug baud rate
DEFINE DEBUG_MODE 1 ' Set Debug mode: 0 = true, 1 = inverted


port1name var byte[8]
name1 var word
i var byte

Main: ' Display "Waiting for input" every 5 seconds if timeout period expires
DEBUGIN 5000, idle, [STR port1name\8\13]

DEBUG "Received: ",STR port1name\8,13,10

for i = 1 to 8
write i,port1name(i)
pause 10
next i
pause 100

for i = 1 to 8
read i, port1name(i)
next i
DEBUG "Read eeprom after write: ",STR port1name\8,13,10

Goto Main

idle:
DEBUG "Waiting for input",13,10

for i = 1 to 8
read i, port1name(i)
next i
DEBUG "Read from eeprom: ",STR port1name\8,13,10

goto main


Now, it receives what i enter, and saves it, and displays it fine, however once power is cycled, and it reads from the eeprom, the 1st character is incorrect:

Received: 12345678
Read eeprom after write: 12345678
Waiting for input

then power cycle

then i get this:

Waiting for input
Read from eeprom: û2345678

any hints on what i may be doing wrong?
Also all comms is serial @ 9600, there is no LCD, therefore the LCDout command cant be used....

DynamoBen
- 24th July 2006, 05:17
When you said "I want to be able to label (name) the inputs on the status display" I thought you were talking about an LCD.

When you use the STR command you are starting at 0 not 1.

Try this instead

for i = 0 to 7
read i, port1name[i]
next i

AND

for i = 0 to 7
write i, port1name[i]
next i