|
|
View previous topic :: View next topic |
Author |
Message |
Mike.B
Joined: 04 Aug 2014 Posts: 5
|
How to copy array from ROM in RAM ? |
Posted: Mon Aug 04, 2014 9:38 am |
|
|
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
|
|
Posted: Mon Aug 04, 2014 11:42 am |
|
|
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
|
|
Posted: Mon Aug 04, 2014 1:39 pm |
|
|
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
|
|
|
Mike.B
Joined: 04 Aug 2014 Posts: 5
|
|
Posted: Mon Aug 04, 2014 2:46 pm |
|
|
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
|
|
Posted: Mon Aug 04, 2014 3:08 pm |
|
|
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
|
|
Posted: Mon Aug 04, 2014 3:38 pm |
|
|
|
|
|
Mike.B
Joined: 04 Aug 2014 Posts: 5
|
|
Posted: Mon Aug 04, 2014 3:46 pm |
|
|
Alright, no problem, I understand
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
|
|
Posted: Mon Aug 04, 2014 3:50 pm |
|
|
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 |
|
|
luckyluke
Joined: 18 Apr 2006 Posts: 45
|
|
Posted: Mon Aug 04, 2014 4:02 pm |
|
|
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? |
|
|
|
|
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
|