|
|
View previous topic :: View next topic |
Author |
Message |
palazzolojoe
Joined: 04 Feb 2012 Posts: 7
|
passing Pin to function |
Posted: Tue Feb 14, 2012 11:34 pm |
|
|
Does anyone know how to pass a pin to a function?
For example,
Lets say I wanted pin=pin_b1
I want to pass pin_b1 into a function as follows:
Code: |
void output (pin ) //this is where i am having difficulty
{
output_high(pin);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Feb 15, 2012 2:34 am |
|
|
How old is your compiler?.
A pin number, is an int16 for passing to functions. This is supported in the V4 compilers _but not in V3_. There have been functions posted here in the past to do this. On the old compilers you have to manually disassemble the number, taking the pin number/8, to get the memory address required, and then the low 3 bits of the pin number to get the bit required.
So for current compilers:
Code: |
void output(int16 pin_no) {
output_high(pin_no);
}
|
Be aware that if (for example) you do a basic 'output_high(fixed_pin_number)' this generates just a couple of machine instructions, while doing it with a variable, either if the compiler supports it or 'DIY', involves perhaps 20+ instructions. So if (for instance) you just need to switch between a couple of pins, it is actually much faster to do a simple test with an if statement, than to use the variable in the function.....
Best Wishes |
|
|
palazzolojoe
Joined: 04 Feb 2012 Posts: 7
|
|
Posted: Wed Feb 15, 2012 8:51 am |
|
|
in the properties for ccsc
File version:4.0.0.207
product version: 4.0.0.0
basically what I need this for is to enhance my servo function, my code is as follows
Code: | void move_servo(int period, float dest)
{
long p, high_duty,low_duty;
float temp;
int j;
p=(period*1000);
temp=((float)(1.0*(dest/90.0))+1.0)*1000.0;
high_duty=(temp+0.5);
low_duty=p-high_duty;
for (j=0;j<30;j++)
{
output_high(pin_b1);
delay_us(high_duty);
output_low(pin_b1);
delay_us(low_duty);
}
} |
I am using an hs-311 servo, my servo only moves 90 degrees at a speed of .2s/60degrees. So I am outputting a wave for .6s to ensure that it moves to the correct position. The pulse range is from 1 to 2 ms and the period is 20ms. I am not sure if there is a better way to do this but this is what I have come up with this far.
So what you're saying is that I can use int16 pin as a parameter and pass whatever pin I want the output to be (in this case pin_b1), but since its a variable it will throw off my timing and won't generate the desired wave form. So its better to just use an if statement based on what pin one wants to use. |
|
|
|
|
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
|