• AVR Freaks

AnsweredHot!How to pass predefined header file variables to a function

Author
ArakelTheDragon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
2019/11/15 13:49:08 (permalink)
0

How to pass predefined header file variables to a function

Hi guys,
 
I am trying to create a function with the following code and need to be able to pass "PORTAbits.RA3" and "PORTA" to the function, so I can change which pins are de-bounced and which PORT updated. Can someone please help me, because its not working. Thank you for your time.
 

 
#include <xc.h>
#include <stdint.h>
                                                /* Global function prototypes. */
int ButtonDebounce (uint8_t PIN, uint8_t PORT);

/***** CONFIGURATION *****/
// int reset, no code protect, no watchdog, int RC clock
__CONFIG(MCLRE_OFF & CP_OFF & WDTE_OFF & FOSC_INTRCIO);

/* GLOABL VARIABLES */
uint8_t sPORT;

/***** MAIN PROGRAM *****/
void main()
{
    // Initialisation
    PORTA = 0;
    TRISA = 0b11111101;            // configure RA1 (only) as an output
    OPTION_REG = 0b01010100; /* Clear "RABPU<7>" in the "OPTION" register or OPTION 2: "OPTION = ~nRABPU" to enable the internal weak pullups(10k) on PORTA/PORTB. For setting "Timer0" TOCS<5> = 0, PSA<3> = 0 with prescaler PS<2:0> = 100. */
    /* Timer0 is normally limited to "65mS". "Prescaler 1:32" => 1 tick of the timer is equal to "32 instructions", 1 instruction = 1/(processor clock(4MHz)/4) = 1uS. 250x32uS = 8mS. */

    // Main loop
    for (;;)
    {
        ButtonDebounce (PORTAbits.RA3, PORTA);
    }  
}

int ButtonDebounce (uint8_t PIN, uint8_t PORT)
{
                        /***Local variables and Initialization ***/
    uint8_t db_cnt = 0; /* Used for all purposes. */
    
    for (db_cnt = 0; db_cnt < 16; db_cnt++)
    {
        _delay (1000);            
        if (PIN == 1)
            db_cnt = 0;
    }
        
    sPORT ^= 0b00000010; /* Shadow port toggle with "^". Toggles bit<1>. */
    PORTA = sPORT; /* Transfer the shadow port to the real port as 1 whole byte. */

    for (db_cnt = 0; db_cnt < 16; db_cnt++)
    {
        _delay (1000);            
        if (PIN == 0)
            db_cnt = 0;
    }
    return 0;
}

#1
ric
Super Member
  • Total Posts : 24622
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to pass predefined header file variables to a function 2019/11/15 13:55:56 (permalink)
0
You can't.
As has been discussed many times on this forum, you cannot have a pointer to a "bit" in C, and the PIC hardware certainly does not support it.
You have to use a PORT address and a mask, but that is what the code you posted appears to do.
How are you testing and what is happening?
Ahhh, no it's not. Hold 10
 
post edited by ric - 2019/11/15 13:57:03

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
ric
Super Member
  • Total Posts : 24622
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to pass predefined header file variables to a function 2019/11/15 13:59:44 (permalink)
+1 (1)
Change the call to pass a mask of the desired bit, and the address of the port.
        ButtonDebounce (PORTAbits.RA3, PORTA);
to
        ButtonDebounce (0b00001000, &PORTA, PORTA);


and the function declaration from
int ButtonDebounce (uint8_t PIN, uint8_t PORT)
to:
int ButtonDebounce (uint8_t PIN, uint8_t * RDPORT, uint8_t PORT)


and in the function, change:
        if (PIN == 1)
to
        if (*RDPORT & PIN)


You never reveal which PIC you are using, but from the shadow port code, I assume it is an old PIC16F part without LAT registers.
 
n.b. why does your function declare an "int" return value, when you always return 0, and never use the return value?
 
post edited by ric - 2019/11/15 14:05:14

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
ArakelTheDragon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/15 23:25:44 (permalink)
0
Hi, thanks for the reply,
 
I use PIC16F690, XC8 v2.10, CMD compiling option as MPLAB 10 is very heavy. Yes its a baseline PIC with no latch registeters requiring the use of a shadow variable.
 
