define an array

This conference allows discussion on any subject not already covered in another PICmicro conference. If a topic becomes popular enough, a new conference will be created for it.

define an array

Postby TucsonDon » Sun Jan 03, 2016 5:16 pm

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;
    }
}
Attachments
Multiple Inputs.pdf
A Guide to Debouncing Jack G. Ganssle
(197.04 KiB) Downloaded 501 times
User avatar
TucsonDon
 
Posts: 5
Joined: Sun Jan 03, 2016 3:51 pm

Re: define an array

Postby ric » Sun Jan 03, 2016 11:36 pm

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;
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: define an array

Postby TucsonDon » Mon Jan 04, 2016 1:39 am

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"?
User avatar
TucsonDon
 
Posts: 5
Joined: Sun Jan 03, 2016 3:51 pm

Re: define an array

Postby TucsonDon » Mon Jan 04, 2016 1:24 pm

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
User avatar
TucsonDon
 
Posts: 5
Joined: Sun Jan 03, 2016 3:51 pm

Re: define an array

Postby ric » Mon Jan 04, 2016 9:07 pm

There's almost certainly an error in the lines before that. Post the whole function.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: define an array

Postby ric » Mon Jan 04, 2016 9:43 pm

There's almost certainly an error in the lines before that. Post the whole function.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: define an array

Postby TucsonDon » Tue Jan 05, 2016 12:54 am

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;
    }
}
User avatar
TucsonDon
 
Posts: 5
Joined: Sun Jan 03, 2016 3:51 pm

Re: define an array

Postby AussieSusan » Tue Jan 05, 2016 3:08 am

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
AussieSusan
Verified identity
 
Posts: 173
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: define an array

Postby TucsonDon » Wed Jan 06, 2016 1:06 am

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"
User avatar
TucsonDon
 
Posts: 5
Joined: Sun Jan 03, 2016 3:51 pm

Re: define an array

Postby ric » Wed Jan 06, 2016 2:15 am

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.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to Other PICmicro topics

Who is online

Users browsing this forum: No registered users and 10 guests

cron