Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to write Ascii to Hex

mini

Member
Hello Senior Member,
I wish to write hex files (EDATA (EEPROM Memory Write)).
Mean :
I write into a my code hex file, something like on below :
Code:
Symbol AScii_Hexa = 20
     EData AScii_Hexa,  $53 $61 $6D $70 $6C &65

Then once the code is generated, I get the hex file result.
this hex file, we can see on a Debugger into EEProm with AScii text "Sample", but it won't work.

Tried to an user manual, but there is AScii
Code:
 Dim i As Word
   i = Asc("ABC")
I Did not expect to write like this, but I tried this and it didn't work either.
So I hope and need help from you seniors here.

Thanks.
Mini
 
ASC only works on one byte (one character), so ASC("ABC") will return the value of "A" only, which would be 65 decimal.
Otherwise, you may have to write a function/subroutine to create a hex string. Those usually involve dividing by 16 and tracking the remainders (0 to 15) and map those to hex characters. There are examples on the Internet for this type of routine.
I don't think Oshonsoft has a built in function for this, except maybe serial output formatting.

As for putting values into EEPROM, just declare the values and the compiler will insert them like:
EEPROM 30, 0x01, 0x15, 0xa0 'T1CON,T1H,T1L, 30ms
EEPROM 35, 0x11, 0x77, 0x48 'T1CON,T1H,T1L, 35ms
EEPROM 40, 0x11, 0x63, 0xc0 'T1CON,T1H,T1L, 40ms
EEPROM 45, 0x11, 0x50, 0x38 'T1CON,T1H,T1L, 45ms
EEPROM 50, 0x11, 0x3c, 0xb0 'T1CON,T1H,T1L, 50ms
 
Last edited:
I think what you need is the structure "LookUp"

UART_Init 4800
Dim char As Byte
Dim index As Byte


For index = 0 To 12
char = LookUp("0123456789ABC"), index
UART_Write char 'By changing the "Uart_Write" command to another you can send the ASCII to any other peripheral.
Next index
UART_Write CrLf, "End", CrLf

End
 
Sagor1 & DogFlu66,
Thank you for your guys information according my question.

Sagor1,
I tested your pieces code, I look on my Debugger "EEPROM" there are some pieces text (ASCII) word, I think this way more closed with my expected, I tried to check your hex into ascii converter then the result is almost same, but something like irregular sentences.

DogFlu66,
In notepad++, I checked I see hex files but I can't see on my Debugger "EEPROM" there are no ASCII.

Here are my sample code :
Code:
Define CONF_WORD = 0x3f41
Define CLOCK_FREQUENCY = 4

'#Processor PIC16F628A
AllDigital

TRISA = %11111011
TRISB = %00000000

PORTB.7 = 1  'LED on PORTB is On

UART_Init 4800  'DogFlu66

'Declaration
Dim ledIndicator As Byte
Dim char As Byte  'DogFlu66
Dim index As Byte  'DogFlu66

'LCD 2x16
Define LCD_LINES = 2
Define LCD_CHARS = 16
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 0
Define LCD_RSREG = PORTA
Define LCD_RSBIT = 2
Define LCD_EREG = PORTA
Define LCD_EBIT = 3
Lcdinit 0

'Show First Time On LCD
Lcdcmdout LcdLine1Pos(1)
Lcdout "PCB Etching"  'First Line
Lcdcmdout LcdLine2Pos(1)
Lcdout "Mini Osh"  'Second Line

'---------DogFlu66 ----------------------------
For index = 0 To 12
char = LookUp("0123456789ABC"), index
UART_Write char  'By changing the "Uart_Write" command to another you can send the ASCII to any other peripheral.
Next index
UART_Write CrLf, "End", CrLf

'End

For ledIndicator = 59 To 0 Step -1  'CountDown till reach 0

Lcdcmdout LcdLine2Pos(15)
Lcdout #ledIndicator  'Place number for CountDown in Second Line C15
If ledIndicator < 10 Then Lcdout " ", #ledIndicator
WaitMs 990  'Wait till reach
Next ledIndicator
Lcdcmdout LcdClear  'Off LCD
PORTB.7 = 0  'Then LED On PORTB is Off

'Continue to main function
main:
'Test function Hex stored to eeprom and show as an ASCII

'--------- SAGOR -------------------------------
EEPROM 30, 0x01, 0x15, 0xa0  'T1CON,T1H,T1L, 30ms '???
EEPROM 35, 0x11, 0x77, 0x48  'T1CON,T1H,T1L, 35ms '??w?
EEPROM 40, 0x11, 0x63, 0xc0  'T1CON,T1H,T1L, 40ms '??c
EEPROM 45, 0x11, 0x50, 0x38  'T1CON,T1H,T1L, 45ms '??P?
EEPROM 50, 0x11, 0x3c, 0xb0  'T1CON,T1H,T1L, 50ms '??<?


Attached files is a result test of hex file into Debugger "EEPROM".

Thanks,
Mini.
 