I am developing the function, my purpose is to create a function for every action which will make things easier for me in the long run. The int part is so I can check if the function has finished correctly or there is a problem.
 
I changed the code, but now the button turns on strangely, it flashes if I press it for a long time and it has to be pressed an exact time so it can go on or off. If I put a delay after the function equal to "1000mS" it works better, I am guessing the function is exited too fast and as re-enterred, is there a solution for this problem? Also I receive a warning from the compiler:
"Illegal conversion between pointer types", which I solved by changing the function definition from "&PORTA" to "(unsigned char *) &PORTA".
 

 
 
 
 
 
 
 
/************************************************************************
*                                                                       *
*   Filename:                                        *
*   Date:                                                         *
*   File Version:                                                   *
*                                                                       *
*   Author:                                            *
*   Company:                                        *
*                                                                       *
*************************************************************************
*                                                                       *
*   Architecture:  Baseline PIC                                         *
*   Processor:     16F690                                           *
*   Compiler:      MPLAB XC8 v2.10 (Free mode)                          *
*                                                                       *
*************************************************************************
*                                                                       *
*   Files required: none                                                *
*                                                                       *
*************************************************************************
*                                                                       *
*   Description:    Simple debounce for 16/32mS                                 *
*                                                                       *
*   Demonstrates reading a switch                                       *
*                                                                       *
*   Turns on LED when pushbutton is pressed                             *
*                                                                       *
*************************************************************************
*                                                                       *
*   Pin assignments:                                                    *
*       RA1 = indicator LED                                             *
*       RA3 = pushbutton switch (active low)                            *
*                                                                       *
************************************************************************/

#include <xc.h>
#include <stdint.h>
                                                /* Global function prototypes. */
uint8_t ButtonDebounce (uint8_t PIN, uint8_t * RDPORT, uint8_t PORT);

/***** CONFIGURATION *****/
// int reset, no code protect, no watchdog, int RC clock
__CONFIG(MCLRE_OFF & CP_OFF & WDTE_OFF & FOSC_INTRCIO);

/* GLOABL VARIABLES */
uint8_t sPORT;

/***** MAIN PROGRAM *****/
void main()
{
    // Initialisation
    PORTA = 0;
    TRISA = 0b11111101;            // configure RA1 (only) as an output
    OPTION_REG = 0b01010100; /* Clear "RABPU<7>" in the "OPTION" register or OPTION 2: "OPTION = ~nRABPU" to enable the internal weak pullups(10k) on PORTA/PORTB. For setting "Timer0" TOCS<5> = 0, PSA<3> = 0 with prescaler PS<2:0> = 100. */
    /* Timer0 is normally limited to "65mS". "Prescaler 1:32" => 1 tick of the timer is equal to "32 instructions", 1 instruction = 1/(processor clock(4MHz)/4) = 1uS. 250x32uS = 8mS. */

    // Main loop
    for (;;)
    {
        ButtonDebounce (0b00001000, &PORTA, PORTA);
    }  
}

uint8_t ButtonDebounce (uint8_t PIN, uint8_t * RDPORT, uint8_t PORT)
{
                        /***Local variables and Initialization ***/
    uint8_t db_cnt = 0; /* Used for all purposes. */
    
    for (db_cnt = 0; db_cnt < 32; db_cnt++)
    {
        _delay (1000);            
        if (*RDPORT & PIN)
            db_cnt = 0;
    }
        
    sPORT ^= 0b00000010; /* Shadow port toggle with "^". Toggles bit<1>. */
    PORTA = sPORT; /* Transfer the shadow port to the real port as 1 whole byte. */

    for (db_cnt = 0; db_cnt < 16; db_cnt++)
    {
        _delay (1000);            
        if (*RDPORT & PIN)
            db_cnt = 0;
    }
    return 0;
}
 
 
 
 
 
 
 

post edited by ArakelTheDragon - 2019/11/15 23:34:38
#4
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11430
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/15 23:36:02 (permalink)
+1 (1)
uint8_t ButtonDebounce (uint8_t PIN, uint8_t volatile * RDPORT, uint8_t PORT)
#5
ArakelTheDragon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/15 23:38:15 (permalink)
0
I solved it by changing the function definition from "&PORTA" to "(unsigned char *) &PORTA".
 
