Jump to content
TTom

Sample Programs Using Strings

Recommended Posts

I need to find some sample programs using strings. Can anyone help me ? I need to send 66 packets of information out PORTA that are 2 bits wide.

Share this post


Link to post
Share on other sites

Actually I need to get more information on Arrays, not strings.

I need to know how to:

Fill and array with values, and access that array.

 

Share this post


Link to post
Share on other sites

According to the Reference manual on page 50 I don't see why my code fails.

I have started with :

 

int arr[6]@0xA0

#define arr[1,2,3,4,5,6]

 

If I can make an array of 6, I should be able to make an array of 66

Share this post


Link to post
Share on other sites

Maybe I should just use the RETLW command and go to assembler. From what I can determine Sourceboost BoostC has about the worst error declarations I have seen in any IDE I have worked with. i.e "general error" - that is vague and useless.

Share this post


Link to post
Share on other sites

Amazing... I forgot to put the semicolon at the end of the lines of code.

int arr[6]@0xA0

#define arr[1,2,3,4,5,6]

When semicolon added it compiles

Share this post


Link to post
Share on other sites

Hi

 

Ok it compiles.

 

But I'm not understanding the "#define". What is it's purpose?

 

 

Best regards

Jorge

Share this post


Link to post
Share on other sites

Jorge... I was wondering where you were.. I am new to "C" , so I will make a lot of mistakes...

What should the code look like ? I just tried anything I thought might work.

How do I access the values in the array ? I am assuming I need to use pointers, but I don't know how. I have been looking on the Internet, but the programs are very involved... I need to start out simple and then go to complex.

Share this post


Link to post
Share on other sites

Hi

 

Any special reason to declare the array at a specific address?

 

As far as "C" goes, an array is simply a contiguos RAM space sized to store the number of elements of the data type in the declaration.

 

char Table[20];  // allocates 20 bytes of contiguos RAM.
int Table[20]; // allocates 40 bytes of contiguos RAM (assuming integers are 16 bit in size).

For an initialized array you can do without the explicit size and let the compiler compute the needed space for the initialization data.

 

char Table[] = 'a', 'b', 'c', 'd';// alocates 4 bytes of contiguos RAM

When the initialization data is a string, an extra byte is allocated for the NULL ('\0') terminator of the string.

 

char Text[] = "this is a string"; // allocates 17 bytes of contiguos RAM, 16 chars and the '\0' terminator.
 
You can use either a pointer or an index to access any member of the array.
As arrays are chuncks of RAM, the name of the array (Table, Text,...) is also the pointer to its first element so an element can be addressed in several equivalent ways like in this example
[code]
int Table[5];
int *ptr = Table;
 
// Expressions that access te 4th element of the array.
Table[3]
*(Table+3)
*(ptr+3);

Bear in mind the when it comes to arithmetics involving pointers, the size of the addressed data type is taken into account, so in reality in the above expressions

Table+3 is in fact Table+3*sizeof(int) --> Table + 6 if we are dealing with 16 bit integers.

the same goes for ptr+3.

 

So if you use an index or a pointer is just a question of two things, wich way best fits the algorithm you are coding and also a great deal of personal preference.

 

So far this is generic 'C'.

 

In the 8 bit PIC world, there are some limitations due to the memory organization of the PICs

Things like the RAM bank size or the existence or absence of a linear address space can impose some limitations on the size or handling of the arrays or other data structures.

Also the separation betwen program memory (ROM) and data memory (RAM) as its impacts.

Boost C uses the "rom" modifier to place an initialized array in program memory, generaly coded as the (in)famous "retlw" table in the smaller PICs.

As addressing program memory or data memory in a PIC uses different hardware and address ranges, the usage/mixing of pointers varies from one PIC family to the other, depending on the hardware specifics, just like when we do it in ASM.

 

Take a look at the BoostC manual on the subjects of the "rom" modifier, pointers and arrays. All the hardware related speciifcs and restrictions are explained there.

 

 

HIH

 

Best regards

Jorge

Share this post


Link to post
Share on other sites

So.. If I create an array with :

 

int arr[6]@0xA0 = {65,22,39,48,57,66};

int x;

 

I want to write an expression that takes the value of the first number and put that value in a variable, say x... Such as:

