|
|
View previous topic :: View next topic |
Author |
Message |
vic481
Joined: 02 Dec 2010 Posts: 2
|
help with array send as argument |
Posted: Thu Dec 02, 2010 2:23 am |
|
|
Hi, all! I'm trying make pwm sound:
Code: |
void snd(int8 how_many_times,int8 freq_val[5])
{
int8 i;
for(i=0;i<how_many_times;i++)
{
setup_timer_2(T2_DIV_BY_4,freq_val[i],1);
set_pwm1_duty(512);
setup_ccp1(CCP_PWM);
delay_ms(90);
}
setup_ccp1(CCP_off);
}
|
If I call this procedure (example for one beep)
Code: |
snd(1,(184,0,0,0,0));
|
only last argument (last 0) is sent to procedure (I see it in .lst file).
What I'm doing wrong? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Dec 02, 2010 5:01 am |
|
|
(184,0,0,0,0), is not an array. It is a mathematical statement, which evaluates each element in turn, and returns the last value in the list. So, '0'. Basic C....
In C, an 'array', is a pointer _to_ a block of values in memory. So you need to pre-declare the array, and hand it's address to the function.
So:
int8 vals[] = {184,0,0,0,0};
snd(1,vals);
Note multiple things:
1) the squirly brackets, not round brackets for the initialisation.
2) You are handing the 'address' of the list to the function, so: &vals[0], which is the address of the first element. However 'C' has a shortcut, which says if you use the 'name' of an array, without a subscript, to automatically assume you want the address. So &vals[0] gives the same address as vals on it's own.
Best Wishes |
|
|
vic481
Joined: 02 Dec 2010 Posts: 2
|
|
Posted: Thu Dec 02, 2010 12:51 pm |
|
|
Tnx for reply and explanation. |
|
|
|
|
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
|