Problem with static variable holding a value

RJohnson

Joined May 29, 2011
21
I just used an enum to declare values that I use elsewhere.
From the header file:
// for returns from KeyPad()
enum keyValues
{
NoKey = 0,
SetKey,
TimeKey,
PgmKey,
FanKey, // This will be an int of 4
CoolKey,
HeatKey,
UpKey,
EscapeKey,
LeftKey,
RightKey,
DownKey,
EnterKey
};

Anything done with these returned values would be in Main loop or elsewhere as needed.

I just presented this as an example of one way to get a button press only once.
In your case you would increment your counter in Main loop like:
// assume your getKey function returns counterKey - not FanKey
if( counterKey )
{
if(++counter >= 4)
{
counter = 1;
}
}
// remember this doesn't debounce as I do that in hardware.
// You will have to debounce when reading keys / buttons.

I've attached the full program below.
 

Attachments

Last edited:

Thread Starter

hunterage2000

Joined May 2, 2010
487
Thanks for the buttons.pdf. Just a few questions about it as I've not got to making structures and unions.

Code:
typedef union
{
INT16 summary;
struct __PACKED
{
UINT8 LB; // Addr 00
UINT8 HB; // Addr 01
}byte;
struct __PACKED
{
unsigned :2; // b0,1
unsigned Time :1; // b2 S5
unsigned Set :1; // b3 S2
unsigned Pgm :1; // b4 S13
unsigned Count :1; // b5 S9
unsigned Cool :1; // b6 S6
unsigned Heat :1; // b7 S3
unsigned Up :1; // b8 S8
unsigned Escape :1; // b9 S12
unsigned Left :1; // b10 S1
unsigned Right :1; // b11 S4
unsigned Down :1; // b12 S7
unsigned Enter :1; // b13 S11
unsigned :2; // b14,15
} bits;
} unionDigIn;
extern unionDigIn Button, ButtonDown;
1) What is UINT8 LB and HB and what are they for?
2) The unsigned values below them, what is the : for after the variable name and what does the comments mean?
3) What is unionDigIn Button and ButtonDown?

Code:
UINT8 counter; // no need for static here
UINT8 buttonPressed;
unionDigIn Button, ButtonDown;
4) What is UINT8 counter?
 

RJohnson

Joined May 29, 2011
21
There is a header file called GenericTypeDefs.h that defines UINT8 and LB/HB.
Low Byte & High Byte. UINT8 is Unsigned 8 bit integer.
Also see the MPLAB XC8 C Compiler Users Guide - Section 5.4.4.2 BIT-FIELDS IN STRUCTURES.
The comments are the bit position and the Switch designator on my drawing.
Count is bit 5 and switch S9.
Besides the C Compilers guide you will most likely need a C Language book for reference.
I still use Kernighan and Ritchie's "The C Programming Language" I bought ages ago.

UINT8 (Unsigned 8 bit integer) counter is what you had used as a static variable. No need for static. I just made it global so
it is usable in all functions in Main.c.

By the way there were a couple of typo's in Main.c. I think you can find them. See the switch() statement.
 

Thread Starter

hunterage2000

Joined May 2, 2010
487
I have been through the buttons pdf and can't get my head around it. I tried doing this but didn't work. Can someone explain why portValue after while(1) doesn't hold the incremented value of portrtnValue after it leaves the if statement?

Code:
void main()
{
    LATB = 0x00;
    ANSELB = 0x00;
    TRISB = 0x01;
    LATC = 0x00;
    TRISC=0;
    while(1)
    {
        PORTC = portValue;

        if(button)
        {
            int portrtnValue;
            portrtnValue = getportValue(portValue);
            portValue = portrtnValue;          
        }
    }

   
}

int getportValue(int)
{
    dl; // wait 100mS
    return portValue = portValue + 1;

}
 

RJohnson

Joined May 29, 2011
21
I mentioned back in post #17 that I didn't think you understood how and when to declare variables.
Your not presenting all the program here. Where is portValue declared?
Why are you creating an int portrtnValue inside the if statement?
The function int getportValue(int) ?????????? whats int for?

A corrected version of function getportValue(int):

at the top of Main.c - outside of main() function declare:
unsigned char portValue; // this makes it visible to all functions in main.c
// ports are unsigned 8 bit, int is signed

void getportValue( void) // no need to return anything and no need for int parameter
{
dl; // ok so wait then
portValue = PORTC; // if this is the port you are reading
portValue++; // increment it (no need to return it as it's global)
}
in Main() just call getportValue() & do what you want with the updated portValue

An alternate way is to declare portValue at the top of main() function:
void(main(void)
{
unsigned char portValue; // this makes it visible to main() only.

portValue =getportValue(); // then gets it value
// do what you want here
}

Then a new getportValue():
unsigned char getportValue( void )
{
unsigned char newValue; // only visible here

dl; // ok so wait then
newValue = PORTC; // if this is the port you are reading
return ++newValue; // increment it and return it (newValue now disappears)
}

My buttons.pdf is pretty basic C.
My feeling is you really need to restudy C . Look at storage class and visibility of variables.
Look at function parameters and return values.

You should be able to follow this. If not back to the books........
A link:
http://www.cprogramming.com/
 

ErnieM

Joined Apr 24, 2011
8,377
Let's face it: most "tutorials" about learning some computer language are just a mish-mash of fragments meant to make you "love my code."

Look, you want to increment some variable and display it on some LEDs. That is not hard, does not need special structures, just a counter.

Here are the steps to follow:

- initialize everything
Loop:
- wait about 20 mS (for debouncing)
- wait for the button to go down
- increment counter
- if counter too large make = 1
- copy counter to Port
- wait about 20 mS
- wait for button to go back up
- restart Loop

Most of this you already have good code to do. You just need to think like your PIC does.
 
Top