String & WRITE


Closed Thread
Results 1 to 17 of 17

Thread: String & WRITE

  1. #1
    Join Date
    Sep 2005
    Location
    Switzerland
    Posts
    46

    Default String & WRITE

    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

  2. #2
    egberttheone's Avatar
    egberttheone Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  3. #3
    Join Date
    Sep 2003
    Location
    Vermont
    Posts
    373


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Lotondo
    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?

  4. #4
    Join Date
    Sep 2003
    Location
    Vermont
    Posts
    373


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Default

    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!)
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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

  7. #7
    Join Date
    Sep 2005
    Location
    Switzerland
    Posts
    46


    Did you find this post helpful? Yes | No

    Default

    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

  8. #8
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Default

    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 !
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    BigWumpus,

    Oh yeah. Your right!

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

  10. #10
    Join Date
    Dec 2017
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

    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 !

  11. #11
    Join Date
    Dec 2017
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

    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

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


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

    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.

  13. #13
    Join Date
    Dec 2017
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

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

    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

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


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

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

  15. #15
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

    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.
    Last edited by mpgmike; - 30th December 2017 at 20:12.

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


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

    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.

  17. #17
    Join Date
    Dec 2017
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: String & WRITE

    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 !

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. Need the code to write to a memory
    By Hamlet in forum General
    Replies: 0
    Last Post: - 20th August 2007, 01:22
  3. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 18:27
  4. Visual Basic 6 & Access 2000
    By Demon in forum Off Topic
    Replies: 33
    Last Post: - 7th September 2006, 05:39
  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