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.

How to declare large arrays in C compiler for PIC18 devices

Status
Not open for further replies.

kekon

Full Member level 3
Joined
Sep 19, 2002
Messages
155
Helped
5
Reputation
10
Reaction score
3
Trophy points
1,298
Location
Poland, Białystok
Activity points
1,493
c18 large array

I use Microchip C compiler for PIC18 devices. My design consists PIC18F252 device which has about 1500 bytes of internal RAM. Despite of that I can't declare arrays larger than 256 bytes as the linker reports error that it is unable to reserve memory.

char buffer[255]; - this causes linker error
char buffer[120];- no error

It also concerns allocating data in assembly language with RES directive:

buffer RES 255 - this causes linker error as well
buffer RES 120 - no error

The same problem appears when other variables occupy more than 255 bytes of RAM. Why is this so ? Is there any solution ?
 

c18 large arrays

it seems like a common problem in some of compiler using with default settings.I am not sure but you may play with compiler options ,memory models something etc...
 

long arrays pic18f4550

You must define larger data fields in linker script. Here is a sample program that allocates array of 350 unsigned char's.
8O
 

defining arrays in mcc18

Thanks for help. I also found a solution in C compiler documentation at Microchip website. But they say any access to such arrays must be done by pointers as follows:

#include <p18f452.h>

#pragma udata FIELD
unsigned char r[350];
#pragma udata

void main (void)
{

char *ptr;

ptr = &r[0];
ptr[300] = 0x12;
}

This is probably due to the compiler which is unable to detect when it's neccesary to switch RAM bank when accesing "r" buffer directly. So probably an indirect access is correct:

ptr[300] = 0x12;
 

c18 array

Well as i saw on disassembly of my code C18 takes care of banked access when you directly write value like:
r[300]=0x12;

11: r[200]=0x12;
0000E2 0102 MOVLB 0x2
0000E4 0E12 MOVLW 0x12
0000E6 6FC8 MOVWF 0xc8, BANKED

and for indexed access C18 uses FSR0.

pp=120;
r[pp]=0x15;

12: pr=120;
0000E8 0E78 MOVLW 0x78
0000EA 0100 MOVLB 0
0000EC 6F8A MOVWF 0x8a, BANKED
0000EE 6B8B CLRF 0x8b, BANKED
13: r[pr]=0x15;
0000F0 0E00 MOVLW 0
0000F2 258A ADDWF 0x8a, W, BANKED
0000F4 6EE9 MOVWF 0xfe9, ACCESS
0000F6 0E02 MOVLW 0x2
0000F8 218B ADDWFC 0x8b, W, BANKED
0000FA 6EEA MOVWF 0xfea, ACCESS
0000FC 0E15 MOVLW 0x15
0000FE 6EEF MOVWF 0xfef, ACCESS

But if you say that Microchip recomands indirect access there must be a good reason for this.
 

Re: defining arrays in mcc18

kekon said:
Thanks for help. I also found a solution in C compiler documentation at Microchip website. But they say any access to such arrays must be done by pointers as follows:

#include <p18f452.h>

#pragma udata FIELD
unsigned char r[350];
#pragma udata

void main (void)
{

char *ptr;

ptr = &r[0];
ptr[300] = 0x12;
}

This is probably due to the compiler which is unable to detect when it's neccesary to switch RAM bank when accesing "r" buffer directly. So probably an indirect access is correct:

ptr[300] = 0x12;

I used that method with my 18F25K20 PIC and I just got the response:
Error - section 'FIELD' can not fit the section. Section 'FIELD' length=0x0000015e
Not enough RAM to use this method I assume? What other steps can I take?
 

Re: How to declare large arrays in C compiler for PIC18 devi

simce, thanks a mountain for that example.
Everyone else - I know it can probably be tempting to drop an RTFM from time to time, but lets be honest, who has the time to read all the manuals for everything. We do the best we can and when a project is due soon, we just need to know how to do what we need to do and we'll get back to the rest later (if we're lucky...).
I've written a few buckets of c in my day, but fooling with the linker for a PIC just isn't something I've taken time (or had to) learn about, my boss just wants results yesterday.
Thanks again, major props.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top