pointer to structure array from main.c?

Discussion in 'C Programming' started by Steve, Mar 30, 2007.

  1. Steve

    Steve Guest

    Hi,
    I have a keypad that I am scanning to get key states, and am putting
    those states in a structure. 16 keys, so I have it like
    keys[0].status=PRESSSED. to keys[16].status etc.

    I can access the elements in keypad.c, but why can't I see it from
    main.c? It says I need a pointer type, but I can't figure it out yet.

    Can someone show me how to access my key information from main.c?
    Thankyou. Here are relevant files

    *********************main.c***********************************
    #include "includes.h" //which includes keypad.h
    //KEY *pKey; //commented out
    //pKey = &keys;
    //*pKEY = &KEY;

    //init I/O for keypad
    keypad_init();
    while(1){
    temp =scankeypad();
    temp =scankeypad();
    if(keys(1).status==KEYPRESSED){// ***This line gives me an error,
    wheras in the keypad.c file I can access this. Gives, must have a
    pointer to type function.
    fprintf(pntr1, "Key1pressed");
    }else{
    fprintf(pntr1, "Key1Notpressed");
    }

    }


    **********************keypad.h*********************************/

    #include <includes.h>

    #ifndef __KEYPAD_H
    #define __KEYPAD_H
    #define KEYDOWN 1
    #define KEYUP 0
    #define MAXDOWNCOUNT 100
    #define KEYPRESSED 1
    #define KEYNOTPRESSED 0



    typedef struct keys {
    unsigned char state;
    unsigned int downcount;
    unsigned char status;
    }KEY;


    typedef enum {//BIT POSITIONS OF KEYS IN keysdown
    UP = 0, //SW4
    DOWN, //SW8
    ENTER, //SW12
    BACK, //SW16
    SW5,
    SW9,
    SW13,
    SW17,
    SW6,
    SW10,
    SW14,
    SW18,
    SW7,
    SW11,
    SW15,
    SW19,
    STRIPSWITCH
    } keyname;


    /* Declare functions */
    void keypad_init(void);
    unsigned int scankeypad(void);
    //void getStatusForKey(struct key *pkey);
    #endif

    **********************keypad.C*********************************/

    #include "keypad.h"
    /***********************************************************
    * Function:
    *
    ***********************************************************/



    //setup I/O of matrix. Internal Pullups on Rows which are inputs.

    #define row0 (1<<16)
    #define row1 (1<<17)
    #define row2 (1<<18)
    #define row3 (1<<19)
    #define col0 (1<<20)
    #define col1 (1<<21)
    #define col2 (1<<22)
    #define col3 (1<<23)

    void keypad_init(){

    IO1DIR |= 0x00f00000;//make rows inputs and columns outputs
    1=output, 0 = input
    IO1DIR &= 0xfff0ffff;//make columns outputs

    //FIO1DIR2 is bits16-23 of port1 which are the rows and columns
    //Set all high, and pull columns low to see which row key is
    pressed.
    /*
    P1_16 // Row0 = GPIO
    P1_17 // Row1 = GPIO
    P1_18 // Row2 = GPIO
    P1_19 // Row3 = GPIO

    P1_20 // Col0 = GPIO
    P1_21 // Col1 = GPIO
    P1_22 // Col2 = GPIO
    P1_23 // Col3 = GPIO
    */
    /*while(1){

    IO1CLR |= (col0 | col1 | col2 | col3);
    }*/
    }//keypad_init


    unsigned int scankeypad(void){
    unsigned int keysdown=0;
    unsigned int temp=0;
    unsigned char colbit, rowbit,keybit=0;

    KEY keys[10];

    IO1SET |= (col0 | col1 | col2 | col3);// set all columns high at
    first

    for(colbit=20;colbit<24;colbit++){
    IO1CLR |= (1<<colbit);//set a column low

    for(rowbit=16;rowbit<20;rowbit++){
    temp=(IO1PIN & (1<<rowbit));
    if(temp==0){//if this row is low update keysdown
    keysdown |= (1<<keybit); //place 1's in down key positions,
    bit0 is first scanned
    keys[keybit].state=KEYDOWN;
    if(keys[keybit].downcount<MAXDOWNCOUNT ){
    keys[keybit].downcount++;
    }else{
    if(keys[keybit].downcount==MAXDOWNCOUNT){
    keys[keybit].status=KEYPRESSED;
    }
    }
    }else{ //key is up
    keysdown =keysdown & ~(0<<keybit);//place 0's in up keypad bit
    positions
    keys[keybit].status=KEYNOTPRESSED;
    keys[keybit].downcount=0;
    keys[keybit].state=KEYUP;
    }
    keybit++;
    }
    IO1SET |= (1<<colbit);//set the column back high
    }
    return keysdown;
    }
     
    Steve, Mar 30, 2007
    #1
    1. Advertising

  2. Steve

    Steve Guest

    On Fri, 30 Mar 2007 09:30:35 -0400, Steve <> wrote:

    >
    >Hi,
    >I have a keypad that I am scanning to get key states, and am putting
    >those states in a structure. 16 keys, so I have it like
    >keys[0].status=PRESSSED. to keys[16].status etc.
    >
    >I can access the elements in keypad.c, but why can't I see it from
    >main.c? It says I need a pointer type, but I can't figure it out yet.
    >
    >Can someone show me how to access my key information from main.c?
    >Thankyou. Here are relevant files


    Joachim, Thankyou very much. I totally missed that () as opposed to
    []. I did have the extern up top at one point, but I can't believe
    you caught that subtle error.
    Thankyou,
    Steve (I accidentally deleted the response by mistake, but I did get
    and compile it now.)
     
    Steve, Mar 30, 2007
    #2
    1. Advertising

  3. "Steve" <> schrieb im Newsbeitrag
    news:...
    >
    > Hi,
    > I have a keypad that I am scanning to get key states, and am putting
    > those states in a structure. 16 keys, so I have it like
    > keys[0].status=PRESSSED. to keys[16].status etc.
    >
    > I can access the elements in keypad.c, but why can't I see it from
    > main.c? It says I need a pointer type, but I can't figure it out yet.
    >
    > Can someone show me how to access my key information from main.c?
    > Thankyou. Here are relevant files
    >
    > *********************main.c***********************************
    > #include "includes.h" //which includes keypad.h
    > //KEY *pKey; //commented out
    > //pKey = &keys;
    > //*pKEY = &KEY;
    >
    > //init I/O for keypad
    > keypad_init();
    > while(1){
    > temp =scankeypad();
    > temp =scankeypad();
    > if(keys(1).status==KEYPRESSED){// ***This line gives me an error,

    if (keys[1].status==KEYPRESSED) { /* need to use [], not ()*/
    /* Beginning of main() is missing as well as an extern KEY keys[]; */

    > wheras in the keypad.c file I can access this. Gives, must have a
    > pointer to type function.
    > fprintf(pntr1, "Key1pressed");
    > }else{
    > fprintf(pntr1, "Key1Notpressed");
    > }
    >
    > }
    >
    >
    > **********************keypad.h*********************************/
    >
    > #include <includes.h>
    >
    > #ifndef __KEYPAD_H
    > #define __KEYPAD_H
    > #define KEYDOWN 1
    > #define KEYUP 0
    > #define MAXDOWNCOUNT 100
    > #define KEYPRESSED 1
    > #define KEYNOTPRESSED 0
    >
    >
    >
    > typedef struct keys {
    > unsigned char state;
    > unsigned int downcount;
    > unsigned char status;
    > }KEY;
    >
    >
    > typedef enum {//BIT POSITIONS OF KEYS IN keysdown
    > UP = 0, //SW4
    > DOWN, //SW8
    > ENTER, //SW12
    > BACK, //SW16
    > SW5,
    > SW9,
    > SW13,
    > SW17,
    > SW6,
    > SW10,
    > SW14,
    > SW18,
    > SW7,
    > SW11,
    > SW15,
    > SW19,
    > STRIPSWITCH
    > } keyname;
    >
    >
    > /* Declare functions */
    > void keypad_init(void);
    > unsigned int scankeypad(void);
    > //void getStatusForKey(struct key *pkey);
    > #endif
    >
    > **********************keypad.C*********************************/
    >
    > #include "keypad.h"
    > /***********************************************************
    > * Function:
    > *
    > ***********************************************************/
    >
    >
    >
    > //setup I/O of matrix. Internal Pullups on Rows which are inputs.
    >
    > #define row0 (1<<16)
    > #define row1 (1<<17)
    > #define row2 (1<<18)
    > #define row3 (1<<19)
    > #define col0 (1<<20)
    > #define col1 (1<<21)
    > #define col2 (1<<22)
    > #define col3 (1<<23)
    >
    > void keypad_init(){
    >
    > IO1DIR |= 0x00f00000;//make rows inputs and columns outputs
    > 1=output, 0 = input
    > IO1DIR &= 0xfff0ffff;//make columns outputs
    >
    > //FIO1DIR2 is bits16-23 of port1 which are the rows and columns
    > //Set all high, and pull columns low to see which row key is
    > pressed.
    > /*
    > P1_16 // Row0 = GPIO
    > P1_17 // Row1 = GPIO
    > P1_18 // Row2 = GPIO
    > P1_19 // Row3 = GPIO
    >
    > P1_20 // Col0 = GPIO
    > P1_21 // Col1 = GPIO
    > P1_22 // Col2 = GPIO
    > P1_23 // Col3 = GPIO
    > */
    > /*while(1){
    >
    > IO1CLR |= (col0 | col1 | col2 | col3);
    > }*/
    > }//keypad_init
    >
    >
    > unsigned int scankeypad(void){
    > unsigned int keysdown=0;
    > unsigned int temp=0;
    > unsigned char colbit, rowbit,keybit=0;
    >
    > KEY keys[10];
    >
    > IO1SET |= (col0 | col1 | col2 | col3);// set all columns high at
    > first
    >
    > for(colbit=20;colbit<24;colbit++){
    > IO1CLR |= (1<<colbit);//set a column low
    >
    > for(rowbit=16;rowbit<20;rowbit++){
    > temp=(IO1PIN & (1<<rowbit));
    > if(temp==0){//if this row is low update keysdown
    > keysdown |= (1<<keybit); //place 1's in down key positions,
    > bit0 is first scanned
    > keys[keybit].state=KEYDOWN;
    > if(keys[keybit].downcount<MAXDOWNCOUNT ){
    > keys[keybit].downcount++;
    > }else{
    > if(keys[keybit].downcount==MAXDOWNCOUNT){
    > keys[keybit].status=KEYPRESSED;
    > }
    > }
    > }else{ //key is up
    > keysdown =keysdown & ~(0<<keybit);//place 0's in up keypad bit
    > positions
    > keys[keybit].status=KEYNOTPRESSED;
    > keys[keybit].downcount=0;
    > keys[keybit].state=KEYUP;
    > }
    > keybit++;
    > }
    > IO1SET |= (1<<colbit);//set the column back high
    > }
    > return keysdown;
    > }
     
    Joachim Schmitz, Mar 30, 2007
    #3
  4. "Steve" <> schrieb im Newsbeitrag
    news:...
    > On Fri, 30 Mar 2007 09:30:35 -0400, Steve <> wrote:
    >
    >>
    >>Hi,
    >>I have a keypad that I am scanning to get key states, and am putting
    >>those states in a structure. 16 keys, so I have it like
    >>keys[0].status=PRESSSED. to keys[16].status etc.
    >>
    >>I can access the elements in keypad.c, but why can't I see it from
    >>main.c? It says I need a pointer type, but I can't figure it out yet.
    >>
    >>Can someone show me how to access my key information from main.c?
    >>Thankyou. Here are relevant files

    >
    > Joachim, Thankyou very much. I totally missed that () as opposed to
    > []. I did have the extern up top at one point, but I can't believe
    > you caught that subtle error.

    I've made enough of these mistakes myself 8.)

    Bye, Jojo
     
    Joachim Schmitz, Mar 30, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    357
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    682
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,338
  4. A
    Replies:
    27
    Views:
    1,661
    Jorgen Grahn
    Apr 17, 2011
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    471
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page