It is currently 20 Apr 2018 01:52

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Bin to hex code snippet
PostPosted: 03 Mar 2018 11:29 
Offline

Joined: 18 Jun 2008 11:43
Posts: 3751
Location: Nieuwpoort, Belgium
Hi,

Sometimes it is necessary to generate a hex output from a binary content (128K bytes max), e.g. the content of flash memory.
This code sends the .hex lines to the uart (the PC can then make a file of the received lines).
The binary content is gathered by means of 'Read_Block', 16 bytes each time. Adapt this routine to the binary data source you read from.
Make sure the NumberOfBytes is a multiple of 16.
Do not forget to init the uart...

var BlockAddr, NumberOfBytes: DWord;
    Str: string[100];
    TmpStr: string[10];
    Nr, Chksum : byte;

    BlockAddr := 0;

    Uart_Write_line('');
    while BlockAddr < NumberOfBytes do
    begin
      if (BlockAddr and $ffff) = 0 then // 64K boundary
      begin
        ChkSum := 6;
        Str := ':02000004';
        if (BlockAddr > $ffff)
        then
        begin
          TmpStr := '0001';
          ChKSum := ChkSum + 1;
        end
        else
        begin
          TmpStr := '0000';
        end;
       
        StrCat(Str, TmpStr);
        ChkSum := - ChkSum;
       
        bytetohex(chksum, TmpStr); strcat(Str, TmpStr);
        Uart_write_line(Str);
      end;
         
      Read_Block(BlockAddr, 16);
     
      Chksum := 0;
      Str := ':';
      ByteToHex(16, TmpStr);           StrCat(Str, TmpStr); ChkSum := ChkSum + 16;
      Byte2Hex(Hi(BlockAddr), TmpStr); StrCat(Str, TmpStr); ChkSum := ChkSum + Hi(BlockAddr);
      Byte2Hex(Lo(BlockAddr), TmpStr); StrCat(Str, TmpStr); ChkSum := ChkSum + Lo(BlockAddr);
      Byte2Hex(0, TmpStr);             StrCat(Str, TmpStr);
     
      For Nr := 0 to 15 do
      begin
        Byte2Hex(Block[Nr], TmpStr); StrCat(Str, TmpStr); ChkSum := ChkSum + Block[Nr];
      end;

      ChkSum := - ChkSum;
      Byte2Hex(ChkSum, TmpStr); StrCat(Str, TmpStr);
     
      Uart_write_line(Str);
     
      BlockAddr := BlockAddr + 16;

    end;


The code for the uart routines:
procedure UART_Write_CRLF;
begin
{$IFDEF P16}
  Uart1_Write(#13);
  Uart1_Write(#10);
{$ELSE}
  Uart_Write(#13);
  Uart_Write(#10);
{$ENDIF}
end;

procedure UART_Write_Line(var UartText : string[255]);
begin
{$IFDEF P16}
  UART1_Write_Text(UartText);
{$ELSE}
  UART_Write_Text(UartText);
{$ENDIF}
  UART_Write_CRLF;
end;

_________________
Kind regards,
Dany.
(happy user of mP PRO for PIC and dsPIC30/33 and PIC24, PicFlash2, RTClock, Max232 board, Ethernet PROTO board, MMC/SD Card Proto board, Yo2Lio's and Janni's libs)
http://www.rosseeld.be/DRO/PIC/index.htm


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: