PDA

View Full Version : String & WRITE



Lotondo
- 9th February 2006, 14:34
Hello everybody.
Pls. I need yr. help.
With HSerin I read a string coming from a serial port and I get this
string "34567890".
Now I'd like to store (with WRITE) into onchip eeprom starting
from location 0 this string, after that the eeprom memory should look
like this:
34 56 78 90 FF FF FF FF ...............
I tried several possible solution but I was not able to 'brake' in 4 parts
the string.
Any help will be appreciated.
Thanks
Lotondo

egberttheone
- 9th February 2006, 20:13
I don't really understand your question, but here is maybe the solution: if you want to store word sized variable's into the eeprom memory you have to split the word into 2 byte's you can use the variable.lowbyte and variable.highbyte command. When reading you do this procidure in reverse.

Ron Marcus
- 9th February 2006, 20:25
Hello everybody.
Pls. I need yr. help.
With HSerin I read a string coming from a serial port and I get this
string "34567890".
Now I'd like to store (with WRITE) into onchip eeprom starting
from location 0 this string, after that the eeprom memory should look
like this:
34 56 78 90 FF FF FF FF ...............

Are the numbers coming in as 34,56,78..... or are they coming in individually?

Ron Marcus
- 9th February 2006, 20:27
To pack single hex digits into a hex byte, just shift the first digit left four times, then add it to the second digit.

Ron

BigWumpus
- 9th February 2006, 21:15
OK, I just try to make the situation clear...

You receive 8 ASCII-Characters "3", "4", "5", "6", ....
Then you want to pack 2 digits in 1 Byte and write them to the eeprom.

Yust try:
Chars Var byte[8] ;here are the 8 digits
For I=0 to 3 ;generate 4 bytes
;cut off unused bits and pack 2 of them
Dummy=(Chars[I<<1]<<4)|(Chars[(I<<1)+1] & $F)
Write I,Dummy
next I

(Very quick and quite dirty!)

Darrel Taylor
- 9th February 2006, 22:16
Hi BigWumpus,

I know you did it real quick, but I think something's missing.

Dummy=((Chars[I<<1]-"0")<<4)|((Chars[(I<<1)+1]-"0") & $F)

Darrel

Lotondo
- 10th February 2006, 07:42
Thanks for answering, I believe you got me the solution.
I'll try and in case I'm not getting the correct result I'll
come back to you.
Regards
Lotondo

BigWumpus
- 10th February 2006, 19:10
Hi Darrel,

I dont subtract the $30 because
-while shifting it 4 bits to the left, they drop out of the PIC ;-)
-while & $F they where masked out ;-)

try it smart !

Darrel Taylor
- 10th February 2006, 21:32
BigWumpus,

Oh yeah. Your right!

That'll change the way I convert ascii numbers in the future. Thanks.
<br>

Andrew
- 29th December 2017, 09:28
Although the topic is quite old I am interested to solve a similar problem for which will really appreciate your support / expertise.

On short, I am receiving serially 16 ASCII-Characters, say "3", "B", "5", "A", "7", "F","1", "D", "2", "2", "4", "F","E", "E", "A", "6".

Then I need to pack 2 digits in 1 Byte and write them to the EEPROM. The final result should be: 2B 5A 7F 1D 22 4F EE A6.
Any clue or tip with regard to algorithm or conversion formula will be highly appreciated. Have a happy and prosperous New Year !

Andrew
- 29th December 2017, 22:23
Hello everybody,

Although this topic might be quite old, I would like to "try my luck" to sort out a problem that bothers me for some time.

From the very begining want to state that I am rather a noobie in relation with PIC's and peculiar with its EEPROM operation and actualy this is my first interaction with this forum.

On short, I use DEBUGIN command to capture a 16 characters alphanumeric strings sent from a PC, something like following example:

85D4E88A3B52AA40 (or more corect said "8","5","D","4","E","8","8","A","3","B","5","2","A","A","4","0").

I do need to WRITE these characters into onchip (PIC16F88) EEPROM, location 0, as pairs consisting of 2 characters in 1 Byte.

At the end of operation the first location of EEPROM memory must looks like this: 85 D4 E8 8A 3B 52 AA 40.
Unfortunately I have ended up with this result: 85 44 58 81 32 52 11 40.

Concernig the pairs of data within the string please note that there are several kind of possible combinations:

- 2 digits (i.e. 85, 40);
- 2 characters (i.e. AA);
- 1 digit and one character, in this order (i.e. 8A, 3B)
- 1 character and one digit, in this order (i.e. D4, E8)

Mention that I have used and adapted BigWumpus' above proposed algorithm.
It works perfectly as long the string contains only digits (0-9).
However, it fails whenever HEX like characters (A,B,C,D,E F) come into picture.

Any help to sort out this problem will be highly appreciated.

Many hanks in advance and wishing you all a Happy and Prosperous New Year !

Best regards,
Andrew

HenrikOlsson
- 30th December 2017, 07:38
Welcome to the forum!
If you use the HEX modifier while receiving the ASCII string each byte in the string/array will contain the numeric value (0-15) represeneted by the ASCII character (instead of the ASCII code FOR the character). Once you have that it's just a matter of taking the first byte, shifiting it 4 steps to the left so it ends up at the four most significant bits and then add the next byte.

