473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pointer to structure array from main.c?


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;
}
Mar 30 '07 #1
3 1574
"Steve" <sr*@nosapm.comschrieb im Newsbeitrag
news:qg********************************@4ax.com...
>
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;
}

Mar 30 '07 #2
On Fri, 30 Mar 2007 09:30:35 -0400, Steve <sr*@nosapm.comwrote:
>
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.)
Mar 30 '07 #3
"Steve" <sr*@nosapm.comschrieb im Newsbeitrag
news:7s********************************@4ax.com...
On Fri, 30 Mar 2007 09:30:35 -0400, Steve <sr*@nosapm.comwrote:
>>
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
Mar 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
5
by: caleb.vandyke | last post by:
I am working with some code that is doing some pointer to structure casts and I can't figure out how the cast is being done. Here is basically the code. #include <stdio.h> #include <stdlib.h> ...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.