CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to copy array from ROM in RAM ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mike.B



Joined: 04 Aug 2014
Posts: 5

View user's profile Send private message

How to copy array from ROM in RAM ?
PostPosted: Mon Aug 04, 2014 9:38 am     Reply with quote

Hello !

I have just bought a PICKIT3 from Microchip (my first PIC !) and I have got problems with my code.
My goal : copy values from an array in ROM into an array in RAM.

Here is my code :

Code:
#include <18F45K20.h>

const int array1[8]={7,8,9,10,15,14,12,22};
int array2[8];

void main(){
   int i=0;
   for(i=0;i<8;i++){
      array2[i]=array1[i];
   }
}


This code works ! All values from array1 are in array2.

Now, I want to do the same thing but I would like to use a function.

Here is my new code :


Code:

#include <18F45K20.h>

const int array1[8]={7,8,9,10,15,14,12,22};
int array2[8];

void transfer(const int array1[8], int array2[8]){
   int i=0;
   for(i=0;i<8;i++){
      array2[i]=array1[i];
   }
}

void main(){
   transfer(array1,array2);
   
}


Unfortunately, it doesn't work.
Here are my errors :

Code:
*** Error 28 "main.c" Line 6(25,28): Expecting an identifier
*** Error 43 "main.c" Line 6(31,32): Expecting a declaration
*** Error 43 "main.c" Line 6(32,33): Expecting a declaration
*** Error 43 "main.c" Line 6(33,34): Expecting a declaration
*** Error 43 "main.c" Line 6(34,35): Expecting a declaration
--- Info 300 "main.c" Line 4(5,11): More info:   First Declaration of array2
*** Error 31 "main.c" Line 6(40,46): Identifier is already used in this scope
*** Error 43 "main.c" Line 6(47,48): Expecting a declaration
*** Error 43 "main.c" Line 6(48,49): Expecting a declaration
*** Error 43 "main.c" Line 6(49,50): Expecting a declaration
*** Error 43 "main.c" Line 6(50,51): Expecting a declaration
*** Error 43 "main.c" Line 8(1,4): Expecting a declaration
*** Error 43 "main.c" Line 8(4,5): Expecting a declaration
*** Error 48 "main.c" Line 8(5,6): Expecting a (
*** Error 43 "main.c" Line 8(7,8): Expecting a declaration
*** Error 43 "main.c" Line 8(8,9): Expecting a declaration
*** Error 48 "main.c" Line 8(9,10): Expecting a (
*** Error 43 "main.c" Line 8(11,12): Expecting a declaration
*** Error 43 "main.c" Line 8(12,13): Expecting a declaration
*** Error 48 "main.c" Line 8(13,14): Expecting a (
*** Error 43 "main.c" Line 8(16,17): Expecting a declaration
*** Error 43 "main.c" Line 8(17,18): Expecting a declaration
*** Error 48 "main.c" Line 9(1,7): Expecting a (
*** Error 48 "main.c" Line 9(8,9): Expecting a (
*** Error 43 "main.c" Line 9(10,11): Expecting a declaration
*** Error 48 "main.c" Line 9(11,17): Expecting a (
*** Error 48 "main.c" Line 9(18,19): Expecting a (
*** Error 43 "main.c" Line 9(20,21): Expecting a declaration
*** Error 43 "main.c" Line 10(1,2): Expecting a declaration
*** Error 43 "main.c" Line 11(1,2): Expecting a declaration
*** Error 58 "main.c" Line 14(10,16): Expecting a close paren



How can I fix that ?

Thank you very much and have a nice day !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 11:42 am     Reply with quote

This revised version works. It displays the following in the MPLAB
simulator output window:
Quote:
7 8 9 10 15 14 12 22

This was tested with compiler vs. 5.026.
Code:

#include <18F45K20.h>
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

const int8 array1[8]={7,8,9,10,15,14,12,22};
int8 array2[8];

void transfer(int8 array1[], int8 array2[])
{
 int8 i=0;
 
 for(i=0;i<8;i++)
    {
     array2[i]=array1[i];
    }
}

//==========================
void main()
{
int8 i;

transfer(array1,array2);

 for(i=0;i<8;i++)
    {
     printf("%d ", array2[i]);
    }

printf("\r");

while(1);   
}
Mike.B



Joined: 04 Aug 2014
Posts: 5

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 1:39 pm     Reply with quote

Hello PCM programmer !

Thank you for your help, it's nice.
It works perfectly !
Just a little question, I use to monitor my values via the WATCH. You seem to use RS232. Where do you display your values ?
In the MPLAB simulator output window, I have got nothing.

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 1:49 pm     Reply with quote

I still mostly use MPLAB vs. 8.92, so the following instructions apply to
that version. This link tells how to display UART output in MPLAB simulator:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1
Mike.B



Joined: 04 Aug 2014
Posts: 5

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 2:46 pm     Reply with quote

Alright, thank you ! I didn't know we could do that.

Now, I would like to do the same thing but I would like to use matrix instead of array.

I use your code and try to adapt it. However, it doesn't work.
Here is the code :



Code:

-----------

#include <18F45K20.h>
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

const int8 matrix1[3][2]={{1,1},{1,1},{2,2}};
int8 matrix2[3][2];

void transfer(int8 m1[][], int8 m2[][])
{
 int8 i=0;
 int8 j=0;
 
for(j=0;j<2;j++){ 
 for(i=0;i<3;i++)
    {
     m2[i][j]=m1[i][j];
    }
}
}

//==========================
void main()
{
int8 i;
int8 j;

transfer(matrix1,matrix2);

for(j=0;j<2;j++){
 for(i=0;i<8;i++)
    {
     printf("%d ", matrix2[i][j]);
    }
}

printf("\r");

while(1);   
}


I don't understand where is the problem because I have just done a few modifications.
What do you think ?
Maybe I should seek about pointer (I am going to look for more information about it because it's new to me)

Thanks for your help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 3:08 pm     Reply with quote

I'm tied up for the next 2-3 hours. If nobody else helps you, when I come
back I'll look at the problem.
luckyluke



Joined: 18 Apr 2006
Posts: 45

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 3:38 pm     Reply with quote

Quote:
for(i=0;i<8;i++)
Mike.B



Joined: 04 Aug 2014
Posts: 5

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 3:46 pm     Reply with quote

Alright, no problem, I understand Very Happy

As for me, I am keep seeking.
Actually, I think I succeeded by using pointers.

I have read this statement :
int matrix[nb_rows][nb_columns] is equivalent to int *(matrix + (nb_columns * rows) + columns)

I have admitted that (I know it is bad...)

Here is my code :

Code:
#include <18F45K20.h>
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

const int8 matrix1[3][2]={{20,70},{120,110},{40,30}};
int8 matrix2[3][2]={0};

void transfer(int8 **m1, int8 **m2)
{
 int8 i=0;
 int8 j=0;
 
for(j=0;j<2;j++){ 
 for(i=0;i<3;i++)
    {
   *(m2+(1*i)+j)=*(m1+(1*i)+j);
    }
}
}

//==========================
void main()
{
int8 i;
int8 j;

transfer(matrix1,matrix2);

for(j=0;j<2;j++){
 for(i=0;i<3;i++)
    {
     printf("%d ", matrix2[i][j]);
    }
}

printf("\r");

while(1);   
}


It works !

If someone can explain me why it works and why the previous code doesn't work, it will great !
It is important to me to understand.

Thanks


Last edited by Mike.B on Mon Aug 04, 2014 3:51 pm; edited 1 time in total
Mike.B



Joined: 04 Aug 2014
Posts: 5

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 3:50 pm     Reply with quote

Hello luckyluke !

Thanks for answering, I have just seen it.
I will fix that.
Actually, I checked the value by using the WATCH.
So, I don't think it is the main problem. But it is a step forward Smile
luckyluke



Joined: 18 Apr 2006
Posts: 45

View user's profile Send private message

PostPosted: Mon Aug 04, 2014 4:02 pm     Reply with quote

Code:

void transfer(int8 m1[][2], int8 m2[][2])
{
 int8 i=0;
 int8 j=0;
 
for(j=0;j<2;j++){ 
 for(i=0;i<3;i++)
    {
     m2[i][j]=m1[i][j];
    }
}
}

what about like this?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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