I would ask others to correct me.
1st - I believe the error is in the passing of the argument, as no matter what the value
2nd - count_rpm will always assign the fan_rpm value as Zero.
3rd - fan_id will be 1 always
What compiler error you are getting? Pls post it...
1. Did you declare PROTOTYPE DEFINITION of the function void count_rpm(int fan_id)??
2. If the point 1 is done, where is the TIMER STARTED?
Lets see your code..
void count_rpm(int fan_id)
{
TMR0=0; //This shows you are trying to use TIMER 0 where the TMR0 value is initialised to '0'
Delay_ms(1000);
fan_rpm = TMR0*120; //This will make the variable FAN_RPM=0 as the TMR0 value is '0' as per the previous step...
if(fan_rpm < 2500) //This condition doesnt serve the purpose as already your FAN_RPM value is '0'
{
fan_id = 1;
}
}
Now the question is what you are actually trying to do with FAN_RPM??
Lets consider that your fan_rpm is assigned a valid value, then where is the TIMER STARTED??
Also is the SBIT declaration is correct??
I think passing sbit as argument may cause error...Try using #define instead of sbit...
check if the below declaration works?
sbit varibale = port bit pin name (for example : P1^1 ----> THIS MEANS P1.1 port 1 pin 1)
fan_0 is passed a s argument and it becomes parameter fan_id in count_rpm() function but fan_id is not used in the count_rpm() function. Then what is the use of passing the argument?
What your are trying to do will never work. SFR is a register in hardware. If you sent it as an argument to a function then a copy of it is sent to the called function. The passed value becomes local in the function and if you changed the local variable and expect it to change the register in hardware then it will not work.
You need to think hardware-related. There are no bit pointers provided in PIC architecture.
At best a PIC compiler can provide a data type and an internal code for port bits that is translated to SFR bit accesses. MikroC has the sbit data type for this purpose.
But can it be used as function parameter? I don't know because I'm not using mikroC. But it's pretty clear that the posted code can't work.
Isn't the problem more fundamental than that - the parameter is being passed IN to the function but being expected to carry a value OUT of it.
The address of the variable should be passed so the function can fill it in with a result, or, if the parameter is a port bit, it should be used as a mask inside the function and no value returned.