x = arr[0]; But this does not compile.

Then I want to be able to put a variable into the expression instead of using a fixed value so I can sequence through the array.

Share this post


Link to post
Share on other sites

int arr[6]@0xA0 = {65,22,39,48,57,66};

 

won't work

int arr[] = {65,22,39,48,57,66};

will

 

why the @0xA0 there is no need to specify a location for the array unless you have a specific need

Share this post


Link to post
Share on other sites

richardc... The reason I put the address location is because that's the way the BoostC document showed on page 50. I realize now that I don't need to do that but I was just trying to do something to make it work.

The line....int arr[6]@0xA0 = {65,22,39,48,57,66}; will compile so I figured it worked.

But my problem now is to try to get the values out of the array and put them in a variable. Say:

x = arr[0]; But this code doesn't compile.

I would also like to write an expression that would contain a variable that would point to a value in the array.

example : x = Some expression using the variable y to point to a specific value, then increment y and get the next value.

Do you know how to do this ?

Share this post


Link to post
Share on other sites

So this is basically what I want to do:

 

int x;

int y;

int arr[] = {65,22,39,48,57,66} // Create an array

x = arr[y]; // where y is some value from 0 to 5 ..... Get some value out of the array

Share this post


Link to post
Share on other sites

int x;

 

int y=0;

 

int arr[] = {65,22,39,48,57,66} ;// Create an array

 

x = arr[y]; // where y is some value from 0 to 5 ..... Get some value out of the array

 

should result in x == 65

 

if it won't compile then the error is elsewhere , you might get more help if you post your entire code ,snippets are never going to address some issues

Edited by richardc

Share this post


Link to post
Share on other sites

Hi

 

richardc... The reason I put the address location is because that's the way the BoostC document showed on page 50. I realize now that I don't need to do that but I was just trying to do something to make it work.

The line....int arr[6]@0xA0 = {65,22,39,48,57,66}; will compile so I figured it worked.

 

 

You did copy the code but didn't read the explanation.

That declaration is an example on how to place the array at a given RAM address, if needed, not as a general rule.

 

 

HIH

 

Best regards

Jorge

Share this post


Link to post
Share on other sites

Hi

 

Here is an example on using arrays with indexes and with pointers

Compiles and runs OK.

 

 
#include <system.h>
 
int iSource[] = {1,2,3,4};
int iDest[4];
char sDest[5];
int x;
int iIdx;
int *iPtr;
char *sPtr;
 
void main(void)
  {
 
      // Copy from iSource to iDest reversing the values order
      // Accessing arrays with indexes
 
      for(iIdx=0; iIdx<4; iIdx++)
        {
        x = iSource[iIdx]; // get value by index
        iDest[3-iIdx] = x; // place it at destination in reverse order by index
        }
 
      // Filling sDest with a string with the digits from iSource
      // Accessing arrays with pointers
      for(iPtr = iSource, sPtr = sDest; iPtr < iSource + 4; iPtr++, sPtr++)
        {
        x = *iPtr; // get value by pointer
        *sPtr = '0' + (char)x; // convert to ASCII digit and place it at destination by index.
        }
      *sPtr = '\0'; // place the null terminator of the string
 
} // void main(void)

 

HIH

 

Best regards

Jorge

Share this post


Link to post
Share on other sites

Jorge... Oh yes, I read the explanation, and reread the explanation, and I understood it completely. Since I was going to use 66 values,( I start out with just 6 just to get it to compile) I wanted to start the array at the beginning of a user block of memory. If you will check out the 16F886 you will find that the starting address 0xA0 would give me 80 bytes of continuous memory. However at this point I see another problem. The "int" is 16 bits wide, so I am going to have to use the "char" instead of the integer since it is only 8 bits wide.

Anyway Jorge, I can't thank you enough for your detailed explanations. I really appreciate the time you have taken to help me get this thing going. I'm sure there will be other problems I will run into.

 

richardc... I also want to thank you for your help. I have worked with about 6 different microprocessors, and CPU chips since 1987. But like I said in the beginning of my post, all of my programming has been done in assembler. In fact the first computer I built using the 68701 chip I programmed in machine language. So my knowledge of "C" is NILL. I want to learn "C" so I can develop my projects quicker.

 