EDIT:
From "ButtonDebounce (0b00001000, &PORTA, PORTA);" to "ButtonDebounce (0b00001000, (unsigned char *) &PORTA, PORTA);".
post edited by ArakelTheDragon - 2019/11/15 23:39:26
#6
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11430
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/15 23:38:27 (permalink)
+1 (1)
(unsigned char *) &PORTA

 
Never use casts to "fix" warnings you don't understand.
#7
ArakelTheDragon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/15 23:40:39 (permalink)
0
I took the fix from here, what is wrong with it?
 
https://www.microchip.com/forums/m707439.aspx
#8
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11430
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 00:14:43 (permalink)
+1 (1)
I took the fix from here, what is wrong with it?

 
It's wrong for the same reason your "fix" is wrong: both are using a cast to throw away a qualifier that should not be thrown away.  In your case, you're throwing away the volatile qualifier, which could result in erratic writes to the port.  In the other post, they're throwing away the const qualifier which can cause different problems.
#9
ric
Super Member
  • Total Posts : 24622
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to pass predefined header file variables to a function 2019/11/16 01:30:26 (permalink)
+2 (2)
jtemples
uint8_t ButtonDebounce (uint8_t PIN, uint8_t volatile * RDPORT, uint8_t PORT)

+1
That's my fault for leaving out the volatile qualifier in my suggested change.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
ArakelTheDragon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 01:33:29 (permalink)
0
How is this variable suppose to change without the code changing? Is it because of the "RWM" issues which may change the port from the outside?
#11
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11430
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 01:37:25 (permalink) ☼ Best Answerby ArakelTheDragon 2019/11/16 01:40:34
+1 (1)
If by "this variable" you mean RDPORT, this doesn't declare RDPORT as volatile.  It declares it as a pointer to volatile.  The pointer isn't volatile.
#12
ric
Super Member
  • Total Posts : 24622
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to pass predefined header file variables to a function 2019/11/16 01:38:38 (permalink)
0
I don't understand your question.
Which variable?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#13
ArakelTheDragon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 01:41:10 (permalink)
0
Yes "RDPORT" is the variable I mean, I understand its a pointer to a volatile value, but my question still stands, how will this value change without the code changing it?
#14
ric
Super Member
  • Total Posts : 24622
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: How to pass predefined header file variables to a function 2019/11/16 01:45:41 (permalink)
+2 (2)
It's pointing to a hardware port (the PORTA register).
The port value can change at any time, depending upon the state of the port pins.
post edited by ric - 2019/11/16 01:47:48

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#15
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11430
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 01:46:40 (permalink)
+1 (1)
Why do you believe RDPORT will change by itself?
#16
ArakelTheDragon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 01:48:19 (permalink)
0
Because "volatile" means the value can change without the code changing it.
 
Yes like "RWM" issues changing it, when the pin is grounded or connected to a power supply and the value does not change immediately.
post edited by ArakelTheDragon - 2019/11/16 01:49:25
#17
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11430
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 01:57:03 (permalink)
+1 (1)
Because "volatile" means the value can change without the code changing it

 
Again, RDPORT is not volatile and thus will not change on its own.  What it points to (i.e., the port) is volatile and may change on its own.
#18
1and0
Access is Denied
  • Total Posts : 10000
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 07:21:44 (permalink)
+1 (1)
ArakelTheDragon
Yes "RDPORT" is the variable I mean, I understand its a pointer to a volatile value, but my question still stands, how will this value change without the code changing it?

"uint8_t volatile * RDPORT" declares RDPORT as a pointer to volatile unsigned char.  
 
RDPORT is a non-volatile pointer. It does not change value on its own. It IS what it points to that changes value -- i.e. the PORTx.
 
Edit: Added non-volatile.
post edited by 1and0 - 2019/11/16 12:23:50
#19
1and0
Access is Denied
  • Total Posts : 10000
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: How to pass predefined header file variables to a function 2019/11/16 08:08:29 (permalink)
0
FYI: This
unsigned char volatile * volatile foo

declares foo as a volatile pointer to volatile unsigned char.
#20
Jump to:
© 2019 APG vNext Commercial Version 4.5