Jump to content

Working with char *


goule
 Share

Recommended Posts

Hello,

I'm trying to work with char * to assign to each sensor its name, plus a parameter name (like "filter","cutoff", etc)

Even when I include both <malloc.h> and <string.h> it keeps on telling me at the link step that it needs some undefined _malloc symbol ...  >:( what am I doing wrong ?  :-\

Thanks,

Goule

Link to comment
Share on other sites

Hi Goule,

In general it isn't a good idea to use dynamic memory allocation for strings which will never change (or especially: those size will never change, and which will preloaded by the C program each time after power-on). Because this means that the same data is stored twice: one time in flash (the routine which preloads the data), and one time in RAM. And RAM is very costly on a PIC!

The simplest method is the use of constant arrays. A constant array is just a table which is stored in flash, and from which the C program reads directly whenever the array is accessed. However, the program cannot write into the table - if this is required, a special technique has to be used, but I guess that this is not for interest in your case.

Here an example:


const char parameter_name[8][16] = {
// <-- 15 chars ->
  "Parameter 1    ",
  "Parameter 2    ",
  "Parameter 3    ",
  "Parameter 4    ",
  "Parameter 5    ",
  "Parameter 6    ",
  "Parameter 7    ",
  "Parameter 8    ",
};
[/code]

Note that the size of each string must be equal. In this example each string contains 15 bytes, the 16th byte is the 0x00 delimiter, which will be added by the C compiler automatically when you are using strings.

So, this array format is very handy, because now you can print out a string in the following way:

MIOS_LCD_PrintCString(parameter_name[pin]);

(the 'pin' variable contains 0..7)

As a sidenote: it's also possible to store strings in a BankStick - if this is for interest, I could create an example which demonstrates, how to preload a BankStick via MIOS Studio

Best Regards, Thorsten.

Link to comment
Share on other sites

Thorsten, thanks a lot, that was exactly what I was needing !!!  :D 8)

I'll have to store quite important quantities of char arrays in BankStick so I'd be really interrested about the example source code you told me, when you'll have time.

Best Regards,

Goule

Link to comment
Share on other sites

Ok, preloading the BankStick isn't that difficult, since MIOS provides an upload mechanism which is very similar to the flash/EEPROM upload. The data needs to be located to 0x400000 - and thats all :)

Here an example. I'm using an assembler program, so that it is easier to locate data.

Filename: bs_1.asm


;; preload file for BankStick #1

        list    p=18f452

        ;; BankStick address range:
        ;; 32k BankStick (24LC256): 0x400000-0x407fff
        ;; 64k BankStick (24LC512): 0x400000-0x40ffff
       
        org    0x400000

        ;; 16 bytes of data
        db      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
        db      0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f

        ;; a string
        db      "This string is stored in BankStick!", 0x00

        ;; some words (16 bit)
        dw      0xdead, 0xbeef
        dw      0xaffe, 0xdead

        ;; another byte at a specific address
        org    0x400400
        db      0x12

        ;; DONT FORGET THE END!
        END
[/code] the .hex file can be created with following command: gpasm bs_1.asm Now you can upload bs_1.hex with MIOS Studio - the BankStick number (#1..#8) can be selected in the upload window. Within the C program you can use the MIOS_BANKSTICK_Read(address) function to read from the BankStick. With MIOS_BANKSTICK_Write(address, data) you can change the data, but note that a write takes 2 mS for each byte The address range is 0x0000...0x7fff for a 32k BankStick, and 0x0000..0xffff for a 64k BankStick If multiple BankSticks are connected to the core, the stick has to be selected with MIOS_BANKSTICK_CtrlSet(bs_number) before. bs_number range: 0..7 Example for printing a 16 character string, located at 0x0010-0x1f, from BankStick 3 (the 4th BankStick)
[code]
  unsigned char i;

  MIOS_LCD_CursorSet(0x00);
  MIOS_BANKSTICK_CtrlSet(3);
  for(i=0; i<16; ++i)
    MIOS_LCD_PrintChar(MIOS_BANKSTICK_Read(0x0010 + i));
For a faster load/store the MIOS_BANKSTICK_PageRead and MIOS_BANKSTICK_PageWrite functions are available, they handle 64 bytes at once. This is especially useful for writes (64 bytes are stored within 2 mS). However, when using these functions, you need a 64 byte buffer (an array of 64 "unsigned char") which is located to an "aligned" address, means: 0x40, 0x80, 0xc0, 0x100, 0x140, 0x180, 0x1c0, 0x200, 0x240, 0x280, 0x2c0 - all other address ranges are already allocated by MIOS or the stack. So, in other words: using byte writes and reads is the easiest method. Best Regards, Thorsten.



			
		
Link to comment
Share on other sites

Thank you so much; I'll test all this when I'll receive my banksticks (I ordered 8, I expect to get them this week).

By the way I'm very pleased with my MIDI filter : I think it's the only simple way that I found to integrate exotic components into the MBHP platform (distance or pressure sensors, etc.) without adding some electronics to the core.  I'm thinking about writing some thread dedicated to this particular topic when I'll have the final code written.

Best Regards and thanks again,

Goule

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...