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.

passing something to a function declared in C

Status
Not open for further replies.

chimera786

Member level 2
Joined
Jun 16, 2011
Messages
42
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,710
Well, here is deal. I ended up creating my own debounce mechanism. The only issue is that I am defining 'button' (please see code). Now, in my interrupt I call my debounce function but I need to pass it 'button'. How can I do it without changing the 'type' of 'button'

Please see attached code. Thanks

Code:
#define buttonz PORTA.B0        //define 'button' at pin0 of port A


void button_debounce(const which_button){               // function for button
if (which_button==0){                                    //if button is pressed, wait for 7ms to settle
   Delay_ms(7);                                           //deboucne
               if (which_button==0){                       // now check again. Indeed the button
                  while(!which_button);                          //is pressed and wait for release
                                 PORTA.B2=~PORTA.B2;}              //toggle LED
               }
}

void timer (void){
     OPTION_REG=0x08;       //assign TMR0 clock source (OSC/4) and prescaler to WDG
     INTCON=0xA0;              //Enable global interrupt and TMR0 interrupt
     TMR0=0;                   //initialize the TMRO tick value
}

unsigned int x=0;

void main() {

        OSCCON=0x74;      //set up oscillator at 8Mhz
        ADCON1=0x06;     //configure all A ports as digital
        timer();         //configure timer0
        
        TRISA.B0=1;      //set RA0 as input
        TRISA.B2=0;      //set RA2 as output--LED to show shit is working

        PORTA.B2=0;     //keep LED off
}

void interrupt(void){

     x++;
     if (x==100){         // wait for 10 ms and check the status of the button
     button_debounce(buttonz);     // my switch_check + plus debounce
     x=0;}                         // reset and repeat
     TMR0IF_BIT=0;

}
 

I feel like you aer trying to re invent the wheel which is always there......just google my friend thousand are there.
 

I feel like you aer trying to re invent the wheel which is always there......just google my friend thousand are there.

Thanks for the insight on whats obvious :D. Now how about you explain me this:

I am doing : #Define Buttonz PORTA.B0. I want to send this to the debounce function. But I am guessing that my debounce function is chnaging its type when i pass it.

Any suggestions
 

There is no point to have a function that has a constant parameter.

Code:
#define buttonz PORTA.B0        //define 'button' at pin0 of port A


void button_debounce(void){               // function for button
if (buttonz==0){                                    //if button is pressed, wait for 7ms to settle
   Delay_ms(7);                                           //deboucne
               if (buttonz==0){                       // now check again. Indeed the button
                  while(!buttonz);                          //is pressed and wait for release
                                 PORTA.B2=~PORTA.B2;}              //toggle LED
               }
}

- - - Updated - - -

You can also take a look at https://hackaday.com/2010/11/09/debounce-code-one-post-to-rule-them-all/
 

yeah I have tried the way you mentioned before and it works. But I guess I am eventually after a more generic code that could be used as a function called for multiple buttons. For instance,

#define button1 PORTA.B0
#define button2 PORTA.B1
#define button3 PORTA.B2

Then in my interrupt routine, I simply pass button1 or button 2 or 3 to that denounce function. Eh, seems to me that I am dealing with this debounce in a very simplistic way. It might need more thought. I intend for a generic code that can be used.

I am using MikroC and I know that it had a built in function for debouncing button but factually, I don't know how to use it efficiently. Its always caused problems for me.

What do you super moderator alexon_e??
 

You need to define the type of the parameter of the function

You can't use
Code:
void button_debounce(const which_button)

You have to use
Code:
void button_debounce(const [B]type[/B] which_button)
but I'm not sure how is your pin defined.

- - - Updated - - -

I feel like you aer trying to re invent the wheel which is always there......just google my friend thousand are there.

I find this type of answer completely useless unless it comes with specific links to search results.
If the answer is so simple then do us a favor and share it with us.
 
I'm not familiar with MikroC details (and don't intend to get bothered by it more than necessary).

In general C and PIC terms, we can say
- a parameter, that is expected to change it's value after function call, can be only passed by reference (as a pointer) rather than by value
- to pass a reference to an individual SFR bit, a bit pointer would be needed. I guess it's not provided by MikroC.

Some PIC compilers have an internal enumeration of port and other specific SFR bits for built-in functions. It may be also available to designate port bits in variable accesses as discussed. If so, it should be described in the compiler documentation.

Otherwise, you can setup your own port bit access method by passing either port number or SFR port address and bit number separately to the function.
 
As both Alex and FvM have alluded, the specifics of how the type is defined are necessary to formulate the correct method of passing it as parameter.

Unfortunately, MikroC does not utilize standard device specific header files as most of us are accustom, instead they utilize a combination of CP and C file to contain said definitions found in the defs directory.

The following is discussion thread addresses this specific issue and includes the recommendations of a representative of MikroE:

Passing port bit to a function

Of course, another option is to define your own port bit definitions in such a way as to facilitate your preferred method of passing such parameters.


BigDog
 
  • Like
Reactions: FvM and alexan_e

    alexan_e

    Points: 2
    Helpful Answer Positive Rating

    FvM

    Points: 2
    Helpful Answer Positive Rating
