|
|
View previous topic :: View next topic |
Author |
Message |
t90
Joined: 07 Aug 2008 Posts: 11 Location: saigon,vietnam
|
How to change the value of the array via function? |
Posted: Tue Dec 02, 2008 2:09 am |
|
|
I work in project that use PIC18f4550. In my project I implement one function to copy value from 1 variable to 1 member in 1 array consist 20 member which 1 member is a char variable has 4 member. But I can't copy the value. Someone has seen my problem. please solve for me. Thank you!
Here my code:
Code: | //my array
struct Node{
int time[4];
};/**/
static struct Node tim[20];
static BYTE temp[4];
//function copy
void bcopy(int a[4],int &b0,int &b1,int &b2,int &b3){
b0=a[0];
b1=a[1];
b2=a[2];
b3=a[3];
return;
}
//another function copy
void bcopy1(int8 a[4]){
temp[0]=a[0];
temp[1]=a[1];
temp[2]=a[2];
temp[3]=a[3];
return;
}
void main(){
int a1[4];
int a2[4];
int a3[4];
int a4[4];
int a5[4];
int a6[4];
a1[0]=0x00;
a1[1]=0x00;
a1[2]=0xFF;
a1[3]=0x7F;
a2[0]=0x00;
a2[1]=0x01;
a2[2]=0x02;
a2[3]=0x03;
//the value not change bcopy(a1,tim[header].time[0],tim[header].time[1],tim[header].time[2],tim[header].time[3]);
//the value not change
bcopy(a1,temp[0],temp[1],temp[2],temp[3]);
} | |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 02, 2008 3:40 am |
|
|
You don't say 'what compiler version'.
Now, you are using reference parameters, which are a sort of 'halfway house', between using pointers, and the normal pass by value. I'd not use reference paramters for this. The manual says _limited support_ for reference parameters, and though they do work for a simple variable, for an array item, I'd not be at all surprised if they gave trouble.
I'd stick to 'standard' C, and pass the addresses.
So:
[code]
//function copy
void bcopy(int a[4],int * b0,int * b1,int * b2,int * b3){
*b0=a[0];
*b1=a[1];
*b2=a[2];
*b3=a[3];
}
//and call with
bcopy(a1,&temp[0],&temp[1],&temp[2],&temp[3]);
[/copy]
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Dec 02, 2008 3:45 am |
|
|
You need to pass the address of the var/array you want to set to the function, I think you have gotten a little confused about pointers. As you are using arrays a better method would be:-
Code: |
void bcopy(int *a, int *b) // This function requires 2 pointers
{
int i;
for (i = 0; i < 4; i++)
{
b[i] = a[i]; // or *b++ = *a++; should work as well
}
}
void main(){
int a1[4];
int a2[4];
int a3[4];
int a4[4];
int a5[4];
int a6[4];
a1[0]=0x00;
a1[1]=0x00;
a1[2]=0xFF;
a1[3]=0x7F;
a2[0]=0x00;
a2[1]=0x01;
a2[2]=0x02;
a2[3]=0x03;
bcopy(a1, tim[header].time); // pass the address of a1 and tim[header].time ti the function
bcopy(a1, temp); // pass the address of a1 and temp
// Specifying just the name of the array will pass the address of that array
// Some people prefer to use
// bcopy(&a1[0], &temp[0]); // & means address of so &a1[0] is the address of the first index of the array
}
|
|
|
|
t90
Joined: 07 Aug 2008 Posts: 11 Location: saigon,vietnam
|
|
Posted: Tue Dec 02, 2008 8:55 am |
|
|
Thank you very much! But the values displayed in LCD isn't values I have input in my array. Here is my entire code. Please see it for me!.
Code: | #include "18f4550.h"
#device HIGH_INTS=TRUE
#use delay(clock=20000000)
#fuses HS,NOPROTECT,NOLVP,NODEBUG,NOWDT
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)
#include "ewl_glcd.c"
#include "ewl_realtime.c"
char glcd_buff[128];
int a[4];
char out[10];
struct Node{
int time[4];
};/**/
static struct Node tim[20];
void bcopy(int *a, int *b) // This function requires 2 pointers
{
int i;
for (i = 0; i < 4; i++)
{
*b[i]++ = *a[i]++; // or *b++ = *a++; should work as well
}
}
// External interrupt xac dinh boi tin hieu goi ve tu chip RealTime clock
#int_ext2
void rtc_card_isr()
{
read_time();
glcd_fill_area(0,0,127,12,OFF);
sprintf(glcd_buff,"%Lu-%u-%u %U:%02U:%02U\n",2000+ds1307_regs[YEAR_REG],ds1307_regs[MONTH_REG],
ds1307_regs[DATE_REG],ds1307_regs[HOURS_REG],ds1307_regs[MINUTES_REG],ds1307_regs[SECONDS_REG]);
glcd_text57(0,0,glcd_buff,1,ON);
}
void main()
{
int header;
int c[4];
set_time();
header=0;
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
c[0]=0xFF;
c[1]=0xFF;
c[2]=0x01;
c[3]=0x01;
set_tris_e(0x00);
set_tris_d(0x00);
set_tris_b(0xfc);
set_tris_c(0b10000000);
setup_adc(ADC_OFF);
set_tris_a(0x00);
///////////////
glcd_init(ON);
glcd_fillScreen(OFF);
// LCD
set_tris_c(get_tris_c()&0b11111011);
setup_timer_2(T2_DIV_BY_16,127,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(500);
bcopy(a,tim[1].time);
enable_interrupts(INT_EXT2);
enable_interrupts(GLOBAL);
init_DS1307();
//set_tris_b(get_tris_b() | 0xF0);
set_tris_e(0x00);
sprintf(out,"%u-%u-%u-%u ",tim[1].time[0],tim[1].time[1],tim[1].time[2],tim[1].time[3]);
while(1){
glcd_text57(0,30,out,1,ON);//display the value in graphic LCD but the value is wrong.
}
} |
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Dec 02, 2008 9:01 am |
|
|
You have changed the line
*b[i]++ = *a[i]++; // or *b++ = *a++; should work as well
It should be
b[i] = a[i];
or
*b++ = *a++;
Not a mixture of both! |
|
|
t90
Joined: 07 Aug 2008 Posts: 11 Location: saigon,vietnam
|
Thank you Wayne_ |
Posted: Tue Dec 02, 2008 9:49 pm |
|
|
It's well active. Thank you! |
|
|
|
|
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
|