CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Keyscan code

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Sugiman



Joined: 12 Jan 2005
Posts: 15

View user's profile Send private message

Keyscan code
PostPosted: Wed Apr 06, 2005 3:20 am     Reply with quote

I'm trying to shorten some of the routines in this program, specifically the keyscan ones as posted below. Is there any way to put it in a "for" loop and have it set the SCAN ports low and high once per cycle instead of having to list them all out? I tried to do it with a const array but the compiler gave me an error msg along the lines of "Expression must evaluate to a constant"

Here's the part of the header where SCAN_xx is initialized

Code:

#define  SCAN_01   PIN_A0   // k01,k02,k03,k04
#define  SCAN_02   PIN_A1   // k05,k06,k07,k08
#define  SCAN_03   PIN_A2   // k09,k10,k11,k12
#define  SCAN_04   PIN_A3   // k13,k14,k15,k16
#define  SCAN_05   PIN_A5   // k17,k18,k19,k20
#define  SCAN_06   PIN_B0   // k21,k22,k23,k24
#define  SCAN_07   PIN_B1   // k25,k26,k27,k28
#define  SCAN_08   PIN_B2   // k29,k30,k31,k32
#define  SCAN_09   PIN_B3   // k33,k34,k35,k36
#define  SCAN_10   PIN_C0   // k37,k38,k39,k40
#define  SCAN_11   PIN_C1   // k41,k42,k43,k44
#define  SCAN_12   PIN_C3   // k45,k46,k47,k48
#define  SCAN_13   PIN_C4   // k49,k50,k51,k52
#define  SCAN_14   PIN_C5   // k53,k54,k55,k56
#define  SCAN_15   PIN_C6   // k57,k58,k59,k60
#define  SCAN_16   PIN_C7   // k61,k62,k63,k64



And here's the key_scan function I'm hoping to shorten

Code:

void key_scan(void)
{
   int n;
   
   n = 0;
   output_low(SCAN_01);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_01);
   
   output_low(SCAN_02);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_02);
   
   output_low(SCAN_03);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_03);
   
   output_low(SCAN_04);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_04);
   
   output_low(SCAN_05);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_05);
   
   output_low(SCAN_06);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_06);
   
   output_low(SCAN_07);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_07);
   
   output_low(SCAN_08);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_08);
   
   output_low(SCAN_09);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_09);
   
   output_low(SCAN_10);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_10);
   
   output_low(SCAN_11);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_11);
   
   output_low(SCAN_12);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_12);
   
   output_low(SCAN_13);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_13);
   
   output_low(SCAN_14);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_14);
   
   output_low(SCAN_15);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_15);
   
   output_low(SCAN_16);   delay_us(500);
   gScanBuf[n++] = ~gScanIn;
   output_high(SCAN_16);
   
}


Thanks for your help =)
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Wed Apr 06, 2005 10:27 am     Reply with quote

Code:
#byte port_a = 5
#byte port_b = 6
#byte port_c = 7

int i;

....

port_a = 0x01;

for(i=0;i<4;i++)
{
gScanBuf[n++] = ~gScanIn;
port_a<<=1;
}
port_a<<=1;
gScanBuf[n++] = ~gScanIn;
......


I think this should work. You sould get the point on how you could probably do the rest and crunch.

You could also probably combine some diff. ports code by using a pointer.

int8 *port;

port = port_a;
_________________
-Matt
Sugiman



Joined: 12 Jan 2005
Posts: 15

View user's profile Send private message

PostPosted: Wed Apr 06, 2005 7:51 pm     Reply with quote

Thanks for the help, I'll give it a try =)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group