Page 1 of 1

define an array

PostPosted: Sun Jan 03, 2016 5:16 pm
by TucsonDon
I am trying to debounce some buttons using some code that was recommended, the issues is that the ports need to be defined in an array. I have done some reading on the internet but to no avail. Jack state to remember to " initialize State and Index to zero" and I'm unsure how to accomplish this

Figure 3 shows one approach. DebounceSwitch(), which is called regularly by a
timer tick, reads an entire byte-wide port that contains up to 8 individual switches. On
each call it stuffs the port’s data into an entry in circular queue State. Though shown as
an array with but a single dimension, a second loiters hidden in the width of the byte.
State consists of columns (array entries) and rows (each defined by bit position in an
individual entry, and corresponding to a particular switch).

A short loop ANDs all column entries of the array. The resulting byte has a one in each
bit position where that particular switch was on for every entry in State. After the loop
completes, variable j contains 8 debounced switch values.
One could exclusive OR this with the last Debounced_State to get a one in each bit
where the corresponding switch has changed from a zero to a one, in a nice debounced
fashion.

Don’t forget to initialize State and Index to zero.


Code: Select all
int State[]={PORTCbits.RC0,PORTDbits.RD0};
char Index=0;
char counter=0;


#define MAX_CHECKS 10
uint8_t Debounce_State;
uint8_t State[MAX_CHECKS];
uint8_t Index;


void DebounceSwitch()
{
    uint8_t i,j
    State[Index]=RawKeyPressed();
    ++Index;
    j=0xff;
    for(i=0; i<MAX_CHECKS;i++)j+j & State[i];
    Debounce_State= j;
    if(Index>=MAX_CHECKS)Index=0;
}

void main(void) {
    // Initialize the device
    SYSTEM_Initialize();

   
    while (1)
    {
    while (!TMR1IF);
   
      TMR1_Reload();
   
    counter++;
   
    if (counter ==5)
    {
        DebounceSwitch();
       
       counter =0;
    }
    TMR1IF = 0;
    }
}

Re: define an array

PostPosted: Sun Jan 03, 2016 11:36 pm
by ric
I think this has already been answered on the Microchip forum, but in short, you can NOT initialise an array with dynamic data, only constants, so you can not do this:
Code: Select all
int State[]={PORTCbits.RC0,PORTDbits.RD0};

As PORTCbits.RC0 is not a constant.

You'd have to do:
Code: Select all
int State[2];
State[0] = PORTCbits.RC0;
State[1] = PORTDbits.RD0;

Re: define an array

PostPosted: Mon Jan 04, 2016 1:39 am
by TucsonDon
ric,
thanks, I went back and read I did not see that so forgive me. I know that everyone is trying "to get the OP to think" but I don't know what I don't know.

In the last sentence he says "Don’t forget to initialize State and Index to zero.", how do I do that? Do I need to define "RawKeyPressed"?

Re: define an array

PostPosted: Mon Jan 04, 2016 1:24 pm
by TucsonDon
ric,
I defined them as you recommended but when I try to build I get the following errors:

main.c:50: error: (284) invalid dimension
main.c:50: warning: (374) missing basic type; int assumed
make[1]: *** [.build-conf] Error 2
main.c:50: error: (251) array dimension redeclared
make: *** [.build-impl] Error 2
main.c:50: error: (1098) conflicting declarations for variable "State" (main.c:49)

line 50 is State[0]=PORTCbits.RC0

Re: define an array

PostPosted: Mon Jan 04, 2016 9:07 pm
by ric
There's almost certainly an error in the lines before that. Post the whole function.

Re: define an array

PostPosted: Mon Jan 04, 2016 9:43 pm
by ric
There's almost certainly an error in the lines before that. Post the whole function.

Re: define an array

PostPosted: Tue Jan 05, 2016 12:54 am
by TucsonDon
Code: Select all
int State[2];
State[0]=PORTCbits.RC0;
State[1]=PORTDbits.RD0;

char Index;
char counter=0;


#define MAX_CHECKS 10
uint8_t Debounce_State;
uint8_t State[MAX_CHECKS];
uint8_t Index;


void DebounceSwitch()
{
    uint8_t i,j
    State[Index]=RawKeyPressed();
    ++Index;
    j=0xff;
    for(i=0; i<MAX_CHECKS;i++)j+j & State[i];
    Debounce_State= j;
    if(Index>=MAX_CHECKS)Index=0;
}

void main(void) {
    // Initialize the device
    SYSTEM_Initialize();

   
    while (1)
    {
    while (!TMR1IF);
   
      TMR1_Reload();
   
    counter++;
   
    if (counter ==5)
    {
        DebounceSwitch();
       
       counter =0;
    }
    TMR1IF = 0;
    }
}

Re: define an array

PostPosted: Tue Jan 05, 2016 3:08 am
by AussieSusan
The "State[0]=PORTCbits.RC0;" (and the following line) is an executable statement and therefore it has to appear within a function - typically 'main'.
You have placed those lines where the compiler will interpret them as declarations, not executable statements.
Also the "for" loop statement in "DebounceSwitch" is wrong : it shoudl be "j = j & State[i];"
I assume the reference to "Jack" is Jack Ganssle as in "http://www.embedded.com/electronics-blogs/break-points/4024981/My-favorite-software-debouncers".
Susan

Re: define an array

PostPosted: Wed Jan 06, 2016 1:06 am
by TucsonDon
Susan,
thanks for your insight, I didn't know that it was a function. And yes your assumption is correct I was referring to Jack Ganssle.
Now on to the next hurdle, not sure how I need to define "RawKeyPressed"

when I try to build I get the following errors:
main.c:68: error: (372) "," expected
main.c:68: error: (188) constant expression required
main.c:68: error: (984) type redeclared
main.c:68: error: (1098) conflicting declarations for variable "State" ((null):0)

Line 68 is: State[Index]=RawKeyPressed();

I got the error to clear on "RawKeyPressed by "extern bool RawKeyPressed"

Re: define an array

PostPosted: Wed Jan 06, 2016 2:15 am
by ric
This line:
State[Index]=RawKeyPressed();
Tries to call a function named "RawKeyPressed"

This line:
extern bool RawKeyPressed"
says that there is a variable of type "bool", named "RawKeyPressed", that is defined somewhere in a seperate module that is linked to the current module.

I think you have jumped in to deep to start with. You need to go back and do some tutorials on simple C programming.