|
|
View previous topic :: View next topic |
Author |
Message |
bells_electronics
Joined: 05 Dec 2009 Posts: 40
|
how to pass Arrays |
Posted: Thu Jan 21, 2010 2:49 am |
|
|
Code: |
const unsigned char message[] = "Hello World"; //11
.
void lcd_text(unsigned char[],unsigned char);
.
.
.
//////////////////////////////////////////////////////////
Main()
{
.
lcd_text(message,11);
.
}
//////////////////////////////////////////////////////////////
void lcd_text(unsigned char text[16],unsigned char length)
{
unsigned char x;
for(x=0 ; x<length ; x++) //16
{
lcddata(text[x]);
delay_ms(20);
}
}
|
Error in declaration |
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 21, 2010 3:14 am |
|
|
Several separate issues.
First critical one, you don't have an unsigned char array, but a _const_ unsigned char array. Do a search here for 'pointers to constants'.
Basically, because of the memory architecture of the PIC, constants are actually stored in a separate memory space to normal variables, so you can't have a pointer to a constant. There are ways of doing this, but they all cost a lot in terms of extra code used, performance etc..
If you remove the 'const' declaration, things will get 'closer' to working.
Then the second one. You are declaring the subroutine to receive an array of fixed length (16 characters), and then sending it an array of less length. Basically, just tell the routine that it is receiving an array, as in your prototype declaration. Prototypes, must match the real routine declaration _exactly_. This is why there is an error.
Third comment, why send a length at all?. The whole point about strings, is that they have a terminating entry, that marks their end. Take advantage of this. Look at the string functions in string.h, to see how this is handled.
Then, if you want to use constant strings, take advantage of the CCS shortcut. Tell your function that it is receiving a single integer, and the compiler will automatically call the function for each character in the string in turn.
Best Wishes |
|
|
Guest
|
|
Posted: Thu Jan 21, 2010 3:30 am |
|
|
Code: |
unsigned char message1[] = "Hello World"; //11
unsigned char message2[] = "Good Morning"; //13
unsigned char message3[] = "Bye Bye"; //7
unsigned char message4[] = "plz wait"; //8
.
.
.
void lcd_text(unsigned char[],unsigned char);
.
.
.
//////////////////////////////////////////////////////////
Main()
{
.
lcd_text(message1,11);
.
.
.
}
//////////////////////////////////////////////////////////////
void lcd_text(unsigned char text[],unsigned char length)
{
unsigned char x;
for(x=0 ; x<length ; x++) //16
{
lcddata(text[x]);
delay_ms(20);
}
}
|
still i'm having errors in declaration
i have 7 different messages to display
i don't want to repeat the code lines again & again by typing
Code: |
for(x=0 ; x<11 ; x++)
{
lcddata(message1[x]);
delay_ms(20);
|
|
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 21, 2010 4:05 am |
|
|
Use the shortcut I mentioned.
Code: |
void lcd_text(int8 chr) {
lcd_data(chr);
delay_ms(20);
}
const char message1[] = "Hello World";
const char message2[] = "Good Morning";
const char message3[] = "Bye Bye";
const char message4[] = "plz wait";
//Then call with:
lcd_text(message1); //change message number as needed.
|
As I said, you don't need to fiddle around with counters or anything.
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Thu Jan 21, 2010 8:59 am |
|
|
Anonymous wrote: | Code: |
unsigned char message1[] = "Hello World"; //11
unsigned char message2[] = "Good Morning"; //13
unsigned char message3[] = "Bye Bye"; //7
unsigned char message4[] = "plz wait"; //8
.
.
.
void lcd_text(unsigned char[],unsigned char);
.
.
.
//////////////////////////////////////////////////////////
Main()
{
.
lcd_text(message1,11);
.
.
.
}
//////////////////////////////////////////////////////////////
void lcd_text(unsigned char text[],unsigned char length)
{
unsigned char x;
for(x=0 ; x<length ; x++) //16
{
lcddata(text[x]);
delay_ms(20);
}
}
|
still i'm having errors in declaration
i have 7 different messages to display
i don't want to repeat the code lines again & again by typing
Code: |
for(x=0 ; x<11 ; x++)
{
lcddata(message1[x]);
delay_ms(20);
|
|
Try this:
Code: |
void lcd_text(unsigned char*,unsigned char);
.
.
.
void lcd_text(unsigned char* text,unsigned char length)
{
unsigned char x;
for(x=0 ; x<length ; x++) //16
{
lcddata(*(text+x));
delay_ms(20);
}
}
|
Basically if you want to pass an array to a function you have to declare a pointer that points to the memory location of the first element of the array. |
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 21, 2010 9:10 am |
|
|
For constant strings, do _exactly_ what I posted. No length, and declaring the function to take a _single_ int8 variable. Don't change the code.
The point is that CCS has a 'special' built in way of handling constant strings. If you call a function that takes a _single_ integer variable, with the name of a constant sting, it _automatically_ calls the function once for each character in the string, giving exactly what you want, _without_ using a counter. What I have posted, will run as it is, and do exactly what you want.
The alternative, is to copy the string into RAM, and then you can do it using pointers, but _why waste the time, and space_.
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Thu Jan 21, 2010 9:20 am |
|
|
Ttelmah wrote: | For constant strings, do _exactly_ what I posted. No The alternative, is to copy the string into RAM, and then you can do it using pointers, but _why waste the time, and space_.
Best Wishes |
Sorry, I just saw his original code when the arrays don't have a constant declaration. |
|
|
Ttelmah Guest
|
|
Posted: Thu Jan 21, 2010 10:24 am |
|
|
I wasn't really worried about your reply, it is perfectly correct.
However his 'very original' post uses constant strings, and for what he is showing, the CCS shortcut is a really neat and easy way to go for these, and will save him a huge amount of RAM....
If you look, he seems 'obsessed' with having to pass length data for the strings (even for non constant strings, this is unnecessary, and just another potential source of errors).
For the 'pointer' version, I'd simplify what you posted, and just use:
Code: |
void lcd_text(char *text)
{
while (*text != '\0') {
lcddata(*(text++));
delay_ms(20);
}
}
|
Best Wishes |
|
|
|
|
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 vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|