OK. SO cumulatively, I need to provide a pointer to my constant and then pass that pointer to the function? or a derivative of that statement I guess.

Thanks for the link bigdog (lol funny)

Truth be told, I am still learning programing. I have been doing it off and on for some time but during these "OFF" times, it easy to forget how to code since my foundation isn't very strong.

But I guess you learn by failing huh? I'll post back soon with what filth i came up with :p
 

If you'd like some feedback, post your code.

I would be more than happy to provide advice.


As several of our replies indicated, you could implement your own definition within an additional header file to facilitate a preferred method of parameter passing.

The following is a typical definition of PORTB and its individual bits contained in a device specific header file, Hi-Tech C Compiler:

Code:
// Register: PORTB
volatile unsigned char           PORTB               @ 0x006;
// bit and bitfield definitions
volatile bit RB0                 @ ((unsigned)&PORTB*8)+0;
volatile bit RB1                 @ ((unsigned)&PORTB*8)+1;
volatile bit RB2                 @ ((unsigned)&PORTB*8)+2;
volatile bit RB3                 @ ((unsigned)&PORTB*8)+3;
volatile bit RB4                 @ ((unsigned)&PORTB*8)+4;
volatile bit RB5                 @ ((unsigned)&PORTB*8)+5;
volatile bit RB6                 @ ((unsigned)&PORTB*8)+6;
volatile bit RB7                 @ ((unsigned)&PORTB*8)+7;
#ifndef _LIB_BUILD
volatile union {
    struct {
        unsigned	RB0                 : 1;
        unsigned	RB1                 : 1;
        unsigned	RB2                 : 1;
        unsigned	RB3                 : 1;
        unsigned	RB4                 : 1;
        unsigned	RB5                 : 1;
        unsigned	RB6                 : 1;
        unsigned	RB7                 : 1;
    };
} PORTBbits @ 0x006;
#endif

Of course you would have to port the above definitions to conform to the MikroC compiler/preprocessor directives.

BigDog
 

The quoted MikroE support suggestion goes in fact for separate port and bit number parameters. This makes me think that MikroC doesn't provide other means for this purpose, so you should adapt it.
 

Here is what I came up with. Its not exactly what I wanted but its still in development.

SO basically here what the code does: TWO buttons for input. A timer expires afters some milliseconds (interrupt service routine) and goes to a button routine. The routine checks if all buttons are pressed (not allowed by design), wait for their either one of to release and then quit out of the routine. If there is no illegal condition, then if any of the two buttons is pressed. If so, wait for a few milliseconds to settle the mechanical bounce and then check if still on of the two button is pressed. If so, then that's what is intended and toggle an LED.

Ideally, I wanted a debounce routine in which I would send a particular button which I wanted to be debounced..kind of like a generic routine that would be able to work with any program I come up with..but I guess the way I have it right now is a 'distant relative' of what I want.

Feel free to add to it . Thanks for all the help. It did point me in some general direction which I could build off.

PS: The person who replied that I should just google one and use it. It seems to me that they are the sort of person that would keep pestering you to give them the code so that they could use it for themselves and not make the effort to learn and develop it themselves. Or in this case, copy one from google and use it in their application with not so much as an request to the author for permission

Im not hating and I dont intend for this thread to turn into a back and forth..the above is just my opinion which I felt I should type out for others to see. LETS KEEP THIS THREAD FOR ITS ORGINAL PURPOSE: to show the world how bad I am at programming :p

Code:
sbit button1 at PORTA.B0;
sbit button2 at PORTA.B1;

void button_debounce(void){     // function for button
     if(PORTA==0x00){ //check if all the buttons are pressed-not allowed
         while(PORTA==0x00){}; //if yes, wait for any buttons to be realeased
               delay_ms(100); //wait for the contact bounce to settle
         return;// get out of this debounce function
     }

    if(button1==0||button2==0){//if button is pressed, wait for 7ms to settle
        Delay_ms(10);//boucne attenuation
          if (button1==0 || button2==0){//now check again. Indeed the button
               while(!button1 || !button2);//is pressed and wait for release
                   PORTA.B2=~PORTA.B2;}//toggle LED
    }
}

void timer (void){
     OPTION_REG=0x08;  //assign TMR0 clock source (OSC/4) and prescaler to WDG
     INTCON=0xA0;      //Enable global interrupt and TMR0 interrupt
     TMR0=0;           //initialize the TMRO tick value
}

unsigned int count_to_switchcheck=0;

void main() {

        OSCCON=0x74;      //set up oscillator at 8Mhz
        ADCON1=0x06;     //configure all A ports as digital
        timer();         //configure timer0
        TRISA=0x03;      //RA0,RA1 input,rest output
        PORTA=0x00;      //inital port values
}

void interrupt(void){
     count_to_switchcheck++;//sets time for timer to expire after
     if (count_to_switchcheck==1000){  //timer exrpired
     button_debounce(); //my switch_check + plus debounce
     count_to_switchcheck=0;}//reset and repeat
     TMR0IF_BIT=0;//clear timer and start
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top