In Conclusion, Without you guys out there helping us who are completely ignorant in a particular field, I don't know what we would do. BoostC is fantastic after you get your program to compile, but if you can't get it to compile it is about useless without your help. Programming can be very rewarding, but it can also be the most aggravating process when you can't get it to compile.

Share this post


Link to post
Share on other sites

Hi

 

You are choosing the hard way to learn 'C'.

Microcontrollers have a lot of special hardware 'quirks' that originate a number of unconspicous details on any programming language.

 

I think you would better learn 'C' programming on a PC environment, or at least a platform with an O.S., like the Raspberry for example.

It would make your life easier and after getting confortable with 'C', then dive into the intrisincs of the PICs.

 

Programming in 'C' for a PIC is very close to do it in 'ASM', just easier to write.

It often reminds me of PL/M a programming language by Intel to their microcontrollers that I used with the 8051 and 8085 at University back in 1980.

By those days each microcontroler manufacturer had their own programming language, but as time passed they all move on to adapting 'C' to their hardware.

 

 

Best regards

Jorge

Edited by JorgeF

Share this post


Link to post
Share on other sites

Jorge... Thanks for the advise... I think I will try the specific code in "C" on my PC before trying it on the PIC.

I've never used the 8051, however I know it is a very popular chip. If my memory serves me correctly it was used in keyboards, maybe still is. I actually started with the 8080 back in 1980.

Share this post


Link to post
Share on other sites

Jorge.... The example you have given me is awesome. I have a question about the line:

*sPtr = '\0'; // Place the null terminator of the string

I am assuming that when the program gets done with the for loop, it will have incremented the sPtr to one memory location beyond the Destination string, Is that correct ?

Share this post


Link to post
Share on other sites

Hi

 

The 8080 is a microprocessor not a microcontroler.

Back then, I worked with the 8051 and 8085 in test and development kits (SDK-51 and SDK-85) from Intel.

Me and my group coleagues made a POS printer (narrow paper roll) controler has a school project with the 8051, and a ping-pong game in the screen of an oscilloscope (vectored beam) with the 8085. It even colected coins for playing. ;)

The 8080 was the microprocessor at the heart of the TRS-80 model II we used while learning high level programming using Pascal.

 

Meanwhile Intel released the 8051 specs and almost all manufacturers produce 8051 replicas, some, like Siemens, even enhanced the design. And I've already stumbled on a few Asian microcontrollers based on the 8051 design.

 

 

 

Back to the 21st century.

 

Yes the code I wrote places the NULL terminator at the 5th position of the array.

Strings in 'C' are arrays of bytes filled with ASCII characters and terminated with a NUL ('\0').

If you look at the declarations, the "sDest" is one byte bigger than the other arrays.

The 'for' loop fills in the ASCII code for the 4 digits and leaves 'sPtr' pointing to the 5th character were the NUL goes.

 

Best regards

Jorge

Share this post


Link to post
Share on other sites

Tomm

#include <system.h>

// First element of arr always starts at index 0 (zero)
// Last element is 5 in this case

#define ARRSIZE   6                                   // array size
unsigned char arr[ARRSIZE] = { 1, 2, 3, 4, 5, 6 };    // array of bytes
unsigned char x;                                      // to hold values from arr
unsigned char y;                                      // arr index

void main( void )
{
    x = arr[0];                                       // x holds 1
    x = arr[5];                                       // x holds 6

    // Loop to get all 6 arr elements and do something with it, one by one
    // starting with first until the last one

    for( y=0; y<ARRSIZE; y++ )
    {
        x = arr[y];                                   // x holds arr values one by one indexed by y
        //DoSomethingWithX( x );                      // do what you want with x
    }
}

This is the best and shortest snipet i can give you to your specific needs if i understood you right.

 

As mentioned before by others, forget absolute addressig of any variables, array, etc, Sourceboost do it for you.

So, you just need a variable to index the whole array elements.

Pointers is another choice, but considering you are starting with c "thing", i would suggest that you start with indexes, as the example above.

 

Code not tested but im 100% sure it compiles ok.

Start the simulator on your Sourceboost IDE and see it working.

 

br

Edited by joli

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
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...

×