S
Steve
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;
}
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;
}