Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Port bit as function parameter

Status
Not open for further replies.

anishpsla

Member level 2
Member level 2
Joined
Dec 15, 2013
Messages
44
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
402

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sbit fan_0 at RB0_bit;
 
void count_rpm(int fan_id)
{
  TMR0=0;
  Delay_ms(1000);
  fan_rpm = TMR0*120;
  if(fan_rpm < 2500)
  {
    fan_id = 1;
  }
}
 
void main()
{
     PORTA = 0;
     PORTB = 0;
     TRISA = 0X11;
     TRISB = 0x00;
 
     while (1){
        count_rpm(fan_0);
     }
}



Which is not working. I think the problem is related to the data type of parameter of count_rpm. sbit as datatype for parameter is allowed in MikroC.

How to set data type for function parameter as pin of a port.
 

The code is not compiling, or just not working as expected ?
Occurs some error during building the code ?.

Code is not working as expected. There is compile errors.

fan_0 is not used in the function. Then what is the purpose of the argument to the function?


Code C - [expand]
1
2
3
while (1){
        count_rpm(fan_0);
     }

 

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)
 
Last edited:

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?
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sbit fan_0 at RB0_bit;
 
void count_rpm(int fan_id)
{
  TMR0=0;
  Delay_ms(1000);
  fan_rpm = TMR0*120;
  if(fan_rpm < 2500)
  {
    fan_id = 1;
  }
}
 
void main()
{
     PORTA = 0;
     PORTB = 0;
     TRISA = 0X11;
     TRISB = 0x00;
 
     while (1){
        count_rpm(fan_0);
     }
}



Please see the 10th line where I am trying to set fan_id as 1. Which is not working.
 

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.
 

Try this...


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sbit fan_0 at RB0_bit;
 
unsigned char count_rpm(unsigned char fan_id)
{
  TMR0=0;
  Delay_ms(1000);
  fan_rpm = TMR0*120;
  if(fan_rpm < 2500)
  {
    fan_id = 1;
    return fan_id;
  }
}
 
void main()
{
     PORTA = 0;
     PORTB = 0;
     TRISA = 0X11;
     TRISB = 0x00;
 
     while (1){
        fan_0 = count_rpm(fan_0);
     }
}

 
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.

Brian.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top