/Henrik.

Andrew
- 30th December 2017, 11:30
Good day Henrik,

Many thanks for your swift reply and suggestion.
Regarding your indication to use HEX modifier all noted. However I am not sure what is the correct SYNTAX for it... :D

Mention that I am using PICBASIC PRO 2.60C & MCS 3.0.0.5 and below follows a fragment of code I try to implement:

////////////////////////////////////////////////////////////

i VAR BYTE
j VAR BYTE

new_data VAR BYTE[16] 'this array variable will store incoming data from serial port
Dummy VAR BYTE

test VAR BYTE[16] 'this array variable will store data READ from EEPROM



begin:

DEBUGIN 150, continue,[str new_data\16] 'receive data from PC

For i = 0 To 7 'read previously stored into EEPROM
READ $0+i,test[i]
Next i

'compare the income string data with one withdrawn from EEPROM and case they are different only then refresh the EEPROM
'this is done to avoid unnecessary time consuming writting / wasting the EEPROM

IF (new_data[0] <> test[0] AND new_data[1] <> test[1] AND new_data[2] <> test[2] AND new_data[3] <> test[3] AND new_data[4] <> test[4]_
AND new_data[5] <> test[5] AND new_data[6] <> test[6] AND new_data[7] <> test[7]) THEN

For j = 0 to 7
Dummy = (new_data[j<<1]<<4)|(new_data[(j<<1)+1] & $F) 'Mr. BigWumpus algorithm to split the data into 2 bits pairs
Write $0+j,Dummy 'write down into EEPROM the pairs consisting of 2 bits
next j

DEBUG "EEPROM data updated !"
ENDIF

continue: 'here follows some regular routines to execute case DEBUGIN timeframe (150 ms) is exceeded
////////////////////////////////////////////////////////////////

Kindly indicate the necessary corrections in order to have a functional code, in terms of suggested HEX modifier.
Also, I do believe that READ instruction need to be revised too in order to function as expected.
I really appreciate your help in this respect.

Many thanks in advance and have a nice day ahead and a Happy New Year !
Best regards,

Andrew


P.S. Kindly accept my apologies case this message appears posted twice. During first editing attempt my computer got stuck :)

HenrikOlsson
- 30th December 2017, 13:13
Hi,
It looks like the HEX modifier can only do 8 digits "by it self" so one way is to resort to the good old FOR loop

For i = 0 to 15 ' We're doing 16 hex digits
DEBUGIN 150, Continue, [HEX New_Data[i]]
NEXT


I haven't looked closely at BigWumpus packing/splitting code, so I don't know if it needs modifying for this to work properly but give it a try as is and see what it does.

/Henrik.

mpgmike
- 30th December 2017, 19:08
Whether you wish to refer to a value in its decimal, hexadecimal, or binary format, when storing to the EEPROM, you are simply storing that value. When you READ it back, you may need to format it in its hexadecimal format for sending information serially, or displaying to an LCD. It looks like you are receiving these digits as ASCii, which allows for letters A-F. My approach would be to first convert the ASCii characters into Nibbles, then group them together into Hex. Finally you can store the Value in your EEPROM.

To convert an ASCii character to a Hex nibble, subtract $30 from the numeric value of that nibble. Characters 0 >> 9 will be their 0 >> 9 raw value. Capital letters start at $41. Example, actual ASCii value of $41 = "A". You could use IF New_Data > $39 THEN : GOSUB DecodeLetters, ELSE : GOSUB DecodeNumbers. Using the shift and mask code listed previously, group your high nibble and low nibble together to create a new and complete byte value. Repeat for each 2-character grouping. When READing from the EEPROM, use the HEX modifier to send "Value" serially (ASCii representation). The key is that your ASCii value is $30 + Numbers (0 >> 9) and $37 + Letters (A >> F); conversely, ASCii - $30 = 0 >> 9 value while ASCii - $37 = A >> F values.

HenrikOlsson
- 31st December 2017, 11:04
When you use the HEX modifier with SEROUT, DEBUG, LCDOUT, HSEROUT, ARRAYWRITE and so on it converts FROM a numeric value TO an ASCII string representing the numeric value as hexadecimal. In other words a value of 127 becomes a "7" and a "F".

Likewise, when you use the HEX modifier with SERIN, DEBUGIN, HSERING, ARRAYREAD etc it converts FROM an ASCII string (or single character) representing a numeric value as hexadecimal TO the actual numeric value. In other words the "7" and the "F" becomes 127.

mpgmike does a good job of explaining how you convert ASCII to the numeric value that the "text" represents but what I'm trying to say is that you don't have to do that "manually" because the HEX- (and DEC and BIN) modifiers works both ways and does the job for you when used with a command that supports them (which DEBUGIN does).

It would be interesting to see if any benefits (size- or speedwise) can be gained from doing the conversion "manually" instead of letting PBP handle it for you using the built in modifiers.

Happy new year by the way!

/Henrik.

Andrew
- 31st December 2017, 16:13
Hello everyone,

I am glad that my "initiative" to re-open this topic has got such an echo.
Will give a try to presented methods and will reverd with results ASAP.

Meanwhile have a joyful and prosperous New Year !