Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC 16F84 I can't use char 2D array

Status
Not open for further replies.

T3STY

Full Member level 4
Full Member level 4
Joined
Apr 17, 2012
Messages
239
Helped
24
Reputation
48
Reaction score
24
Trophy points
1,308
Visit site
Activity points
3,715
I tried to declare a bidimensional array of chars of size [][16] which I will be using for storing the strings to display on a 16x2 LCD display. The code I'm using is this:
Code:
#define _XTAL_FREQ 4000000
#include <htc.h>

__CONFIG(FOSC_XT & WDTE_OFF & PWRTE_ON & CP_OFF);

/* LCD DRIVER */
#include "HD44780.h"
#include "HD44780.c"

// note: strings are *NOT* NULL terminated !
char Menu[][16]={
	"Item1",
	"Item2",
	"Item3",
	"Item4"
};

void main(){
	LCD_Initialize();
	
	LCD_WriteString(Menu[1]);
	
	while(1){
		// do nothing
	}
}

/*
// The same happens when using the array
// as function parameter declaration

void ShowMenu(char [][16]);

void ShowMenu(char M[][16]){
	for (char c=0; c<4; c++){
		LCD_Write(LCD_CLEAR);
		LCD_WriteString(M[c]);
	}
}

void main(){
	LCD_Initialize();
	
	ShowMenu(Menu);
	while(1){
		// do nothing
	}
}
*/
If the array is not used anywhere the compiler will only show a warning about it, then it will compile fine (probably it will skip compiling that array). If I try to use the array as a function parameter declaration (the block commented code after the main() function ) or as parameter of a function call (like LCD_WriteString(Menu[1]) ), the compiler will tell it can't find the necessary memory to allocate the array items for variable Menu. The output error is this:
Error [1250] C:\Users\T3STY\Documents\MPLAB\LCD V3\HD44780.c; 25. could not find space (64 bytes) for variable _Menu
********** Build failed! **********

When using the array as a function parameter declaration I may understand that the stack can get full with such a big array (even if I remember arrays are passed by reference/address, not copy of values). But even if I'm not using it as parameter, any call to the array will bring the error.

Now, I'm trying to understand what am I doing wrong for the compiler to complain about it. The whole code I wrote is not using all the available PIC memory, and the error before will show up even with an 'empty' program that only uses a function call with a call to an array item.

Could someone help me with this?
Thanks in advance.
 

OK... so the summary is... you can't use bidimensional char arrays large enough for your needs.

Is there any other way I can store strings in my program and access them array-like ?
 

If the contents of the array are fixed text, declare them as 'const' so the compiler places them in program memory instead of RAM. The problem is that in such small PICs there isn't enough continuous RAM space to hold the array. The 16F84 only has 68 bytes of RAM in total and you are asking the compiler to use 64 of them for the existing menu items alone.

Brian.
 

OK... so the summary is... you can't use bidimensional char arrays large enough for your needs.

Is there any other way I can store strings in my program and access them array-like ?

Try using an array of pointers to string

const char *days_list[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

since it is const, it wont even consume any RAM memory since it is stored in the program memory.

to display to LCD for example, just use

lcd_puts(days_list[0]); //Display "Sun" to LCD
 

I was suspecting the RAM was filling up too much.
Anyway, thank you very much guys! I think the const char * array is going to work just fine; I have just compiled with 4 strings of 16 chars and it went all fine! I guess adding a few, if needed, will not make much of a difference.

Thanks again to all of you!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top