Attachments

  • DogFlu66.JPG
    DogFlu66.JPG
    72.9 KB · Views: 51
  • Normal Code.JPG
    Normal Code.JPG
    56.7 KB · Views: 56
  • Sagor.JPG
    Sagor.JPG
    72.7 KB · Views: 51
  • Test Function EEprom.bas
    1.7 KB · Views: 39
  • Oshonsoft Eeprom.rar
    178.3 KB · Views: 45
Sagor1 & DogFlu66,
Here is attached sample I did in another compiler, I write into compiler Hex files format then into Debugger in EEPROM side, I can see ASCII word text.

Thanks.
Mini.
 

Attachments

  • Sample.JPG
    Sample.JPG
    79 KB · Views: 45
In my version 5.33 of the IDE it would have to be this other way:

For index = 0 To 12
char = LookUp("0123456789ABC"), index
EEPROM_Write index, char
Next index

And EEPROM would only be to incorporate series of bytes:

EEPROM 0, "0", "1", "2" ....... "C"
 
Sagor1, DogFlu66, Ian Roger,
Thank you for all of you guys, has been given me an idea.
After few times tested, then I found how to do, I did it refer to Sagor1 code, based him code then it has been working well.

Thanks,
Mini. :):)
 
EEPROM and EEPROM_WRITE have not been updated and still work with the old internal engine of the IDE, so they do not accept all types of data, but we can create our own function to bypass the IDE restrictions, and make data entry more friendly If necessary.

'Writes a string to the EEPROM.
'Pic18 v5.33
'----------------------------------------------
UART_Init 4800
Dim index As Byte

index = WriteEEPROM(0, "Fhilip..........")
index = WriteEEPROM(index, "Carlos..........")
index = WriteEEPROM(index, "Machine 1 on....")
index = WriteEEPROM(index, "0123456789ABCDEF")
index = WriteEEPROM(index, "0123456789ABCDEF")
index = WriteEEPROM(index, "0123456789ABCDEF")
index = WriteEEPROM(index, "0123456789ABCDEF")
WriteEEPROM(index, "0123456789ABCDEF")

UART_Write "End", CrLf

End
'Writes a string to the EEPROM and returns the next memory address to write to.
'_address: Starting memory address.
'_string: String to write to the EEPROM.
Function WriteEEPROM(_address As Word, _string[16] As String) As Word
Dim _index As Word
_index = 0
While _string(_index) > 0 'Terminates if the end of the string is reached.
EEPROM_Write _address, _string(_index) 'Writes a character to the EEPROM.
_index++ 'Moves to the next character in the string to be stored in the EEPROM.
_address++ 'Moves to the next EEPROM address to write to.
Wend
ReturnValue _address 'Returns the next eeprom address.
End Function
 
Ian Rogers, DogFlu66,
Thanks Ian.

DogFlu66,
I am sure your piece code is working great, but it is not what I expected.
Watch my video, I did get your hex files on Debugger, and that your code can see only into Flash Column, but won't work into EEPROM Column, that my expected ASCII Text should appear into EEPROM Column.
I am sure it is my mistake to explain here.

Please, watch my video attached..., Up..., can't attached a video.
Here a linkLink

Thanks,
Mini.
 
Last edited:
When the program is compiled and subsequently downloaded to the microcontroller, the code is stored in flash memory. When the code is executed, the strings are sent to the EEPROM. This process ensures that the specified data is transferred to the EEPROM during program execution, allowing data preservation between power cycles or reboots. Additionally, this method requires the programmer to incorporate a routine to check whether the EEPROM was written during a previous boot (so as not to repeat the EEPROM burn) or otherwise by hardware using a pin used as a jumper to enable writing or not. in the EEPROM during boot.

In short, if the program is not executed at least once there will be no data in the EEPROM.
 
When the program is compiled and subsequently downloaded to the microcontroller, the code is stored in flash memory. When the code is executed, the strings are sent to the EEPROM. This process ensures that the specified data is transferred to the EEPROM during program execution, allowing data preservation between power cycles or reboots. Additionally, this method requires the programmer to incorporate a routine to check whether the EEPROM was written during a previous boot (so as not to repeat the EEPROM burn) or otherwise by hardware using a pin used as a jumper to enable writing or not. in the EEPROM during boot.

In short, if the program is not executed at least once there will be no data in the EEPROM.
DogFlu66,
Thank you so much for the explanation, it's very helpful for me to understand and for my knowledge.

Thanks,
Mini.
 
View the .Hex with several programs, and whenever the simulator is run once to preload the data into the EEPROM and a new compilation is performed, they are added to the .Hex.
 

Attachments

  • Test_01.jpg
    Test_01.jpg
    558.5 KB · Views: 53
  • Test_01 (2).jpg
    Test_01 (2).jpg
    347.6 KB · Views: 55
In case I don't explain it well, everything that is loaded into "EEPROM Memori Editor" will be added to the .hex regardless of the program that is compiled, whether it uses the EEPROM or not.
 

Latest threads

Back
Top